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"
|
"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
|
||||||
|
@ -125,36 +126,45 @@ 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,
|
||||||
}
|
}
|
||||||
request := gorequest.New()
|
const maxRetry = 2
|
||||||
resp, body, errs := request.Post(OPENAI_BaseURL).
|
for retries := 0; retries <= maxRetry; retries++ {
|
||||||
Set("Content-Type", "application/json").
|
request := gorequest.New()
|
||||||
Set("Authorization", "Bearer "+OPENAI_API_KEY).
|
resp, body, errs := request.Post(OPENAI_BaseURL).
|
||||||
Send(requestBody).
|
Set("Content-Type", "application/json").
|
||||||
End()
|
Set("Authorization", "Bearer "+OPENAI_API_KEY).
|
||||||
|
Send(requestBody).
|
||||||
|
End()
|
||||||
|
|
||||||
if errs != nil {
|
if errs != nil {
|
||||||
log.Println(errs)
|
log.Println(errs)
|
||||||
return "请求失败"
|
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{})
|
println(resp.StatusCode)
|
||||||
if len(choices) > 0 {
|
if resp.StatusCode == 200 {
|
||||||
choice := choices[0].(map[string]interface{})
|
var responseBody map[string]interface{}
|
||||||
msg := choice["message"].(map[string]interface{})["content"].(string)
|
if err := json.Unmarshal([]byte(body), &responseBody); err != nil {
|
||||||
return fmt.Sprintf("[CQ:at,qq=%s] %s", UID, msg)
|
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解析失败"
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Println("choices为空")
|
log.Printf("请求失败,状态码:%d,重试中...(%d/%d)\n", resp.StatusCode, retries+1, maxRetry)
|
||||||
return "api解析失败"
|
time.Sleep(time.Second * 1)
|
||||||
|
// return "请求失败: " + fmt.Sprintf("%d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return "请求失败: " + string(resp.StatusCode)
|
|
||||||
}
|
}
|
||||||
|
return "请求失败!"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue