Skip to content

Commit ca42e6c

Browse files
cfloressgemini-code-assist[bot]msampathkumar
authored
feat(genai): Add samples for Content-cache and Text-generation (#5358)
* updated genai go sdk from 1.6.0 to 1.17.0 and fix broken test * genai:updated go sdk from 1.6.0 to 1.17.0 and fix broken test * genai: PR comments addressed * genai: go mod tidy * genai: textgen_with_routing test case commented out * genai: textgen_with_routing test case commented out * genai: added content_cache list sample * genai: added textgen samples * Update genai/content_cache/content_cache_test.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update genai/text_generation/text_generation_examples_test.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update content_cache_test.go * genai: PR comments --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Sampath Kumar <[email protected]>
1 parent da9ab8f commit ca42e6c

File tree

7 files changed

+413
-4
lines changed

7 files changed

+413
-4
lines changed

genai/content_cache/content_cache_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ func TestContentCaching(t *testing.T) {
4545
t.Errorf("updateContentCache: %v", err.Error())
4646
}
4747

48-
// 3) Use cached content with a text prompt.
48+
// 3) List and inspect the content cache.
49+
err = listContentCache(buf)
50+
if err != nil {
51+
t.Errorf("listContentCache: %v", err.Error())
52+
}
53+
54+
// 4) Use cached content with a text prompt.
4955
err = useContentCacheWithTxt(buf, cacheName)
5056
if err != nil {
5157
t.Errorf("useContentCacheWithTxt: %v", err.Error())
5258
}
5359

54-
// 4) Delete the content cache.
60+
// 5) Delete the content cache.
5561
buf.Reset()
5662
err = deleteContentCache(buf, cacheName)
5763
if err != nil {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package content_cache shows examples of using content caching with the GenAI SDK.
16+
package content_cache
17+
18+
// [START googlegenaisdk_contentcache_list]
19+
import (
20+
"context"
21+
"fmt"
22+
"io"
23+
"net/http"
24+
"time"
25+
26+
"google.golang.org/genai"
27+
)
28+
29+
// listContentCache shows how to retrieve details about cached content.
30+
func listContentCache(w io.Writer) error {
31+
ctx := context.Background()
32+
33+
client, err := genai.NewClient(ctx, &genai.ClientConfig{
34+
HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
35+
})
36+
if err != nil {
37+
return fmt.Errorf("failed to create genai client: %w", err)
38+
}
39+
40+
// Retrieve cached content metadata
41+
cache, err := client.Caches.List(ctx, &genai.ListCachedContentsConfig{
42+
HTTPOptions: &genai.HTTPOptions{
43+
Headers: http.Header{"X-Custom-Header": []string{"example"}},
44+
APIVersion: "v1",
45+
},
46+
})
47+
if err != nil {
48+
return fmt.Errorf("failed to get content cache: %w", err)
49+
}
50+
51+
// Print basic info about the cached content
52+
fmt.Fprintf(w, "Cache name: %s\n", cache.Name)
53+
fmt.Fprintf(w, "Display name: %s\n", cache.Items[0].DisplayName)
54+
fmt.Fprintf(w, "Model: %s\n", cache.Items[0].Model)
55+
fmt.Fprintf(w, "Create time: %s\n", cache.Items[0].CreateTime.Format(time.RFC3339))
56+
fmt.Fprintf(w, "Update time: %s\n", cache.Items[0].UpdateTime.Format(time.RFC3339))
57+
fmt.Fprintf(w, "Expire time: %s (in %s)\n", cache.Items[0].ExpireTime.Format(time.RFC3339), time.Until(cache.Items[0].ExpireTime).Round(time.Second))
58+
59+
if cache.Items[0].UsageMetadata != nil {
60+
fmt.Fprintf(w, "Usage metadata: %+v\n", cache.Items[0].UsageMetadata)
61+
}
62+
63+
// Example response:
64+
// Cache name: projects/111111111111/locations/us-central1/cachedContents/1234567890123456789
65+
// Display name: product_recommendations_prompt
66+
// Model: models/gemini-2.5-flash
67+
// Create time: 2025-04-08T02:15:23Z
68+
// Update time: 2025-04-08T03:05:11Z
69+
// Expire time: 2025-04-20T03:05:11Z (in 167h59m59s)
70+
// Usage metadata: &{AudioDurationSeconds:0 ImageCount:167 TextCount:153 TotalTokenCount:43124 VideoDurationSeconds:0}
71+
72+
return nil
73+
}
74+
75+
// [END googlegenaisdk_contentcache_list]

genai/text_generation/text_generation_examples_test.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func TestTextGeneration(t *testing.T) {
173173
}
174174
})
175175

