feat: add support for chatglm

This commit is contained in:
ikechan8370 2023-03-20 00:26:04 +08:00
parent 9929d55bea
commit 94d5691e8a
5 changed files with 164 additions and 15 deletions

View file

@ -22,6 +22,7 @@ import fetch from 'node-fetch'
import { deleteConversation, getConversations, getLatestMessageIdByConversationId } from '../utils/conversation.js'
import { convertSpeaker, generateAudio, speakers } from '../utils/tts.js'
import { segment } from 'oicq'
import ChatGLMClient from "../utils/chatglm.js";
try {
await import('keyv')
} catch (err) {
@ -194,6 +195,21 @@ export class chatgpt extends plugin {
logger.info(`SydneyUser_${e.sender.user_id}`, await conversationsCache.get(`SydneyUser_${e.sender.user_id}`))
await conversationsCache.delete(`SydneyUser_${e.sender.user_id}`)
await this.reply('已退出当前对话,该对话仍然保留。请@我进行聊天以开启新的对话', true)
} else if (use === 'chatglm') {
const conversation = {
store: new KeyvFile({ filename: 'cache.json' }),
namespace: 'chatglm'
}
let Keyv
try {
Keyv = (await import('keyv')).default
} catch (err) {
await this.reply('依赖keyv未安装请执行pnpm install keyv', true)
}
const conversationsCache = new Keyv(conversation)
logger.info(`ChatGLMUser_${e.sender.user_id}`, await conversationsCache.get(`SydneyUser_${e.sender.user_id}`))
await conversationsCache.delete(`SydneyUser_${e.sender.user_id}`)
await this.reply('已退出当前对话,该对话仍然保留。请@我进行聊天以开启新的对话', true)
} else {
let c = await redis.get(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
if (!c) {
@ -224,6 +240,21 @@ export class chatgpt extends plugin {
const conversationsCache = new Keyv(conversation)
await conversationsCache.delete(`SydneyUser_${qq}`)
await this.reply('已退出当前对话,该对话仍然保留。请@我进行聊天以开启新的对话', true)
} else if (use === 'chatglm') {
const conversation = {
store: new KeyvFile({ filename: 'cache.json' }),
namespace: 'chatglm'
}
let Keyv
try {
Keyv = (await import('keyv')).default
} catch (err) {
await this.reply('依赖keyv未安装请执行pnpm install keyv', true)
}
const conversationsCache = new Keyv(conversation)
logger.info(`SydneyUser_${e.sender.user_id}`, await conversationsCache.get(`SydneyUser_${e.sender.user_id}`))
await conversationsCache.delete(`ChatGLMUser_${qq}`)
await this.reply('已退出当前对话,该对话仍然保留。请@我进行聊天以开启新的对话', true)
} else {
let c = await redis.get(`CHATGPT:CONVERSATIONS:${qq}`)
if (!c) {
@ -527,9 +558,6 @@ export class chatgpt extends plugin {
if (!lastMessageId) {
lastMessageId = await getLatestMessageIdByConversationId(conversationId, newFetch)
}
// let lastMessagePrompt = await redis.get(`CHATGPT:CONVERSATION_LAST_MESSAGE_PROMPT:${conversationId}`)
// let conversationCreateTime = await redis.get(`CHATGPT:CONVERSATION_CREATE_TIME:${conversationId}`)
// let conversationLength = await redis.get(`CHATGPT:CONVERSATION_LENGTH:${conversationId}`)
conversation = {
conversationId,
parentMessageId: lastMessageId
@ -855,6 +883,18 @@ export class chatgpt extends plugin {
}
return sendMessageResult
}
case 'chatglm': {
const cacheOptions = {
namespace: 'chatglm_6b',
store: new KeyvFile({ filename: 'cache.json' })
}
this.chatGPTApi = new ChatGLMClient({
user: e.sender.user_id,
cache: cacheOptions
})
let sendMessageResult = await this.chatGPTApi.sendMessage(prompt, conversation)
return sendMessageResult
}
default: {
let completionParams = {}
if (Config.model) {

View file

@ -128,8 +128,8 @@ let helpData = [
},
{
icon: 'switch',
title: '#chatgpt切换浏览器/API/API2/API3/Bing',
desc: '切换使用的后端为浏览器或OpenAI API/第三方API/反代官网API/Bing'
title: '#chatgpt切换浏览器/API/API3/Bing/ChatGLM',
desc: '切换使用的后端为浏览器或OpenAI API/反代官网API/Bing/自建ChatGLM'
},
{
icon: 'confirm',

View file

@ -43,8 +43,8 @@ export class ChatgptManagement extends plugin {
permission: 'master'
},
{
reg: '^#chatgpt切换API2$',
fnc: 'useReversedAPIBasedSolution',
reg: '^#chatgpt切换(ChatGLM|chatglm)$',
fnc: 'useChatGLMSolution',
permission: 'master'
},
{
@ -207,10 +207,9 @@ export class ChatgptManagement extends plugin {
}
}
async useReversedAPIBasedSolution (e) {
await this.reply('API2已废弃处于不可用状态不会为你切换')
// await redis.set('CHATGPT:USE', 'apiReverse')
// await this.reply('【暂时不可用请关注仓库更新和群公告】已切换到基于第三方Reversed CompletionAPI的解决方案如果已经对话过建议执行`#结束对话`避免引起404错误')
async useChatGLMSolution (e) {
await redis.set('CHATGPT:USE', 'chatglm')
await this.reply('已切换到ChatGLM-6B解决方案如果已经对话过建议执行`#结束对话`避免引起404错误')
}
async useReversedAPIBasedSolution2 (e) {
@ -340,10 +339,11 @@ export class ChatgptManagement extends plugin {
let mode = await redis.get('CHATGPT:USE')
const modeMap = {
browser: '浏览器',
apiReverse: 'API2',
// apiReverse: 'API2',
api: 'API',
bing: '必应',
api3: 'API3'
api3: 'API3',
chatglm: 'ChatGLM-6B'
}
let modeText = modeMap[mode || 'api']
let message = ` API模式和浏览器模式如何选择
@ -356,8 +356,10 @@ export class ChatgptManagement extends plugin {
浏览器模式通过在本地启动Chrome等浏览器模拟用户访问ChatGPT网站使得获得和官方以及API2模式一模一样的回复质量同时保证安全性缺点是本方法对环境要求较高需要提供桌面环境和一个可用的代理能够访问ChatGPT的IP地址且响应速度不如API而且高峰期容易无法使用
必应Bing将调用微软新必应接口进行对话需要在必应网页能够正常使用新必应且设置有效的Bing 登录Cookie方可使用#chatgpt设置必应token
自建ChatGLM模式会调用自建的ChatGLM-6B服务器API进行对话需要自建参考https://github.com/ikechan8370/SimpleChatGLM6BAPI
您可以使用#chatgpt切换浏览器/API/API2/API3/Bing来切换到指定模式
您可以使用#chatgpt切换浏览器/API/API3/Bing/ChatGLM来切换到指定模式
当前为${modeText}模式
`