feat: add image generation sub-agent with confirmation and organize ADK example
This commit is contained in:
1 parent
86e86bc9fa
commit
dab8cbb55d
21 files changed
+1402
-181
No files matched your search
@@ -0,0 +1,127 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"example.com/ueino/adk/internal/config"
|
||||
"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"
|
||||
)
|
||||
|
||||
const mainAgentName = "terminal_chat"
|
||||
const imageAgentName = "image_creator"
|
||||
|
||||
// Agent 管理主助手的执行和会话历史。
|
||||
type Agent struct {
|
||||
runner *adk.Runner
|
||||
memory *memory.Memory
|
||||
checkpoints *checkpointStore
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg config.Config) (*Agent, error) {
|
||||
chat, err := newChatModel(ctx, cfg.Chat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tools := []einotool.BaseTool{tool.NewWeather()}
|
||||
instruction := opt.SYSTEM_PROMPT
|
||||
|
||||
if cfg.Image.Model != "" {
|
||||
imageAgent, err := newImageAgent(ctx, cfg, chat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tools = append(tools, adk.NewAgentTool(ctx, imageAgent))
|
||||
instruction += imageDelegationPrompt
|
||||
}
|
||||
|
||||
mainAgent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
||||
Name: mainAgentName, Instruction: instruction, Model: chat, MaxIterations: opt.MAX_TOOL_ROUND,
|
||||
ToolsConfig: adk.ToolsConfig{ToolsNodeConfig: compose.ToolsNodeConfig{Tools: tools}, EmitInternalEvents: true},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
store := &checkpointStore{data: make(map[string][]byte)}
|
||||
return &Agent{
|
||||
runner: adk.NewRunner(ctx, adk.RunnerConfig{Agent: mainAgent, EnableStreaming: true, CheckPointStore: store}),
|
||||
memory: memory.NewMemory(cfg.MaxToken),
|
||||
checkpoints: store,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newChatModel(ctx context.Context, cfg config.ModelConfig) (*openai.ChatModel, error) {
|
||||
return openai.NewChatModel(ctx, &openai.ChatModelConfig{
|
||||
BaseURL: strings.TrimRight(cfg.BaseURL, "/"), Model: cfg.Model, APIKey: cfg.APIKey,
|
||||
})
|
||||
}
|
||||
|
||||
// Chat 只输出主助手正文,状态单独回调;只有成功完成的最终回复才写入记忆。
|
||||
func (a *Agent) Chat(ctx context.Context, input string, onText, onStatus func(string), confirm func(string) (bool, error)) 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))
|
||||
// 示例只允许终端顺序处理一轮,轮次结束后清理对应的 checkpoint。
|
||||
const checkpointID = "chat"
|
||||
defer a.checkpoints.Delete(context.Background(), checkpointID)
|
||||
events := a.runner.Run(ctx, messages, adk.WithCheckPointID(checkpointID))
|
||||
for {
|
||||
reply, interrupted, err := readResponse(events, onText, onStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if interrupted == nil {
|
||||
if reply != "" {
|
||||
a.memory.Save(input, reply)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if confirm == nil {
|
||||
return fmt.Errorf("生图需要用户确认,当前未提供确认入口")
|
||||
}
|
||||
targets := make(map[string]any)
|
||||
for _, point := range interrupted.InterruptContexts {
|
||||
if !point.IsRootCause {
|
||||
continue
|
||||
}
|
||||
prompt, ok := point.Info.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("无法识别的生图确认信息")
|
||||
}
|
||||
approved, err := confirm(prompt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
targets[point.ID] = approved
|
||||
}
|
||||
if len(targets) == 0 {
|
||||
return fmt.Errorf("没有可恢复的生图确认点")
|
||||
}
|
||||
events, err = a.runner.ResumeWithParams(ctx, checkpointID, &adk.ResumeParams{Targets: targets})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user