接入deepSeek后,前端如何给“深度思考“内容单独设置样式?

问题描述:

SSE接口响应回来的数据是markdown格式,理论上来说markdown中是包含样式的,前端只需要将markdown转化为html响应到页面上即可,但是接入deepSeek后深度思考的内容只有文字描述没有样式,需要前端单独设置。

解决思路:

让后端在响应的数据流中加入标识,用来区分 “ 思考内容 ” 还是 “ 回答内容 ”,前端将“ 思考内容 ”截取出来,给 “ 思考内容 ” 加上html标签并添加行内样式生成字符串thinkingStr,然后将“ 回答内容 ”通过第三方库MarkdownIt将markdown转化为html字符串answerStr。最后将thinkingStr+answerStr=currentHTML拼接起来就生成完整且附带样式的html字符串,通过v-html将currentHTML渲染到页面即可。

实现代码:

import MarkdownIt from "markdown-it"

const md = new MarkdownIt()

const currentHTML = computed(() => {

      // 注:contentItems是在onmessage中实时接收并拼接好的字符串
      // </sy_think>是用来区分“思考内容”和“响应内容”的标识
      // md.render是将markdown转化为html

      if (contentItems.value) {
        if (contentItems.value.includes("</sy_think>")) {
          const resArr = contentItems.value.split("</sy_think>")
          const thinkingStr= `<h4> 师爷模型深度思考中...</h4> 
                              <div style="color: gray;">${resArr[0]}</div>
                             `
          return thinkingStr+ md.render(resArr[1])
        } else {
          const thinkingStr= `<h4> 师爷模型深度思考中...</h4>
                             <div style="color: gray;">${contentItems.value}</div>
                             `
          return thinkingStr
        }
      }
    })

效果对比:

### 关于 Vue 实现深度思考功能的探讨 要实现类似于“深度思考”的功能,可以将其理解为一种高级的数据处理逻辑或者动态交互能力。以下是通过 Vue 和其他技术栈(如 Spring Boot)结合的方式构建此类功能的具体方法。 #### 1. 数据驱动与状态管理 在 Vue 中,“深度思考”功能可能涉及复杂的业务逻辑和数据流控制。为了更好地管理和更新这些复杂的状态变化,推荐使用 Vuex 或 Pinia 来进行全局状态管理[^2]。 ```javascript // 使用Pinia作为状态管理库 import { defineStore } from 'pinia'; export const useDeepThoughtStore = defineStore('deepThought', { state: () => ({ thoughts: [], currentFocus: null, }), actions: { addThought(thought) { this.thoughts.push({ id: Date.now(), content: thought }); }, focusOn(id) { this.currentFocus = this.thoughts.find(t => t.id === id); } } }); ``` #### 2. 组件化设计 对于“深度思考”,通常会涉及到多个子组件之间的协作。例如,一个输入框用于接收用户的初始想法,另一个区域展示经过 AI 处理后的扩展思路。这种场景非常适合采用 Vue 的单文件组件结构。 ```vue <template> <div> <input v-model="newIdea" placeholder="写下你的新想法..." /> <button @click="submitNewIdea">提交</button> <!-- 展示已有的想法 --> <ul> <li v-for="(thought, index) in deepThoughtState.thoughts" :key="index"> {{ thought.content }} <span @click="expandThought(index)">展开更多...</span> </li> </ul> </div> </template> <script> import { useDeepThoughtStore } from '@/stores/deepThought'; export default { data() { return { newIdea: '', deepThoughtState: useDeepThoughtStore(), }; }, methods: { submitNewIdea() { this.deepThoughtState.addThought(this.newIdea); this.newIdea = ''; }, expandThought(index) { console.log(`正在尝试扩展第${index}条想法`); // 可在此处调用后端API获取更多信息 } } }; </script> ``` #### 3. 后端支持与 AI 集成 如果希望进一步增强用户体验,则可以在后台引入人工智能模型来辅助生成更深层次的内容。这里提到的一个例子就是利用 Java 编写的 `AIMindMapService` 类型的服务接口[^1]。当用户触发某个事件时,前端发送请求到服务器并等待响应结果。 ```java @RestController @RequestMapping("/api/ai") public class AIDeepThinkingController { private final AIMindMapService aiMindMapService; public AIDeepThinkingController(AIMindMapService aiMindMapService){ this.aiMindMapService=aiMindMapService; } @PostMapping("/generate-thoughts") ResponseEntity<MindMap> generateThoughts(@RequestBody String userInput){ try{ MindMap mindMapResult=this.aiMindMapService.generateMindMap(userInput); return ResponseEntity.ok(mindMapResult); }catch(Exception e){ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } } ``` #### 4. 动态加载优化 考虑到性能因素,在实际项目中还应该考虑如何减少不必要的资源消耗。比如只按需加载某些模块而不是一次性全部渲染出来。此时就可以借助现代 JavaScript 打包工具所提供的特性——懒加载(lazy-loading)[^3]。 ```javascript const DeepAnalysisComponent = () => import('./components/DeepAnalysis.vue'); routes:[ ... { path:'/analysis/:id', component:DeepAnalysisComponent}, ]; ``` 以上便是围绕着 Vue 架构下实现所谓“深度思考”这一目标所作的一些基本指导方向以及具体实践案例分享。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值