链式Agent是Blades框架中用于实现顺序执行逻辑的重要组件,它将复杂的任务分解为更简单的、更容易管理的子任务。

链式Agent的核心函数为 NewSequentialAgent ,用于构建一个顺序工作流,其初始化一个链式工作流时需要输入配置 SequentialConfig :
type SequentialConfig struct { Name string Description string SubAgents []blades.Agent}构建一个链式Agent的方式如下:
sequentialAgent := flow.NewSequentialAgent( flow.SequentialConfig{ Name: "WritingReviewFlow", SubAgents: []blades.Agent{ writerAgent, reviewerAgent, },})blades.NewRunner 方法可以直接接收 sequentialAgent 作为输入参数:
input := blades.UserMessage("Please write a short paragraph about climate change.")runner := blades.NewRunner(sequentialAgent)stream := runner.RunStream(context.Background(), input)for message, err := range stream { if err != nil { log.Fatal(err) } log.Println(message.Author, message.Text())}package main
import ( "context" "log" "os"
"github.com/go-kratos/blades" "github.com/go-kratos/blades/contrib/openai" "github.com/go-kratos/blades/flow")
func main() { model := openai.NewModel(os.Getenv("OPENAI_MODEL"), openai.Config{ APIKey: os.Getenv("OPENAI_API_KEY"), }) writerAgent, err := blades.NewAgent( "WriterAgent", blades.WithModel(model), blades.WithInstruction("Draft a short paragraph on climate change."), blades.WithOutputKey("draft"), ) if err != nil { log.Fatal(err) } reviewerAgent, err := blades.NewAgent( "ReviewerAgent", blades.WithModel(model), blades.WithInstruction(`Review the draft and suggest improvements. Draft: {{.draft}}`), ) if err != nil { log.Fatal(err) } sequentialAgent := flow.NewSequentialAgent( flow.SequentialConfig{ Name: "WritingReviewFlow", SubAgents: []blades.Agent{ writerAgent, reviewerAgent, }, }) if err != nil { log.Fatal(err) } input := blades.UserMessage("Please write a short paragraph about climate change.") runner := blades.NewRunner(sequentialAgent) stream := runner.RunStream(context.Background(), input) for message, err := range stream { if err != nil { log.Fatal(err) } log.Println(message.Author, message.Text()) }}