feat(workers/ai.go): 修改AI对话模型配置和请求处理逻辑
This commit is contained in:
parent
23b6885df7
commit
4ec2cee24a
1 changed files with 38 additions and 28 deletions
|
@ -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 "请求失败!"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue