-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (93 loc) · 2.8 KB
/
Copy pathmain.go
File metadata and controls
108 lines (93 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// This example demonstrates various ways to register and use tools:
// manual registration, reflection-based registration, and built-in tools.
package main
import (
"context"
"fmt"
"log"
"github.com/vearne/agentscope-go/pkg/tool"
)
func main() {
tk := tool.NewToolkit()
// --- Manual registration ---
err := tk.Register(
"greet",
"Generate a greeting message for a given name",
map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{
"type": "string",
"description": "The name to greet",
},
},
"required": []string{"name"},
},
greetFunc,
)
if err != nil {
log.Fatal(err)
}
// --- Reflection-based registration ---
err = tk.RegisterFunc(searchFunc, tool.RegisterOption{
Name: "search",
Description: "Search the web for information",
})
if err != nil {
log.Fatal(err)
}
// --- Built-in tools ---
if err := tool.RegisterPrintTool(tk); err != nil {
log.Fatal(err)
}
if err := tool.RegisterShellTool(tk); err != nil {
log.Fatal(err)
}
fmt.Println("=== Registered Tools ===")
for _, name := range tk.GetToolNames() {
fmt.Printf(" - %s\n", name)
}
fmt.Println("\n=== Tool Schemas ===")
for _, schema := range tk.GetSchemas() {
fmt.Printf(" %s: %s\n", schema.Function.Name, schema.Function.Description)
}
fmt.Println("\n=== HasTool Check ===")
fmt.Printf(" Has 'greet': %v\n", tk.HasTool("greet"))
fmt.Printf(" Has 'nonexistent': %v\n", tk.HasTool("nonexistent"))
fmt.Println("\n=== Execute Tools ===")
result, err := tk.Execute(context.Background(), "greet", map[string]interface{}{"name": "Alice"})
if err != nil {
log.Fatal(err)
}
fmt.Printf(" greet(name=Alice) -> %v\n", result.Content)
result, err = tk.Execute(context.Background(), "search", map[string]interface{}{
"query": "golang agents",
"limit": float64(5),
})
if err != nil {
log.Fatal(err)
}
fmt.Printf(" search(query=golang agents, limit=5) -> %v\n", result.Content)
result, err = tk.Execute(context.Background(), "print_text", map[string]interface{}{
"text": "Hello from built-in tool!",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf(" print_text(text=Hello...) -> %v\n", result.Content)
}
func greetFunc(_ context.Context, args map[string]interface{}) (*tool.ToolResponse, error) {
name, _ := args["name"].(string)
return &tool.ToolResponse{
Content: fmt.Sprintf("Hello, %s! Nice to meet you!", name),
}, nil
}
type SearchArgs struct {
Query string `json:"query" description:"the search query string"`
Limit int `json:"limit,omitempty" description:"maximum number of results to return"`
}
func searchFunc(_ context.Context, args SearchArgs) (*tool.ToolResponse, error) {
return &tool.ToolResponse{
Content: fmt.Sprintf("Found %d results for '%s' (mock)", args.Limit, args.Query),
}, nil
}