mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
feat: add support for chatglm
This commit is contained in:
parent
9929d55bea
commit
94d5691e8a
5 changed files with 164 additions and 15 deletions
106
utils/chatglm.js
Normal file
106
utils/chatglm.js
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
import { Config } from './config.js'
|
||||
import fetch from 'node-fetch'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
async function getKeyv () {
|
||||
let Keyv
|
||||
try {
|
||||
Keyv = (await import('keyv')).default
|
||||
} catch (error) {
|
||||
throw new Error('keyv依赖未安装,请使用pnpm install keyv安装')
|
||||
}
|
||||
return Keyv
|
||||
}
|
||||
|
||||
export default class ChatGLMClient {
|
||||
constructor (opts) {
|
||||
// user: qq号
|
||||
this.opts = opts
|
||||
}
|
||||
|
||||
async initCache () {
|
||||
if (!this.conversationsCache) {
|
||||
const cacheOptions = this.opts.cache || {}
|
||||
cacheOptions.namespace = cacheOptions.namespace || 'chatglm'
|
||||
let Keyv = await getKeyv()
|
||||
this.conversationsCache = new Keyv(cacheOptions)
|
||||
}
|
||||
}
|
||||
|
||||
async sendMessage (prompt, opts) {
|
||||
const {
|
||||
conversationId = uuidv4(),
|
||||
messageId = uuidv4(),
|
||||
parentMessageId,
|
||||
temperature = Config.temperature
|
||||
} = opts
|
||||
await this.initCache()
|
||||
let url = Config.chatglmBaseUrl + '/api/chat'
|
||||
if (Config.debug) {
|
||||
logger.info('use chatglm api server endpoint: ' + url)
|
||||
}
|
||||
const conversationKey = `ChatGLMUser_${this.opts.user}`
|
||||
const conversation = (await this.conversationsCache.get(conversationKey)) || {
|
||||
messages: [],
|
||||
createdAt: Date.now()
|
||||
}
|
||||
let history = getMessagesForConversation(conversation.messages, parentMessageId)
|
||||
if (Config.debug) {
|
||||
logger.info(history)
|
||||
}
|
||||
console.log(history)
|
||||
let option = {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
prompt,
|
||||
temperature,
|
||||
history
|
||||
}),
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
library: 'chatgpt-plugin'
|
||||
}
|
||||
}
|
||||
let response = await fetch(url, option)
|
||||
let result = await response.text()
|
||||
try {
|
||||
result = JSON.parse(result)
|
||||
conversation.messages.push({
|
||||
id: messageId,
|
||||
role: 'user',
|
||||
content: prompt,
|
||||
parentMessageId
|
||||
})
|
||||
let responseId = uuidv4()
|
||||
conversation.messages.push({
|
||||
id: responseId,
|
||||
role: 'AI',
|
||||
content: result.data,
|
||||
parentMessageId: messageId
|
||||
})
|
||||
await this.conversationsCache.set(conversationKey, conversation)
|
||||
return {
|
||||
conversationId,
|
||||
id: responseId,
|
||||
text: result.data
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(result)
|
||||
throw new Error(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getMessagesForConversation (messages, parentMessageId) {
|
||||
const orderedMessages = []
|
||||
let currentMessageId = parentMessageId
|
||||
while (currentMessageId) {
|
||||
const message = messages.find((m) => m.id === currentMessageId)
|
||||
if (!message) {
|
||||
break
|
||||
}
|
||||
orderedMessages.unshift(message)
|
||||
currentMessageId = message.parentMessageId
|
||||
}
|
||||
|
||||
return orderedMessages
|
||||
}
|
||||
|
|
@ -66,7 +66,8 @@ const defaultConfig = {
|
|||
initiativeChatGroups: [],
|
||||
enableDraw: true,
|
||||
helloPrompt: '写一段话让大家来找我聊天。类似于“有人找我聊天吗?"这种风格,轻松随意一点控制在20个字以内',
|
||||
version: 'v2.2.3'
|
||||
chatglmBaseUrl: 'http://localhost:8080',
|
||||
version: 'v2.2.4'
|
||||
}
|
||||
const _path = process.cwd()
|
||||
let config = {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue