openai/chat.go
2024-09-07 23:13:36 +08:00

169 lines
5.0 KiB
Go

package openai
import (
"apigo.cc/ai/agent"
"context"
"github.com/sashabaranov/go-openai"
"github.com/ssgo/log"
"strings"
)
func (ag *Agent) FastAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4o_mini_2024_07_18,
}, callback)
}
func (ag *Agent) LongAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4_32k_0613,
}, callback)
}
func (ag *Agent) BatterAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4_turbo,
}, callback)
}
func (ag *Agent) BestAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4o_2024_08_06,
}, callback)
}
func (ag *Agent) MultiAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4o_mini_2024_07_18,
}, callback)
}
func (ag *Agent) BestMultiAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4o_2024_08_06,
}, callback)
}
func (ag *Agent) CodeInterpreterAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4o,
Tools: map[string]any{agent.ToolCodeInterpreter: nil},
}, callback)
}
func (ag *Agent) WebSearchAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) {
return ag.Ask(messages, &agent.ChatModelConfig{
Model: ModelGPT_4o_mini_2024_07_18,
Tools: map[string]any{agent.ToolWebSearch: nil},
}, callback)
}
func (ag *Agent) Ask(messages []agent.ChatMessage, config *agent.ChatModelConfig, callback func(answer string)) (string, agent.TokenUsage, error) {
openaiConf := openai.DefaultConfig(ag.config.ApiKey)
if ag.config.Endpoint != "" {
openaiConf.BaseURL = ag.config.Endpoint
}
if config == nil {
config = &agent.ChatModelConfig{}
}
config.SetDefault(&ag.config.DefaultChatModelConfig)
agentMessages := make([]openai.ChatCompletionMessage, len(messages))
for i, msg := range messages {
var contents []openai.ChatMessagePart
if msg.Contents != nil {
contents = make([]openai.ChatMessagePart, len(msg.Contents))
for j, inPart := range msg.Contents {
part := openai.ChatMessagePart{}
part.Type = TypeMap[inPart.Type]
switch inPart.Type {
case agent.TypeText:
part.Text = inPart.Content
case agent.TypeImage:
part.ImageURL = &openai.ChatMessageImageURL{
URL: inPart.Content,
Detail: openai.ImageURLDetailAuto,
}
}
contents[j] = part
}
}
agentMessages[i] = openai.ChatCompletionMessage{
Role: RoleMap[msg.Role],
MultiContent: contents,
}
}
opt := openai.ChatCompletionRequest{
Model: config.GetModel(),
Messages: agentMessages,
MaxTokens: config.GetMaxTokens(),
Temperature: float32(config.GetTemperature()),
TopP: float32(config.GetTopP()),
StreamOptions: &openai.StreamOptions{
IncludeUsage: true,
},
}
for name := range config.GetTools() {
switch name {
case agent.ToolCodeInterpreter:
opt.Tools = append(opt.Tools, openai.Tool{Type: "code_interpreter"})
case agent.ToolWebSearch:
}
}
c := openai.NewClientWithConfig(openaiConf)
if callback != nil {
opt.Stream = true
r, err := c.CreateChatCompletionStream(context.Background(), opt)
if err == nil {
results := make([]string, 0)
usage := agent.TokenUsage{}
for {
if r2, err := r.Recv(); err == nil {
if r2.Choices != nil {
for _, ch := range r2.Choices {
text := ch.Delta.Content
callback(text)
results = append(results, text)
}
}
if r2.Usage != nil {
usage.AskTokens += int64(r2.Usage.PromptTokens)
usage.AnswerTokens += int64(r2.Usage.CompletionTokens)
usage.TotalTokens += int64(r2.Usage.TotalTokens)
}
} else {
break
}
}
_ = r.Close()
return strings.Join(results, ""), usage, nil
} else {
log.DefaultLogger.Error(err.Error())
return "", agent.TokenUsage{}, err
}
} else {
r, err := c.CreateChatCompletion(context.Background(), opt)
if err == nil {
results := make([]string, 0)
if r.Choices != nil {
for _, ch := range r.Choices {
results = append(results, ch.Message.Content)
}
}
return strings.Join(results, ""), agent.TokenUsage{
AskTokens: int64(r.Usage.PromptTokens),
AnswerTokens: int64(r.Usage.CompletionTokens),
TotalTokens: int64(r.Usage.TotalTokens),
}, nil
} else {
//fmt.Println(u.BMagenta(err.Error()), u.BMagenta(u.JsonP(r)))
return "", agent.TokenUsage{}, err
}
}
}