-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (86 loc) · 2.83 KB
/
Copy pathmain.go
File metadata and controls
108 lines (86 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"fmt"
"os"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
client := anthropic.NewClient(option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")))
ctx := context.Background()
fmt.Println("=== Streaming Response ===")
streamResponse(ctx, client)
fmt.Println("\n=== Streaming Multi-Turn Conversation ===")
streamMultiTurn(ctx, client)
}
// streamResponse demonstrates basic streaming — tokens appear as they're generated.
func streamResponse(ctx context.Context, client *anthropic.Client) {
stream := client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
Model: anthropic.ModelClaudeOpus4_7,
MaxTokens: 1024,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Explain goroutines in Go in 3 sentences.")),
},
})
// Accumulate the full message so we can inspect it after streaming
message := anthropic.Message{}
for stream.Next() {
event := stream.Current()
message.Accumulate(event)
// Print text deltas as they arrive — this is what makes the UI feel live
switch delta := event.Delta.(type) {
case anthropic.ContentBlockDeltaEventDelta:
if delta.Type == "text_delta" {
fmt.Print(delta.Text)
}
}
}
if err := stream.Err(); err != nil {
fmt.Fprintf(os.Stderr, "stream error: %v\n", err)
os.Exit(1)
}
fmt.Println() // newline after stream ends
fmt.Printf("\n[Stop reason: %s | Input tokens: %d | Output tokens: %d]\n",
message.StopReason,
message.Usage.InputTokens,
message.Usage.OutputTokens,
)
}
// streamMultiTurn shows streaming inside a multi-turn loop.
// We accumulate each streamed response into the history before sending the next turn.
func streamMultiTurn(ctx context.Context, client *anthropic.Client) {
history := []anthropic.MessageParam{}
turns := []string{
"What are channels in Go?",
"How do channels relate to goroutines?",
}
for _, userMsg := range turns {
fmt.Printf("\n[User]: %s\n[Claude]: ", userMsg)
history = append(history, anthropic.NewUserMessage(anthropic.NewTextBlock(userMsg)))
stream := client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
Model: anthropic.ModelClaudeOpus4_7,
MaxTokens: 512,
Messages: history,
})
// Accumulate so we can add the full assistant turn to history
message := anthropic.Message{}
for stream.Next() {
event := stream.Current()
message.Accumulate(event)
switch delta := event.Delta.(type) {
case anthropic.ContentBlockDeltaEventDelta:
if delta.Type == "text_delta" {
fmt.Print(delta.Text)
}
}
}
if err := stream.Err(); err != nil {
fmt.Fprintf(os.Stderr, "stream error: %v\n", err)
os.Exit(1)
}
fmt.Println()
// Add accumulated assistant message to history for context in next turn
history = append(history, message.ToParam())
}
}