fix: redis expire time; picture mode title; queue lock problem

This commit is contained in:
ikechan8370 2023-02-03 21:49:55 +08:00
parent 8871987b1d
commit e6738e5527
2 changed files with 17 additions and 17 deletions

View file

@ -241,9 +241,7 @@ export class chatgpt extends plugin {
} }
let response = chatMessage?.text let response = chatMessage?.text
previousConversation.num = previousConversation.num + 1 previousConversation.num = previousConversation.num + 1
await redis.set(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`, JSON.stringify(previousConversation), { EX: CONVERSATION_PRESERVE_TIME }) await redis.set(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`, JSON.stringify(previousConversation), CONVERSATION_PRESERVE_TIME > 0 ? { EX: CONVERSATION_PRESERVE_TIME } : {})
// 移除队列首位,释放锁
await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
// 检索是否有屏蔽词 // 检索是否有屏蔽词
const blockWord = blockWords.split(',').find(word => response.toLowerCase().includes(word.toLowerCase())) const blockWord = blockWords.split(',').find(word => response.toLowerCase().includes(word.toLowerCase()))
if (blockWord) { if (blockWord) {
@ -260,37 +258,37 @@ export class chatgpt extends plugin {
} }
if (userSetting.usePicture) { if (userSetting.usePicture) {
let endTokens = ['.', '。', '……', '!', '', ']', ')', '', '】', '?', '', '~', '"', "'"] let endTokens = ['.', '。', '……', '!', '', ']', ')', '', '】', '?', '', '~', '"', "'"]
while (!endTokens.find(token => response.trimEnd().endsWith(token))) { let maxTries = 3
while (maxTries >= 0 && !endTokens.find(token => response.trimEnd().endsWith(token))) {
maxTries--
// while (!response.trimEnd().endsWith('.') && !response.trimEnd().endsWith('。') && !response.trimEnd().endsWith('……') && // while (!response.trimEnd().endsWith('.') && !response.trimEnd().endsWith('。') && !response.trimEnd().endsWith('……') &&
// !response.trimEnd().endsWith('') && !response.trimEnd().endsWith('!') && !response.trimEnd().endsWith(']') && !response.trimEnd().endsWith('】') // !response.trimEnd().endsWith('') && !response.trimEnd().endsWith('!') && !response.trimEnd().endsWith(']') && !response.trimEnd().endsWith('】')
// ) { // ) {
await this.reply('内容有点多,我正在奋笔疾书,请再等一会', true, { recallMsg: 5 }) await this.reply('内容有点多,我正在奋笔疾书,请再等一会', true, { recallMsg: 5 })
option = { option = {
onConversationResponse: function (c) { timeoutMs: 120000
previousConversation.conversation = {
conversationId: c.conversation_id,
parentMessageId: c.message.id
}
redis.set(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`, JSON.stringify(previousConversation), { EX: CONVERSATION_PRESERVE_TIME }).then(res => {
logger.debug('redis set conversation')
})
}
} }
option = Object.assign(option, previousConversation.conversation) option = Object.assign(option, previousConversation.conversation)
const responseAppend = await this.chatGPTApi.sendMessage('Continue', option) const responseAppend = await this.chatGPTApi.sendMessage('Continue', option)
previousConversation.conversation = {
conversationId: responseAppend.conversationId,
parentMessageId: responseAppend.id
}
let responseAppendText = responseAppend?.text
await redis.set(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`, JSON.stringify(previousConversation), CONVERSATION_PRESERVE_TIME > 0 ? { EX: CONVERSATION_PRESERVE_TIME } : {})
// console.log(responseAppend) // console.log(responseAppend)
// 检索是否有屏蔽词 // 检索是否有屏蔽词
const blockWord = blockWords.split(',').find(word => responseAppend.toLowerCase().includes(word.toLowerCase())) const blockWord = blockWords.split(',').find(word => responseAppendText.toLowerCase().includes(word.toLowerCase()))
if (blockWord) { if (blockWord) {
await this.reply('返回内容存在敏感词,我不想回答你', true) await this.reply('返回内容存在敏感词,我不想回答你', true)
return return
} }
if (responseAppend.indexOf('conversation') > -1 || responseAppend.startsWith("I'm sorry")) { if (responseAppendText.indexOf('conversation') > -1 || responseAppendText.startsWith("I'm sorry")) {
logger.warn('chatgpt might forget what it had said') logger.warn('chatgpt might forget what it had said')
break break
} }
response = response + responseAppend response = response + responseAppendText
} }
// logger.info(response) // logger.info(response)
// markdown转为html // markdown转为html
@ -302,6 +300,8 @@ export class chatgpt extends plugin {
} else { } else {
await this.reply(`${response}`, e.isGroup) await this.reply(`${response}`, e.isGroup)
} }
// 移除队列首位,释放锁
await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
} catch (e) { } catch (e) {
logger.error(e) logger.error(e)
// 异常了也要腾地方todo 大概率后面的也会异常,要不要一口气全杀了) // 异常了也要腾地方todo 大概率后面的也会异常,要不要一口气全杀了)

View file

@ -16,7 +16,7 @@
<!-- </div>--> <!-- </div>-->
<div class="question"> <div class="question">
<span> <span>
{{question}} {{prompt}}
</span> </span>
</div> </div>