Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(genai sdk): content cache: create and update #5156

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions genai/content_cache/content_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2025 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 content_cache

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)

func TestContentCaching(t *testing.T) {
tc := testutil.SystemTest(t)

t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "1")
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1")
t.Setenv("GOOGLE_CLOUD_PROJECT", tc.ProjectID)

buf := new(bytes.Buffer)

// 1) Create a content cache.
// The name of the cache resource created in this step will be used in the next test steps.
cacheName, err := createContentCache(buf)
if err != nil {
t.Fatalf("createContentCache: %v", err.Error())
}

// 2) Update the expiration time of the content cache.
err = updateContentCache(buf, cacheName)
if err != nil {
t.Errorf("updateContentCache: %v", err.Error())
}

// 3) Use cached content with a text prompt.
err = useContentCacheWithTxt(buf, cacheName)
if err != nil {
t.Errorf("useContentCacheWithTxt: %v", err.Error())
}

// 4) Delete the content cache.
buf.Reset()
err = deleteContentCache(buf, cacheName)
if err != nil {
t.Errorf("deleteContentCache: %v", err.Error())
}

exp := fmt.Sprintf("Deleted cache %q", cacheName)
act := buf.String()
if !strings.Contains(act, exp) {
t.Errorf("deleteContentCache: got %q, want %q", act, exp)
}
}
102 changes: 102 additions & 0 deletions genai/content_cache/create_with_txt_gcs_pdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2025 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
//
// https://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 content_cache shows examples of using content caching with the GenAI SDK.
package content_cache

// [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
import (
"context"
"encoding/json"
"fmt"
"io"

genai "google.golang.org/genai"
)

// createContentCache shows how to create a content cache with an expiration parameter.
func createContentCache(w io.Writer) (string, error) {
ctx := context.Background()

client, err := genai.NewClient(ctx, &genai.ClientConfig{
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"},
})
if err != nil {
return "", fmt.Errorf("failed to create genai client: %w", err)
}

modelName := "gemini-1.5-pro-002"

systemInstruction := "You are an expert researcher. You always stick to the facts " +
"in the sources provided, and never make up new facts. " +
"Now look at these research papers, and answer the following questions."

cacheContents := []*genai.Content{
{
Parts: []*genai.Part{
{FileData: &genai.FileData{
FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf",
MIMEType: "application/pdf",
}},
{FileData: &genai.FileData{
FileURI: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf",
MIMEType: "application/pdf",
}},
},
Role: "user",
},
}
config := &genai.CreateCachedContentConfig{
Contents: cacheContents,
SystemInstruction: &genai.Content{
Parts: []*genai.Part{
{Text: systemInstruction},
},
},
DisplayName: "example-cache",
TTL: "86400s",
}

res, err := client.Caches.Create(ctx, modelName, config)
if err != nil {
return "", fmt.Errorf("failed to create content cache: %w", err)
}

cachedContent, err := json.MarshalIndent(res, "", " ")
if err != nil {
return "", fmt.Errorf("failed to marshal cache info: %w", err)
}

// See the documentation: https://pkg.go.dev/google.golang.org/genai#CachedContent
fmt.Fprintln(w, string(cachedContent))

// Example response:
// {
// "name": "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111",
// "displayName": "example-cache",
// "model": "projects/111111111111/locations/us-central1/publishers/google/models/gemini-1.5-pro-002",
// "createTime": "2025-02-18T15:05:08.29468Z",
// "updateTime": "2025-02-18T15:05:08.29468Z",
// "expireTime": "2025-02-19T15:05:08.280828Z",
// "usageMetadata": {
// "imageCount": 167,
// "textCount": 153,
// "totalTokenCount": 43125
// }
// }

return res.Name, nil
}

// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
51 changes: 51 additions & 0 deletions genai/content_cache/delete_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2025 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
//
// https://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 content_cache shows examples of using content caching with the GenAI SDK.
package content_cache

// [START googlegenaisdk_contentcache_update]
import (
"context"
"fmt"
"io"

genai "google.golang.org/genai"
)

// deleteContentCache shows how to delete content cache.
func deleteContentCache(w io.Writer, cacheName string) error {
ctx := context.Background()

client, err := genai.NewClient(ctx, &genai.ClientConfig{
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"},
})
if err != nil {
return fmt.Errorf("failed to create genai client: %w", err)
}

_, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{})
if err != nil {
return fmt.Errorf("failed to delete content cache: %w", err)
}

fmt.Fprintf(w, "Deleted cache %q\n", cacheName)

// Example response:
// Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111"

return nil
}

// [END googlegenaisdk_contentcache_update]
67 changes: 67 additions & 0 deletions genai/content_cache/update_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2025 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
//
// https://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 content_cache shows examples of using content caching with the GenAI SDK.
package content_cache

// [START googlegenaisdk_contentcache_update]
import (
"context"
"fmt"
"io"
"time"

genai "google.golang.org/genai"
)

// updateContentCache shows how to update content cache expiration time.
func updateContentCache(w io.Writer, cacheName string) error {
ctx := context.Background()

client, err := genai.NewClient(ctx, &genai.ClientConfig{
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"},
})
if err != nil {
return fmt.Errorf("failed to create genai client: %w", err)
}

// Update expire time using TTL
resp, err := client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{
TTL: "36000s",
})
if err != nil {
return fmt.Errorf("failed to update content cache exp. time with TTL: %w", err)
}

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

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

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

return nil
}

// [END googlegenaisdk_contentcache_update]
65 changes: 65 additions & 0 deletions genai/content_cache/use_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2025 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
//
// https://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 content_cache shows examples of using content caching with the GenAI SDK.
package content_cache

// [START googlegenaisdk_contentcache_use_with_txt]
import (
"context"
"fmt"
"io"

genai "google.golang.org/genai"
)

// useContentCacheWithTxt shows how to use content cache to generate text content.
func useContentCacheWithTxt(w io.Writer, cacheName string) error {
ctx := context.Background()

client, err := genai.NewClient(ctx, &genai.ClientConfig{
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"},
})
if err != nil {
return fmt.Errorf("failed to create genai client: %w", err)
}

resp, err := client.Models.GenerateContent(ctx,
"gemini-1.5-pro-002",
genai.Text("Summarize the pdfs"),
&genai.GenerateContentConfig{
CachedContent: cacheName,
},
)
if err != nil {
return fmt.Errorf("failed to use content cache to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
fmt.Fprintln(w, respText)

// Example response:
// The provided research paper introduces Gemini 1.5 Pro, a multimodal model capable of recalling
// and reasoning over information from very long contexts (up to 10 million tokens). Key findings include:
//
// * **Long Context Performance:**
// ...

return nil
}

// [END googlegenaisdk_contentcache_use_with_txt]
Loading
Loading