176-
/*t.Run("generate with routing", func(t *testing.T) {
176+
t.Run("generate with routing", func(t *testing.T) {
177+
t.Skip("skipping because of model used in this test. The model 'model-optimizer-exp-04-09' is not consistently available in all test environments.")
177178
buf.Reset()
178179
err := generateWithRouting(buf)
179180
if err != nil {
@@ -184,6 +185,58 @@ func TestTextGeneration(t *testing.T) {
184185
if output == "" {
185186
t.Error("expected non-empty output, got empty")
186187
}
187-
})*/
188+
})
189+
190+
t.Run("generate chat stream with text prompt", func(t *testing.T) {
191+
buf.Reset()
192+
err := generateChatStreamWithText(buf)
193+
if err != nil {
194+
t.Fatalf("generateChatStreamWithText failed: %v", err)
195+
}
196+
197+
output := buf.String()
198+
if output == "" {
199+
t.Error("expected non-empty output, got empty")
200+
}
201+
})
202+
203+
t.Run("generate Text With PDF", func(t *testing.T) {
204+
buf.Reset()
205+
err := generateTextWithPDF(buf)
206+
if err != nil {
207+
t.Fatalf("generateTextWithPDF failed: %v", err)
208+
}
209+
210+
output := buf.String()
211+
if output == "" {
212+
t.Error("expected non-empty output, got empty")
213+
}
214+
})
215+
216+
t.Run("generate thinking with text prompt", func(t *testing.T) {
217+
buf.Reset()
218+
err := generateThinkingWithText(buf)
219+
if err != nil {
220+
t.Fatalf("generateThinkingWithText failed: %v", err)
221+
}
222+
223+
output := buf.String()
224+
if output == "" {
225+
t.Error("expected non-empty output, got empty")
226+
}
227+
})
228+
229+
t.Run("generate with model optimizer", func(t *testing.T) {
230+
buf.Reset()
231+
err := generateModelOptimizerWithTxt(buf)
232+
if err != nil {
233+
t.Fatalf("generateModelOptimizerWithTxt failed: %v", err)
234+
}
235+
236+
output := buf.String()
237+
if output == "" {
238+
t.Error("expected non-empty output, got empty")
239+
}
240+
})
188241

