Debian Codegraph @

examples/tools/main.go

raw
1 package main
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7
8 "github.com/anthropics/anthropic-sdk-go"
9 )
10
11 func main() {
12 client := anthropic.NewClient()
13
14 content := "What is the weather in San Francisco, CA?"
15
16 println(color("[user]: ") + content)
17
18 messages := []anthropic.MessageParam{
19 anthropic.NewUserMessage(anthropic.NewTextBlock(content)),
20 }
21
22 toolParams := []anthropic.ToolParam{
23 {
24 Name: "get_coordinates",
25 Description: anthropic.String("Accepts a place as an address, then returns the latitude and longitude coordinates."),
26 InputSchema: anthropic.ToolInputSchemaParam{
27 Properties: map[string]any{
28 "location": map[string]any{
29 "type": "string",
30 "description": "The location to look up.",
31 },
32 },
33 },
34 },
35 {
36 Name: "get_temperature_unit",
37 InputSchema: anthropic.ToolInputSchemaParam{
38 Properties: map[string]any{
39 "country": map[string]any{
40 "type": "string",
41 "description": "The country",
42 },
43 },
44 },
45 },
46 {
47 Name: "get_weather",
48 Description: anthropic.String("Get the weather at a specific location"),
49 InputSchema: anthropic.ToolInputSchemaParam{
50 Properties: map[string]any{
51 "lat": map[string]any{
52 "type": "number",
53 "description": "The lattitude of the location to check weather.",
54 },
55 "long": map[string]any{
56 "type": "number",
57 "description": "The longitude of the location to check weather.",
58 },
59 "unit": map[string]any{
60 "type": "string",
61 "enum": []string{"celsius", "fahrenheit"},
62 "description": "Unit for the output",
63 },
64 },
65 },
66 },
67 }
68 tools := make([]anthropic.ToolUnionParam, len(toolParams))
69 for i, toolParam := range toolParams {
70 tools[i] = anthropic.ToolUnionParam{OfTool: &toolParam}
71 }
72
73 for {
74 message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
75 Model: anthropic.ModelClaudeSonnet4_5_20250929,
76 MaxTokens: 1024,
77 Messages: messages,
78 Tools: tools,
79 })
80
81 if err != nil {
82 panic(err)
83 }
84
85 print(color("[assistant]: "))
86
87 for _, block := range message.Content {
88 switch block := block.AsAny().(type) {
89 case anthropic.TextBlock:
90 println(block.Text)
91 println()
92 case anthropic.ToolUseBlock:
93 inputJSON, _ := json.Marshal(block.Input)
94 println(block.Name + ": " + string(inputJSON))
95 println()
96 }
97 }
98 println()
99
100 messages = append(messages, message.ToParam())
101 toolResults := []anthropic.ContentBlockParamUnion{}
102
103 for _, block := range message.Content {
104 switch variant := block.AsAny().(type) {
105 case anthropic.ToolUseBlock:
106 print(color("[user (" + block.Name + ")]: "))
107
108 var response any
109 switch block.Name {
110 case "get_coordinates":
111 var input struct {
112 Location string `json:"location"`
113 }
114
115 err := json.Unmarshal([]byte(variant.JSON.Input.Raw()), &input)
116 if err != nil {
117 panic(err)
118 }
119
120 response = GetCoordinates(input.Location)
121 case "get_temperature_unit":
122 var input struct {
123 Country string `json:"country"`
124 }
125 err := json.Unmarshal([]byte(variant.JSON.Input.Raw()), &input)
126 if err != nil {
127 panic(err)
128 }
129 response = GetTemperatureUnit(input.Country)
130 case "get_weather":
131 var input struct {
132 Lat float64 `json:"lat"`
133 Long float64 `json:"long"`
134 Unit string `json:"unit"`
135 }
136 err := json.Unmarshal([]byte(variant.JSON.Input.Raw()), &input)
137 if err != nil {
138 panic(err)
139 }
140 response = GetWeather(input.Lat, input.Long, input.Unit)
141 }
142
143 b, err := json.Marshal(response)
144 if err != nil {
145 panic(err)
146 }
147
148 println(string(b))
149
150 toolResults = append(toolResults, anthropic.NewToolResultBlock(block.ID, string(b), false))
151 }
152 }
153
154 if len(toolResults) == 0 {
155 break
156 }
157
158 messages = append(messages, anthropic.NewUserMessage(toolResults...))
159 }
160 }
161
162 type CoordinateResponse struct {
163 Long float64 `json:"long"`
164 Lat float64 `json:"lat"`
165 }
166
167 func GetCoordinates(location string) CoordinateResponse {
168 return CoordinateResponse{
169 Long: -122.4194,
170 Lat: 37.7749,
171 }
172 }
173
174 func GetTemperatureUnit(country string) string {
175 return "farenheit"
176 }
177
178 type WeatherResponse struct {
179 Unit string `json:"unit"`
180 Temperature float64 `json:"temperature"`
181 }
182
183 func GetWeather(lat, long float64, unit string) WeatherResponse {
184 return WeatherResponse{
185 Unit: "farenheit",
186 Temperature: 122,
187 }
188 }
189
190 func color(s string) string {
191 return fmt.Sprintf("\033[1;%sm%s\033[0m", "33", s)
192 }