From 560989512ec8125fde69844b58e9e7edcaf69a02 Mon Sep 17 00:00:00 2001 From: donghaolin <17667510818@163.com> Date: Tue, 25 Feb 2025 13:55:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=BA=86=E6=95=B0=E5=AD=A6=E5=85=AC?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E6=A0=B7=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/chat.vue | 100 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 38 deletions(-) diff --git a/src/views/chat.vue b/src/views/chat.vue index ca68658..34d5460 100644 --- a/src/views/chat.vue +++ b/src/views/chat.vue @@ -5,6 +5,8 @@ import axios from 'axios' import { ElMessage } from 'element-plus'; import { Loading, Position } from '@element-plus/icons-vue'; // 引入图标组件 import { marked } from 'marked'; // 引入marked库 +import katex from 'katex'; // 引入 KaTeX 库 +import 'katex/dist/katex.min.css'; // 引入 KaTeX 样式 const { getQueryVariable } = useUserInfo() // 假设 getSessionId 已经正确定义 @@ -35,7 +37,7 @@ const sessionId = ref(localStorage.getItem('sessionId') || {}); const add = async () => { try { const result = await axios.post( - "http://td7idg.natappfree.cc/api/v1/chats/8b37cd9cf0c811efa4210242ac120003/completions", + "http://asp69f.natappfree.cc/api/v1/chats/8b37cd9cf0c811efa4210242ac120003/completions", {}, { headers: { @@ -133,7 +135,7 @@ const sendMessage = async () => { try { // 调用API获取回复 - const response = await fetch("http://td7idg.natappfree.cc/api/v1/chats/8b37cd9cf0c811efa4210242ac120003/completions", { + const response = await fetch("http://asp69f.natappfree.cc/api/v1/chats/8b37cd9cf0c811efa4210242ac120003/completions", { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ragflow-hkNjEwYjcwZjBlMDExZWZiYjYzMDI0Mm' }, body: JSON.stringify({ question: content, stream: false, session_id: sessionId.value }) @@ -143,47 +145,69 @@ const sendMessage = async () => { // 移除加载消息 messages.value = messages.value.filter(msg => !msg.isLoading); - // 提取推理思考部分和结果部分 - const regex = /(.*?)<\/think>(.*)/s; - const match = data.data.answer.match(regex); - let thinking = ''; - let result = data.data.answer; - - if (match) { - thinking = match[1].trim(); - result = match[2].trim(); - } - - // 组合推理思考内容和结果内容,并添加 HTML 标签 - let combinedContent = ''; - // if (thinking) { - // combinedContent += `推理思考:${thinking}
`; - // } - combinedContent += `${result}`; - // 使用marked库将Markdown转换为HTML - const markdownContent = marked(combinedContent); + // 检查 data 对象的结构,确保正确提取所需属性 + if (data && data.data && typeof data.data.answer === 'string') { + // 提取推理思考部分和结果部分 + const regex = /(.*?)<\/think>(.*)/s; + const match = data.data.answer.match(regex); + let thinking = ''; + let result = data.data.answer; + + if (match) { + thinking = match[1].trim(); + result = match[2].trim(); + } - // 添加初始空消息 - const botMessageIndex = messages.value.length; - messages.value.push({ - content: '', - sender: 'bot', - timestamp: new Date() - }); + // 组合推理思考内容和结果内容,并添加 HTML 标签 + let combinedContent = ''; + // if (thinking) { + // combinedContent += `推理思考:${thinking}
`; + // } + combinedContent += `${result}`; - // 使用定时器逐个字符更新消息内容 - let currentIndex = 0; - const intervalId = setInterval(() => { + // 使用marked库将Markdown转换为HTML + let markdownContent = marked(combinedContent); + + // 使用 KaTeX 渲染数学公式 + const katexRegex = /\$\$(.*?)\$\$/g; + markdownContent = markdownContent.replace(katexRegex, (match, formula) => { + try { + return katex.renderToString(formula, { throwOnError: false }); + } catch (error) { + console.error('KaTeX 渲染错误:', error); + return match; + } + }); + + // 添加初始空消息 + const botMessageIndex = messages.value.length; + messages.value.push({ + content: '', + sender: 'bot', + timestamp: new Date() + }); + + // 使用定时器逐个字符更新消息内容 + let currentIndex = 0; + const intervalId = setInterval(() => { if (currentIndex < markdownContent.length) { messages.value[botMessageIndex].content += markdownContent[currentIndex]; - scrollToBottom(); - currentIndex++; - } else { - clearInterval(intervalId); - } - }, 100); // 每100毫秒添加一个字符,可以根据需要调整 - + scrollToBottom(); + currentIndex++; + } else { + clearInterval(intervalId); + } + }, 100); // 每100毫秒添加一个字符,可以根据需要调整 + } else { + console.error('API 返回的数据格式不正确:', data); + messages.value.push({ + content: '服务返回数据格式错误,请稍后再试', + sender: 'bot', + timestamp: new Date() + }); + scrollToBottom(); + } } catch (error) { console.error('API请求失败:', error); // 移除加载消息