典籍知识问答模块AI问答bug修改

一、修改流式数据处理问题

1.问题描述:由于传来的数据形式如下:

event:START

data:350

data:<

data:t

data:h

data:i

data:n

data:k

data:>

data:

data:

data:

data:

data:嗯

data:,

导致需要修改获取正常的当前信息id并更新的逻辑

2.修改代码如下:

while (true) {

const { done, value } = await reader.read();

if (done) break;

buffer += decoder.decode(value, { stream: true });

const lines = buffer.split('\n');

buffer = lines.pop() || '';

for (const line of lines) {

if (line.startsWith('event:')) {

currentEvent = line.replace('event:', '').trim();

continue;

}

if (line.startsWith('data:')) {

const data = line.replace('data:', '').trim();

// 处理 START 事件

if (currentEvent === 'START') {

serverAIId = data;

currentMessages.value.push({

id: serverAIId,

role: 'assistant',

content: '',

createdAt: new Date().toISOString(),

streaming: true

});

currentEvent = null; // 重置事件状态

//console.log('START event received, serverAIId:', serverAIId);

continue;

}

// 处理普通内容(MESSAGE 或未指定 event)

if (currentEvent === null) {

aiContent += data;

currentMessages.value = currentMessages.value.map(msg =>

msg.id === serverAIId ? { ...msg, content: aiContent } : msg

);

}

// 处理 COMPLETE 事件(结束)

if (currentEvent === 'COMPLETE') {

currentMessages.value = currentMessages.value.map(msg =>

msg.id === serverAIId ? { ...msg, streaming: false } : msg

);

}

}

}

}

currentMessages.value = currentMessages.value.map(msg =>

msg.id === serverAIId ? { ...msg, streaming: false } : msg

);

await fetchSessions(classic.value.id);

二、重新编辑问题时出现的问题

1.问题描述:由于发送信息后的userId也没能及时更新到前端,导致在使用修改问题获取msgid时无法获取原始id的信息.

2.解决方法:后端返回时加上问题的id信息,具体返回如下:

event:USER_MSG

data:347

event:START

data:348

data:<

data:t

因此就可以正常更新问题id了

相关推荐
武子康3 分钟前
同一台 ESP32,GSM 能消回声,VoIP 为什么不行?
人工智能·后端·agent
Eider19985 分钟前
Python基础知识之并发相关-2
开发语言·前端·python
用户69371750013847 分钟前
AI 能写代码,但工程架构还是应该由人来定
前端·后端
lqbzlll7 分钟前
Spring Boot 开启虚拟线程后接口反而变慢了?4 个排查方向和对应修法
java·spring boot·后端
Hilaku15 分钟前
前端的技术判断力是怎么练出来的?
前端·javascript·程序员
悠仁さん18 分钟前
【C++】set map 使用
java·前端·c++
用户0936717554120 分钟前
用 4500 行 TypeScript 复刻了童年方格纸上的海战棋
前端·vue.js
回家路上绕了弯21 分钟前
让 AI 成为团队的共同能力
后端
CCIE-Yasuo24 分钟前
网络安全-学习web开发-基础概念、常用标签
前端·学习
70后写java的老码农30 分钟前
一个 starter 通吃 9 家主流大模型:Spring Boot 3.x + Spring AI 1.x 极简落地
后端