Debian Codegraph @

examples/message-streaming/main.go

raw
1 package main
2
3 import (
4 "context"
5
6 "github.com/anthropics/anthropic-sdk-go"
7 )
8
9 func main() {
10 client := anthropic.NewClient()
11
12 content := "Write me a function to call the Anthropic message API in Node.js using the Anthropic Typescript SDK."
13
14 println("[user]: " + content)
15
16 stream := client.Messages.NewStreaming(context.TODO(), anthropic.MessageNewParams{
17 MaxTokens: 1024,
18 Messages: []anthropic.MessageParam{
19 anthropic.NewUserMessage(anthropic.NewTextBlock(content)),
20 },
21 Model: anthropic.ModelClaudeSonnet4_5_20250929,
22 StopSequences: []string{"```\n"},
23 })
24
25 print("[assistant]: ")
26
27 for stream.Next() {
28 event := stream.Current()
29
30 switch eventVariant := event.AsAny().(type) {
31 case anthropic.MessageDeltaEvent:
32 print(eventVariant.Delta.StopSequence)
33 case anthropic.ContentBlockDeltaEvent:
34 switch deltaVariant := eventVariant.Delta.AsAny().(type) {
35 case anthropic.TextDelta:
36 print(deltaVariant.Text)
37 }
38 }
39 }
40
41 println()
42
43 if stream.Err() != nil {
44 panic(stream.Err())
45 }
46 }