修复引用,为必应开启并发 (#151)

* 修复引用转发,默认bing模式并发

* 开启stream增加稳定性

* fix: remove queue element only in non-bing mode

---------

Co-authored-by: ikechan8370 <geyinchibuaa@gmail.com>
This commit is contained in:
HalcyonAlcedo 2023-02-15 16:16:37 +08:00 committed by GitHub
parent 5237975fbd
commit 1296eccb48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -201,32 +201,33 @@ export class chatgpt extends plugin {
return false return false
} }
} }
const use = await redis.get('CHATGPT:USE')
let randomId = uuid() if (use != 'bing') {
// 队列队尾插入,开始排队 let randomId = uuid()
await redis.rPush('CHATGPT:CHAT_QUEUE', [randomId]) // 队列队尾插入,开始排队
let confirm = await redis.get('CHATGPT:CONFIRM') await redis.rPush('CHATGPT:CHAT_QUEUE', [randomId])
let confirmOn = confirm === 'on' let confirm = await redis.get('CHATGPT:CONFIRM')
if (await redis.lIndex('CHATGPT:CHAT_QUEUE', 0) === randomId) { let confirmOn = confirm === 'on'
if (confirmOn) { if (await redis.lIndex('CHATGPT:CHAT_QUEUE', 0) === randomId) {
await this.reply('我正在思考如何回复你,请稍等', true, { recallMsg: 8 }) if (confirmOn) {
} await this.reply('我正在思考如何回复你,请稍等', true, { recallMsg: 8 })
} else { }
if (confirmOn) { } else {
let length = await redis.lLen('CHATGPT:CHAT_QUEUE') - 1 if (confirmOn) {
await this.reply(`我正在思考如何回复你,请稍等,当前队列前方还有${length}个问题`, true, { recallMsg: 8 }) let length = await redis.lLen('CHATGPT:CHAT_QUEUE') - 1
logger.info(`chatgpt队列前方还有${length}个问题。管理员可通过#清空队列来强制清除所有等待的问题。`) await this.reply(`我正在思考如何回复你,请稍等,当前队列前方还有${length}个问题`, true, { recallMsg: 8 })
} logger.info(`chatgpt队列前方还有${length}个问题。管理员可通过#清空队列来强制清除所有等待的问题。`)
// 开始排队 }
while (true) { // 开始排队
if (await redis.lIndex('CHATGPT:CHAT_QUEUE', 0) === randomId) { while (true) {
break if (await redis.lIndex('CHATGPT:CHAT_QUEUE', 0) === randomId) {
} else { break
await delay(1500) } else {
await delay(1500)
}
} }
} }
} }
logger.info(`chatgpt prompt: ${prompt}`) logger.info(`chatgpt prompt: ${prompt}`)
// try { // try {
// await this.chatGPTApi.init() // await this.chatGPTApi.init()
@ -254,7 +255,6 @@ export class chatgpt extends plugin {
conversationSignature: previousConversation.conversationSignature conversationSignature: previousConversation.conversationSignature
} }
} }
const use = await redis.get('CHATGPT:USE')
try { try {
if (Config.debug) { if (Config.debug) {
logger.mark(conversation) logger.mark(conversation)
@ -331,20 +331,25 @@ export class chatgpt extends plugin {
if (chatMessage?.quote) { if (chatMessage?.quote) {
let quotemessage = [] let quotemessage = []
chatMessage.quote.forEach(function (item, index) { chatMessage.quote.forEach(function (item, index) {
if (item) { if (item != '') {
quotemessage.push(`${item}\n`) quotemessage.push(`${item}\n`)
} }
}) })
if(quotemessage.length > 0)
this.reply(await makeForwardMsg(this.e, quotemessage)) this.reply(await makeForwardMsg(this.e, quotemessage))
} }
} }
// 移除队列首位,释放锁 if (use !== 'bing') {
await redis.lPop('CHATGPT:CHAT_QUEUE', 0) // 移除队列首位,释放锁
await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
}
} catch (e) { } catch (e) {
logger.error(e) logger.error(e)
// 异常了也要腾地方todo 大概率后面的也会异常,要不要一口气全杀了) if (use !== 'bing') {
await redis.lPop('CHATGPT:CHAT_QUEUE', 0) // 异常了也要腾地方todo 大概率后面的也会异常,要不要一口气全杀了)
await this.reply(`与OpenAI通信异常请稍后重试${e}`, true, { recallMsg: e.isGroup ? 10 : 0 }) await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
}
await this.reply(`通信异常,请稍后重试:${e}`, true, { recallMsg: e.isGroup ? 10 : 0 })
} }
} }
@ -411,7 +416,9 @@ export class chatgpt extends plugin {
try { try {
const responseP = new Promise( const responseP = new Promise(
async (resolve, reject) => { async (resolve, reject) => {
let bingResponse = await bingAIClient.sendMessage(prompt, conversation || {}) let bingResponse = await bingAIClient.sendMessage(prompt, conversation || {},(token) => {
console.debug(token);
})
return resolve(bingResponse) return resolve(bingResponse)
}) })
response = await pTimeout(responseP, { response = await pTimeout(responseP, {
@ -422,7 +429,7 @@ export class chatgpt extends plugin {
response.response = response.response.replace(/\[\^[0-9]+\^\]/g, (str) => { response.response = response.response.replace(/\[\^[0-9]+\^\]/g, (str) => {
return str.replace(/[/^]/g, '') return str.replace(/[/^]/g, '')
}) })
response.quote = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim().replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n') response.quote = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n')
} }
} catch (error) { } catch (error) {
const code = error?.data?.code || 503 const code = error?.data?.code || 503