mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
fix: 加入重试机制
This commit is contained in:
parent
233fc7417c
commit
b0738c6db6
4 changed files with 39 additions and 27 deletions
42
apps/chat.js
42
apps/chat.js
|
|
@ -6,7 +6,7 @@ import mjAPI from 'mathjax-node'
|
|||
import { uuid } from 'oicq/lib/common.js'
|
||||
import delay from 'delay'
|
||||
import { ChatGPTAPI } from 'chatgpt'
|
||||
import { getMessageById, upsertMessage } from '../utils/common.js'
|
||||
import {getMessageById, tryTimes, upsertMessage} from '../utils/common.js'
|
||||
// import puppeteer from '../utils/browser.js'
|
||||
// import showdownKatex from 'showdown-katex'
|
||||
const blockWords = Config.blockWords
|
||||
|
|
@ -28,7 +28,7 @@ const converter = new showdown.Converter({
|
|||
* @type {number}
|
||||
*/
|
||||
const CONVERSATION_PRESERVE_TIME = Config.conversationPreserveTime
|
||||
|
||||
const defaultPropmtPrefix = 'You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short.'
|
||||
mjAPI.config({
|
||||
MathJax: {
|
||||
// traditional MathJax configuration
|
||||
|
|
@ -51,7 +51,7 @@ export class chatgpt extends plugin {
|
|||
rule: [
|
||||
{
|
||||
/** 命令正则匹配 */
|
||||
reg: toggleMode === 'at' ? '^[^#][sS]*' : '#chat[sS]*',
|
||||
reg: toggleMode === 'at' ? '^[^#][sS]*' : '#chat[^gpt][sS]*',
|
||||
/** 执行方法 */
|
||||
fnc: 'chatgpt'
|
||||
},
|
||||
|
|
@ -256,20 +256,9 @@ export class chatgpt extends plugin {
|
|||
parentMessageId: previousConversation.conversation.parentMessageId
|
||||
}
|
||||
}
|
||||
const currentDate = new Date().toISOString().split('T')[0]
|
||||
let defaultPropmtPrefix = 'You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short.'
|
||||
let promptPrefix = `You are ${Config.assistantLabel}, a large language model trained by OpenAI. ${Config.promptPrefixOverride || defaultPropmtPrefix}
|
||||
Knowledge cutoff: 2021-09
|
||||
Current date: ${currentDate}`
|
||||
|
||||
try {
|
||||
let option = {
|
||||
timeoutMs: 120000,
|
||||
promptPrefix
|
||||
}
|
||||
if (conversation) {
|
||||
option = Object.assign(option, conversation)
|
||||
}
|
||||
let chatMessage = await this.chatGPTApi.sendMessage(prompt, option)
|
||||
let chatMessage = await this.sendMessage(prompt, conversation, this.chatGPTApi)
|
||||
previousConversation.conversation = {
|
||||
conversationId: chatMessage.conversationId,
|
||||
parentMessageId: chatMessage.id
|
||||
|
|
@ -300,12 +289,7 @@ export class chatgpt extends plugin {
|
|||
// !response.trimEnd().endsWith('!') && !response.trimEnd().endsWith('!') && !response.trimEnd().endsWith(']') && !response.trimEnd().endsWith('】')
|
||||
// ) {
|
||||
await this.reply('内容有点多,我正在奋笔疾书,请再等一会', true, { recallMsg: 5 })
|
||||
option = {
|
||||
timeoutMs: 120000,
|
||||
promptPrefix
|
||||
}
|
||||
option = Object.assign(option, previousConversation.conversation)
|
||||
const responseAppend = await this.chatGPTApi.sendMessage('Continue', option)
|
||||
let responseAppend = await this.sendMessage('Continue', conversation, this.chatGPTApi)
|
||||
previousConversation.conversation = {
|
||||
conversationId: responseAppend.conversationId,
|
||||
parentMessageId: responseAppend.id
|
||||
|
|
@ -346,6 +330,20 @@ export class chatgpt extends plugin {
|
|||
}
|
||||
}
|
||||
|
||||
async sendMessage (prompt, conversation, api) {
|
||||
const currentDate = new Date().toISOString().split('T')[0]
|
||||
let promptPrefix = `You are ${Config.assistantLabel}, a large language model trained by OpenAI. ${Config.promptPrefixOverride || defaultPropmtPrefix}
|
||||
Current date: ${currentDate}`
|
||||
let option = {
|
||||
timeoutMs: 120000,
|
||||
promptPrefix
|
||||
}
|
||||
if (conversation) {
|
||||
option = Object.assign(option, conversation)
|
||||
}
|
||||
return await tryTimes(async () => await api.sendMessage(prompt, option), 5)
|
||||
}
|
||||
|
||||
async emptyQueue (e) {
|
||||
await redis.lTrim('CHATGPT:CHAT_QUEUE', 1, 0)
|
||||
await this.reply('已清空当前等待队列')
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ export class ChatgptManagement extends plugin {
|
|||
priority: 500,
|
||||
rule: [
|
||||
{
|
||||
reg: '#chatgpt开启(问题)?确认',
|
||||
reg: '#chatgpt开启(问题)?(回复)?确认',
|
||||
fnc: 'turnOnConfirm'
|
||||
},
|
||||
{
|
||||
reg: '#chatgpt关闭(问题)?确认',
|
||||
reg: '#chatgpt关闭(问题)?(回复)?确认',
|
||||
fnc: 'turnOffConfirm'
|
||||
}
|
||||
]
|
||||
|
|
@ -23,10 +23,12 @@ export class ChatgptManagement extends plugin {
|
|||
async turnOnConfirm (e) {
|
||||
await redis.set('CHATGPT:CONFIRM', 'on')
|
||||
await this.reply('已开启消息确认', true)
|
||||
return false
|
||||
}
|
||||
|
||||
async turnOffConfirm (e) {
|
||||
await redis.set('CHATGPT:CONFIRM', 'off')
|
||||
await this.reply('已关闭消息确认', true)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ export const Config = {
|
|||
// 触发方式 可选值:at 或 prefix 。at模式下只有at机器人才会回复。prefix模式下不需要at,但需要添加前缀#chat
|
||||
toggleMode: 'at',
|
||||
// 默认完整值:`You are ${this._assistantLabel}, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. Current date: ${currentDate}\n\n
|
||||
// 此项配置会覆盖掉中间部分
|
||||
// 你可以在这里写入你希望AI回答的风格,比如希望优先回答中文,去掉尽可能简洁的要求等
|
||||
// 此项配置会覆盖掉中间部分。保持为空将使用网友从对话中推测出的指令。
|
||||
// 你可以在这里写入你希望AI回答的风格,比如希望优先回答中文,回答长一点等
|
||||
promptPrefixOverride: '',
|
||||
// AI认为的自己的名字。
|
||||
// AI认为的自己的名字,当你问他你是谁是他会回答这里的名字。
|
||||
assistantLabel: 'ChatGPT'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,3 +15,15 @@ export async function getMessageById (id) {
|
|||
let messageStr = await redis.get(`CHATGPT:MESSAGE:${id}`)
|
||||
return JSON.parse(messageStr)
|
||||
}
|
||||
|
||||
export async function tryTimes (promiseFn, maxTries = 10) {
|
||||
try {
|
||||
return await promiseFn()
|
||||
} catch (e) {
|
||||
if (maxTries > 0) {
|
||||
logger.warn('Failed, retry ' + maxTries)
|
||||
return tryTimes(promiseFn, maxTries - 1)
|
||||
}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue