feat: add Eino ADK terminal chat with reusable agent package
This commit is contained in:
1 parent
43fc905c82
commit
6e6171f481
4 files changed
+376
-13
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"example.com/ueino/internal/opt"
|
||||
"example.com/ueino/pkg/memory"
|
||||
|
||||
"github.com/cloudwego/eino-ext/components/model/openai"
|
||||
"github.com/cloudwego/eino/adk"
|
||||
"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,
|
||||
})
|
||||
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.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user