-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample_test.go
More file actions
252 lines (231 loc) · 9.44 KB
/
Copy pathexample_test.go
File metadata and controls
252 lines (231 loc) · 9.44 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package v1_test
import (
"context"
"errors"
"fmt"
"io"
"os"
claude "github.com/potproject/claude-sdk-go"
)
func Example() {
apiKey := os.Getenv("API_KEY")
c := claude.NewClient(apiKey)
m := claude.RequestBodyMessages{
Model: "claude-3-7-sonnet-20250219",
MaxTokens: 1024,
Messages: []claude.RequestBodyMessagesMessages{
{
Role: claude.MessagesRoleUser,
Content: "Hello, world!",
},
},
}
ctx := context.Background()
res, err := c.CreateMessages(ctx, m)
if err != nil {
panic(err)
}
fmt.Println(res.Content[0].Text)
// Output:
// Hello! How can I assist you today?
}
func Example_cache() {
apiKey := os.Getenv("API_KEY")
c := claude.NewClient(apiKey)
m := claude.RequestBodyMessages{
Model: "claude-3-7-sonnet-20250219",
MaxTokens: 1024,
SystemTypeText: []claude.RequestBodySystemTypeText{
claude.UseSystemCacheEphemeral("Please speak in Japanese."),
},
Messages: []claude.RequestBodyMessagesMessages{
{
Role: claude.MessagesRoleUser,
ContentTypeText: []claude.RequestBodyMessagesMessagesContentTypeText{
{
Text: "Hello!",
CacheControl: claude.UseCacheEphemeral(),
},
},
},
},
}
ctx := context.Background()
res, err := c.CreateMessages(ctx, m)
if err != nil {
panic(err)
}
fmt.Println(res.Content[0].Text)
// Output:
// こんにちは!日本語でお話しましょう。
}
func Example_typeImage() {
apiKey := os.Getenv("API_KEY")
c := claude.NewClient(apiKey)
m := claude.RequestBodyMessages{
Model: "claude-3-7-sonnet-20250219",
MaxTokens: 1024,
Messages: []claude.RequestBodyMessagesMessages{
{
Role: claude.MessagesRoleUser,
ContentTypeImage: []claude.RequestBodyMessagesMessagesContentTypeImage{
{
Source: claude.TypeImageSourceLoadBase64("image/png", "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMO0TAAD//2Anhf4QtqobAAAAAElFTkSuQmCC"),
},
},
},
},
}
ctx := context.Background()
res, err := c.CreateMessages(ctx, m)
if err != nil {
panic(err)
}
fmt.Println(res.Content[0].Text)
// Output:
// The image shows a smooth gradient transitioning from a bright, vibrant orange at the bottom to a light blue at the top. The colors blend seamlessly into each other, creating a visually striking and aesthetically pleasing effect. The simplicity of the gradient allows the colors to be the main focus, showcasing their luminosity and the way they interact with one another. This type of gradient is often used as a background or design element to add depth, warmth, and visual interest to various digital or print media projects.
}
func Example_stream() {
apiKey := os.Getenv("API_KEY")
c := claude.NewClient(apiKey)
m := claude.RequestBodyMessages{
Model: "claude-3-7-sonnet-20250219",
MaxTokens: 1024,
Messages: []claude.RequestBodyMessagesMessages{
{
Role: claude.MessagesRoleUser,
Content: "Hello, world!",
},
},
}
ctx := context.Background()
stream, err := c.CreateMessagesStream(ctx, m)
if err != nil {
panic(err)
}
defer stream.Close()
for {
res, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%s", res.Content[0].Text)
}
fmt.Println()
// Output:
// Hello! How can I assist you today?
//
}
func Example_thinking() {
apiKey := os.Getenv("API_KEY")
c := claude.NewClient(apiKey)
m := claude.RequestBodyMessages{
Model: "claude-3-7-sonnet-20250219",
MaxTokens: 8192,
Thinking: claude.UseThinking(4096),
Messages: []claude.RequestBodyMessagesMessages{
{
Role: claude.MessagesRoleUser,
Content: "Hello, world!",
},
},
}
ctx := context.Background()
res, err := c.CreateMessages(ctx, m)
if err != nil {
panic(err)
}
// Output:
// [thinking] This is a simple "Hello, world!" greeting from the user. It's a common first phrase in programming and also a standard greeting in conversations with AI assistants. I should respond in a friendly and welcoming manner.
// [text] Hi there! It's nice to meet you. "Hello, world!" is such a classic greeting - it brings back memories of first programming lessons for many! How are you doing today? Is there something specific I can help you with?
for _, v := range res.Content {
if v.Type == claude.ResponseBodyMessagesContentTypeThinking {
fmt.Println("[thinking]", v.Thinking)
}
if v.Type == claude.ResponseBodyMessagesContentTypeText {
fmt.Println("[text]", v.Text)
}
}
}
func Example_thinking_stream() {
apiKey := os.Getenv("API_KEY")
c := claude.NewClient(apiKey)
m := claude.RequestBodyMessages{
Model: "claude-3-7-sonnet-20250219",
MaxTokens: 8192,
Thinking: claude.UseThinking(4096),
Messages: []claude.RequestBodyMessagesMessages{
{
Role: claude.MessagesRoleUser,
Content: "Guess the Earth's population in 2100",
},
},
}
ctx := context.Background()
stream, err := c.CreateMessagesStream(ctx, m)
if err != nil {
panic(err)
}
defer stream.Close()
streamType := ""
for {
res, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
panic(err)
}
if res.Content[0].Type == claude.ResponseBodyMessagesContentTypeThinking && streamType != claude.ResponseBodyMessagesContentTypeThinking {
fmt.Println("[thinking]")
streamType = claude.ResponseBodyMessagesContentTypeThinking
}
if res.Content[0].Type == claude.ResponseBodyMessagesContentTypeText && streamType != claude.ResponseBodyMessagesContentTypeText {
fmt.Println()
fmt.Println("[text]")
streamType = claude.ResponseBodyMessagesContentTypeText
}
fmt.Printf("%s", res.Content[0].Thinking)
fmt.Printf("%s", res.Content[0].Text)
}
fmt.Println()
// Output:
// [thinking]
// I need to make a prediction about the Earth's population in the year 2100. This is asking me to make a forecast about demographic trends over the next ~75 years.
// To make a reasonable estimate, I should consider:
// 1. Current population (as of 2023, the world population is about 8 billion people)
// 2. Current population growth trends (the rate has been slowing)
// 3. UN and other expert demographic projections
// The UN Population Division regularly makes projections for future population growth. In their most recent projections, they typically provide several scenarios:
// - Low variant (assuming lower fertility rates)
// - Medium variant (their "most likely" scenario)
// - High variant (assuming higher fertility rates)
// Based on my knowledge, the UN's medium projection for 2100 is somewhere around 10-11 billion people. The range across different scenarios is quite wide, from potentially as low as 8-9 billion (if fertility rates drop significantly worldwide) to as high as 12-13 billion in high-growth scenarios.
// Some key factors that will influence the actual outcome:
// - Fertility rates in developing regions, especially Africa (currently with the highest growth rates)
// - Further demographic transition in middle-income countries
// - Aging populations in developed countries
// - Advances in medicine and healthcare
// - Climate change impacts
// - Migration patterns
// - Potential unforeseen events (pandemics, etc.)
// I should note that population projections become less certain the further into the future they go, and 2100 is quite far away. There's significant uncertainty in any estimate.
// My best estimate based on current projections would be around 10-11 billion people by 2100, with the understanding that this is subject to many variables and unknowns.owns.
// [text]
// # Earth's Population in 2100: Projection
// Based on demographic trends and expert forecasts, Earth's population in 2100 is projected to be approximately **10-11 billion people**.
// This estimate reflects the United Nations' medium-variant projection, which is generally considered the most likely scenario. However, there's significant uncertainty in long-term population forecasts due to several factors:
// ## Key Variables
// - Declining fertility rates worldwide
// - Aging populations in developed nations
// - Economic development in currently high-growth regions
// - Advances in healthcare and life expectancy
// - Climate change impacts
// - Migration patterns
// - Policy decisions regarding family planning
// Different models produce a range of estimates from as low as 8.5 billion to as high as 13 billion people by 2100, depending on how these variables evolve over time.
// Would you like me to elaborate on any particular aspect of future population trends?
}