-
Notifications
You must be signed in to change notification settings - Fork 59
/
example_test.go
565 lines (500 loc) · 15 KB
/
example_test.go
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package genai_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/googleapi"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
func ExampleGenerativeModel_GenerateContent() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
// This example shows how to a configure a model. See [GenerationConfig]
// for the complete set of configuration options.
func ExampleGenerativeModel_GenerateContent_config() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-pro-latest")
model.SetTemperature(0.9)
model.SetTopP(0.5)
model.SetTopK(20)
model.SetMaxOutputTokens(100)
model.SystemInstruction = &genai.Content{
Parts: []genai.Part{genai.Text("You are Yoda from Star Wars.")},
}
model.ResponseMIMEType = "application/json"
resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
// This example shows how to use SafetySettings to change the threshold
// for unsafe responses.
func ExampleGenerativeModel_GenerateContent_safetySetting() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
model.SafetySettings = []*genai.SafetySetting{
{
Category: genai.HarmCategoryDangerousContent,
Threshold: genai.HarmBlockLowAndAbove,
},
{
Category: genai.HarmCategoryHarassment,
Threshold: genai.HarmBlockMediumAndAbove,
},
}
resp, err := model.GenerateContent(ctx, genai.Text("I want to be bad. Please help."))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
func ExampleGenerativeModel_GenerateContentStream() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
iter := model.GenerateContentStream(ctx, genai.Text("Tell me a story about a lumberjack and his giant ox. Keep it very short."))
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
}
func ExampleGenerativeModel_CountTokens() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
resp, err := model.CountTokens(ctx, genai.Text("What kind of fish is this?"))
if err != nil {
log.Fatal(err)
}
fmt.Println("Num tokens:", resp.TotalTokens)
}
// This example shows how to get a JSON response that conforms to a schema.
func ExampleGenerativeModel_JSONSchema() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-pro-latest")
// Ask the model to respond with JSON.
model.ResponseMIMEType = "application/json"
// Specify the format of the JSON.
model.ResponseSchema = &genai.Schema{
Type: genai.TypeArray,
Items: &genai.Schema{Type: genai.TypeString},
}
res, err := model.GenerateContent(ctx, genai.Text("List the primary colors."))
if err != nil {
log.Fatal(err)
}
for _, part := range res.Candidates[0].Content.Parts {
if txt, ok := part.(genai.Text); ok {
var colors []string
if err := json.Unmarshal([]byte(txt), &colors); err != nil {
log.Fatal(err)
}
fmt.Println(colors)
}
}
}
func ExampleChatSession() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
cs := model.StartChat()
send := func(msg string) *genai.GenerateContentResponse {
fmt.Printf("== Me: %s\n== Model:\n", msg)
res, err := cs.SendMessage(ctx, genai.Text(msg))
if err != nil {
log.Fatal(err)
}
return res
}
res := send("Can you name some brands of air fryer?")
printResponse(res)
iter := cs.SendMessageStream(ctx, genai.Text("Which one of those do you recommend?"))
for {
res, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(res)
}
for i, c := range cs.History {
log.Printf(" %d: %+v", i, c)
}
res = send("Why do you like the Philips?")
if err != nil {
log.Fatal(err)
}
printResponse(res)
}
// This example shows how to set the History field on ChatSession explicitly.
func ExampleChatSession_history() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
cs := model.StartChat()
cs.History = []*genai.Content{
{
Parts: []genai.Part{
genai.Text("Hello, I have 2 dogs in my house."),
},
Role: "user",
},
{
Parts: []genai.Part{
genai.Text("Great to meet you. What would you like to know?"),
},
Role: "model",
},
}
res, err := cs.SendMessage(ctx, genai.Text("How many paws are in my house?"))
if err != nil {
log.Fatal(err)
}
printResponse(res)
}
func ExampleEmbeddingModel_EmbedContent() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
em := client.EmbeddingModel("embedding-001")
res, err := em.EmbedContent(ctx, genai.Text("cheddar cheese"))
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Embedding.Values)
}
func ExampleEmbeddingBatch() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
em := client.EmbeddingModel("embedding-001")
b := em.NewBatch().
AddContent(genai.Text("cheddar cheese")).
AddContentWithTitle("My Cheese Report", genai.Text("I love cheddar cheese."))
res, err := em.BatchEmbedContents(ctx, b)
if err != nil {
panic(err)
}
for _, e := range res.Embeddings {
fmt.Println(e.Values)
}
}
// This example shows how to get more information from an error.
func ExampleGenerativeModel_GenerateContentStream_errors() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
model := client.GenerativeModel("gemini-1.0-pro")
iter := model.GenerateContentStream(ctx, genai.ImageData("foo", []byte("bar")))
res, err := iter.Next()
if err != nil {
var gerr *googleapi.Error
if !errors.As(err, &gerr) {
log.Fatalf("error: %s\n", err)
} else {
log.Fatalf("error details: %s\n", gerr)
}
}
_ = res
}
func ExampleClient_ListModels() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
iter := client.ListModels(ctx)
for {
m, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
panic(err)
}
fmt.Println(m.Name, m.Description)
}
}
func ExampleTool() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
currentWeather := func(city string) string {
switch city {
case "New York, NY":
return "cold"
case "Miami, FL":
return "warm"
default:
return "unknown"
}
}
// To use functions / tools, we have to first define a schema that describes
// the function to the model. The schema is similar to OpenAPI 3.0.
//
// In this example, we create a single function that provides the model with
// a weather forecast in a given location.
schema := &genai.Schema{
Type: genai.TypeObject,
Properties: map[string]*genai.Schema{
"location": {
Type: genai.TypeString,
Description: "The city and state, e.g. San Francisco, CA",
},
"unit": {
Type: genai.TypeString,
Enum: []string{"celsius", "fahrenheit"},
},
},
Required: []string{"location"},
}
weatherTool := &genai.Tool{
FunctionDeclarations: []*genai.FunctionDeclaration{{
Name: "CurrentWeather",
Description: "Get the current weather in a given location",
Parameters: schema,
}},
}
model := client.GenerativeModel("gemini-1.0-pro")
// Before initiating a conversation, we tell the model which tools it has
// at its disposal.
model.Tools = []*genai.Tool{weatherTool}
// For using tools, the chat mode is useful because it provides the required
// chat context. A model needs to have tools supplied to it in the chat
// history so it can use them in subsequent conversations.
//
// The flow of message expected here is:
//
// 1. We send a question to the model
// 2. The model recognizes that it needs to use a tool to answer the question,
// an returns a FunctionCall response asking to use the CurrentWeather
// tool.
// 3. We send a FunctionResponse message, simulating the return value of
// CurrentWeather for the model's query.
// 4. The model provides its text answer in response to this message.
session := model.StartChat()
res, err := session.SendMessage(ctx, genai.Text("What is the weather like in New York?"))
if err != nil {
log.Fatal(err)
}
part := res.Candidates[0].Content.Parts[0]
funcall, ok := part.(genai.FunctionCall)
if !ok {
log.Fatalf("expected FunctionCall: %v", part)
}
if funcall.Name != "CurrentWeather" {
log.Fatalf("expected CurrentWeather: %v", funcall.Name)
}
// Expect the model to pass a proper string "location" argument to the tool.
locArg, ok := funcall.Args["location"].(string)
if !ok {
log.Fatalf("expected string: %v", funcall.Args["location"])
}
weatherData := currentWeather(locArg)
res, err = session.SendMessage(ctx, genai.FunctionResponse{
Name: weatherTool.FunctionDeclarations[0].Name,
Response: map[string]any{
"weather": weatherData,
},
})
if err != nil {
log.Fatal(err)
}
printResponse(res)
}
func ExampleToolConfig() {
// This example shows how to affect how the model uses the tools provided to it.
// By setting the ToolConfig, you can disable function calling.
// Assume we have created a Model and have set its Tools field with some functions.
// See the Example for Tool for details.
var model *genai.GenerativeModel
// By default, the model will use the functions in its responses if it thinks they are
// relevant, by returning FunctionCall parts.
// Here we set the model's ToolConfig to disable function calling completely.
model.ToolConfig = &genai.ToolConfig{
FunctionCallingConfig: &genai.FunctionCallingConfig{
Mode: genai.FunctionCallingNone,
},
}
// Subsequent calls to ChatSession.SendMessage will not result in FunctionCall responses.
session := model.StartChat()
res, err := session.SendMessage(context.Background(), genai.Text("What is the weather like in New York?"))
if err != nil {
log.Fatal(err)
}
for _, part := range res.Candidates[0].Content.Parts {
if _, ok := part.(genai.FunctionCall); ok {
log.Fatal("did not expect FunctionCall")
}
}
// It is also possible to force a function call by using FunctionCallingAny
// instead of FunctionCallingNone. See the documentation for FunctionCallingMode
// for details.
}
func ExampleClient_UploadFile() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Use Client.UploadFile to Upload a file to the service.
// Pass it an io.Reader.
f, err := os.Open("path/to/file")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// You can choose a name, or pass the empty string to generate a unique one.
file, err := client.UploadFile(ctx, "", f, nil)
if err != nil {
log.Fatal(err)
}
// The return value's URI field should be passed to the model in a FileData part.
model := client.GenerativeModel("gemini-1.5-pro")
resp, err := model.GenerateContent(ctx, genai.FileData{URI: file.URI})
if err != nil {
log.Fatal(err)
}
_ = resp // Use resp as usual.
}
// ProxyRoundTripper is an implementation of http.RoundTripper that supports
// setting a proxy server URL for genai clients. This type should be used with
// a custom http.Client that's passed to WithHTTPClient. For such clients,
// WithAPIKey doesn't apply so the key has to be explicitly set here.
type ProxyRoundTripper struct {
// APIKey is the API Key to set on requests.
APIKey string
// ProxyURL is the URL of the proxy server. If empty, no proxy is used.
ProxyURL string
}
func (t *ProxyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
transport := http.DefaultTransport.(*http.Transport).Clone()
if t.ProxyURL != "" {
proxyURL, err := url.Parse(t.ProxyURL)
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(proxyURL)
}
newReq := req.Clone(req.Context())
vals := newReq.URL.Query()
vals.Set("key", t.APIKey)
newReq.URL.RawQuery = vals.Encode()
resp, err := transport.RoundTrip(newReq)
if err != nil {
return nil, err
}
return resp, nil
}
func ExampleClient_setProxy() {
c := &http.Client{Transport: &ProxyRoundTripper{
APIKey: os.Getenv("GEMINI_API_KEY"),
ProxyURL: "http://<proxy-url>",
}}
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithHTTPClient(c))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.0-pro")
resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
func printResponse(resp *genai.GenerateContentResponse) {
for _, cand := range resp.Candidates {
if cand.Content != nil {
for _, part := range cand.Content.Parts {
fmt.Println(part)
}
}
}
fmt.Println("---")
}