feat(adk): add Exit tool for loop_for_reflection (#127)

* feat(adk): add Exit tool for loop_for_reflection

* feat(adk): add break_loop event for loop_for_reflection

* feat(adk): add break_loop event for loop_for_reflection
drew/english
IPender 6 months ago committed by GitHub
parent b3cc87e04e
commit 0795ca30b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -20,11 +20,12 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"time"
"github.com/cloudwego/eino-examples/adk/common/trace"
"github.com/cloudwego/eino/adk" "github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino-examples/adk/common/prints" "github.com/cloudwego/eino-examples/adk/common/prints"
"github.com/cloudwego/eino-examples/adk/common/trace"
"github.com/cloudwego/eino-examples/adk/intro/workflow/loop/subagents" "github.com/cloudwego/eino-examples/adk/intro/workflow/loop/subagents"
) )
@ -35,7 +36,7 @@ func main() {
defer traceCloseFn(ctx) defer traceCloseFn(ctx)
a, err := adk.NewLoopAgent(ctx, &adk.LoopAgentConfig{ a, err := adk.NewLoopAgent(ctx, &adk.LoopAgentConfig{
Name: "ReflectionAgent", Name: "reflection_agent",
Description: "Reflection agent with main and critique agent for iterative task solving.", Description: "Reflection agent with main and critique agent for iterative task solving.",
SubAgents: []adk.Agent{subagents.NewMainAgent(), subagents.NewCritiqueAgent()}, SubAgents: []adk.Agent{subagents.NewMainAgent(), subagents.NewCritiqueAgent()},
MaxIterations: 5, MaxIterations: 5,
@ -45,7 +46,7 @@ func main() {
} }
query := "briefly introduce what a multimodal embedding model is." query := "briefly introduce what a multimodal embedding model is."
ctx, endSpanFn := startSpanFn(ctx, "layered-supervisor", query) ctx, endSpanFn := startSpanFn(ctx, "ReflectionAgents", query)
runner := adk.NewRunner(ctx, adk.RunnerConfig{ runner := adk.NewRunner(ctx, adk.RunnerConfig{
EnableStreaming: true, // you can disable streaming here EnableStreaming: true, // you can disable streaming here
@ -73,4 +74,5 @@ func main() {
} }
endSpanFn(ctx, lastMessage) endSpanFn(ctx, lastMessage)
time.Sleep(20 * time.Second)
} }

@ -21,13 +21,16 @@ import (
"log" "log"
"github.com/cloudwego/eino/adk" "github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/components/tool/utils"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino-examples/adk/common/model" "github.com/cloudwego/eino-examples/adk/common/model"
) )
func NewMainAgent() adk.Agent { func NewMainAgent() adk.Agent {
a, err := adk.NewChatModelAgent(context.Background(), &adk.ChatModelAgentConfig{ a, err := adk.NewChatModelAgent(context.Background(), &adk.ChatModelAgentConfig{
Name: "MainAgent", Name: "main_agent",
Description: "Main agent that attempts to solve the user's task.", Description: "Main agent that attempts to solve the user's task.",
Instruction: `You are the main agent responsible for solving the user's task. Instruction: `You are the main agent responsible for solving the user's task.
Provide a comprehensive solution based on the given requirements. Provide a comprehensive solution based on the given requirements.
@ -41,18 +44,40 @@ Focus on delivering accurate and complete results.`,
} }
func NewCritiqueAgent() adk.Agent { func NewCritiqueAgent() adk.Agent {
exitAndSummarizeTool, err := utils.InferTool("exit_and_summarize", "exit from the loop and provide a final summary response",
func(ctx context.Context, req *exitAndSummarize) (string, error) {
_ = adk.SendToolGenAction(ctx, "exit_and_summarize", adk.NewBreakLoopAction("critique_agent"))
return req.Summary, nil
})
if err != nil {
log.Fatalf("create tool failed, name=%v, err=%v", "exit_and_summarize", err)
}
a, err := adk.NewChatModelAgent(context.Background(), &adk.ChatModelAgentConfig{ a, err := adk.NewChatModelAgent(context.Background(), &adk.ChatModelAgentConfig{
Name: "CritiqueAgent", Name: "critique_agent",
Description: "Critique agent that reviews the main agent's work and provides feedback.", Description: "Critique agent that reviews the main agent's work and provides feedback.",
Instruction: `You are a critique agent responsible for reviewing the main agent's work. Instruction: `You are a critique agent responsible for reviewing the main agent's work.
Analyze the provided solution for accuracy, completeness, and quality. Analyze the provided solution for accuracy, completeness, and quality.
If you find issues or areas for improvement, provide specific feedback. If you find issues or areas for improvement, provide specific feedback.
If the work is satisfactory, call the 'exit' tool and provide a final summary response.`, If the work is satisfactory, call the 'exit_and_summarize' tool and provide a final summary response.`,
Model: model.NewChatModel(), Model: model.NewChatModel(),
// Exit: nil, // use default exit tool ToolsConfig: adk.ToolsConfig{
ToolsNodeConfig: compose.ToolsNodeConfig{
Tools: []tool.BaseTool{
exitAndSummarizeTool,
},
},
ReturnDirectly: map[string]bool{
"exit_and_summarize": true,
},
},
}) })
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("create agent failed, name=%v, err=%v", "critique_agent", err)
} }
return a return a
} }
type exitAndSummarize struct {
Summary string `json:"summary" jsonschema_description:"final summary of the solution"`
}

Loading…
Cancel
Save