feat: initialize Eino examples and ADK applications
Include chat, weather, image, travel, streaming ASR and text examples, shared model initialization, documentation, and existing tests. Co-authored-by: TRAE CLI <traecli@bytedance.com>
This commit is contained in:
commit
81deca8c8e
75 files changed
+7048
No files matched your search
@@ -0,0 +1,133 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"example.com/ueino/adk/internal/config"
|
||||
"example.com/ueino/pkg/memory"
|
||||
"example.com/ueino/pkg/opt"
|
||||
"example.com/ueino/pkg/provider"
|
||||
"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"
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
store := &checkpointStore{data: make(map[string][]byte)}
|
||||
weather := tool.NewWeather()
|
||||
tools := []einotool.BaseTool{weather}
|
||||
instruction := opt.SYSTEM_PROMPT
|
||||
var returnDirectly map[string]bool
|
||||
|
||||
if cfg.Image.Model != "" {
|
||||
image := provider.NewImage(cfg.Image.BaseURL, cfg.Image.Model, cfg.Image.APIKey, cfg.Image.OutputDir)
|
||||
imageTool := &confirmedImage{InvokableTool: tool.NewImage(image)}
|
||||
travel, err := newTravelTool(ctx, chat, weather, imageTool, store)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建旅游攻略流程失败: %w", err)
|
||||
}
|
||||
tools = append(tools, imageTool, travel)
|
||||
instruction += imagePrompt + travelPrompt
|
||||
returnDirectly = map[string]bool{travelToolName: true}
|
||||
}
|
||||
|
||||
mainAgent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
||||
Name: mainAgentName, Instruction: instruction, Model: chat, MaxIterations: opt.MAX_TOOL_ROUND,
|
||||
GenModelInput: genModelInput,
|
||||
ToolsConfig: adk.ToolsConfig{ToolsNodeConfig: compose.ToolsNodeConfig{Tools: tools}, ReturnDirectly: returnDirectly},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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))
|
||||
// 终端顺序处理一轮,结束时清理 Runner 和内部 Graph 的 checkpoint。
|
||||
const checkpointID = "chat"
|
||||
defer a.checkpoints.Clear()
|
||||
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