fix: 收拾一下

This commit is contained in:
ikechan8370 2023-06-25 00:52:13 +08:00
parent a70b25987e
commit 9c3caec780
5 changed files with 155 additions and 102 deletions

View file

@ -1931,112 +1931,132 @@ export class chatgpt extends plugin {
if (conversation) { if (conversation) {
option = Object.assign(option, conversation) option = Object.assign(option, conversation)
} }
let isAdmin = e.sender.role === 'admin' || e.sender.role === 'owner' if (Config.smartMode) {
let sender = e.sender.user_id let isAdmin = e.sender.role === 'admin' || e.sender.role === 'owner'
let serpTool let sender = e.sender.user_id
switch (Config.serpSource) { let serpTool
case 'ikechan8370': { switch (Config.serpSource) {
serpTool = new SerpIkechan8370Tool() case 'ikechan8370': {
break serpTool = new SerpIkechan8370Tool()
} break
case 'azure': { }
if (!Config.azSerpKey) { case 'azure': {
logger.warn('未配置bing搜索密钥转为使用ikechan8370搜索源') if (!Config.azSerpKey) {
logger.warn('未配置bing搜索密钥转为使用ikechan8370搜索源')
serpTool = new SerpIkechan8370Tool()
} else {
serpTool = new SerpTool()
}
break
}
default: {
serpTool = new SerpIkechan8370Tool() serpTool = new SerpIkechan8370Tool()
} else {
serpTool = new SerpTool()
} }
break
} }
default: {
serpTool = new SerpIkechan8370Tool()
}
}
let tools = [ let tools = [
// new SendAvatarTool(),
// new SendAvatarTool(), // new SendDiceTool(),
// new SendDiceTool(), new EditCardTool(),
new EditCardTool(), new QueryStarRailTool(),
new QueryStarRailTool(), new WebsiteTool(),
new WebsiteTool(), new JinyanTool(),
new JinyanTool(), new KickOutTool(),
new KickOutTool(), new WeatherTool(),
new WeatherTool(), new SendPictureTool(),
new SendPictureTool(), serpTool
serpTool ]
] let img = []
let img = [] if (e.source) {
if (e.source) { // 优先从回复找图
// 优先从回复找图 let reply
let reply if (e.isGroup) {
if (e.isGroup) { reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message
reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message } else {
} else { reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message
reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message }
} if (reply) {
if (reply) { for (let val of reply) {
for (let val of reply) { if (val.type === 'image') {
if (val.type === 'image') { console.log(val)
console.log(val) img.push(val.url)
img.push(val.url) }
} }
} }
} }
} if (e.img) {
if (e.img) { img.push(...e.img)
img.push(...e.img)
}
if (img.length > 0 && Config.extraUrl) {
tools.push(new ImageCaptionTool())
prompt += `\nthe url of the picture(s) above: ${img.join(', ')}`
} else {
tools.push(new SerpImageTool())
tools.push(...[new SearchVideoTool(),
new SendVideoTool(),
new SearchMusicTool(),
new SendMusicTool()])
}
// if (e.sender.role === 'admin' || e.sender.role === 'owner') {
// tools.push(...[new JinyanTool(), new KickOutTool()])
// }
let funcMap = {}
tools.forEach(tool => {
funcMap[tool.name] = {
exec: tool.func,
function: tool.function()
} }
}) if (img.length > 0 && Config.extraUrl) {
if (!option.completionParams) { tools.push(new ImageCaptionTool())
option.completionParams = {} prompt += `\nthe url of the picture(s) above: ${img.join(', ')}`
}
option.completionParams.functions = Object.keys(funcMap).map(k => funcMap[k].function)
let msg
try {
msg = await this.chatGPTApi.sendMessage(prompt, option)
logger.info(msg)
while (msg.functionCall) {
let { name, arguments: args } = msg.functionCall
let functionResult = await funcMap[name].exec(Object.assign({ isAdmin, sender }, JSON.parse(args)))
logger.mark(`function ${name} execution result: ${functionResult}`)
option.parentMessageId = msg.id
option.name = name
msg = await this.chatGPTApi.sendMessage(functionResult, option, 'function')
logger.info(msg)
}
} catch (err) {
if (err.message?.indexOf('context_length_exceeded') > 0) {
logger.warn(err)
await redis.del(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
await redis.del(`CHATGPT:WRONG_EMOTION:${e.sender.user_id}`)
await e.reply('字数超限啦,将为您自动结束本次对话。')
return null
} else { } else {
logger.error(err) tools.push(new SerpImageTool())
throw new Error(err) tools.push(...[new SearchVideoTool(),
new SendVideoTool(),
new SearchMusicTool(),
new SendMusicTool()])
} }
// if (e.sender.role === 'admin' || e.sender.role === 'owner') {
// tools.push(...[new JinyanTool(), new KickOutTool()])
// }
let funcMap = {}
tools.forEach(tool => {
funcMap[tool.name] = {
exec: tool.func,
function: tool.function()
}
})
if (!option.completionParams) {
option.completionParams = {}
}
option.completionParams.functions = Object.keys(funcMap).map(k => funcMap[k].function)
let msg
try {
msg = await this.chatGPTApi.sendMessage(prompt, option)
logger.info(msg)
while (msg.functionCall) {
let { name, arguments: args } = msg.functionCall
let functionResult = await funcMap[name].exec(Object.assign({ isAdmin, sender }, JSON.parse(args)))
logger.mark(`function ${name} execution result: ${functionResult}`)
option.parentMessageId = msg.id
option.name = name
// 不然普通用户可能会被openai限速
await delay(300)
msg = await this.chatGPTApi.sendMessage(functionResult, option, 'function')
logger.info(msg)
}
} catch (err) {
if (err.message?.indexOf('context_length_exceeded') > 0) {
logger.warn(err)
await redis.del(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
await redis.del(`CHATGPT:WRONG_EMOTION:${e.sender.user_id}`)
await e.reply('字数超限啦,将为您自动结束本次对话。')
return null
} else {
logger.error(err)
throw new Error(err)
}
}
return msg
} else {
let msg
try {
msg = await this.chatGPTApi.sendMessage(prompt, option)
} catch (err) {
if (err.message?.indexOf('context_length_exceeded') > 0) {
logger.warn(err)
await redis.del(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
await redis.del(`CHATGPT:WRONG_EMOTION:${e.sender.user_id}`)
await e.reply('字数超限啦,将为您自动结束本次对话。')
return null
} else {
logger.error(err)
throw new Error(err)
}
}
return msg
} }
return msg
} }
} }
} }

