From c11732be4cde88e4d183e4f0f65f4c2b3bf01ea4 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sun, 5 Mar 2023 20:34:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20api=E6=A8=A1=E5=BC=8F=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E5=AD=97=E6=95=B0=E8=B6=85=E9=99=90=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E5=AF=B9=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/chat.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/chat.js b/apps/chat.js index 21a1fad..bad45da 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -574,6 +574,10 @@ export class chatgpt extends plugin { logger.mark({ conversation }) } let chatMessage = await this.sendMessage(prompt, conversation, use, e) + if (use === 'api' && !chatMessage) { + // 字数超限直接返回 + return false + } if (use !== 'api3') { previousConversation.conversation = { conversationId: chatMessage.conversationId @@ -837,7 +841,19 @@ export class chatgpt extends plugin { if (conversation) { option = Object.assign(option, conversation) } - return await tryTimes(async () => await this.chatGPTApi.sendMessage(prompt, option), 1) + let msg + try { + msg = await this.chatGPTApi.sendMessage(prompt, option) + } catch (err) { + if (err.message?.indexOf('context_length_exceeded')) { + await redis.del(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`) + await e.reply('字数超限啦,将为您自动结束本次对话。') + return null + } else { + throw new Error(err) + } + } + return msg } } }