From 9ad5b17438e461e5ec90c5deb62fc9c6160615f3 Mon Sep 17 00:00:00 2001 From: liyp Date: Sat, 13 Jul 2024 18:00:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9AI=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E5=92=8C=E7=8A=B6=E6=80=81=E7=A0=81=E6=89=93?= =?UTF-8?q?=E5=8D=B0=EF=BC=9B=E4=BC=98=E5=8C=96AUR=E5=8C=85=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E8=BD=AC=E6=8D=A2=E5=92=8C=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/ai.go | 57 ++++++++++++++++++++++++-------------------------- workers/pkg.go | 50 ++++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/workers/ai.go b/workers/ai.go index e998a58..3fbe3c5 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -3,6 +3,7 @@ package workers import ( "fmt" "log" + "net/http" "strings" "time" @@ -131,40 +132,36 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string "frequency_penalty": 0, "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 { - log.Println(errs) - return "请求失败" + request := gorequest.New() + resp, body, errs := request.Post(OPENAI_BaseURL). + 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) - 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) - return fmt.Sprintf("[CQ:at,qq=%s] %s", UID, msg) - } else { - log.Println("choices为空") - return "api解析失败" - } + 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.Printf("请求失败,状态码:%d,重试中...(%d/%d)\n", resp.StatusCode, retries+1, maxRetry) - time.Sleep(time.Second * 1) - // return "请求失败: " + fmt.Sprintf("%d", resp.StatusCode) + log.Println("choices为空") + return "api解析失败" } + } return "请求失败!" } diff --git a/workers/pkg.go b/workers/pkg.go index 2a7ab38..29dcb45 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -93,33 +93,37 @@ func (a *Pkg) GetMsg() string { searchResult = searchMap["results"].([]interface{})[0].(map[string]interface{}) } 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 + 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 { - resultMap := result.(map[string]interface{}) - numVotes := resultMap["NumVotes"].(float64) - if numVotes > maxVotes { - maxVotes = numVotes - searchResult = resultMap - } + for _, result := range searchResults { + // resultMap := result.(map[string]interface{}) + numVotes := result["NumVotes"].(float64) + if numVotes > maxVotes { + maxVotes = numVotes + searchResult = result } - // fmt.Println(len(searchResult)) } + } maintainer, ok := searchResult["Maintainer"].(string) if !ok { @@ -150,9 +154,7 @@ func (a *Pkg) GetMsg() string { } result := resultSlipe[0].(map[string]interface{}) - // 输出结果字符串 - // fmt.Println("result:", result) - // if result != nil { + var msg string last_update := result["last_update"].(string) last_update = strings.Replace(last_update, "T", " ", 1)