| 1 |
// Example demonstrating the Tool Runner framework with streaming |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
|
| 8 |
"github.com/anthropics/anthropic-sdk-go" |
| 9 |
"github.com/anthropics/anthropic-sdk-go/toolrunner" |
| 10 |
) |
| 11 |
|
| 12 |
type WeatherRequest struct { |
| 13 |
City string `json:"city" jsonschema:"required,description=The city to get weather for"` |
| 14 |
Units string `json:"units,omitempty" jsonschema:"description=Temperature units,enum=celsius,fahrenheit"` |
| 15 |
Include string `json:"include,omitempty" jsonschema:"description=Additional data to include,enum=forecast,historical"` |
| 16 |
} |
| 17 |
|
| 18 |
func colorWith(code string, s string) string { return fmt.Sprintf("\033[1;%sm%s\033[0m", code, s) } |
| 19 |
func colorUser(s string) string { return colorWith("36", s) } // cyan |
| 20 |
func colorAssistant(s string) string { return colorWith("32", s) } // green |
| 21 |
func colorTool(s string) string { return colorWith("33", s) } // yellow |
| 22 |
func colorThinking(s string) string { return colorWith("90", s) } // grey |
| 23 |
|
| 24 |
func getWeather(ctx context.Context, req WeatherRequest) (anthropic.BetaToolResultBlockParamContentUnion, error) { |
| 25 |
fmt.Printf("%s%s %+v\n", colorTool("[tool get_weather]: "), "called with", req) |
| 26 |
|
| 27 |
temp := 22 |
| 28 |
if req.Units == "fahrenheit" { |
| 29 |
temp = 72 |
| 30 |
} |
| 31 |
|
| 32 |
return anthropic.BetaToolResultBlockParamContentUnion{ |
| 33 |
OfText: &anthropic.BetaTextBlockParam{ |
| 34 |
Text: fmt.Sprintf("The current weather in %s is %d degrees %s. Tomorrow's weather will be cloudy and colder.", req.City, temp, req.Units), |
| 35 |
}, |
| 36 |
}, nil |
| 37 |
} |
| 38 |
|
| 39 |
func main() { |
| 40 |
client := anthropic.NewClient() |
| 41 |
ctx := context.Background() |
| 42 |
|
| 43 |
weatherTool, err := toolrunner.NewBetaToolFromJSONSchema("get_weather", "Get current weather information for a city", getWeather) |
| 44 |
if err != nil { |
| 45 |
fmt.Printf("Error creating weather tool: %v\n", err) |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
userQuestion := "What's the weather like in San Francisco? Please use Fahrenheit and include the forecast." |
| 50 |
fmt.Println(colorUser("[user]: ") + userQuestion) |
| 51 |
|
| 52 |
tools := []anthropic.BetaTool{weatherTool} |
| 53 |
|
| 54 |
runner := client.Beta.Messages.NewToolRunnerStreaming(tools, anthropic.BetaToolRunnerParams{ |
| 55 |
BetaMessageNewParams: anthropic.BetaMessageNewParams{ |
| 56 |
Model: anthropic.ModelClaudeSonnet4_20250514, |
| 57 |
MaxTokens: 1000, |
| 58 |
Messages: []anthropic.BetaMessageParam{ |
| 59 |
anthropic.NewBetaUserMessage( |
| 60 |
anthropic.NewBetaTextBlock(userQuestion), |
| 61 |
), |
| 62 |
}, |
| 63 |
}, |
| 64 |
MaxIterations: 5, |
| 65 |
}) |
| 66 |
|
| 67 |
for eventsIterator := range runner.AllStreaming(ctx) { |
| 68 |
_ = runner.IterationCount() |
| 69 |
for event, err := range eventsIterator { |
| 70 |
if err != nil { |
| 71 |
fmt.Printf("%s %+v\n", colorWith("31", "[error]: "+err.Error()), event) |
| 72 |
return |
| 73 |
} |
| 74 |
switch eventVariant := event.AsAny().(type) { |
| 75 |
case anthropic.BetaRawMessageStartEvent: |
| 76 |
fmt.Print(colorAssistant("[assistant]: ")) |
| 77 |
case anthropic.BetaRawContentBlockStartEvent: |
| 78 |
switch cb := eventVariant.ContentBlock.AsAny().(type) { |
| 79 |
case anthropic.BetaToolUseBlock: |
| 80 |
// Assistant is initiating a tool call; stream its JSON input deltas next |
| 81 |
label := fmt.Sprintf("[tool call %s]: ", cb.Name) |
| 82 |
fmt.Print(colorTool(label)) |
| 83 |
case anthropic.BetaTextBlock: |
| 84 |
// nothing, normal assistant text will follow via deltas |
| 85 |
case anthropic.BetaThinkingBlock: |
| 86 |
fmt.Print(colorThinking("[assistant thinking]: ")) |
| 87 |
} |
| 88 |
case anthropic.BetaRawContentBlockDeltaEvent: |
| 89 |
switch deltaVariant := eventVariant.Delta.AsAny().(type) { |
| 90 |
case anthropic.BetaTextDelta: |
| 91 |
fmt.Print(colorAssistant(deltaVariant.Text)) |
| 92 |
case anthropic.BetaInputJSONDelta: |
| 93 |
if deltaVariant.PartialJSON != "" { |
| 94 |
fmt.Print(colorTool(deltaVariant.PartialJSON)) |
| 95 |
} |
| 96 |
case anthropic.BetaThinkingDelta: |
| 97 |
fmt.Print(colorThinking(deltaVariant.Thinking)) |
| 98 |
} |
| 99 |
case anthropic.BetaRawContentBlockStopEvent: |
| 100 |
fmt.Println() |
| 101 |
case anthropic.BetaRawMessageDeltaEvent: |
| 102 |
// No visible text here; keep for completeness |
| 103 |
case anthropic.BetaRawMessageStopEvent: |
| 104 |
fmt.Println() |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |