diff --git a/args/configer.go b/args/configer.go index cf369208..fe6d61c9 100644 --- a/args/configer.go +++ b/args/configer.go @@ -9,8 +9,6 @@ import ( var Configer *Config type ConfigData struct { - Bot *IBot `yaml:"Bot"` - LLM *ILLM `yaml:"LLM"` Log *ILog `yaml:"Log"` Wcf *IWcf `yaml:"Wcf"` Web *IWeb `yaml:"Web"` @@ -30,7 +28,7 @@ func init() { Configer = &Config{ File: "config.yml", - Data: &ConfigData{Bot, LLM, Log, Wcf, Web}, + Data: &ConfigData{Log, Wcf, Web}, } if len(os.Args) > 1 { diff --git a/args/params.go b/args/params.go index 19d7c948..71f8c159 100644 --- a/args/params.go +++ b/args/params.go @@ -1,32 +1,5 @@ package args -// 机器人参数 - -type IBot struct { - Enable bool `yaml:"Enable"` // 是否启用内置机器人 - PatReturn bool `yaml:"PatReturn"` // 是否自动回应拍一拍 - FriendAccept bool `yaml:"FriendAccept"` // 是否自动同意新的好友请求 - RevokeMsg string `yaml:"RevokeMsg"` // 私聊撤回消息时响应的内容,留空则忽略 - FriendHello string `yaml:"FriendHello"` // 添加好友后的响应内容,留空则忽略 - WhiteLimit bool `yaml:"WhiteLimit"` // 开启后只有白名单内的群或好友可以使用机器人 -} - -var Bot = &IBot{ - Enable: true, -} - -// 大语言模型 - -type ILLM struct { - Default string `yaml:"Default"` // 默认模型 - HistoryNum int `yaml:"HistoryNum"` // 历史消息数量 - RoleContext string `yaml:"RoleContext"` // 定义模型扮演的身份 -} - -var LLM = &ILLM{ - HistoryNum: 20, -} - // 日志配置 type ILog struct { diff --git a/config.yml b/config.yml index 0fbaa08c..e4377090 100644 --- a/config.yml +++ b/config.yml @@ -1,19 +1,3 @@ -# 机器人 -Bot: - Enable: true # 是否启用内置机器人 - FriendAccept: true # 是否自动同意新的好友请求 - FriendHello: 回复 /help 查询指令 # 确认好友后的响应内容,留空则忽略 - PatReturn: true # 是否自动回应拍一拍 - RevokeMsg: 撤回了寂寞? # 私聊撤回消息的响应内容,留空则忽略 - WhiteLimit: false # 开启后只有白名单内的群或好友可以使用机器人 - -# 大模型 -LLM: - Default: gem # 默认模型 Mid,必须在 llmodel 表中 - HistoryNum: 20 # 历史消息数量,数量越多消耗的 Token 越多 - RoleContext: | # 自定义智能模型扮演的身份,适用于所有模型 - 你是由OpenTDP开发的群助手,必须使用尽可能少的字数回答接下来的所有问题。 - # 运行日志 Log: Dir: logs # 日志存储目录 diff --git a/dbase/setting/extra.go b/dbase/setting/extra.go index f7bc49b4..19d01414 100644 --- a/dbase/setting/extra.go +++ b/dbase/setting/extra.go @@ -20,7 +20,7 @@ var ( // 默认模型 ModelDefault = "" // 定义模型扮演的身份 - ModelContext = "用最少的字数回复" + ModelContext = "你是由OpenTDP开发的群助手,必须使用尽可能少的字数回答接下来的所有问题" // 历史消息数量 ModelHistory = 20 ) @@ -79,20 +79,20 @@ func Save() { func DataMigrate() { - if c, _ := Count(&CountParam{}); c != 0 { + if c, _ := Count(&CountParam{}); c == 0 { return } - Create(&CreateParam{"BotEnable", "bool", "bot", "", "机器人", "是否启用机器人"}) + Create(&CreateParam{"BotEnable", "bool", "bot", "", "机器人", "是否启用机器人,重启后生效"}) Create(&CreateParam{"FriendAccept", "bool", "bot", "", "确认好友", "是否自动同意新的好友请求"}) Create(&CreateParam{"FriendHello", "string", "bot", "", "好友打招呼", "添加好友后的响应内容,留空则忽略"}) - Create(&CreateParam{"PatReturn", "bool", "bot", "", "回应拍拍", "是否自动回应拍一拍"}) + Create(&CreateParam{"PatReturn", "bool", "bot", "", "回应拍拍", "私聊是否自动回应拍一拍"}) Create(&CreateParam{"RevokeMsg", "string", "bot", "", "撤回提醒", "私聊撤回消息时响应的内容,留空则忽略"}) Create(&CreateParam{"WhiteLimit", "bool", "bot", "", "白名单", "开启后只有白名单内的群或好友可以使用机器人"}) - Create(&CreateParam{"ModelDefault", "string", "bot", "", "默认模型", "默认模型"}) + Create(&CreateParam{"ModelDefault", "string", "bot", "", "默认模型", "用户的默认大模型代码"}) Create(&CreateParam{"ModelContext", "string", "bot", "", "模型预定义", "定义模型扮演的身份"}) Create(&CreateParam{"ModelHistory", "number", "bot", "", "上下文总量", "历史消息最大数量"}) - Save() + Save() // 将默认配置存入数据库 } diff --git a/httpd/wrobot/setting.go b/httpd/wrobot/setting.go index 7afe528a..016fb763 100644 --- a/httpd/wrobot/setting.go +++ b/httpd/wrobot/setting.go @@ -95,6 +95,7 @@ func (*Setting) update(c *gin.Context) { if err := setting.Update(rq); err == nil { c.Set("Message", "更新成功") + setting.Laod() } else { c.Set("Error", err) } diff --git a/wclient/aichat/adapter.go b/wclient/aichat/adapter.go index 58bc5c0b..9436f2bf 100644 --- a/wclient/aichat/adapter.go +++ b/wclient/aichat/adapter.go @@ -1,9 +1,9 @@ package aichat import ( - "github.com/opentdp/wechat-rest/args" "github.com/opentdp/wechat-rest/dbase/llmodel" "github.com/opentdp/wechat-rest/dbase/profile" + "github.com/opentdp/wechat-rest/dbase/setting" "github.com/opentdp/wechat-rest/dbase/tables" ) @@ -47,7 +47,7 @@ func UserModel(id, rid string) *tables.LLModel { } if llmc == nil { - llmc, _ = llmodel.Fetch(&llmodel.FetchParam{Mid: args.LLM.Default}) + llmc, _ = llmodel.Fetch(&llmodel.FetchParam{Mid: setting.ModelDefault}) } if llmc == nil { @@ -85,7 +85,7 @@ func CountHistory(id string) int { func AppendHistory(id string, items ...*MsgHistory) { - if len(msgHistories[id]) >= args.LLM.HistoryNum { + if len(msgHistories[id]) >= setting.ModelHistory { msgHistories[id] = msgHistories[id][len(items):] } diff --git a/wclient/aichat/google.go b/wclient/aichat/google.go index fd4ff057..750849f6 100644 --- a/wclient/aichat/google.go +++ b/wclient/aichat/google.go @@ -6,9 +6,8 @@ import ( "fmt" "github.com/google/generative-ai-go/genai" + "github.com/opentdp/wechat-rest/dbase/setting" "google.golang.org/api/option" - - "github.com/opentdp/wechat-rest/args" ) func GoogleText(id, rid, ask string) (string, error) { @@ -40,9 +39,9 @@ func GoogleText(id, rid, ask string) (string, error) { // 设置上下文 - if args.LLM.RoleContext != "" { + if setting.ModelContext != "" { req.History = []*genai.Content{ - {Parts: []genai.Part{genai.Text(args.LLM.RoleContext)}, Role: "user"}, + {Parts: []genai.Part{genai.Text(setting.ModelContext)}, Role: "user"}, {Parts: []genai.Part{genai.Text("OK")}, Role: "model"}, } } diff --git a/wclient/aichat/openai.go b/wclient/aichat/openai.go index 324bfd84..bd1268d2 100644 --- a/wclient/aichat/openai.go +++ b/wclient/aichat/openai.go @@ -3,9 +3,8 @@ package aichat import ( "context" + "github.com/opentdp/wechat-rest/dbase/setting" "github.com/sashabaranov/go-openai" - - "github.com/opentdp/wechat-rest/args" ) func OpenaiText(id, rid, ask string) (string, error) { @@ -29,9 +28,9 @@ func OpenaiText(id, rid, ask string) (string, error) { // 设置上下文 - if args.LLM.RoleContext != "" { + if setting.ModelContext != "" { req.Messages = []openai.ChatCompletionMessage{ - {Content: args.LLM.RoleContext, Role: openai.ChatMessageRoleUser}, + {Content: setting.ModelContext, Role: openai.ChatMessageRoleUser}, {Content: "OK", Role: openai.ChatMessageRoleAssistant}, } } diff --git a/wclient/aichat/xunfei.go b/wclient/aichat/xunfei.go index 19e2e64c..865d183f 100644 --- a/wclient/aichat/xunfei.go +++ b/wclient/aichat/xunfei.go @@ -7,8 +7,7 @@ import ( "strings" "github.com/liudding/go-llm-api/xunfei" - - "github.com/opentdp/wechat-rest/args" + "github.com/opentdp/wechat-rest/dbase/setting" ) func XunfeiText(id, rid, ask string) (string, error) { @@ -31,9 +30,9 @@ func XunfeiText(id, rid, ask string) (string, error) { // 设置上下文 - if args.LLM.RoleContext != "" { + if setting.ModelContext != "" { req.Messages = []xunfei.ChatCompletionMessage{ - {Content: args.LLM.RoleContext, Role: xunfei.ChatMessageRoleUser}, + {Content: setting.ModelContext, Role: xunfei.ChatMessageRoleUser}, {Content: "OK", Role: xunfei.ChatMessageRoleAssistant}, } } diff --git a/wclient/robot/handler_help.go b/wclient/robot/handler_help.go index 6ab1dbce..30b38e2e 100644 --- a/wclient/robot/handler_help.go +++ b/wclient/robot/handler_help.go @@ -5,10 +5,10 @@ import ( "sort" "strings" - "github.com/opentdp/wechat-rest/args" "github.com/opentdp/wechat-rest/dbase/chatroom" "github.com/opentdp/wechat-rest/dbase/llmodel" "github.com/opentdp/wechat-rest/dbase/profile" + "github.com/opentdp/wechat-rest/dbase/setting" "github.com/opentdp/wechat-rest/wcferry" "github.com/opentdp/wechat-rest/wclient/aichat" ) @@ -64,7 +64,7 @@ func helpCallback(msg *wcferry.WxMsg) string { text += fmt.Sprintf("唤醒词 %s;", up.AiArgot) } text += fmt.Sprintf("对话模型 %s;", aichat.UserModel(msg.Sender, msg.Roomid).Family) - text += fmt.Sprintf("上下文长度 %d/%d;", aichat.CountHistory(msg.Sender), args.LLM.HistoryNum) + text += fmt.Sprintf("上下文长度 %d/%d;", aichat.CountHistory(msg.Sender), setting.ModelHistory) } return text + "祝你好运!" @@ -73,7 +73,7 @@ func helpCallback(msg *wcferry.WxMsg) string { func helpPreCheck(msg *wcferry.WxMsg) string { - if args.Bot.WhiteLimit { + if setting.WhiteLimit { if msg.IsGroup { room, _ := chatroom.Fetch(&chatroom.FetchParam{Roomid: msg.Roomid}) if room.Level < 2 { diff --git a/wclient/robot/register.go b/wclient/robot/register.go index 9d5bc248..46de1341 100644 --- a/wclient/robot/register.go +++ b/wclient/robot/register.go @@ -7,9 +7,9 @@ import ( "strings" "github.com/opentdp/go-helper/logman" - "github.com/opentdp/wechat-rest/args" "github.com/opentdp/wechat-rest/dbase/chatroom" "github.com/opentdp/wechat-rest/dbase/message" + "github.com/opentdp/wechat-rest/dbase/setting" "github.com/opentdp/wechat-rest/wcferry" "github.com/opentdp/wechat-rest/wcferry/types" "github.com/opentdp/wechat-rest/wclient" @@ -20,7 +20,9 @@ var selfInfo *wcferry.UserInfo func Register() { - if !args.Bot.Enable { + setting.Laod() + + if !setting.BotEnable { logman.Warn("robot disabled") return } @@ -100,7 +102,7 @@ func hook1(msg *wcferry.WxMsg) { func hook37(msg *wcferry.WxMsg) { // 自动接受新朋友 - if args.Bot.FriendAccept { + if setting.FriendAccept { ret := &types.FriendRequestMsg{} err := xml.Unmarshal([]byte(msg.Content), ret) if err == nil && ret.FromUserName != "" { @@ -115,8 +117,8 @@ func hook10000(msg *wcferry.WxMsg) { // 接受好友后响应 if strings.Contains(msg.Content, "现在可以开始聊天了") { - if len(args.Bot.FriendHello) > 1 { - wc.CmdClient.SendTxt(args.Bot.FriendHello, msg.Sender, "") + if len(setting.FriendHello) > 1 { + wc.CmdClient.SendTxt(setting.FriendHello, msg.Sender, "") } return } @@ -138,7 +140,7 @@ func hook10000(msg *wcferry.WxMsg) { if strings.Trim(room.PatReturn, "-") != "" { wc.CmdClient.SendPatMsg(msg.Roomid, msg.Sender) } - } else if args.Bot.PatReturn { + } else if setting.PatReturn { wc.CmdClient.SendPatMsg(msg.Roomid, msg.Sender) } return @@ -155,7 +157,7 @@ func hook10002(msg *wcferry.WxMsg) { room, _ := chatroom.Fetch(&chatroom.FetchParam{Roomid: msg.Roomid}) output = room.RevokeMsg } else { - output = args.Bot.RevokeMsg + output = setting.RevokeMsg } if len(output) < 2 {