feat: 修改AI接口请求,增加重试机制和状态码打印;优化AUR包查询逻辑,移除不必要的转换和循环

This commit is contained in:
liyp 2024-07-13 18:00:03 +08:00
parent b91b18f29c
commit 9ad5b17438
2 changed files with 53 additions and 54 deletions

View file

@ -3,6 +3,7 @@ package workers
import ( import (
"fmt" "fmt"
"log" "log"
"net/http"
"strings" "strings"
"time" "time"
@ -131,40 +132,36 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
"frequency_penalty": 0, "frequency_penalty": 0,
"top_p": 1, "top_p": 1,
} }
const maxRetry = 2
for retries := 0; retries <= maxRetry; retries++ {
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 { request := gorequest.New()
log.Println(errs) resp, body, errs := request.Post(OPENAI_BaseURL).
return "请求失败" Retry(3, 5*time.Second, http.StatusServiceUnavailable, http.StatusBadGateway).
Set("Content-Type", "application/json").
Set("Authorization", "Bearer "+OPENAI_API_KEY).
Send(requestBody).
End()
if errs != nil {
log.Println(errs)
return "请求失败"
}
println(resp.StatusCode)
if resp.StatusCode == 200 {
var responseBody map[string]interface{}
if err := json.Unmarshal([]byte(body), &responseBody); err != nil {
log.Println(err)
return "解析失败"
} }
println(resp.StatusCode) choices := responseBody["choices"].([]interface{})
if resp.StatusCode == 200 { if len(choices) > 0 {
var responseBody map[string]interface{} choice := choices[0].(map[string]interface{})
if err := json.Unmarshal([]byte(body), &responseBody); err != nil { msg := choice["message"].(map[string]interface{})["content"].(string)
log.Println(err) return fmt.Sprintf("[CQ:at,qq=%s] %s", UID, msg)
return "解析失败"
}
choices := responseBody["choices"].([]interface{})
if len(choices) > 0 {
choice := choices[0].(map[string]interface{})
msg := choice["message"].(map[string]interface{})["content"].(string)
return fmt.Sprintf("[CQ:at,qq=%s] %s", UID, msg)
} else {
log.Println("choices为空")
return "api解析失败"
}
} else { } else {
log.Printf("请求失败,状态码:%d重试中...%d/%d\n", resp.StatusCode, retries+1, maxRetry) log.Println("choices为空")
time.Sleep(time.Second * 1) return "api解析失败"
// return "请求失败: " + fmt.Sprintf("%d", resp.StatusCode)
} }
} }
return "请求失败!" return "请求失败!"
} }

View file

@ -93,33 +93,37 @@ func (a *Pkg) GetMsg() string {
searchResult = searchMap["results"].([]interface{})[0].(map[string]interface{}) searchResult = searchMap["results"].([]interface{})[0].(map[string]interface{})
} else { } else {
searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name"
_, body, errs = request.Get(searchUrl).End()
if len(errs) > 0 {
fmt.Println("searchUrl err:", errs)
}
err = json.Unmarshal([]byte(body), &searchMap)
if err != nil {
fmt.Println(err)
}
searchResults := searchMap["results"].([]interface{})
// println("searchResults:", len(searchResults))
maxVotes := 0.0 maxVotes := 0.0
var searchResults []map[string]interface{}
for _, suggestion := range suggestions {
searchUrl := "https://aur.archlinux.org/rpc/v5/info/" + suggestion
_, body, errs = request.Get(searchUrl).End()
if len(errs) > 0 {
fmt.Println("searchUrl err:", errs)
}
err = json.Unmarshal([]byte(body), &searchMap)
if err != nil {
fmt.Println(err)
}
results := searchMap["results"].([]interface{})
searchResults = append(searchResults, results[0].(map[string]interface{}))
// searchResults = append(searchResults, searchMap["results"].([]interface{})[0].(map[string]interface{}))
// println("searchResults:", len(searchResults))
}
// 增加根据投票人数排序 // 增加根据投票人数排序
if len(searchResults) > 1 { for _, result := range searchResults {
for _, result := range searchResults { // resultMap := result.(map[string]interface{})
resultMap := result.(map[string]interface{}) numVotes := result["NumVotes"].(float64)
numVotes := resultMap["NumVotes"].(float64) if numVotes > maxVotes {
if numVotes > maxVotes { maxVotes = numVotes
maxVotes = numVotes searchResult = result
searchResult = resultMap
}
} }
// fmt.Println(len(searchResult))
} }
} }
maintainer, ok := searchResult["Maintainer"].(string) maintainer, ok := searchResult["Maintainer"].(string)
if !ok { if !ok {
@ -150,9 +154,7 @@ func (a *Pkg) GetMsg() string {
} }
result := resultSlipe[0].(map[string]interface{}) result := resultSlipe[0].(map[string]interface{})
// 输出结果字符串
// fmt.Println("result:", result)
// if result != nil {
var msg string var msg string
last_update := result["last_update"].(string) last_update := result["last_update"].(string)
last_update = strings.Replace(last_update, "T", " ", 1) last_update = strings.Replace(last_update, "T", " ", 1)