From be83757074cfe8a7648cb6c77bc00c42e54e8969 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 30 Jun 2024 23:34:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(workers):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9Ope?= =?UTF-8?q?nAI=E6=A8=A1=E5=9E=8B=E5=88=97=E8=A1=A8=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 向AI工作者添加了查询OpenAI支持模型列表的功能。当用户输入特定的 命令时,AI将返回可用模型的列表。这增强了AI的实用性并为用户提供 了更多资源的信息。 同时,对配置加载方式进行了优化,确保了配置的正确性和系统的稳定性。 还对AI的响应消息进行了改进,增加了语言的灵活性和友好性。 BREAKING CHANGE: 配置文件格式有所更改,新增了PROMPT配置项,并调整了 APIURL和MODEL的配置。需要更新配置文件以适配这些变化。 --- main.go | 5 ++- workers/ai.go | 109 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 82 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 2820249..99d992b 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,10 @@ func handlePost(w http.ResponseWriter, r *http.Request) { func main() { cfg := config.GetConfig() - APIURL := cfg["APIURL"].(string) + APIURL, ok := cfg["APIURL"].(string) + if !ok { + log.Fatal("加载配置失败!") + } // config.PrintConfig(cfg, "") // print(cfg["AllowGroup"].([]interface{})[0].(string)) diff --git a/workers/ai.go b/workers/ai.go index 457b2fe..382a1e5 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -2,6 +2,7 @@ package workers import ( "log" + "strings" "github.com/goccy/go-json" "github.com/parnurzeal/gorequest" @@ -16,6 +17,10 @@ func (a *AI) GetMsg() string { return "使用!ai xxx 向我提问吧" } + ask := a.Parms[1] + if ask == "" { + return "不问问题你说个屁!" + } var msg string var OPENAI_API_KEY string if cfg["OPENAI_API_KEY"] != nil { @@ -29,7 +34,7 @@ func (a *AI) GetMsg() string { OPENAI_BaseURL = cfg["OPENAI_BaseURL"].(string) } else { log.Println("OPENAI_BaseURL 未配置,使用openai默认配置") - OPENAI_BaseURL = "https://api.openai.com/v1/chat/completions" + OPENAI_BaseURL = "https://api.openai.com/v1" } var MODEL string if cfg["MODEL"] != nil { @@ -38,37 +43,79 @@ func (a *AI) GetMsg() string { log.Println("模型 未配置,使用默认chatglm_pro模型") MODEL = "chatglm_pro" } - ask := a.Parms[1] - if ask == "" { - return "不问问题你说个屁!" - } - requestBody := map[string]interface{}{ - "model": MODEL, - "messages": []map[string]string{{"role": "user", "content": ask}}, - "temperature": 0.7, - } - request := gorequest.New() - resp, body, errs := request.Post(OPENAI_BaseURL). - Set("Content-Type", "application/json"). - Set("Authorization", "Bearer "+OPENAI_API_KEY). - Send(requestBody). - End() - if errs != nil { - log.Println(errs) - return "请求失败" - } else { - if resp.StatusCode == 200 { - var responseBody map[string]interface{} - if err := json.Unmarshal([]byte(body), &responseBody); err != nil { - log.Println(err) - return "解析失败" - } - choices := responseBody["choices"].([]interface{}) - if len(choices) > 0 { - choice := choices[0].(map[string]interface{}) - msg = choice["message"].(map[string]interface{})["content"].(string) + + if strings.ToLower(a.Parms[1]) == "models" { + OPENAI_BaseURL = OPENAI_BaseURL + "/models" + request := gorequest.New() + resp, body, errs := request.Get(OPENAI_BaseURL). + Set("Content-Type", "application/json"). + Set("Authorization", "Bearer "+OPENAI_API_KEY). + End() + if errs != nil { + log.Println(errs) + return "请求失败" + } else { + if resp.StatusCode == 200 { + var responseBody map[string]interface{} + if err := json.Unmarshal([]byte(body), &responseBody); err != nil { + log.Println(err) + return "解析模型列表失败" + } + choices := responseBody["data"].([]interface{}) + if len(choices) > 0 { + msg = "支持的模型列表:\n" + for _, choice := range choices { + msg = msg + choice.(map[string]interface{})["id"].(string) + "\n" + } + } else { + msg = "没查到支持模型列表" + } + return msg } else { - log.Println("choices为空") + log.Println("请求失败") + return "请求模型列表失败" + } + } + } else { + OPENAI_BaseURL = OPENAI_BaseURL + "/chat/completions" + PROMPT, ok := cfg["PROMPT"].(string) + if !ok { + log.Println("PROMRT 未配置") + PROMPT = "" + } + println("PROMPT:", PROMPT) + requestBody := map[string]interface{}{ + "model": MODEL, + "messages": []map[string]string{{ + "role": "system", + "content": PROMPT, + }, + {"role": "user", "content": ask}}, + "temperature": 0.7, + } + request := gorequest.New() + resp, body, errs := request.Post(OPENAI_BaseURL). + Set("Content-Type", "application/json"). + Set("Authorization", "Bearer "+OPENAI_API_KEY). + Send(requestBody). + End() + if errs != nil { + log.Println(errs) + return "请求失败" + } else { + if resp.StatusCode == 200 { + var responseBody map[string]interface{} + if err := json.Unmarshal([]byte(body), &responseBody); err != nil { + log.Println(err) + return "解析失败" + } + choices := responseBody["choices"].([]interface{}) + if len(choices) > 0 { + choice := choices[0].(map[string]interface{}) + msg = choice["message"].(map[string]interface{})["content"].(string) + } else { + log.Println("choices为空") + } } } }