feat(workers/ai.go): 修改AI对话模型配置和请求处理逻辑

This commit is contained in:
liyp 2024-07-06 22:35:01 +08:00
parent 23b6885df7
commit 4ec2cee24a

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"strings" "strings"
"time"
"github.com/goccy/go-json" "github.com/goccy/go-json"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
@ -62,8 +63,8 @@ func getConfig() (string, string, string) {
if cfg["MODEL"] != nil { if cfg["MODEL"] != nil {
MODEL = cfg["MODEL"].(string) MODEL = cfg["MODEL"].(string)
} else { } else {
log.Println("模型 未配置,使用默认chatglm_pro模型") log.Println("模型 未配置,使用默认 gpt-4o 模型")
MODEL = "chatglm_pro" MODEL = "gpt-4o"
} }
return OPENAI_API_KEY, OPENAI_BaseURL, MODEL return OPENAI_API_KEY, OPENAI_BaseURL, MODEL
@ -126,7 +127,12 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
{"role": "user", "content": rawMsg[strings.Index(rawMsg, " ")+1:]}, {"role": "user", "content": rawMsg[strings.Index(rawMsg, " ")+1:]},
}, },
"temperature": 0.7, "temperature": 0.7,
"presence_penalty": 0,
"frequency_penalty": 0,
"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).
Set("Content-Type", "application/json"). Set("Content-Type", "application/json").
@ -155,6 +161,10 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
return "api解析失败" return "api解析失败"
} }
} else { } else {
return "请求失败: " + string(resp.StatusCode) log.Printf("请求失败,状态码:%d重试中...%d/%d\n", resp.StatusCode, retries+1, maxRetry)
time.Sleep(time.Second * 1)
// return "请求失败: " + fmt.Sprintf("%d", resp.StatusCode)
} }
} }
return "请求失败!"
}