forked from zendev-sh/goai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (74 loc) · 2.44 KB
/
Copy pathmain.go
File metadata and controls
82 lines (74 loc) · 2.44 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
//go:build ignore
// Example: multi-step agent loop with tool calls and step callbacks.
//
// Usage:
//
// export GEMINI_API_KEY=...
// go run examples/agent-loop/main.go
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/zendev-sh/goai"
"github.com/zendev-sh/goai/provider/google"
)
func main() {
model := google.Chat("gemini-2.0-flash", google.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
// Define multiple tools for a multi-step workflow.
searchTool := goai.Tool{
Name: "search",
Description: "Search for information on a topic.",
InputSchema: json.RawMessage(`{
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"]
}`),
Execute: func(_ context.Context, input json.RawMessage) (string, error) {
var args struct{ Query string }
_ = json.Unmarshal(input, &args)
return fmt.Sprintf("Search results for %q: Go was created by Google in 2009.", args.Query), nil
},
}
calculatorTool := goai.Tool{
Name: "calculate",
Description: "Calculate: subtract second number from first. Input: {\"a\": 2026, \"b\": 2009}",
InputSchema: json.RawMessage(`{
"type": "object",
"properties": {
"a": {"type": "integer", "description": "First number"},
"b": {"type": "integer", "description": "Second number"}
},
"required": ["a", "b"]
}`),
Execute: func(_ context.Context, input json.RawMessage) (string, error) {
var args struct{ A, B int }
_ = json.Unmarshal(input, &args)
return fmt.Sprintf("%d", args.A-args.B), nil
},
}
result, err := goai.GenerateText(context.Background(), model,
goai.WithSystem("You are a research assistant. Use tools to find information and calculate."),
goai.WithPrompt("How old is the Go programming language? Calculate 2026 minus the year it was created."),
goai.WithTools(searchTool, calculatorTool),
goai.WithMaxSteps(5),
goai.WithOnStepFinish(func(step goai.StepResult) {
fmt.Printf("--- Step %d (finish: %s, tools: %d) ---\n",
step.Number, step.FinishReason, len(step.ToolCalls))
}),
goai.WithOnToolCall(func(info goai.ToolCallInfo) {
fmt.Printf(" Tool: %s (%d bytes input)\n", info.ToolName, info.InputSize)
}),
)
if err != nil {
log.Fatal(err)
}
fmt.Println("\n=== Final Answer ===")
fmt.Println(result.Text)
fmt.Printf("Total steps: %d, Tokens: %d in, %d out\n",
len(result.Steps), result.TotalUsage.InputTokens, result.TotalUsage.OutputTokens)
}