From ae541f533e492ef0df4d91ec221b2a6b6c293e66 Mon Sep 17 00:00:00 2001 From: Vinlic Date: Mon, 1 Apr 2024 15:46:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9C=89=E5=87=A0=E7=8E=87?= =?UTF-8?q?=E9=81=87=E5=88=B0=EF=BF=BD=E8=BE=93=E5=87=BA=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index dee46b4..c9e2618 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -299,7 +299,7 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre */ async function fakeRequest(refreshToken: string) { const token = await acquireToken(refreshToken); - const options = { + const options = { headers: { Authorization: `Bearer ${token}`, Referer: `https://kimi.moonshot.cn/`, @@ -570,8 +570,9 @@ async function receiveStream(model: string, convId: string, stream: any) { if (_.isError(result)) throw new Error(`Stream response invalid: ${event.data}`); // 处理消息 - if (result.event == 'cmpl') { - data.choices[0].message.content += result.text; + if (result.event == 'cmpl' && result.text) { + const exceptCharIndex = result.text.indexOf("�"); + data.choices[0].message.content += result.text.substring(0, exceptCharIndex == -1 ? result.text.length : exceptCharIndex); } // 处理结束或错误 else if (result.event == 'all_done' || result.event == 'error') { @@ -632,12 +633,14 @@ function createTransStream(model: string, convId: string, stream: any, endCallba throw new Error(`Stream response invalid: ${event.data}`); // 处理消息 if (result.event == 'cmpl') { + const exceptCharIndex = result.text.indexOf("�"); + const chunk = result.text.substring(0, exceptCharIndex == -1 ? result.text.length : exceptCharIndex); const data = `data: ${JSON.stringify({ id: convId, model, object: 'chat.completion.chunk', choices: [ - { index: 0, delta: { content: (searchFlag ? '\n' : '') + result.text }, finish_reason: null } + { index: 0, delta: { content: (searchFlag ? '\n' : '') + chunk }, finish_reason: null } ], created })}\n\n`;