添加多必应token均衡支持 (#299)

This commit is contained in:
HalcyonAlcedo 2023-03-31 10:44:21 +08:00 committed by GitHub
parent 7d89849c88
commit 8fb2615811
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 132 deletions

View file

@ -712,7 +712,8 @@ export class chatgpt extends plugin {
parentMessageId: previousConversation.parentMessageId,
clientId: previousConversation.clientId,
invocationId: previousConversation.invocationId,
conversationSignature: previousConversation.conversationSignature
conversationSignature: previousConversation.conversationSignature,
bingToken: previousConversation.bingToken
}
}
@ -734,6 +735,7 @@ export class chatgpt extends plugin {
previousConversation.invocationId = chatMessage.invocationId
previousConversation.parentMessageId = chatMessage.parentMessageId
previousConversation.conversationSignature = chatMessage.conversationSignature
previousConversation.bingToken = chatMessage.bingToken
} else {
previousConversation.parentMessageId = chatMessage.id
}
@ -754,18 +756,7 @@ export class chatgpt extends plugin {
}
// 分离内容和情绪
if (Config.sydneyMood) {
let temp_response = {}
try {
temp_response = JSON.parse(response)
} catch (error) {
// 尝试还原json格式
try {
temp_response = completeJSON(response)
temp_response = JSON.parse(temp_response)
} catch (error) {
logger.error('数据格式错误', error)
}
}
let temp_response = completeJSON(response)
if (temp_response.text) response = temp_response.text
if (temp_response.mood) mood = temp_response.mood
} else {
@ -1005,6 +996,13 @@ export class chatgpt extends plugin {
}
case 'bing': {
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) {
throw new Error('未绑定Bing Cookie请使用#chatgpt设置必应token命令绑定Bing Cookie')
}
@ -1121,7 +1119,8 @@ export class chatgpt extends plugin {
clientId: response.clientId,
invocationId: response.invocationId,
conversationSignature: response.conversationSignature,
parentMessageId: response.apology ? conversation.parentMessageId : response.messageId
parentMessageId: response.apology ? conversation.parentMessageId : response.messageId,
bingToken: bingToken
}
}
}

View file

@ -28,10 +28,15 @@ export class ChatgptManagement extends plugin {
permission: 'master'
},
{
reg: '#chatgpt(设置|绑定)(必应|Bing |bing )(token|Token)',
reg: '#chatgpt(设置|绑定|添加)(必应|Bing |bing )(token|Token)',
fnc: 'setBingAccessToken',
permission: 'master'
},
{
reg: '#chatgpt(删除|移除)(必应|Bing |bing )(token|Token)',
fnc: 'delBingAccessToken',
permission: 'master'
},
{
reg: '^#chatgpt切换浏览器$',
fnc: 'useBrowserBasedSolution',
@ -148,12 +153,23 @@ export class ChatgptManagement extends plugin {
return false
}
async delBingAccessToken (e) {
this.setContext('deleteBingToken')
let tokens = await redis.get('CHATGPT:BING_TOKEN')
tokens = tokens.split('|')
tokens = tokens.map((item, index) => (
`${index}】 Token${item.substring(0, 5 / 2) + '...' + item.substring(item.length - 5 / 2, item.length)}`
)).join('\n')
await this.reply(`请发送要删除的token编号\n${tokens}`, true)
return false
}
async saveBingToken () {
if (!this.e.msg) return
let token = this.e.msg
if (token.length < 215) {
await this.reply('Bing Token格式错误请确定获取了有效的_U Cookie或完整的Cookie', true)
this.finish('saveToken')
this.finish('saveBingToken')
return
}
let cookie
@ -171,14 +187,37 @@ export class ChatgptManagement extends plugin {
logger.info('bing token 有效')
} else {
logger.error('bing token 无效', res)
// 移除无效token
await this.reply(`经检测Bing Token无效。来自Bing的错误提示${res.result?.message}`)
}
})
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
bingToken = bingToken.split('|')
if (!bingToken.includes(token)) bingToken.push(token)
token = bingToken.join('|')
await redis.set('CHATGPT:BING_TOKEN', token)
await this.reply('Bing Token设置成功', true)
this.finish('saveBingToken')
}
async deleteBingToken () {
if (!this.e.msg) return
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
bingToken = bingToken.split('|')
let tokenId = this.e.msg
if (!bingToken[tokenId]) {
await this.reply('Token编号错误', true)
this.finish('deleteBingToken')
return
}
const removeToken = bingToken[tokenId]
bingToken.splice(tokenId, 1)
let token = bingToken.join('|')
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)
this.finish('deleteBingToken')
}
async saveToken () {
if (!this.e.msg) return
let token = this.e.msg