fix: 对话流处理

This commit is contained in:
shaohuzhang1 2023-12-15 18:25:08 +08:00
parent d3409d9845
commit d7f6ec1fba
3 changed files with 20 additions and 8 deletions

View File

@ -165,8 +165,10 @@ class ChatMessageSerializer(serializers.Serializer):
for chunk in response: for chunk in response:
all_text += chunk.content all_text += chunk.content
yield 'data: ' + json.dumps({'chat_id': chat_id, 'id': _id, 'operate': paragraph is not None, yield 'data: ' + json.dumps({'chat_id': chat_id, 'id': _id, 'operate': paragraph is not None,
'content': chunk.content}) + "\n\n" 'content': chunk.content, 'is_end': False}) + "\n\n"
yield 'data: ' + json.dumps({'chat_id': chat_id, 'id': _id, 'operate': paragraph is not None,
'content': '', 'is_end': True}) + "\n\n"
chat_info.append_chat_message( chat_info.append_chat_message(
ChatMessage(_id, message, title, content, embedding_id, dataset_id, document_id, ChatMessage(_id, message, title, content, embedding_id, dataset_id, document_id,
paragraph_id, paragraph_id,

View File

@ -57,7 +57,6 @@ export class ChatRecordManage {
this.chat.write_ed = true this.chat.write_ed = true
this.write_ed = true this.write_ed = true
if (this.loading) { if (this.loading) {
console.log('停止')
this.loading.value = false this.loading.value = false
} }
if (this.id) { if (this.id) {
@ -76,7 +75,6 @@ export class ChatRecordManage {
} }
} }
close() { close() {
console.log('close')
this.is_close = true this.is_close = true
} }
append(answer_text_block: string) { append(answer_text_block: string) {

View File

@ -282,13 +282,17 @@ function chatMessage() {
if (str && str.startsWith('data:')) { if (str && str.startsWith('data:')) {
const split = str.match(/data:.*}\n\n/g) const split = str.match(/data:.*}\n\n/g)
if (split) { if (split) {
split.forEach((item_str) => { for (const index in split) {
row.record_id = JSON?.parse(item_str.replace('data:', '')).id const chunk = JSON?.parse(split[index].replace('data:', ''))
const content = JSON?.parse(item_str.replace('data:', ''))?.content row.record_id = chunk.id
const content = chunk?.content
if (content) { if (content) {
ChatManagement.append(id, content) ChatManagement.append(id, content)
} }
}) if (chunk.is_end) {
ChatManagement.close(id)
}
}
} }
} }
} catch (e) { } catch (e) {
@ -297,7 +301,15 @@ function chatMessage() {
} }
return reader.read().then(write) return reader.read().then(write)
} }
reader.read().then(write) reader
.read()
.then(write)
.finally((ok: any) => {
ChatManagement.close(id)
})
.catch((e: any) => {
ChatManagement.close(id)
})
} }
}) })
} }