From 62456b2e525b1b0c62356f9748a13b24a4267e40 Mon Sep 17 00:00:00 2001 From: Vinlic Date: Mon, 25 Mar 2024 10:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dqwen=E6=B5=81=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E5=BC=82=E5=B8=B8=E5=AD=97=E7=AC=A6=E6=97=B6=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=B5=81=E6=96=87=E6=9C=AC=E5=88=86=E5=89=B2=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index b0d4d72..969b8f9 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -109,6 +109,7 @@ async function createCompletion( headers: { Cookie: generateCookie(ticket), ...FAKE_HEADERS, + Accept: 'text/event-stream' }, timeout: 120000, validateStatus: () => true, @@ -183,6 +184,7 @@ async function createCompletionStream( headers: { Cookie: generateCookie(ticket), ...FAKE_HEADERS, + Accept: 'text/event-stream' }, timeout: 120000, validateStatus: () => true, @@ -310,7 +312,6 @@ async function receiveStream(stream: any): Promise { usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, created: util.unixTimestamp(), }; - let textOffset = 0; const parser = createParser((event) => { try { if (event.type !== "event") return; @@ -325,9 +326,10 @@ async function receiveStream(stream: any): Promise { if (role != "assistant" && !_.isString(content)) return str; return str + content; }, ""); + const exceptCharIndex = text.indexOf('�'); let chunk = text.substring( - data.choices[0].message.content.length - textOffset, - text.length + data.choices[0].message.content.length, + exceptCharIndex == -1 ? text.length : exceptCharIndex ); if (chunk && result.contentType == "text2image") { chunk = chunk.replace( @@ -406,7 +408,8 @@ function createTransStream(stream: any, endCallback?: Function) { if (role != "assistant" && !_.isString(content)) return str; return str + content; }, ""); - let chunk = text.substring(content.length, text.length); + const exceptCharIndex = text.indexOf('�'); + let chunk = text.substring(content.length, exceptCharIndex == -1 ? text.length : exceptCharIndex); if (chunk && result.contentType == "text2image") { chunk = chunk.replace( /https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi, @@ -495,6 +498,11 @@ function generateCookie(ticket: string) { `login_tongyi_ticket=${ticket}`, '_samesite_flag_=true', `t=${util.uuid(false)}`, + 'channel=oug71n2fX3Jd5ualEfKACRvnsceUtpjUC5jHBpfWnSOXKhkvBNuSO8bG3v4HHjCgB722h7LqbHkB6sAxf3OvgA%3D%3D', + 'currentRegionId=cn-shenzhen', + 'aliyun_country=CN', + 'aliyun_lang=zh', + 'aliyun_site=CN', // `login_aliyunid_csrf=_csrf_tk_${util.generateRandomString({ charset: 'numeric', length: 15 })}`, // `cookie2=${util.uuid(false)}`, // `munb=22${util.generateRandomString({ charset: 'numeric', length: 11 })}`,