121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
|
|
"example.com/ueino/internal/opt"
|
|
"example.com/ueino/pkg/memory"
|
|
"example.com/ueino/pkg/tool"
|
|
|
|
"github.com/cloudwego/eino-ext/components/model/openai"
|
|
"github.com/cloudwego/eino/adk"
|
|
einotool "github.com/cloudwego/eino/components/tool"
|
|
"github.com/cloudwego/eino/compose"
|
|
"github.com/cloudwego/eino/schema"
|
|
)
|
|
|
|
// Config 配置模型连接和记忆预算。
|
|
type Config struct {
|
|
BaseURL string
|
|
Model string
|
|
APIKey string
|
|
MaxToken int
|
|
}
|
|
|
|
// Agent 使用 Eino ADK 完成流式对话,并管理当前会话的历史。
|
|
type Agent struct {
|
|
runner *adk.Runner
|
|
memory *memory.Memory
|
|
}
|
|
|
|
func New(ctx context.Context, config Config) (*Agent, error) {
|
|
chat, err := openai.NewChatModel(ctx, &openai.ChatModelConfig{
|
|
BaseURL: strings.TrimRight(config.BaseURL, "/"),
|
|
Model: config.Model,
|
|
APIKey: config.APIKey,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
chatAgent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
|
Name: "terminal_chat",
|
|
Instruction: opt.SYSTEM_PROMPT,
|
|
Model: chat,
|
|
MaxIterations: opt.MAX_TOOL_ROUND,
|
|
ToolsConfig: adk.ToolsConfig{ToolsNodeConfig: compose.ToolsNodeConfig{Tools: []einotool.BaseTool{tool.NewWeather()}}},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Agent{
|
|
runner: adk.NewRunner(ctx, adk.RunnerConfig{Agent: chatAgent, EnableStreaming: true}),
|
|
memory: memory.NewMemory(config.MaxToken),
|
|
}, nil
|
|
}
|
|
|
|
// Chat 将回答片段交给 onChunk,只有成功完成的对话才写入记忆。
|
|
func (a *Agent) Chat(ctx context.Context, input string, onChunk func(string)) error {
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
var messages []*schema.Message
|
|
for _, message := range a.memory.LoadHistory() {
|
|
messages = append(messages, &schema.Message{Role: schema.RoleType(message.Role), Content: message.Content.(string)})
|
|
}
|
|
messages = append(messages, schema.UserMessage(input))
|
|
reply, err := readResponse(a.runner.Run(ctx, messages), onChunk)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if reply != "" {
|
|
a.memory.Save(input, reply)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// readResponse 逐片段传递 Agent 回答,并返回完整文本。
|
|
func readResponse(events *adk.AsyncIterator[*adk.AgentEvent], onChunk func(string)) (string, error) {
|
|
var reply strings.Builder
|
|
for {
|
|
event, ok := events.Next()
|
|
if !ok {
|
|
return reply.String(), nil
|
|
}
|
|
if event.Err != nil {
|
|
return "", event.Err
|
|
}
|
|
if event.Output == nil || event.Output.MessageOutput == nil {
|
|
continue
|
|
}
|
|
output := event.Output.MessageOutput
|
|
if output.Role != schema.Assistant {
|
|
if output.IsStreaming {
|
|
output.MessageStream.Close()
|
|
}
|
|
continue
|
|
}
|
|
reply.Reset()
|
|
if !output.IsStreaming {
|
|
onChunk(output.Message.Content)
|
|
reply.WriteString(output.Message.Content)
|
|
continue
|
|
}
|
|
for {
|
|
chunk, err := output.MessageStream.Recv()
|
|
if err != nil {
|
|
output.MessageStream.Close()
|
|
if err != io.EOF {
|
|
return "", err
|
|
}
|
|
break
|
|
}
|
|
onChunk(chunk.Content)
|
|
reply.WriteString(chunk.Content)
|
|
}
|
|
}
|
|
}
|