feat: 添加星火支持(beta)

This commit is contained in:
ikechan8370 2023-05-12 20:11:08 +08:00
parent 76f0328a8e
commit 926f07c8e9
7 changed files with 268 additions and 5 deletions

View file

@ -35,6 +35,7 @@ import { ChatgptManagement } from './management.js'
import { getPromptByName } from '../utils/prompts.js'
import Translate from '../utils/baiduTranslate.js'
import emojiStrip from 'emoji-strip'
import XinghuoClient from "../utils/xinghuo/xinghuo.js";
try {
await import('keyv')
} catch (err) {
@ -115,6 +116,12 @@ export class chatgpt extends plugin {
/** 执行方法 */
fnc: 'claude'
},
{
/** 命令正则匹配 */
reg: '^#xh[sS]*',
/** 执行方法 */
fnc: 'xh'
},
{
/** 命令正则匹配 */
reg: toggleMode === 'at' ? '^[^#][sS]*' : '^#chat[^gpt][sS]*',
@ -239,6 +246,11 @@ export class chatgpt extends plugin {
await e.reply('claude对话已结束')
return
}
if (use === 'xh') {
await redis.del(`CHATGPT:CONVERSATIONS_XH:${e.sender.user_id}`)
await e.reply('星火对话已结束')
return
}
let ats = e.message.filter(m => m.type === 'at')
if (ats.length === 0) {
if (use === 'api3') {
@ -389,6 +401,17 @@ export class chatgpt extends plugin {
}
break
}
case 'xh': {
let cs = await redis.keys('CHATGPT:CONVERSATIONS_XH:*')
for (let i = 0; i < cs.length; i++) {
await redis.del(cs[i])
if (Config.debug) {
logger.info('delete slack conversation of qq: ' + cs[i])
}
deleted++
}
break
}
case 'bing': {
let cs = await redis.keys('CHATGPT:CONVERSATIONS_BING:*')
let we = await redis.keys('CHATGPT:WRONG_EMOTION:*')
@ -929,6 +952,10 @@ export class chatgpt extends plugin {
key = `CHATGPT:CONVERSATIONS_BROWSER:${e.sender.user_id}`
break
}
case 'xh': {
key = `CHATGPT:CONVERSATIONS_XH:${e.sender.user_id}`
break
}
}
let ctime = new Date()
previousConversation = (key ? await redis.get(key) : null) || JSON.stringify({
@ -1351,7 +1378,7 @@ export class chatgpt extends plugin {
let ats = e.message.filter(m => m.type === 'at')
if (!e.atme && ats.length > 0) {
if (Config.debug) {
logger.mark('艾特别人了,没艾特我,忽略#bing')
logger.mark('艾特别人了,没艾特我,忽略#claude')
}
return false
}
@ -1362,6 +1389,28 @@ export class chatgpt extends plugin {
await this.abstractChat(e, prompt, 'claude')
return true
}
async xh (e) {
if (!e.isMaster && e.isPrivate && !Config.enablePrivateChat) {
// await this.reply('ChatGpt私聊通道已关闭。')
return false
}
if (!Config.allowOtherMode) {
return false
}
let ats = e.message.filter(m => m.type === 'at')
if (!e.atme && ats.length > 0) {
if (Config.debug) {
logger.mark('艾特别人了,没艾特我,忽略#xh')
}
return false
}
let prompt = _.replace(e.raw_message.trimStart(), '#xh', '').trim()
if (prompt.length === 0) {
return false
}
await this.abstractChat(e, prompt, 'xh')
return true
}
async cacheContent (e, use, content, prompt, quote = [], mood = '', suggest = '', imgUrls = []) {
let cacheData = { file: '', cacheUrl: Config.cacheUrl, status: '' }
@ -1744,6 +1793,17 @@ export class chatgpt extends plugin {
text
}
}
case 'xh': {
const ssoSessionId = Config.xinghuoToken
if (!ssoSessionId) {
throw new Error('未绑定星火token请使用#chatgpt设置星火token命令绑定token。获取对话页面的ssoSessionId cookie值')
}
let client = new XinghuoClient({
ssoSessionId
})
let response = await client.sendMessage(prompt, conversation?.conversationId)
return response
}
default: {
let completionParams = {}
if (Config.model) {

View file

@ -13,8 +13,8 @@ let helpData = [
},
{
icon: 'chat',
title: '#chat1/#chat3/#chatglm/#bing',
desc: '分别使用API/API3/ChatGLM/Bing模式与机器人聊天,无论主人设定了何种全局模式'
title: '#chat1/#chat3/#chatglm/#bing/#claude/#xh',
desc: '分别使用API/API3/ChatGLM/Bing/Claude/星火模式与机器人聊天,无论主人设定了何种全局模式'
},
{
icon: 'chat-private',
@ -191,6 +191,11 @@ let helpData = [
title: '#chatgpt设置APIKey',
desc: '设置APIKey'
},
{
icon: 'key',
title: '#chatgpt设置星火token',
desc: '设置星火ssoSessionId对话页面的ssoSessionId cookie值'
},
{
icon: 'eat',
title: '#chatgpt设置(API|Sydney)设定',

View file

@ -102,6 +102,11 @@ export class ChatgptManagement extends plugin {
fnc: 'useSlackClaudeBasedSolution',
permission: 'master'
},
{
reg: '^#chatgpt切换星火$',
fnc: 'useXinghuoBasedSolution',
permission: 'master'
},
{
reg: '^#chatgpt(必应|Bing)切换',
fnc: 'changeBingTone',
@ -149,6 +154,11 @@ export class ChatgptManagement extends plugin {
fnc: 'setAPIPromptPrefix',
permission: 'master'
},
{
reg: '^#chatgpt设置星火token',
fnc: 'setXinghuoToken',
permission: 'master'
},
{
reg: '^#chatgpt设置(Bing|必应|Sydney|悉尼|sydney|bing)设定',
fnc: 'setBingPromptPrefix',
@ -805,6 +815,16 @@ export class ChatgptManagement extends plugin {
}
}
async useXinghuoBasedSolution () {
let use = await redis.get('CHATGPT:USE')
if (use !== 'xh') {
await redis.set('CHATGPT:USE', 'xh')
await this.reply('已切换到基于星火的解决方案')
} else {
await this.reply('当前已经是星火模式了')
}
}
async changeBingTone (e) {
let tongStyle = e.msg.replace(/^#chatgpt(必应|Bing)切换/, '')
if (!tongStyle) {
@ -1083,6 +1103,21 @@ export class ChatgptManagement extends plugin {
this.finish('saveAPIKey')
}
async setXinghuoToken () {
this.setContext('saveXinghuoToken')
await this.reply('请发送星火的ssoSessionId', true)
return false
}
async saveXinghuoToken () {
if (!this.e.msg) return
let token = this.e.msg
// todo
Config.xinghuoToken = token
await this.reply('星火ssoSessionId设置成功', true)
this.finish('saveXinghuoToken')
}
async setAPIPromptPrefix (e) {
this.setContext('saveAPIPromptPrefix')
await this.reply('请发送用于API模式的设定', true)