Skip to content

Commit 5783f3e

Browse files
committed
fix: recommend questions doesn't show during chatting
1 parent 667536e commit 5783f3e

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

backend/apps/chat/curd/chat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ def save_recommend_question_answer(session: SessionDep, record_id: int,
546546
session.commit()
547547

548548
record = get_chat_record_by_id(session, record_id)
549+
record.recommended_question_answer = recommended_question_answer
550+
record.recommended_question = recommended_question
549551

550552
return record
551553

backend/apps/chat/task/llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,10 +1029,10 @@ def run_recommend_questions_task(self):
10291029

10301030
for chunk in res:
10311031
if chunk.get('recommended_question'):
1032-
yield orjson.dumps(
1032+
yield 'data:' + orjson.dumps(
10331033
{'content': chunk.get('recommended_question'), 'type': 'recommended_question'}).decode() + '\n\n'
10341034
else:
1035-
yield orjson.dumps(
1035+
yield 'data:' + orjson.dumps(
10361036
{'content': chunk.get('content'), 'reasoning_content': chunk.get('reasoning_content'),
10371037
'type': 'recommended_question_result'}).decode() + '\n\n'
10381038

frontend/src/views/chat/RecommendQuestion.vue

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ async function getRecommendQuestions() {
6363
const controller: AbortController = new AbortController()
6464
const response = await chatApi.recommendQuestions(props.recordId, controller)
6565
const reader = response.body.getReader()
66-
const decoder = new TextDecoder()
66+
const decoder = new TextDecoder('utf-8')
67+
68+
let tempResult = ''
6769
6870
while (true) {
6971
if (stopFlag.value) {
@@ -77,60 +79,56 @@ async function getRecommendQuestions() {
7779
break
7880
}
7981
80-
const chunk = decoder.decode(value)
81-
82-
let _list = [chunk]
83-
84-
const lines = chunk.trim().split('}\n\n{')
85-
if (lines.length > 1) {
86-
_list = []
87-
for (let line of lines) {
88-
if (!line.trim().startsWith('{')) {
89-
line = '{' + line.trim()
90-
}
91-
if (!line.trim().endsWith('}')) {
92-
line = line.trim() + '}'
93-
}
94-
_list.push(line)
95-
}
82+
let chunk = decoder.decode(value, { stream: true })
83+
tempResult += chunk
84+
const split = tempResult.match(/data:.*}\n\n/g)
85+
if (split) {
86+
chunk = split.join('')
87+
tempResult = tempResult.replace(chunk, '')
88+
} else {
89+
continue
9690
}
9791
98-
for (const str of _list) {
99-
let data
100-
try {
101-
data = JSON.parse(str)
102-
} catch (err) {
103-
console.error('JSON string:', str)
104-
throw err
105-
}
92+
if (chunk && chunk.startsWith('data:{')) {
93+
if (split) {
94+
for (const str of split) {
95+
let data
96+
try {
97+
data = JSON.parse(str.replace('data:{', '{'))
98+
} catch (err) {
99+
console.error('JSON string:', str)
100+
throw err
101+
}
106102
107-
if (data.code && data.code !== 200) {
108-
ElMessage({
109-
message: data.msg,
110-
type: 'error',
111-
showClose: true,
112-
})
113-
return
114-
}
103+
if (data.code && data.code !== 200) {
104+
ElMessage({
105+
message: data.msg,
106+
type: 'error',
107+
showClose: true,
108+
})
109+
return
110+
}
115111
116-
switch (data.type) {
117-
case 'recommended_question':
118-
if (
119-
data.content &&
120-
data.content.length > 0 &&
121-
startsWith(data.content.trim(), '[') &&
122-
endsWith(data.content.trim(), ']')
123-
) {
124-
if (_currentChat.value?.records) {
125-
for (let record of _currentChat.value.records) {
126-
if (record.id === props.recordId) {
127-
record.recommended_question = data.content
128-
129-
await nextTick()
112+
switch (data.type) {
113+
case 'recommended_question':
114+
if (
115+
data.content &&
116+
data.content.length > 0 &&
117+
startsWith(data.content.trim(), '[') &&
118+
endsWith(data.content.trim(), ']')
119+
) {
120+
if (_currentChat.value?.records) {
121+
for (let record of _currentChat.value.records) {
122+
if (record.id === props.recordId) {
123+
record.recommended_question = data.content
124+
125+
await nextTick()
126+
}
127+
}
130128
}
131129
}
132-
}
133130
}
131+
}
134132
}
135133
}
136134
}

0 commit comments

Comments
 (0)