Browse Source

加了数学公式的样式修改

hongxilin
donghaolin 3 months ago
parent
commit
560989512e
  1. 100
      src/views/chat.vue

100
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>(.*?)<\/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 += `<span class="thinking-content">${thinking}</span><br>`;
// }
combinedContent += `<span class="result-content">${result}</span>`;
// 使markedMarkdownHTML
const markdownContent = marked(combinedContent);
// data
if (data && data.data && typeof data.data.answer === 'string') {
//
const regex = /<think>(.*?)<\/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 += `<span class="thinking-content">${thinking}</span><br>`;
// }
combinedContent += `<span class="result-content">${result}</span>`;
// 使
let currentIndex = 0;
const intervalId = setInterval(() => {
// 使markedMarkdownHTML
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);
//

Loading…
Cancel
Save