View file

@ -210,6 +210,11 @@ let helpData = [
icon: 'token', icon: 'token',
title: '#chatgpt设置后台刷新token', title: '#chatgpt设置后台刷新token',
desc: '用于查看API余额。注意和配置的key保持同一账号。' desc: '用于查看API余额。注意和配置的key保持同一账号。'
},
{
icon: 'token',
title: '#chatgpt(开启|关闭)智能模式',
desc: 'API模式下打开或关闭智能模式。'
} }
] ]
}, },

View file

@ -1,8 +1,6 @@
import plugin from '../../../lib/plugins/plugin.js' import plugin from '../../../lib/plugins/plugin.js'
import { Config } from '../utils/config.js' import { Config } from '../utils/config.js'
import { exec } from 'child_process'
import { import {
checkPnpm,
formatDuration, formatDuration,
getAzureRoleList, getAzureRoleList,
getPublicIP, getPublicIP,
@ -260,6 +258,11 @@ export class ChatgptManagement extends plugin {
reg: '^#chatgpt导入配置', reg: '^#chatgpt导入配置',
fnc: 'importConfig', fnc: 'importConfig',
permission: 'master' permission: 'master'
},
{
reg: '^#chatgpt(开启|关闭)智能模式$',
fnc: 'switchSmartMode',
permission: 'master'
} }
] ]
}) })
@ -1006,6 +1009,7 @@ azure语音Azure 语音是微软 Azure 平台提供的一项语音服务,
} }
return true return true
} }
async versionChatGPTPlugin (e) { async versionChatGPTPlugin (e) {
await renderUrl(e, `http://127.0.0.1:${Config.serverPort || 3321}/version`, { Viewport: { width: 800, height: 600 } }) await renderUrl(e, `http://127.0.0.1:${Config.serverPort || 3321}/version`, { Viewport: { width: 800, height: 600 } })
} }
@ -1391,7 +1395,7 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
}) })
console.log(configJson) console.log(configJson)
const buf = Buffer.from(configJson) const buf = Buffer.from(configJson)
e.friend.sendFile(buf, `ChatGPT-Plugin Config ${new Date}.json`) e.friend.sendFile(buf, `ChatGPT-Plugin Config ${new Date()}.json`)
return true return true
} }
@ -1419,8 +1423,8 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
if (Config[keyPath] != value) { if (Config[keyPath] != value) {
changeConfig.push({ changeConfig.push({
item: keyPath, item: keyPath,
value: typeof(value) === 'object' ? JSON.stringify(value): value, value: typeof (value) === 'object' ? JSON.stringify(value) : value,
old: typeof(Config[keyPath]) === 'object' ? JSON.stringify(Config[keyPath]): Config[keyPath], old: typeof (Config[keyPath]) === 'object' ? JSON.stringify(Config[keyPath]) : Config[keyPath],
type: 'config' type: 'config'
}) })
Config[keyPath] = value Config[keyPath] = value
@ -1461,11 +1465,28 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
} }
} }
} else { } else {
await this.reply(`未找到配置文件`, false) await this.reply('未找到配置文件', false)
return false return false
} }
this.finish('doImportConfig') this.finish('doImportConfig')
} }
async switchSmartMode (e) {
if (e.msg.includes('开启')) {
if (Config.smartMode) {
await e.reply('已经开启了')
return
}
Config.smartMode = true
await e.reply('好的已经打开智能模式注意API额度哦')
} else {
if (!Config.smartMode) {
await e.reply('已经是关闭得了')
return
}
Config.smartMode = false
await e.reply('好的,已经关闭智能模式')
}
}
} }

