From 2ca69ed75875f4d5b31a7f5e3ae034c05f6dec5b Mon Sep 17 00:00:00 2001 From: HalcyonAlcedo <41666148+HalcyonAlcedo@users.noreply.github.com> Date: Thu, 16 Feb 2023 16:15:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8chatgpt-api=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E7=9A=84=E8=B6=85=E6=97=B6=E6=9C=BA=E5=88=B6=EF=BC=8C=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E6=96=87=E5=AD=97=E8=BF=87=E5=A4=9A=E6=97=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=9B=BE=E7=89=87=E5=8F=91=E9=80=81=20(#154)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复引用转发,默认bing模式并发 * 开启stream增加稳定性 * fix: remove queue element only in non-bing mode * 使用chatgpt-api自带的超时逻辑,文字过多时启动切换到图片输出防止被吞 * Update chat.js --------- Co-authored-by: ikechan8370 --- apps/chat.js | 22 ++++++++++++++++++---- config/index.js | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/chat.js b/apps/chat.js index c29583a..3e18d23 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -327,11 +327,16 @@ export class chatgpt extends plugin { /** 最后回复消息 */ await e.runtime.render('chatgpt-plugin', 'content/index', { content: converted, prompt, senderName: e.sender.nickname }) } else { + if (response.length > 1000 ) { + // 文字过多时自动切换到图片模式输出 + let converted = converter.makeHtml(response) + await e.runtime.render('chatgpt-plugin', 'content/index', { content: converted, prompt, quote: chatMessage.quote, senderName: e.sender.nickname }) + } else await this.reply(`${response}`, e.isGroup) if (chatMessage?.quote) { let quotemessage = [] chatMessage.quote.forEach(function (item, index) { - if (item != '') { + if (item.trim() != '') { quotemessage.push(`${item}\n`) } }) @@ -418,19 +423,28 @@ export class chatgpt extends plugin { debug: Config.debug }) let response + let reply = '' try { + /* bingAIClient中设置了无响应2分钟超时,应该不用单独处理了,后续看情况可以删掉这些代码 const responseP = new Promise( async (resolve, reject) => { let bingResponse = await bingAIClient.sendMessage(prompt, conversation || {},(token) => { - console.debug(token); + reply += token }) return resolve(bingResponse) }) response = await pTimeout(responseP, { milliseconds: Config.bingTimeoutMs, - message: 'Bing timed out waiting for response' + message: reply != '' ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑子不好使了,不知道怎么回答!' + }) + */ + response = await bingAIClient.sendMessage(prompt, conversation || {}, (token) => { + reply += token }) if (response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim()) { + if (response.response === undefined) { + response.response = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim() + } response.response = response.response.replace(/\[\^[0-9]+\^\]/g, (str) => { return str.replace(/[/^]/g, '') }) @@ -444,7 +458,7 @@ export class chatgpt extends plugin { console.error(error) const message = error?.message || error?.data?.message || '与Bing通信时出错.' return { - text: message + text: message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply != '' ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message } } return { diff --git a/config/index.js b/config/index.js index 14a609b..f227d98 100644 --- a/config/index.js +++ b/config/index.js @@ -60,11 +60,11 @@ export const Config = { '2captchaToken': '', // http或socks5代理 proxy: PROXY, - debug: true, + debug: false, // 各个地方的默认超时时间 defaultTimeoutMs: 120000, // bing默认超时时间,bing太慢了有的时候 - bingTimeoutMs: 360000, + //bingTimeoutMs: 360000, // 浏览器默认超时,浏览器可能需要更高的超时时间 chromeTimeoutMS: 120000 }