feat(workers/ai.go): 修改handleModelRequest和handleChatRequest函数中的字符串格式化, 增加错误状态码返回

refactor(workers/pkg.go): 移除Pkg中的冗余打印语句, 优化代码可读性
This commit is contained in:
liyp 2024-07-06 21:57:14 +08:00
parent 31b7ab9f67
commit 23b6885df7
2 changed files with 9 additions and 9 deletions

View file

@ -92,10 +92,10 @@ func handleModelRequest(OPENAI_API_KEY, OPENAI_BaseURL string) string {
choices := responseBody["data"].([]interface{}) choices := responseBody["data"].([]interface{})
// var models []interface{} // var models []interface{}
if len(choices) > 0 { if len(choices) > 0 {
msg := "支持的模型列表:\\n" msg := "支持的模型列表:\n"
for _, choice := range choices { for _, choice := range choices {
model := choice.(map[string]interface{})["id"] model := choice.(map[string]interface{})["id"]
msg += fmt.Sprintf("%s\\n", model) msg += fmt.Sprintf("%s\n", model)
} }
return msg return msg
} else { } else {
@ -138,7 +138,7 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
log.Println(errs) log.Println(errs)
return "请求失败" return "请求失败"
} }
println(resp.StatusCode)
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
var responseBody map[string]interface{} var responseBody map[string]interface{}
if err := json.Unmarshal([]byte(body), &responseBody); err != nil { if err := json.Unmarshal([]byte(body), &responseBody); err != nil {
@ -155,6 +155,6 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
return "api解析失败" return "api解析失败"
} }
} else { } else {
return "请求失败" return "请求失败: " + string(resp.StatusCode)
} }
} }

View file

@ -38,7 +38,7 @@ func (a *Pkg) GetMsg() string {
url += "&repo=" + strings.ToUpper(parms[2][:1]) + parms[2][1:] url += "&repo=" + strings.ToUpper(parms[2][:1]) + parms[2][1:]
} }
// 输出请求地址 // 输出请求地址
fmt.Println("pkg url:", url) // fmt.Println("pkg url:", url)
request := gorequest.New() request := gorequest.New()
_, body, errs := request.Get(url).End() _, body, errs := request.Get(url).End()
if len(errs) > 0 { if len(errs) > 0 {
@ -61,7 +61,7 @@ func (a *Pkg) GetMsg() string {
fmt.Println(errs) fmt.Println(errs)
} }
fmt.Println("aur suggest url:", url) // fmt.Println("aur suggest url:", url)
// fmt.Println(string(re)) // fmt.Println(string(re))
var suggestions []string var suggestions []string
@ -98,7 +98,7 @@ func (a *Pkg) GetMsg() string {
searchResult = resultMap searchResult = resultMap
} }
} }
fmt.Println(len(searchResult)) // fmt.Println(len(searchResult))
} else if len(searchResults) == 1 { } else if len(searchResults) == 1 {
searchResult = searchResults[0].(map[string]interface{}) searchResult = searchResults[0].(map[string]interface{})
} }
@ -106,7 +106,7 @@ func (a *Pkg) GetMsg() string {
if !ok { if !ok {
maintainer = "孤儿包" maintainer = "孤儿包"
} }
println("maintainer:", maintainer) // println("maintainer:", maintainer)
// last_update := '' // last_update := ''
last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05")
@ -123,7 +123,7 @@ func (a *Pkg) GetMsg() string {
msg += "上游:" + upstream + "\n" msg += "上游:" + upstream + "\n"
msg += "更新时间:" + last_update msg += "更新时间:" + last_update
fmt.Println(msg) // fmt.Println(msg)
return msg return msg
} }