修复对话断断续续的问题,锁定在 fastapi 上

This commit is contained in:
Wenjie Zhang 2024-10-24 13:10:17 +08:00
parent f0984191ae
commit 601034e739

View File

@ -434,31 +434,47 @@ const fetchChatResponse = (user_input, cur_res_id) => {
groupRefs(cur_res_id); groupRefs(cur_res_id);
}) })
} else { } else {
groupRefs(cur_res_id); updateMessage({
id: cur_res_id,
status: "finished",
});
} }
isStreaming.value = false; isStreaming.value = false;
if (conv.value.messages.length === 2) { renameTitle(); } if (conv.value.messages.length === 2) { renameTitle(); }
return; return;
} }
const chunk = decoder.decode(value, { stream: true }); buffer += decoder.decode(value, { stream: true });
buffer += chunk; const lines = buffer.split('\n');
try {
const data = JSON.parse(chunk); //
updateMessage({ for (let i = 0; i < lines.length - 1; i++) {
id: cur_res_id, const line = lines[i].trim();
text: data.response, if (line) {
model_name: data.model_name, try {
status: data.status, const data = JSON.parse(line);
meta: data.meta, updateMessage({
}); id: cur_res_id,
console.debug(data.response) text: data.response,
if (data.history) { model_name: data.model_name,
conv.value.history = data.history; status: data.status,
meta: data.meta,
});
// console.log("Last message", conv.value.messages[conv.value.messages.length - 1].text)
// console.log("Last message", conv.value.messages[conv.value.messages.length - 1].status)
if (data.history) {
conv.value.history = data.history;
}
} catch (e) {
console.error('JSON 解析错误:', e, line);
}
} }
} catch (e) {
// console.debug('JSON :', e, chunk);
} }
//
buffer = lines[lines.length - 1];
return readChunk(); // return readChunk(); //
}); });
}; };
@ -471,7 +487,7 @@ const fetchChatResponse = (user_input, cur_res_id) => {
status: "error", status: "error",
}); });
isStreaming.value = false; isStreaming.value = false;
}) });
} }
const fetchRefs = (cur_res_id) => { const fetchRefs = (cur_res_id) => {
@ -975,3 +991,4 @@ watch(
} }
} }
</style> </style>