chore: update approval example to be easier to understand

Change-Id: Ia999549cce2940aee6d2e1e57ad90c28d5d2f15f
drew/english
shentong.martin 6 months ago committed by shentongmartin
parent 85592f893f
commit 3ce08012fd

@ -29,7 +29,6 @@ type ApprovalInfo struct {
ToolName string
ArgumentsInJSON string
ToolCallID string
ApprovalResult *ApprovalResult
}
type ApprovalResult struct {
@ -72,7 +71,7 @@ func (i InvokableApprovableTool) InvokableRun(ctx context.Context, argumentsInJS
}, argumentsInJSON)
}
isResumeTarget, hasData, data := compose.GetResumeContext[*ApprovalInfo](ctx)
isResumeTarget, hasData, data := compose.GetResumeContext[*ApprovalResult](ctx)
if !isResumeTarget { // was interrupted but not explicitly resumed, reinterrupt and wait for approval again
return "", compose.StatefulInterrupt(ctx, &ApprovalInfo{
ToolName: toolInfo.Name,
@ -84,16 +83,12 @@ func (i InvokableApprovableTool) InvokableRun(ctx context.Context, argumentsInJS
return "", fmt.Errorf("tool '%s' resumed with no data", toolInfo.Name)
}
if data.ApprovalResult == nil {
return "", fmt.Errorf("tool '%s' resumed with no approval result", toolInfo.Name)
}
if data.ApprovalResult.Approved {
if data.Approved {
return i.InvokableTool.InvokableRun(ctx, storedArguments, opts...)
}
if data.ApprovalResult.DisapproveReason != nil {
return fmt.Sprintf("tool '%s' disapproved, reason: %s", toolInfo.Name, *data.ApprovalResult.DisapproveReason), nil
if data.DisapproveReason != nil {
return fmt.Sprintf("tool '%s' disapproved, reason: %s", toolInfo.Name, *data.DisapproveReason), nil
}
return fmt.Sprintf("tool '%s' disapproved", toolInfo.Name), nil

@ -22,14 +22,33 @@ import (
"log"
"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"
tool2 "github.com/cloudwego/eino-examples/adk/common/tool"
)
func NewTicketBookingAgent() adk.Agent {
ctx := context.Background()
type bookInput struct {
Location string `json:"location"`
PassengerName string `json:"passenger_name"`
PassengerPhoneNumber string `json:"passenger_phone_number"`
}
getWeather, err := utils.InferTool(
"BookTicket",
"this tool can book ticket of the specific location",
func(ctx context.Context, input bookInput) (output string, err error) {
return "success", nil
})
if err != nil {
log.Fatal(err)
}
a, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
Name: "TicketBooker",
Description: "An agent that can book tickets",
@ -38,7 +57,9 @@ Based on the user's request, use the "BookTicket" tool to book tickets.`,
Model: model.NewChatModel(),
ToolsConfig: adk.ToolsConfig{
ToolsNodeConfig: compose.ToolsNodeConfig{
Tools: getTools(),
Tools: []tool.BaseTool{
&tool2.InvokableApprovableTool{InvokableTool: getWeather},
},
},
},
})

@ -37,6 +37,10 @@ func main() {
runner := adk.NewRunner(ctx, adk.RunnerConfig{
EnableStreaming: true, // you can disable streaming here
Agent: a,
// provide a CheckPointStore for eino to persist the execution state of the agent for later resumption.
// Here we use an in-memory store for simplicity.
// In the real world, you can use a distributed store like Redis to persist the checkpoints.
CheckPointStore: store.NewInMemoryStore(),
})
iter := runner.Query(ctx, "book a ticket for Martin, to Beijing, on 2025-12-01, the phone number is 1234567. directly call tool.", adk.WithCheckPointID("1"))
@ -62,9 +66,12 @@ func main() {
if lastEvent.Action == nil || lastEvent.Action.Interrupted == nil {
log.Fatal("last event is not an interrupt event")
}
apInfo := lastEvent.Action.Interrupted.InterruptContexts[0].Info.(*tool.ApprovalInfo)
// this interruptID is crucial 'locator' for Eino to know where the interrupt happens,
// so when resuming later, you have to provide this same `interruptID` along with the approval result back to Eino
interruptID := lastEvent.Action.Interrupted.InterruptContexts[0].ID
var apResult *tool.ApprovalResult
for {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("your input here: ")
@ -72,7 +79,7 @@ func main() {
fmt.Println()
nInput := scanner.Text()
if strings.ToUpper(nInput) == "Y" {
apInfo.ApprovalResult = &tool.ApprovalResult{Approved: true}
apResult = &tool.ApprovalResult{Approved: true}
break
} else if strings.ToUpper(nInput) == "N" {
// Prompt for reason when denying
@ -80,16 +87,20 @@ func main() {
scanner.Scan()
reason := scanner.Text()
fmt.Println()
apInfo.ApprovalResult = &tool.ApprovalResult{Approved: false, DisapproveReason: &reason}
apResult = &tool.ApprovalResult{Approved: false, DisapproveReason: &reason}
break
}
fmt.Println("invalid input, please input Y or N")
}
// here we directly resumes right in the same instance where the original `Runner.Query` happened.
// In the real world, the original `Runner.Run/Query` and the subsequent `Runner.ResumeWithParams`
// can happen in different processes or machines, as long as you use the same `CheckPointID`,
// and you provided a distributed `CheckPointStore` when creating the `Runner` instance.
iter, err := runner.ResumeWithParams(ctx, "1", &adk.ResumeParams{
Targets: map[string]any{
interruptID: apInfo,
interruptID: apResult,
},
})
if err != nil {

@ -1,49 +0,0 @@
/*
* Copyright 2025 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"log"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/components/tool/utils"
tool2 "github.com/cloudwego/eino-examples/adk/common/tool"
)
type bookInput struct {
Location string `json:"location"`
PassengerName string `json:"passenger_name"`
PassengerPhoneNumber string `json:"passenger_phone_number"`
}
func getTools() []tool.BaseTool {
getWeather, err := utils.InferTool(
"BookTicket",
"this tool can book ticket of the specific location",
func(ctx context.Context, input bookInput) (output string, err error) {
return "success", nil
})
if err != nil {
log.Fatal(err)
}
return []tool.BaseTool{
&tool2.InvokableApprovableTool{InvokableTool: getWeather},
}
}
Loading…
Cancel
Save