Debian Codegraph @

examples/vertex-streaming/main.go

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