feat(workers): 添加对OpenAI模型列表的支持
向AI工作者添加了查询OpenAI支持模型列表的功能。当用户输入特定的 命令时,AI将返回可用模型的列表。这增强了AI的实用性并为用户提供 了更多资源的信息。 同时,对配置加载方式进行了优化,确保了配置的正确性和系统的稳定性。 还对AI的响应消息进行了改进,增加了语言的灵活性和友好性。 BREAKING CHANGE: 配置文件格式有所更改,新增了PROMPT配置项,并调整了 APIURL和MODEL的配置。需要更新配置文件以适配这些变化。
This commit is contained in:
parent
13483b9643
commit
be83757074
2 changed files with 82 additions and 32 deletions
5
main.go
5
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))
|
||||
|
||||
|
|
|
@ -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,13 +43,54 @@ func (a *AI) GetMsg() string {
|
|||
log.Println("模型 未配置,使用默认chatglm_pro模型")
|
||||
MODEL = "chatglm_pro"
|
||||
}
|
||||
ask := a.Parms[1]
|
||||
if ask == "" {
|
||||
return "不问问题你说个屁!"
|
||||
|
||||
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("请求失败")
|
||||
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": "user", "content": ask}},
|
||||
"messages": []map[string]string{{
|
||||
"role": "system",
|
||||
"content": PROMPT,
|
||||
},
|
||||
{"role": "user", "content": ask}},
|
||||
"temperature": 0.7,
|
||||
}
|
||||
request := gorequest.New()
|
||||
|
@ -72,6 +118,7 @@ func (a *AI) GetMsg() string {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return msg
|
||||
|
||||
|
|
Loading…
Reference in a new issue