Merge branch 'main' of nix13.pw:ScuroNeko/KurumiBotGo
This commit is contained in:
@@ -835,6 +835,7 @@ func _compress(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
res, err := api.CompressChat(messages)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
if len(res.Choices) == 0 {
|
||||
ctx.Answer("Не удалось сжать чат")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -72,6 +73,7 @@ func NewOpenAIAPI(baseURL, token, model string) *OpenAIAPI {
|
||||
proxy, err := url.Parse(os.Getenv("HTTPS_PROXY"))
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
logger.Close()
|
||||
}
|
||||
t := &http.Transport{}
|
||||
if proxy.Host != "" {
|
||||
@@ -111,11 +113,13 @@ func (o *OpenAIAPI) DoRequest(url string, params any, retries int) ([]byte, erro
|
||||
responseBody := make([]byte, 0)
|
||||
data, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
log.Println("json marshal failed:", err)
|
||||
return responseBody, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
log.Println("create request failed:", err)
|
||||
return responseBody, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -125,6 +129,7 @@ func (o *OpenAIAPI) DoRequest(url string, params any, retries int) ([]byte, erro
|
||||
|
||||
res, err := o.client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("do request failed:", err)
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
@@ -138,23 +143,25 @@ func (o *OpenAIAPI) DoRequest(url string, params any, retries int) ([]byte, erro
|
||||
}
|
||||
responseBody, err = io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Println("read response failed:", err)
|
||||
return responseBody, err
|
||||
}
|
||||
|
||||
tempData := make(map[string]any)
|
||||
err = json.Unmarshal(responseBody, &tempData)
|
||||
if err != nil {
|
||||
log.Println("json unmarshal failed:", err)
|
||||
return responseBody, err
|
||||
}
|
||||
// {"error":{"message":"openai_error","type":"bad_response_status_code","param":"","code":"bad_response_status_code"}}
|
||||
if errorData, ok := tempData["error"]; ok {
|
||||
if errorPayload, ok := errorData.(map[string]interface{}); ok {
|
||||
if errorPayload, ok := errorData.(map[string]any); ok {
|
||||
code := errorPayload["code"].(string)
|
||||
if code == "bad_response_status_code" {
|
||||
if retries >= 3 {
|
||||
return responseBody, BadResponseErr
|
||||
}
|
||||
o.Logger.Debug("Retrying because of bad response status code")
|
||||
o.Logger.Warnln("Retrying because of bad response status code")
|
||||
return o.DoRequest(url, params, retries+1)
|
||||
}
|
||||
return nil, errors.New(code)
|
||||
@@ -163,20 +170,6 @@ func (o *OpenAIAPI) DoRequest(url string, params any, retries int) ([]byte, erro
|
||||
return nil, errors.New(string(responseBody))
|
||||
}
|
||||
|
||||
//if errorData, ok := tempData["error"]; ok {
|
||||
// o.Logger.Error(errorData)
|
||||
// errorPayload := errorData.(map[string]interface{})
|
||||
// code := errorPayload["code"].(string)
|
||||
// if code == "bad_response_status_code" {
|
||||
// if retries >= 3 {
|
||||
// return responseBody, BadResponseErr
|
||||
// }
|
||||
// o.Logger.Debug("Retrying because of bad response status code")
|
||||
// return o.DoRequest(url, params, retries+1)
|
||||
// }
|
||||
// return nil, errors.New(code)
|
||||
//}
|
||||
|
||||
return responseBody, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user