-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback_example_test.go
More file actions
90 lines (74 loc) · 2.53 KB
/
callback_example_test.go
File metadata and controls
90 lines (74 loc) · 2.53 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
package gai_test
import (
"context"
"fmt"
"slices"
"github.com/google/jsonschema-go/jsonschema"
"github.com/spachava753/gai"
)
// Define a parameter struct for our weather tool
type WeatherParams struct {
Location string `json:"location"`
Unit string `json:"unit,omitempty"`
}
func (w WeatherParams) Validate() error {
knownLocs := []string{"San Francisco", "New York", "London"}
if !slices.Contains(knownLocs, w.Location) {
return fmt.Errorf("unknown location: %s", w.Location)
}
return nil
}
// ExampleToolCallBackFunc demonstrates how to use ToolCallBackFunc to easily create
// tool callbacks with strongly-typed parameters.
func ExampleToolCallBackFunc() {
// Create a simple weather function that will be wrapped by ToolCallBackFunc
getWeather := func(ctx context.Context, params WeatherParams) (string, error) {
unit := "celsius"
if params.Unit == "fahrenheit" {
unit = "fahrenheit"
}
// In a real implementation, you would call an external weather API here
temp := 22.5
if unit == "fahrenheit" {
temp = temp*9/5 + 32
}
return fmt.Sprintf("Weather in %s: %.1f°%s",
params.Location,
temp,
unit[0:1]), // C or F
nil
}
// Create a tool
weatherTool := gai.Tool{
Name: "get_weather",
Description: "Get the current weather for a location",
InputSchema: func() *jsonschema.Schema {
schema, err := gai.GenerateSchema[struct {
Location string `json:"location" jsonschema:"required" jsonschema_description:"The city and state, e.g. San Francisco, CA"`
Unit string `json:"unit" jsonschema:"enum=celsius,enum=fahrenheit" jsonschema_description:"The unit of temperature"`
}]()
if err != nil {
panic(err)
}
return schema
}(),
}
// Create an instance of the ToolGenerator
// In a real application, you would use a real generator like OpenAiGenerator
toolGen := &gai.ToolGenerator{
G: &ExampleMockGenerator{},
}
// Register the tool with the wrapped callback function
_ = toolGen.Register(weatherTool, gai.ToolCallBackFunc[WeatherParams](getWeather))
// The tool is now registered and ready to use
fmt.Println("Weather tool registered successfully")
// Output: Weather tool registered successfully
}
// ExampleMockGenerator is a simple mock implementation of the ToolCapableGenerator interface
type ExampleMockGenerator struct{}
func (m *ExampleMockGenerator) Generate(ctx context.Context, dialog gai.Dialog, options *gai.GenOpts) (gai.Response, error) {
return gai.Response{}, nil
}
func (m *ExampleMockGenerator) Register(tool gai.Tool) error {
return nil
}