From 79f62f1b7fab1a97c54b0808ca64f22fffea88b6 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 8 Mar 2025 17:08:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=AE=A1=E7=90=86=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=BC=BA?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=85=8D=E7=BD=AE=E7=BB=93=E6=9E=84=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 21 +++++++++++++++++---- handler/headmaster/headmaster.go | 8 ++++---- handler/steamplaying/service.go | 4 ++-- main.go | 2 +- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index b3a21f9..5a8c0e1 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,18 @@ import ( var ConfigManager = &configManager{} +type Config struct { + Admin int64 `yaml:"admin"` + SteamApiKey string `yaml:"steam_api_key"` + ProxyAddr string `yaml:"proxy_addr"` + NapcatWsSrv string `yaml:"napcat_ws_srv"` + NapcatHttpSrv string `yaml:"napcat_http_srv"` + OpenaiApiKey string `yaml:"openai_api_key"` + OpenaiApiBaseUrl string `yaml:"openai_api_base_url"` + OpenaiModelName string `yaml:"openai_model_name"` + OpenaiPrompt string `yaml:"openai_prompt"` +} + func init() { err := ConfigManager.LoadConfig("config.yml") if err != nil { @@ -16,7 +28,8 @@ func init() { } type configManager struct { - propertys map[string]string + // propertys map[string]string + config *Config } func (cm *configManager) LoadConfig(path string) error { @@ -25,13 +38,13 @@ func (cm *configManager) LoadConfig(path string) error { return err } - err = yaml.Unmarshal(data, &cm.propertys) + err = yaml.Unmarshal(data, &cm.config) if err != nil { return err } return nil } -func (cm *configManager) GetProperty(key string) string { - return cm.propertys[key] +func (cm *configManager) GetConfig() *Config { + return cm.config } diff --git a/handler/headmaster/headmaster.go b/handler/headmaster/headmaster.go index 6404894..baba479 100644 --- a/handler/headmaster/headmaster.go +++ b/handler/headmaster/headmaster.go @@ -46,14 +46,14 @@ func headmasterHandler(msg model.Message) (reply model.Reply) { } func ask(from string, question string) (reply string) { - llmConfig := openai.DefaultAzureConfig(config.ConfigManager.GetProperty("openai_api_key"), config.ConfigManager.GetProperty("openai_api_base_url")) + llmConfig := openai.DefaultAzureConfig(config.ConfigManager.GetConfig().OpenaiApiKey, config.ConfigManager.GetConfig().OpenaiApiBaseUrl) llmConfig.APIType = openai.APITypeOpenAI llmConfig.APIVersion = "" client := openai.NewClientWithConfig(llmConfig) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ - Model: config.ConfigManager.GetProperty("openai_model_name"), + Model: config.ConfigManager.GetConfig().OpenaiModelName, Messages: GenRequestFromUsr(from, question), // Tools: []openai.Tool{ // { @@ -91,7 +91,7 @@ func GenRequestFromUsr(from string, question string) []openai.ChatCompletionMess histories[from] = make([]openai.ChatCompletionMessage, 0) histories[from] = append(histories[from], openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleSystem, - Content: config.ConfigManager.GetProperty("openai_prompt"), + Content: config.ConfigManager.GetConfig().OpenaiPrompt, }, ) } @@ -112,7 +112,7 @@ func AppendReplyToHistory(from string, reply string) { histories[from] = histories[from][1:] histories[from][0] = openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleSystem, - Content: config.ConfigManager.GetProperty("openai_prompt"), + Content: config.ConfigManager.GetConfig().OpenaiPrompt, } } } diff --git a/handler/steamplaying/service.go b/handler/steamplaying/service.go index 128c1fc..c7cf862 100644 --- a/handler/steamplaying/service.go +++ b/handler/steamplaying/service.go @@ -19,8 +19,8 @@ var SteamAPIKey = "" var ProxyAddr = "" func init() { - SteamAPIKey = config.ConfigManager.GetProperty("steam_api_key") - ProxyAddr = config.ConfigManager.GetProperty("proxy_addr") + SteamAPIKey = config.ConfigManager.GetConfig().SteamApiKey + ProxyAddr = config.ConfigManager.GetConfig().ProxyAddr } func init() { diff --git a/main.go b/main.go index e207f44..da8b014 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ func main() { go func() { const reconnectDelay = 5 * time.Second for { - client, err := wsclient.NewWebSocketClient("ws", config.ConfigManager.GetProperty("napcat_srv"), "") + client, err := wsclient.NewWebSocketClient("ws", config.ConfigManager.GetConfig().NapcatWsSrv, "") if err != nil { log.Printf("WebSocket连接失败: %v, %v 后重试", err, reconnectDelay) time.Sleep(reconnectDelay)