Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
easydu2002 committed Jan 8, 2023
2 parents 8dbf76a + 3f5ac6a commit 616f5b6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
65 changes: 49 additions & 16 deletions src/command/commands/official.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import { writeConfig } from 'src/util/config'
import { Sender } from '../../model/sender'
import { BaseCommand } from '../command'

async function reloadConfig (key: string, value: any) {
async function reloadConfig (sender: Sender, key: string, value: any) {
const handler = messageHandlers.find(item => item instanceof ChatGPTOfficialHandler) as ChatGPTOfficialHandler
if (!handler) return
if (!config.officialAPI[key]) {
throw Error(`没有 officialAPI.[${key}] 配置项`)
}
config.officialAPI[key] = value
handler.load()
await writeConfig(config)
sender.reply(`已更新officialAPI.${key}${value}`)
}

class OfficialCommand extends BaseCommand {
Expand All @@ -27,34 +25,69 @@ class OfficialCommand extends BaseCommand {
'maxTokens [n] // 设置回复消息占用token',
'maxTrackCount [n] // 设置最大记忆对话次数',
'temperature [0-1] // 设置回答问题的概率系数 0-1',
'stop [Q, A] // 设置问答名称,使用==连接 Humen==AI'
'name [Q] [A] // 设置问答名称'
// 'prop [key] [value] // 设置配置项'
]

requiredAdministrator = true

description = '官方api配置'

getUsageByProp (prop: string) {
return `用法: ${this.usage.find(item => item.startsWith(prop))}`
}

async execute (sender: Sender, params: string[]) {
switch (params[0]) {
const [prop, value] = params

switch (prop) {
case 'get':
sender.reply(JSON.stringify(config.officialAPI, null, 2))
break
case 'key':
case 'stop':
if (!value) return sender.reply(this.getUsageByProp(prop), true)
reloadConfig(sender, prop, value)
break
case 'name': {
const [prop, QName, AName] = params
if (!QName) return sender.reply(this.getUsageByProp(prop), true)
reloadConfig(sender, prop, [QName || config.officialAPI.name[0], AName || config.officialAPI.name[1]])
break
}
case 'model':
if (!value) return sender.reply(this.getUsageByProp(prop), true)
reloadConfig(sender, prop, value)
break
case 'identity':
case 'maxTokens':
case 'temperature':
case 'maxTrackCount':
if (params[0] === 'identity' ||
params[0] === 'stop') {
await reloadConfig(params[0], params[1]?.split('==') ?? [])
} else {
await reloadConfig(params[0], params[1])
await reloadConfig(sender, prop, value?.split('==') ?? [])
break
case 'maxTokens': {
if (!value) return sender.reply(this.getUsageByProp(prop), true)
const maxTokens = Number(value)
if (isNaN(maxTokens) || maxTokens < 0) {
return sender.reply(`maxTokens 必须为不小于0的数字 当前设置为: ${value}`)
}
reloadConfig(sender, prop, maxTokens)
break
}
case 'temperature': {
if (!value) return sender.reply(this.getUsageByProp(prop), true)
const temperature = Number(value)
if (!value || isNaN(temperature) || temperature < 0 || temperature > 1) {
return sender.reply(`temperature 取值范围为: 0-1 当前设置为: ${value}`)
}
reloadConfig(sender, prop, temperature)
break
}
case 'maxTrackCount': {
if (!value) return sender.reply(this.getUsageByProp(prop), true)
const count = Number(value)
if (isNaN(count) || count < 0) {
return sender.reply(`maxTrackCount 必须为不小于0的数字 当前设置为: ${value}`)
}
sender.reply(`${params[0]}重置成功!`)
reloadConfig(sender, prop, count)
break
}
default:
sender.reply(this.helpDoc, true)
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const config = {
maxTokens: 256,
maxTrackCount: 1,
temperature: 0.9,
stop: ['Humen', 'AI']
stop: ['Human', 'AI'],
name: ['Human', 'AI']
},
api: {

Expand Down
2 changes: 1 addition & 1 deletion src/handler/chatgpt-official.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ChatGPTOfficialHandler extends BaseMessageHandler {
handle = async (sender: Sender) => {
if (!config.officialAPI.enable) return true

let [Q, A] = config.officialAPI.stop ?? []
let [Q, A] = config.officialAPI.name ?? config.officialAPI.stop ?? []
Q = Q ?? 'Human'
A = A ?? 'AI'

Expand Down

0 comments on commit 616f5b6

Please sign in to comment.