feat: 暂时不使用chatgpt-api库,拷贝了部分其代码进行修改

This commit is contained in:
葛胤池 2022-12-17 11:35:59 +08:00
parent b8bd3dfec9
commit d0dcc50764
5 changed files with 1166 additions and 19 deletions

View file

@ -4,6 +4,7 @@ import _ from 'lodash'
import { Config } from '../config/index.js'
import showdown from 'showdown'
import mjAPI from 'mathjax-node'
import { ChatGPTPuppeteer } from '../utils/browser.js'
// import puppeteer from '../utils/browser.js'
// import showdownKatex from 'showdown-katex'
const blockWords = '屏蔽词1,屏蔽词2,屏蔽词3'
@ -181,11 +182,11 @@ export class chatgpt extends plugin {
// option.debug = true
option.email = Config.username
option.password = Config.password
this.chatGPTApi = new ChatGPTAPIBrowser(option)
this.chatGPTApi = new ChatGPTPuppeteer(option)
await redis.set('CHATGPT:API_OPTION', JSON.stringify(option))
} else {
let option = JSON.parse(api)
this.chatGPTApi = new ChatGPTAPIBrowser(option)
this.chatGPTApi = new ChatGPTPuppeteer(option)
}
let question = e.msg.trimStart()
await this.reply('我正在思考如何回复你,请稍等', true, { recallMsg: 5 })
@ -194,6 +195,7 @@ export class chatgpt extends plugin {
await this.chatGPTApi.init()
} catch (e) {
await this.reply('chatgpt初始化出错' + e.msg, true)
await this.chatGPTApi.close()
}
let previousConversation = await redis.get(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
let conversation = null
@ -234,6 +236,7 @@ export class chatgpt extends plugin {
const blockWord = blockWords.split(',').find(word => response.toLowerCase().includes(word.toLowerCase()))
if (blockWord) {
await this.reply('返回内容存在敏感词,我不想回答你', true)
await this.chatGPTApi.close()
return
}
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
@ -247,18 +250,24 @@ export class chatgpt extends plugin {
if (userSetting.usePicture) {
let endTokens = ['.', '。', '……', '!', '', ']', ')', '', '】', '?', '', '~', '"', "'"]
while (!endTokens.find(token => response.trimEnd().endsWith(token))) {
// while (!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 })
const responseAppend = await this.chatGPTApi.sendMessage('Continue')
// console.log(responseAppend)
// 检索是否有屏蔽词
const blockWord = blockWords.split(',').find(word => responseAppend.toLowerCase().includes(word.toLowerCase()))
if (blockWord) {
await this.reply('返回内容存在敏感词,我不想回答你', true)
await this.chatGPTApi.close()
return
}
if (responseAppend.indexOf('conversation') > -1 || responseAppend.startsWith("I'm sorry")) {
logger.warn('chatgpt might forget what it had said')
break
}
response = response + responseAppend
}
// logger.info(response)
@ -275,5 +284,6 @@ export class chatgpt extends plugin {
logger.error(e)
await this.reply(`与OpenAI通信异常请稍后重试${e}`, true, { recallMsg: e.isGroup ? 10 : 0 })
}
await this.chatGPTApi.close()
}
}