Skip to content

Commit

Permalink
perf: 优化流式输出,加快数据展示 1Panel-dev#217
Browse files Browse the repository at this point in the history
* perf: 优化流式输出,加快数据展示 1Panel-dev#217
  • Loading branch information
shaohuzhang1 authored Apr 23, 2024
1 parent 7be349c commit 73dfcb6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ui/src/components/ai-chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,15 @@ const getWrite = (chat: any, reader: any, stream: boolean) => {
let str = decoder.decode(value, { stream: true })
// 这里解释一下 start 因为数据流返回流并不是按照后端chunk返回 我们希望得到的chunk是data:{xxx}\n\n 但是它获取到的可能是 data:{ -> xxx}\n\n 总而言之就是 fetch不能保证每个chunk都说以data:开始 \n\n结束
tempResult += str
if (tempResult.endsWith('\n\n')) {
str = tempResult
tempResult = ''
const split = tempResult.match(/data:.*}\n\n/g)
if (split) {
str = split.join('')
tempResult = tempResult.replace(str, '')
} else {
return reader.read().then(write_stream)
}
// 这里解释一下 end
if (str && str.startsWith('data:')) {
const split = str.match(/data:.*}\n\n/g)
if (split) {
for (const index in split) {
const chunk = JSON?.parse(split[index].replace('data:', ''))
Expand Down

0 comments on commit 73dfcb6

Please sign in to comment.