Skip to content

Commit df08f9f

Browse files
feat(genai): Add generate content with routing option (#5327)
* feat(genai): Add generate content with routing option * feat(genai): Add generate content with routing option * feat(genai): Add generate content with routing option * feat(genai): Add generate content with routing option * feat(genai): Add generate content with routing option --------- Co-authored-by: Eric Schmidt <[email protected]>
1 parent cc3aa10 commit df08f9f

28 files changed

+173
-106
lines changed

genai/content_cache/contentcache_create_with_txt_gcs_pdf.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"time"
2425

2526
genai "google.golang.org/genai"
2627
)
@@ -65,7 +66,7 @@ func createContentCache(w io.Writer) (string, error) {
6566
},
6667
},
6768
DisplayName: "example-cache",
68-
TTL: "86400s",
69+
TTL: time.Duration(time.Duration.Seconds(86400)),
6970
}
7071

7172
res, err := client.Caches.Create(ctx, modelName, config)

genai/content_cache/contentcache_update.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@ func updateContentCache(w io.Writer, cacheName string) error {
3838

3939
// Update expire time using TTL
4040
resp, err := client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{
41-
TTL: "36000s",
41+
TTL: time.Duration(time.Duration.Seconds(36000)),
4242
})
4343
if err != nil {
4444
return fmt.Errorf("failed to update content cache exp. time with TTL: %w", err)
4545
}
4646

47-
fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(*resp.ExpireTime))
47+
fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(resp.ExpireTime))
4848
// Example response:
4949
// Cache expires in: 10h0m0.005875s
5050

5151
// Update expire time using specific time stamp
5252
inSevenDays := time.Now().Add(7 * 24 * time.Hour)
5353
resp, err = client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{
54-
ExpireTime: &inSevenDays,
54+
ExpireTime: inSevenDays,
5555
})
5656
if err != nil {
5757
return fmt.Errorf("failed to update content cache expire time: %w", err)
5858
}
5959

60-
fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(*resp.ExpireTime))
60+
fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(resp.ExpireTime))
6161
// Example response:
6262
// Cache expires in: 167h59m59.80327s
6363

genai/content_cache/contentcache_use_with_txt.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ func useContentCacheWithTxt(w io.Writer, cacheName string) error {
4646
return fmt.Errorf("failed to use content cache to generate content: %w", err)
4747
}
4848

49-
respText, err := resp.Text()
50-
if err != nil {
51-
return fmt.Errorf("failed to convert model response to text: %w", err)
52-
}
49+
respText := resp.Text()
50+
5351
fmt.Fprintln(w, respText)
5452

5553
// Example response:

genai/controlled_generation/ctrlgen_with_enum_schema.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func generateWithEnumSchema(w io.Writer) error {
3939
contents := []*genai.Content{
4040
{Parts: []*genai.Part{
4141
{Text: "What type of instrument is an oboe?"},
42-
}},
42+
}, Role: "user"},
4343
}
4444
config := &genai.GenerateContentConfig{
4545
ResponseMIMEType: "text/x.enum",
@@ -54,10 +54,8 @@ func generateWithEnumSchema(w io.Writer) error {
5454
return fmt.Errorf("failed to generate content: %w", err)
5555
}
5656

57-
respText, err := resp.Text()
58-
if err != nil {
59-
return fmt.Errorf("failed to convert model response to text: %w", err)
60-
}
57+
respText := resp.Text()
58+
6159
fmt.Fprintln(w, respText)
6260

6361
// Example response:

genai/controlled_generation/ctrlgen_with_nullable_schema.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F,
4949
contents := []*genai.Content{
5050
{Parts: []*genai.Part{
5151
{Text: prompt},
52-
}},
52+
},
53+
Role: "user"},
5354
}
5455
config := &genai.GenerateContentConfig{
5556
ResponseMIMEType: "application/json",
@@ -63,11 +64,11 @@ Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F,
6364
Items: &genai.Schema{
6465
Type: "object",
6566
Properties: map[string]*genai.Schema{
66-
"Day": {Type: "string", Nullable: true},
67-
"Forecast": {Type: "string", Nullable: true},
68-
"Temperature": {Type: "integer", Nullable: true},
69-
"Humidity": {Type: "string", Nullable: true},
70-
"Wind Speed": {Type: "integer", Nullable: true},
67+
"Day": {Type: "string", Nullable: genai.Ptr(true)},
68+
"Forecast": {Type: "string", Nullable: genai.Ptr(true)},
69+
"Temperature": {Type: "integer", Nullable: genai.Ptr(true)},
70+
"Humidity": {Type: "string", Nullable: genai.Ptr(true)},
71+
"Wind Speed": {Type: "integer", Nullable: genai.Ptr(true)},
7172
},
7273
Required: []string{"Day", "Temperature", "Forecast", "Wind Speed"},
7374
},
@@ -81,10 +82,8 @@ Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F,
8182
return fmt.Errorf("failed to generate content: %w", err)
8283
}
8384

84-
respText, err := resp.Text()
85-
if err != nil {
86-
return fmt.Errorf("failed to convert model response to text: %w", err)
87-
}
85+
respText := resp.Text()
86+
8887
fmt.Fprintln(w, respText)
8988

9089
// Example response:

genai/controlled_generation/ctrlgen_with_resp_schema.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func generateWithRespSchema(w io.Writer) error {
5757
contents := []*genai.Content{
5858
{Parts: []*genai.Part{
5959
{Text: "List a few popular cookie recipes."},
60-
}},
60+
},
61+
Role: "user"},
6162
}
6263
modelName := "gemini-2.0-flash-001"
6364

@@ -66,10 +67,8 @@ func generateWithRespSchema(w io.Writer) error {
6667
return fmt.Errorf("failed to generate content: %w", err)
6768
}
6869

69-
respText, err := resp.Text()
70-
if err != nil {
71-
return fmt.Errorf("failed to convert model response to text: %w", err)
72-
}
70+
respText := resp.Text()
71+
7372
fmt.Fprintln(w, respText)
7473

7574
// Example response:

genai/count_tokens/counttoken_compute_with_txt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func computeWithTxt(w io.Writer) error {
4040
contents := []*genai.Content{
4141
{Parts: []*genai.Part{
4242
{Text: "What's the longest word in the English language?"},
43-
}},
43+
},
44+
Role: "user"},
4445
}
4546

4647
resp, err := client.Models.ComputeTokens(ctx, modelName, contents, nil)

genai/count_tokens/counttoken_resp_with_txt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func generateTextAndCount(w io.Writer) error {
4040
contents := []*genai.Content{
4141
{Parts: []*genai.Part{
4242
{Text: "Why is the sky blue?"},
43-
}},
43+
},
44+
Role: "user"},
4445
}
4546

4647
resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)

genai/count_tokens/counttoken_with_txt_vid.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ func countWithTxtAndVid(w io.Writer) error {
4343
FileURI: "gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
4444
MIMEType: "video/mp4",
4545
}},
46-
}},
46+
},
47+
Role: "user"},
4748
}
4849

4950
resp, err := client.Models.CountTokens(ctx, modelName, contents, nil)

genai/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.23.0
44

55
require (
66
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974
7-
google.golang.org/genai v0.4.0
7+
google.golang.org/genai v1.6.0
88
)
99

1010
require (

0 commit comments

Comments
 (0)