View file

@ -317,9 +317,15 @@ export function supportGuoba () {
{ {
field: 'model', field: 'model',
label: 'OpenAI 模型', label: 'OpenAI 模型',
bottomHelpMessage: 'gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301。默认为gpt-3.5-turbogpt-4需账户支持', bottomHelpMessage: 'gpt-4, gpt-4-0613, gpt-4-32k, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k-0613。默认为gpt-3.5-turbogpt-4需账户支持',
component: 'Input' component: 'Input'
}, },
{
field: 'smartMode',
label: '智能模式',
bottomHelpMessage: '仅建议gpt-4-32k和gpt-3.5-turbo-16k-0613开启gpt-4-0613也可。开启后机器人可以群管、收发图片、发视频发音乐、联网搜索等。注意较费token。',
component: 'Switch'
},
{ {
field: 'openAiBaseUrl', field: 'openAiBaseUrl',
label: 'OpenAI API服务器地址', label: 'OpenAI API服务器地址',

View file

@ -129,6 +129,7 @@ const defaultConfig = {
azSerpKey: '', azSerpKey: '',
serpSource: 'ikechan8370', serpSource: 'ikechan8370',
extraUrl: 'https://cpe.ikechan8370.com', extraUrl: 'https://cpe.ikechan8370.com',
smartMode: false,
version: 'v2.7.0' version: 'v2.7.0'
} }
const _path = process.cwd() const _path = process.cwd()