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