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,10 +132,10 @@ 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() request := gorequest.New()
resp, body, errs := request.Post(OPENAI_BaseURL). resp, body, errs := request.Post(OPENAI_BaseURL).
Retry(3, 5*time.Second, http.StatusServiceUnavailable, http.StatusBadGateway).
Set("Content-Type", "application/json"). Set("Content-Type", "application/json").
Set("Authorization", "Bearer "+OPENAI_API_KEY). Set("Authorization", "Bearer "+OPENAI_API_KEY).
Send(requestBody). Send(requestBody).
@ -160,11 +161,7 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
log.Println("choices为空") log.Println("choices为空")
return "api解析失败" return "api解析失败"
} }
} else {
log.Printf("请求失败,状态码:%d重试中...%d/%d\n", resp.StatusCode, retries+1, maxRetry)
time.Sleep(time.Second * 1)
// return "请求失败: " + fmt.Sprintf("%d", resp.StatusCode)
}
} }
return "请求失败!" return "请求失败!"
} }

View file

@ -93,7 +93,11 @@ 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"
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() _, body, errs = request.Get(searchUrl).End()
if len(errs) > 0 { if len(errs) > 0 {
fmt.Println("searchUrl err:", errs) fmt.Println("searchUrl err:", errs)
@ -103,23 +107,23 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
results := searchMap["results"].([]interface{})
searchResults := 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)) // println("searchResults:", len(searchResults))
maxVotes := 0.0 }
// 增加根据投票人数排序 // 增加根据投票人数排序
if len(searchResults) > 1 {
for _, result := range searchResults { for _, result := range searchResults {
resultMap := result.(map[string]interface{}) // resultMap := result.(map[string]interface{})
numVotes := resultMap["NumVotes"].(float64) numVotes := result["NumVotes"].(float64)
if numVotes > maxVotes { if numVotes > maxVotes {
maxVotes = numVotes maxVotes = numVotes
searchResult = resultMap searchResult = result
} }
} }
// 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)