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"
"log"
"strings"
"time"
"github.com/goccy/go-json"
"github.com/parnurzeal/gorequest"
@ -62,8 +63,8 @@ func getConfig() (string, string, string) {
if cfg["MODEL"] != nil {
MODEL = cfg["MODEL"].(string)
} else {
log.Println("模型 未配置,使用默认chatglm_pro模型")
MODEL = "chatglm_pro"
log.Println("模型 未配置,使用默认 gpt-4o 模型")
MODEL = "gpt-4o"
}
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:]},
},
"temperature": 0.7,
"presence_penalty": 0,
"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").
@ -155,6 +161,10 @@ func handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, rawMsg, UID string
return "api解析失败"
}
} 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 "请求失败!"
}