feat 修改usn类型为string

This commit is contained in:
2026-01-11 16:16:27 +08:00
parent 1b893cad87
commit 11141edcda
17 changed files with 1019 additions and 43 deletions

55
internal/ai/ai_client.go Normal file
View File

@@ -0,0 +1,55 @@
package ai
import (
"git.hlsq.asia/mmorpg/service-common/log"
)
type Client struct {
enableSearch bool // 是否开启搜索
searchStrategy string // 搜索策略(开了搜索才生效)
temperature float32 // 采样温度
}
func NewAIClient(enableSearch bool, searchStrategy string, temperature float32) *Client {
return &Client{
enableSearch: enableSearch,
searchStrategy: searchStrategy,
temperature: temperature,
}
}
func (c *Client) RequestAI(prompt []string, cb func(content string, i int) error) error {
inToken, outToken := int32(0), int32(0)
messages := make([]Message, 0)
for i, step := range prompt {
messages = append(messages, Message{
Role: "user",
Content: step,
})
r, err := request("deepseek-v3.2", Parameters{
EnableSearch: c.enableSearch,
SearchOptions: SearchOptions{
SearchStrategy: c.searchStrategy,
},
ResultFormat: "message",
Temperature: c.temperature,
}, messages)
if err != nil {
return err
}
inToken += r.Usage.InputTokens
outToken += r.Usage.OutputTokens
if len(r.Output.Choices) == 0 {
log.Infof("AI response err, r: %#+v", r)
return err
} else {
messages = append(messages, r.Output.Choices[0].Message)
}
log.Infof("AI 回答 %v: %v", i, r.Output.Choices[0].Message.Content)
if err = cb(r.Output.Choices[0].Message.Content, i); err != nil {
return err
}
}
log.Infof("AI finished, use token: in-%v out-%v", inToken, outToken)
return nil
}