fix: 修复多token功能带来的一些影响

This commit is contained in:
ikechan8370 2023-04-01 12:25:20 +08:00
parent e93614ca35
commit fd408586a8
2 changed files with 32 additions and 13 deletions

View file

@ -206,6 +206,13 @@ export class chatgpt extends plugin {
await redis.del(`CHATGPT:QQ_CONVERSATION:${e.sender.user_id}`) await redis.del(`CHATGPT:QQ_CONVERSATION:${e.sender.user_id}`)
await this.reply('已退出当前对话,该对话仍然保留。请@我进行聊天以开启新的对话', true) await this.reply('已退出当前对话,该对话仍然保留。请@我进行聊天以开启新的对话', true)
} else if (use === 'bing' && (Config.toneStyle === 'Sydney' || Config.toneStyle === 'Custom')) { } else if (use === 'bing' && (Config.toneStyle === 'Sydney' || Config.toneStyle === 'Custom')) {
let c = await redis.get(`CHATGPT:CONVERSATIONS_BING:${e.sender.user_id}`)
if (!c) {
await this.reply('当前没有开启对话', true)
return
} else {
await redis.del(`CHATGPT:CONVERSATIONS_BING:${e.sender.user_id}`)
}
const conversation = { const conversation = {
store: new KeyvFile({ filename: 'cache.json' }), store: new KeyvFile({ filename: 'cache.json' }),
namespace: Config.toneStyle namespace: Config.toneStyle
@ -735,7 +742,11 @@ export class chatgpt extends plugin {
previousConversation.invocationId = chatMessage.invocationId previousConversation.invocationId = chatMessage.invocationId
previousConversation.parentMessageId = chatMessage.parentMessageId previousConversation.parentMessageId = chatMessage.parentMessageId
previousConversation.conversationSignature = chatMessage.conversationSignature previousConversation.conversationSignature = chatMessage.conversationSignature
if (Config.toneStyle !== 'Sydney' && Config.toneStyle !== 'Custom') {
previousConversation.bingToken = chatMessage.bingToken previousConversation.bingToken = chatMessage.bingToken
} else {
previousConversation.bingToken = ''
}
} else { } else {
previousConversation.parentMessageId = chatMessage.id previousConversation.parentMessageId = chatMessage.id
} }
@ -996,16 +1007,24 @@ export class chatgpt extends plugin {
} }
case 'bing': { case 'bing': {
let bingToken = await redis.get('CHATGPT:BING_TOKEN') let bingToken = await redis.get('CHATGPT:BING_TOKEN')
// 负载均衡
if (!conversation.bingToken) {
const bingTokens = bingToken.split('|')
const select = Math.floor(Math.random() * bingTokens.length)
bingToken = bingTokens[select]
} else bingToken = conversation.bingToken
if (!bingToken) { if (!bingToken) {
throw new Error('未绑定Bing Cookie请使用#chatgpt设置必应token命令绑定Bing Cookie') throw new Error('未绑定Bing Cookie请使用#chatgpt设置必应token命令绑定Bing Cookie')
} }
const bingTokens = bingToken.split('|')
// 负载均衡
if (Config.toneStyle === 'Sydney' || Config.toneStyle === 'Custom') {
// sydney下不需要保证同一token
const select = Math.floor(Math.random() * bingTokens.length)
bingToken = bingTokens[select]
} else {
// bing 下需要保证同一对话使用同一账号的token
if (!conversation.bingToken) {
const select = Math.floor(Math.random() * bingTokens.length)
bingToken = bingTokens[select]
} else if (bingTokens.indexOf(conversation.bingToken) > -1) {
bingToken = conversation.bingToken
}
}
let cookies let cookies
if (bingToken?.indexOf('=') > -1) { if (bingToken?.indexOf('=') > -1) {
cookies = bingToken cookies = bingToken

View file

@ -3,7 +3,7 @@ import { Config } from '../utils/config.js'
import { BingAIClient } from '@waylaidwanderer/chatgpt-api' import { BingAIClient } from '@waylaidwanderer/chatgpt-api'
import { exec } from 'child_process' import { exec } from 'child_process'
import { checkPnpm, formatDuration, parseDuration } from '../utils/common.js' import { checkPnpm, formatDuration, parseDuration } from '../utils/common.js'
import SydneyAIClient from "../utils/SydneyAIClient.js"; import SydneyAIClient from '../utils/SydneyAIClient.js'
export class ChatgptManagement extends plugin { export class ChatgptManagement extends plugin {
constructor (e) { constructor (e) {
@ -211,7 +211,7 @@ export class ChatgptManagement extends plugin {
let bingToken = await redis.get('CHATGPT:BING_TOKEN') let bingToken = await redis.get('CHATGPT:BING_TOKEN')
bingToken = bingToken.split('|') bingToken = bingToken.split('|')
if (!bingToken.includes(token)) bingToken.push(token) if (!bingToken.includes(token)) bingToken.push(token)
bingToken = bingToken.filter (function (element) { return element !== '' }) bingToken = bingToken.filter(function (element) { return element !== '' })
token = bingToken.join('|') token = bingToken.join('|')
} }
await redis.set('CHATGPT:BING_TOKEN', token) await redis.set('CHATGPT:BING_TOKEN', token)
@ -224,14 +224,14 @@ export class ChatgptManagement extends plugin {
let bingToken = await redis.get('CHATGPT:BING_TOKEN') let bingToken = await redis.get('CHATGPT:BING_TOKEN')
bingToken = bingToken.split('|') bingToken = bingToken.split('|')
let tokenId = this.e.msg let tokenId = this.e.msg
if (!bingToken[tokenId]) { if (bingToken[tokenId] === null || bingToken[tokenId] === undefined) {
await this.reply('Token编号错误', true) await this.reply('Token编号错误', true)
this.finish('deleteBingToken') this.finish('deleteBingToken')
return return
} }
const removeToken = bingToken[tokenId] const removeToken = bingToken[tokenId]
bingToken.splice(tokenId, 1) bingToken.splice(tokenId, 1)
bingToken = bingToken.filter (function (element) { return element !== '' }) bingToken = bingToken.filter(function (element) { return element !== '' })
let token = bingToken.join('|') let token = bingToken.join('|')
await redis.set('CHATGPT:BING_TOKEN', token) await redis.set('CHATGPT:BING_TOKEN', token)
await this.reply(`Token ${removeToken.substring(0, 5 / 2) + '...' + removeToken.substring(removeToken.length - 5 / 2, removeToken.length)} 移除成功`, true) await this.reply(`Token ${removeToken.substring(0, 5 / 2) + '...' + removeToken.substring(removeToken.length - 5 / 2, removeToken.length)} 移除成功`, true)