From 8b7ab6a252bc5f836cda0ff8aba5f84beb0dbb37 Mon Sep 17 00:00:00 2001 From: Vinlic Date: Thu, 21 Mar 2024 13:17:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=83=E7=94=A8tool?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E6=96=87=E6=9C=AC=E5=AF=BC=E8=87=B4=E5=90=8E?= =?UTF-8?q?=E7=BB=AD=E5=86=85=E5=AE=B9=E6=B7=B7=E4=B9=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 4fc43fb..e278fc7 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -480,6 +480,7 @@ async function receiveStream(stream: any): Promise { }; let toolCall = false; let codeGenerating = false; + let textChunkLength = 0; let codeTemp = ''; let lastExecutionOutput = ''; let textOffset = 0; @@ -499,12 +500,19 @@ async function receiveStream(stream: any): Promise { return str; const partText = content.reduce((innerStr, value) => { const { status: partStatus, type, text, image, code, content } = value; + if(partStatus == 'init' && textChunkLength > 0) { + textOffset += textChunkLength + 1; + textChunkLength = 0; + innerStr += '\n'; + } if(type == 'text') { if(toolCall) { innerStr += '\n'; textOffset++; toolCall = false; } + if(partStatus == 'finish') + textChunkLength = text.length; return innerStr + text; } else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) { @@ -531,7 +539,7 @@ async function receiveStream(stream: any): Promise { return innerStr + codeHead + chunk; } else if(type == 'code' && partStatus == 'finish' && codeGenerating) { - const codeFooter = '\n```'; + const codeFooter = '```\n'; codeGenerating = false; codeTemp = ''; textOffset += codeFooter.length; @@ -583,6 +591,7 @@ function createTransStream(stream: any, endCallback?: Function) { let content = ''; let toolCall = false; let codeGenerating = false; + let textChunkLength = 0; let codeTemp = ''; let lastExecutionOutput = ''; let textOffset = 0; @@ -609,13 +618,20 @@ function createTransStream(stream: any, endCallback?: Function) { return str; const partText = content.reduce((innerStr, value) => { const { status: partStatus, type, text, image, code, content } = value; + if(partStatus == 'init' && textChunkLength > 0) { + textOffset += textChunkLength + 1; + textChunkLength = 0; + innerStr += '\n'; + } if(type == 'text') { if(toolCall) { innerStr += '\n'; textOffset++; toolCall = false; } - return innerStr + text.replace(/【\d+†source】/g, ''); + if(partStatus == 'finish') + textChunkLength = text.length; + return innerStr + text; } else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) { const searchText = meta_data.metadata_list.reduce((meta, v) => meta + `检索 ${v.title}(${v.url}) ...`, '') + '\n'; @@ -641,7 +657,7 @@ function createTransStream(stream: any, endCallback?: Function) { return innerStr + codeHead + chunk; } else if(type == 'code' && partStatus == 'finish' && codeGenerating) { - const codeFooter = '\n```'; + const codeFooter = '```\n'; codeGenerating = false; codeTemp = ''; textOffset += codeFooter.length;