Skip to content

Commit

Permalink
fix word; update official command
Browse files Browse the repository at this point in the history
  • Loading branch information
easydu2002 committed Jan 8, 2023
1 parent 4b0cfa0 commit 3f5ac6a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 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
4 changes: 2 additions & 2 deletions src/handler/chatgpt-official.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class ChatGPTOfficialHandler extends BaseMessageHandler {
handle = async (sender: Sender) => {
if (!config.officialAPI.enable) return true

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

try {
Expand Down

0 comments on commit 3f5ac6a

Please sign in to comment.