56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
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
|
|
}
|