189242
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package text_generation shows examples of generating text using the GenAI SDK.
16+
package text_generation
17+
18+
// [START googlegenaisdk_model_optimizer_textgen_with_txt]
19+
import (
20+
"context"
21+
"fmt"
22+
"io"
23+
24+
"google.golang.org/genai"
25+
)
26+
27+
// generateModelOptimizerWithTxt shows how to generate text using a text prompt and model optimizer.
28+
func generateModelOptimizerWithTxt(w io.Writer) error {
29+
ctx := context.Background()
30+
31+
clientConfig := &genai.ClientConfig{
32+
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"},
33+
}
34+
35+
client, err := genai.NewClient(ctx, clientConfig)
36+
37+
if err != nil {
38+
return fmt.Errorf("failed to create genai client: %w", err)
39+
}
40+
41+
modelSelectionConfig := &genai.ModelSelectionConfig{
42+
FeatureSelectionPreference: genai.FeatureSelectionPreferenceBalanced,
43+
}
44+
45+
generateContentConfig := &genai.GenerateContentConfig{
46+
ModelSelectionConfig: modelSelectionConfig,
47+
}
48+
49+
modelName := "gemini-2.5-flash"
50+
contents := genai.Text("How does AI work?")
51+
52+
resp, err := client.Models.GenerateContent(ctx,
53+
modelName,
54+
contents,
55+
generateContentConfig,
56+
)
57+
if err != nil {
58+
return fmt.Errorf("failed to generate content: %w", err)
59+
}
60+
61+
respText := resp.Text()
62+
63+
fmt.Fprintln(w, respText)
64+
// Example response:
65+
// That's a great question! Understanding how AI works can feel like ...
66+
// ...
67+
// **1. The Foundation: Data and Algorithms**
68+
// ...
69+
70+
return nil
71+
}
72+
73+
// [END googlegenaisdk_model_optimizer_textgen_with_txt]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package text_generation shows examples of generating text using the GenAI SDK.
16+
package text_generation
17+
18+
// [START googlegenaisdk_thinking_textgen_with_txt]
19+
import (
20+
"context"
21+
"fmt"
22+
"io"
23+
24+
"google.golang.org/genai"
25+
)
26+
27+
// generateThinkingWithText shows how to generate thinking using a text prompt.
28+
func generateThinkingWithText(w io.Writer) error {
29+
ctx := context.Background()
30+
31+
client, err := genai.NewClient(ctx, &genai.ClientConfig{
32+
HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
33+
})
34+
if err != nil {
35+
return fmt.Errorf("failed to create genai client: %w", err)
36+
}
37+
38+
resp, err := client.Models.GenerateContent(ctx,
39+
"gemini-2.5-flash",
40+
genai.Text("solve x^2 + 4x + 4 = 0"),
41+
nil,
42+
)
43+
if err != nil {
44+
return fmt.Errorf("failed to generate content: %w", err)
45+
}
46+
47+
respText := resp.Text()
48+
49+
fmt.Fprintln(w, respText)
50+
// Example response:
51+
// To solve the quadratic equation $x^2 + 4x + 4 = 0$, we can use a few methods:
52+
//
53+
// **Method 1: Factoring (Recognizing a Perfect Square Trinomial)**
54+
// **1. The Foundation: Data and Algorithms**
55+
//
56+
// Notice that the left side of the equation is a perfect square trinomial.
57+
// ...
58+
59+
return nil
60+
}
61+
62+
// [END googlegenaisdk_thinking_textgen_with_txt]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package text_generation shows examples of generating text using the GenAI SDK.
16+
package text_generation
17+
18+
// [START googlegenaisdk_textgen_chat_stream_with_txt]
19+
import (
20+
"context"
21+
"fmt"
22+
"io"
23+
24+
"google.golang.org/genai"
25+
)
26+
27+
// generateChatStreamWithText shows how to generate chat stream using a text prompt.
28+
func generateChatStreamWithText(w io.Writer) error {
29+
ctx := context.Background()
30+
31+
client, err := genai.NewClient(ctx, &genai.ClientConfig{
32+
HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
33+
})
34+
if err != nil {
35+
return fmt.Errorf("failed to create genai client: %w", err)
36+
}
37+
38+
modelName := "gemini-2.5-flash"
39+
40+
chatSession, err := client.Chats.Create(ctx, modelName, nil, nil)
41+
if err != nil {
42+
return fmt.Errorf("failed to create genai chat session: %w", err)
43+
}
44+
45+
var streamErr error
46+
contents := genai.Part{Text: "Why is the sky blue?"}
47+
48+
stream := chatSession.SendMessageStream(ctx, contents)
49+
stream(func(resp *genai.GenerateContentResponse, err error) bool {
50+
if err != nil {
51+
streamErr = err
52+
return false
53+
}
54+
for _, cand := range resp.Candidates {
55+
for _, part := range cand.Content.Parts {
56+
fmt.Fprintln(w, part.Text)
57+
}
58+
}
59+
return true
60+
})
61+
62+
// Example response:
63+
// The
64+
// sky appears blue due to a phenomenon called **Rayleigh scattering**.
65+
// Here's a breakdown:
66+
// ...
67+
68+
return streamErr
69+
}
70+
71+
// [END googlegenaisdk_textgen_chat_stream_with_txt]

0 commit comments

Comments
 (0)