Skip to content

Commit 91269c9

Browse files
fix: 修复最后一个流为[xxxx]格式时会重复输出1次 (#5673)
* fix: 修复最后一个流为[xxxx]格式时会重复输出1次 issue #5292 当最后一个流中包含[xxxx]格式时,会输出2次。第一次在parseCite方法中if (isStreamEnd) {分支内输出,第二次在调用getResponseData时会输出buffer中的内容,导致重复。 * add test case --------- Co-authored-by: archer <545436317@qq.com>
1 parent 206fd7e commit 91269c9

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

packages/service/core/ai/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ export const parseLLMStreamResponse = () => {
243243
// 结束时,返回所有剩余内容
244244
if (isStreamEnd) {
245245
const content = citeBuffer + text;
246+
citeBuffer = ''; // 清空缓冲区,避免重复输出
246247
return {
247248
content: removeDatasetCiteText(content, false)
248249
};

test/cases/service/core/ai/parseStreamResponse.test.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ describe('Parse dataset cite content test', async () => {
158158
data: [
159159
{ content: '知识库' },
160160
{ content: '问答系统' },
161-
{ content: '[67e517e747' },
162-
{ content: '67063e882d' },
163-
{ content: '6861](CITE)' }
161+
{ content: '[67e517e74767063e882d6861](CITE)' }
164162
],
165163
correct: {
166164
content: '知识库问答系统[67e517e74767063e882d6861](CITE)',
@@ -400,6 +398,19 @@ describe('Parse dataset cite content test', async () => {
400398
content: '知识库问答系统[id](CITE)[67e517e74767063e882d6861](CITE)',
401399
responseContent: '知识库问答系统'
402400
}
401+
},
402+
{
403+
// [id](CITE)
404+
data: [
405+
{ content: '知识库' },
406+
{ content: '问答系统' },
407+
{ content: '[i' },
408+
{ content: 'd](CITE)' }
409+
],
410+
correct: {
411+
content: '知识库问答系统[id](CITE)',
412+
responseContent: '知识库问答系统'
413+
}
403414
}
404415
];
405416

@@ -409,7 +420,8 @@ describe('Parse dataset cite content test', async () => {
409420

410421
let answer = '';
411422
let responseContent = '';
412-
part.data.forEach((item, index) => {
423+
const list = [...part.data, { content: '' }];
424+
list.forEach((item, index) => {
413425
const formatPart = {
414426
choices: [
415427
{
@@ -418,9 +430,7 @@ describe('Parse dataset cite content test', async () => {
418430
content: item.content,
419431
reasoning_content: ''
420432
},
421-
finish_reason: (index === part.data.length - 1
422-
? 'stop'
423-
: null) as CompletionFinishReason
433+
finish_reason: (index === list.length - 2 ? 'stop' : null) as CompletionFinishReason
424434
}
425435
]
426436
};

0 commit comments

Comments
 (0)