mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 13:57:10 +00:00
feat: 通过降级messageType手段复活限流的账户
This commit is contained in:
parent
14b4d3cdf4
commit
95a4d7a69c
3 changed files with 69 additions and 27 deletions
78
apps/chat.js
78
apps/chat.js
|
|
@ -1006,25 +1006,8 @@ export class chatgpt extends plugin {
|
||||||
return await this.chatgptBrowserBased(prompt, conversation)
|
return await this.chatgptBrowserBased(prompt, conversation)
|
||||||
}
|
}
|
||||||
case 'bing': {
|
case 'bing': {
|
||||||
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
|
let throttledTokens = []
|
||||||
if (!bingToken) {
|
let { bingToken, allThrottled } = await getAvailableBingToken(conversation, throttledTokens)
|
||||||
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
|
||||||
|
|
@ -1064,11 +1047,21 @@ export class chatgpt extends plugin {
|
||||||
let reply = ''
|
let reply = ''
|
||||||
let retry = 3
|
let retry = 3
|
||||||
let errorMessage = ''
|
let errorMessage = ''
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
let abtrs = await getAvailableBingToken(conversation, throttledTokens)
|
||||||
|
bingToken = abtrs.bingToken
|
||||||
|
allThrottled = abtrs.allThrottled
|
||||||
|
if (bingToken?.indexOf('=') > -1) {
|
||||||
|
cookies = bingToken
|
||||||
|
}
|
||||||
|
bingAIClient.opts.userToken = bingToken
|
||||||
|
bingAIClient.opts.cookies = cookies
|
||||||
try {
|
try {
|
||||||
let opt = _.cloneDeep(conversation) || {}
|
let opt = _.cloneDeep(conversation) || {}
|
||||||
opt.toneStyle = Config.toneStyle
|
opt.toneStyle = Config.toneStyle
|
||||||
opt.context = Config.sydneyContext
|
opt.context = Config.sydneyContext
|
||||||
|
opt.messageType = allThrottled ? 'Chat' : 'SearchQuery'
|
||||||
if (Config.enableGroupContext && e.isGroup) {
|
if (Config.enableGroupContext && e.isGroup) {
|
||||||
try {
|
try {
|
||||||
opt.groupId = e.group_id
|
opt.groupId = e.group_id
|
||||||
|
|
@ -1119,8 +1112,13 @@ export class chatgpt extends plugin {
|
||||||
break
|
break
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error?.message || error?.data?.message || error || '出错了'
|
const message = error?.message || error?.data?.message || error || '出错了'
|
||||||
retry--
|
if (message.indexOf('限流') > -1) {
|
||||||
errorMessage = message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message
|
throttledTokens.push(bingToken)
|
||||||
|
// 不减次数
|
||||||
|
} else {
|
||||||
|
retry--
|
||||||
|
errorMessage = message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} while (retry > 0)
|
} while (retry > 0)
|
||||||
if (errorMessage) {
|
if (errorMessage) {
|
||||||
|
|
@ -1139,7 +1137,7 @@ export class chatgpt extends plugin {
|
||||||
invocationId: response.invocationId,
|
invocationId: response.invocationId,
|
||||||
conversationSignature: response.conversationSignature,
|
conversationSignature: response.conversationSignature,
|
||||||
parentMessageId: response.apology ? conversation.parentMessageId : response.messageId,
|
parentMessageId: response.apology ? conversation.parentMessageId : response.messageId,
|
||||||
bingToken: bingToken
|
bingToken
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1368,3 +1366,39 @@ export class chatgpt extends plugin {
|
||||||
return await this.chatGPTApi.sendMessage(prompt, sendMessageOption)
|
return await this.chatGPTApi.sendMessage(prompt, sendMessageOption)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getAvailableBingToken (conversation, throttled = []) {
|
||||||
|
let allThrottled = false
|
||||||
|
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
|
||||||
|
if (!bingToken) {
|
||||||
|
throw new Error('未绑定Bing Cookie,请使用#chatgpt设置必应token命令绑定Bing Cookie')
|
||||||
|
}
|
||||||
|
const bingTokens = bingToken.split('|')
|
||||||
|
// 负载均衡
|
||||||
|
if (Config.toneStyle === 'Sydney' || Config.toneStyle === 'Custom') {
|
||||||
|
// sydney下不需要保证同一token
|
||||||
|
let notThrottled = bingTokens.filter(t => throttled.indexOf(t) === -1)
|
||||||
|
if (notThrottled.length > 0) {
|
||||||
|
bingToken = notThrottled[0]
|
||||||
|
} else {
|
||||||
|
// 全都被限流了,随便找一个算了
|
||||||
|
allThrottled = true
|
||||||
|
const select = Math.floor(Math.random() * bingTokens.length)
|
||||||
|
bingToken = bingTokens[select]
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
bingToken,
|
||||||
|
allThrottled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -217,8 +217,12 @@ export default class SydneyAIClient {
|
||||||
abortController = new AbortController(),
|
abortController = new AbortController(),
|
||||||
timeout = Config.defaultTimeoutMs,
|
timeout = Config.defaultTimeoutMs,
|
||||||
firstMessageTimeout = Config.sydneyFirstMessageTimeout,
|
firstMessageTimeout = Config.sydneyFirstMessageTimeout,
|
||||||
groupId, nickname, qq, groupName, chats, botName, masterName
|
groupId, nickname, qq, groupName, chats, botName, masterName,
|
||||||
|
messageType = 'SearchQuery'
|
||||||
} = opts
|
} = opts
|
||||||
|
if (messageType === 'Chat') {
|
||||||
|
logger.warn('该Bing账户token已被限流,降级至使用非搜索模式。本次对话AI将无法使用Bing搜索返回的内容')
|
||||||
|
}
|
||||||
if (typeof onProgress !== 'function') {
|
if (typeof onProgress !== 'function') {
|
||||||
onProgress = () => {}
|
onProgress = () => {}
|
||||||
}
|
}
|
||||||
|
|
@ -382,8 +386,8 @@ export default class SydneyAIClient {
|
||||||
author: 'user',
|
author: 'user',
|
||||||
inputMethod: 'Keyboard',
|
inputMethod: 'Keyboard',
|
||||||
text: message,
|
text: message,
|
||||||
// messageType: 'Chat'
|
messageType
|
||||||
messageType: 'SearchQuery'
|
// messageType: 'SearchQuery'
|
||||||
},
|
},
|
||||||
conversationSignature,
|
conversationSignature,
|
||||||
participant: {
|
participant: {
|
||||||
|
|
@ -621,6 +625,10 @@ export default class SydneyAIClient {
|
||||||
if (event.item?.result) {
|
if (event.item?.result) {
|
||||||
if (event.item?.result?.exception?.indexOf('maximum context length') > -1) {
|
if (event.item?.result?.exception?.indexOf('maximum context length') > -1) {
|
||||||
reject('对话长度太长啦!超出8193token,请结束对话重新开始')
|
reject('对话长度太长啦!超出8193token,请结束对话重新开始')
|
||||||
|
} else if (event.item?.result.value === 'Throttled') {
|
||||||
|
reject('该账户的SERP请求已被限流')
|
||||||
|
logger.warn('该账户的SERP请求已被限流')
|
||||||
|
logger.warn(JSON.stringify(event.item?.result))
|
||||||
} else {
|
} else {
|
||||||
reject(`${event.item?.result.value}\n${event.item?.result.error}\n${event.item?.result.exception}`)
|
reject(`${event.item?.result.value}\n${event.item?.result.error}\n${event.item?.result.exception}`)
|
||||||
}
|
}
|
||||||
|
|
@ -717,7 +725,7 @@ export default class SydneyAIClient {
|
||||||
conversationExpiryTime,
|
conversationExpiryTime,
|
||||||
response: reply.text,
|
response: reply.text,
|
||||||
details: reply,
|
details: reply,
|
||||||
apology: Config.sydneyApologyIgnored && apology
|
apology: Config.sydneyApologyIgnored && apology,
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.conversationsCache.set(conversationKey, conversation)
|
await this.conversationsCache.set(conversationKey, conversation)
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ const defaultConfig = {
|
||||||
maxNumUserMessagesInConversation: 20,
|
maxNumUserMessagesInConversation: 20,
|
||||||
sydneyApologyIgnored: true,
|
sydneyApologyIgnored: true,
|
||||||
enforceMaster: false,
|
enforceMaster: false,
|
||||||
version: 'v2.4.10'
|
version: 'v2.4.11'
|
||||||
}
|
}
|
||||||
const _path = process.cwd()
|
const _path = process.cwd()
|
||||||
let config = {}
|
let config = {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue