Skip to content

Commit

Permalink
feat: 全局配置更改为从数据库读取
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 1, 2024
1 parent e6722b5 commit ee20b99
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 77 deletions.
4 changes: 1 addition & 3 deletions args/configer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
27 changes: 0 additions & 27 deletions args/params.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
16 changes: 0 additions & 16 deletions config.yml
Original file line number Diff line number Diff line change
@@ -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 # 日志存储目录
Expand Down
12 changes: 6 additions & 6 deletions dbase/setting/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
// 默认模型
ModelDefault = ""
// 定义模型扮演的身份
ModelContext = "用最少的字数回复"
ModelContext = "你是由OpenTDP开发的群助手,必须使用尽可能少的字数回答接下来的所有问题"
// 历史消息数量
ModelHistory = 20
)
Expand Down Expand Up @@ -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() // 将默认配置存入数据库

}
1 change: 1 addition & 0 deletions httpd/wrobot/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions wclient/aichat/adapter.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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):]
}

Expand Down
7 changes: 3 additions & 4 deletions wclient/aichat/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"},
}
}
Expand Down
7 changes: 3 additions & 4 deletions wclient/aichat/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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},
}
}
Expand Down
7 changes: 3 additions & 4 deletions wclient/aichat/xunfei.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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},
}
}
Expand Down
6 changes: 3 additions & 3 deletions wclient/robot/handler_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 + "祝你好运!"
Expand All @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions wclient/robot/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +20,9 @@ var selfInfo *wcferry.UserInfo

func Register() {

if !args.Bot.Enable {
setting.Laod()

if !setting.BotEnable {
logman.Warn("robot disabled")
return
}
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit ee20b99

Please sign in to comment.