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,185 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
"example.com/ueino/pkg/tool"
|
||||
|
||||
einomodel "github.com/cloudwego/eino/components/model"
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
"github.com/cloudwego/eino/compose"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
const travelToolName = "create_travel_guide"
|
||||
|
||||
func init() {
|
||||
// Graph 暂停时,节点之间传递的数据需要能写入 checkpoint。
|
||||
schema.RegisterName[*model.TravelGuide]("sample_travel_guide")
|
||||
schema.RegisterName[travelProgress]("sample_travel_progress")
|
||||
}
|
||||
|
||||
// travelTool 将固定 Graph 作为主助手的一项普通工具能力。
|
||||
type travelTool struct {
|
||||
graph compose.Runnable[*model.TravelGuide, *model.TravelGuide]
|
||||
}
|
||||
|
||||
func newTravelTool(ctx context.Context, chat einomodel.BaseChatModel, weather *tool.Weather, image einotool.InvokableTool, store *checkpointStore) (*travelTool, error) {
|
||||
graph := compose.NewGraph[*model.TravelGuide, *model.TravelGuide]()
|
||||
for _, err := range []error{
|
||||
graph.AddLambdaNode("weather", compose.InvokableLambda(func(ctx context.Context, guide *model.TravelGuide) (*model.TravelGuide, error) {
|
||||
args, _ := json.Marshal(model.WeatherArguments{Location: guide.Request.Location, StartDate: guide.Request.StartDate, EndDate: guide.Request.EndDate})
|
||||
result, err := weather.Execute(ctx, string(args))
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
if err == nil {
|
||||
err = json.Unmarshal([]byte(result), &guide.Weather)
|
||||
}
|
||||
if err != nil {
|
||||
guide.Problem = "无法获取出行日期的天气预报,未规划或生成图片:" + err.Error()
|
||||
} else if err := emitTravelText(ctx, travelWeatherText(guide.Weather)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return guide, nil
|
||||
})),
|
||||
graph.AddLambdaNode("plan", compose.InvokableLambda(func(ctx context.Context, guide *model.TravelGuide) (*model.TravelGuide, error) {
|
||||
data, _ := json.Marshal(guide)
|
||||
stream, err := chat.Stream(ctx, []*schema.Message{
|
||||
schema.SystemMessage(travelPlanPrompt), schema.UserMessage(string(data)),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer stream.Close()
|
||||
var plan strings.Builder
|
||||
for {
|
||||
chunk, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if chunk.Content != "" {
|
||||
plan.WriteString(chunk.Content)
|
||||
if err := emitTravelText(ctx, chunk.Content); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
guide.Plan = strings.TrimSpace(plan.String())
|
||||
if guide.Plan == "" {
|
||||
return nil, fmt.Errorf("模型未生成旅游行程,未生成图片")
|
||||
}
|
||||
if err := emitTravelText(ctx, "\n\n接下来我会把这份行程制作成旅游攻略图,请先确认绘图提示词。\n"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return guide, nil
|
||||
})),
|
||||
graph.AddLambdaNode("image", compose.InvokableLambda(func(ctx context.Context, guide *model.TravelGuide) (*model.TravelGuide, error) {
|
||||
wasInterrupted, hasState, saved := compose.GetInterruptState[*model.TravelGuide](ctx)
|
||||
if wasInterrupted {
|
||||
if !hasState {
|
||||
return nil, fmt.Errorf("缺少已规划的行程,无法恢复生图")
|
||||
}
|
||||
guide = saved
|
||||
}
|
||||
if guide.ImagePrompt == "" {
|
||||
guide.ImagePrompt = "制作一张清晰、美观的中文旅游攻略信息图,按实际日期分区,突出主要景点、游玩顺序和天气提醒。不要伪造精确地图,避免密集小字。遵循用户偏好:" + guide.Request.Requirements + "\n" + travelWeatherText(guide.Weather) + "行程:\n" + guide.Plan
|
||||
}
|
||||
// 内部中断不会自动保存节点输入,显式保留已完成的行程和提示词。
|
||||
target, hasData, approved := compose.GetResumeContext[bool](ctx)
|
||||
if !wasInterrupted || !target || !hasData {
|
||||
return nil, compose.StatefulInterrupt(ctx, guide.ImagePrompt, guide)
|
||||
}
|
||||
if approved {
|
||||
if err := emitTravelText(ctx, "已确认,正在生成旅游攻略图,请稍候……\n"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
args, _ := json.Marshal(model.ImageArguments{Prompt: guide.ImagePrompt})
|
||||
result, err := image.InvokableRun(ctx, string(args))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var generated model.ImageResult
|
||||
if err := json.Unmarshal([]byte(result), &generated); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
guide.Path = generated.Path
|
||||
return guide, nil
|
||||
})),
|
||||
graph.AddLambdaNode("unavailable", compose.InvokableLambda(func(_ context.Context, guide *model.TravelGuide) (*model.TravelGuide, error) {
|
||||
return guide, nil
|
||||
})),
|
||||
graph.AddEdge(compose.START, "weather"),
|
||||
graph.AddBranch("weather", compose.NewGraphBranch(func(_ context.Context, guide *model.TravelGuide) (string, error) {
|
||||
if guide.Problem != "" {
|
||||
return "unavailable", nil
|
||||
}
|
||||
return "plan", nil
|
||||
}, map[string]bool{"plan": true, "unavailable": true})),
|
||||
graph.AddEdge("plan", "image"),
|
||||
graph.AddEdge("image", compose.END),
|
||||
graph.AddEdge("unavailable", compose.END),
|
||||
} {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
runnable, err := graph.Compile(ctx, compose.WithCheckPointStore(store))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &travelTool{graph: runnable}, nil
|
||||
}
|
||||
|
||||
func (t *travelTool) Info(context.Context) (*schema.ToolInfo, error) {
|
||||
return &schema.ToolInfo{
|
||||
Name: travelToolName,
|
||||
Desc: "结合指定城市和出行日期的真实天气,规划游玩行程并生成一张旅游攻略图。用户同时要求天气、游玩规划和攻略图时调用本工具,一次完成,不必再单独调用天气或生图工具。日期须根据当前时间转换为 YYYY-MM-DD;缺少城市或无法确定日期时先询问。",
|
||||
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
|
||||
"location": {Type: schema.String, Required: true, Desc: "旅游城市,例如成都"},
|
||||
"start_date": {Type: schema.String, Required: true, Desc: "出行开始日期,YYYY-MM-DD"},
|
||||
"end_date": {Type: schema.String, Desc: "出行结束日期,YYYY-MM-DD,含当天;不传则只玩一天"},
|
||||
"requirements": {Type: schema.String, Desc: "用户完整的旅游需求和偏好,例如休闲、美食、同行人及绘图风格"},
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *travelTool) InvokableRun(ctx context.Context, arguments string, _ ...einotool.Option) (string, error) {
|
||||
var request model.TravelRequest
|
||||
if err := json.Unmarshal([]byte(arguments), &request); err != nil {
|
||||
return "", fmt.Errorf("旅游参数无效: %w", err)
|
||||
}
|
||||
request.Location = strings.TrimSpace(request.Location)
|
||||
request.StartDate = strings.TrimSpace(request.StartDate)
|
||||
request.EndDate = strings.TrimSpace(request.EndDate)
|
||||
if request.Location == "" || request.StartDate == "" {
|
||||
return "", fmt.Errorf("请提供旅游城市和具体出行日期")
|
||||
}
|
||||
if request.EndDate == "" {
|
||||
request.EndDate = request.StartDate
|
||||
}
|
||||
_, hasState, checkpointID := einotool.GetInterruptState[string](ctx)
|
||||
if !hasState {
|
||||
checkpointID = "travel-" + rand.Text()
|
||||
}
|
||||
guide, err := t.graph.Invoke(ctx, &model.TravelGuide{Request: request}, compose.WithCheckPointID(checkpointID))
|
||||
if _, interrupted := compose.ExtractInterruptInfo(err); interrupted {
|
||||
// 保存内部 Graph 的 checkpoint ID,把生图确认透传到外层 Runner。
|
||||
return "", einotool.CompositeInterrupt(ctx, nil, checkpointID, err)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data, err := json.Marshal(guide)
|
||||
return string(data), err
|
||||
}
|
||||
Reference in new issue
Block a user