Skip to content

Commit 0586ab2

Browse files
feat(genai): Add Content Cache example using Gemini 1.5 models (#13164)
* feat(genai): Add Content Cache example using Gemini 1.5 models Example include how to create, update, delete and use Content Cache. * Update genai/content_cache/contentcache_list.py Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * Update genai/content_cache/contentcache_list.py Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * Update genai/content_cache/contentcache_list.py Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * Update genai/content_cache/contentcache_update.py Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * feat: Using `input` instead of `raw_input` * feat: Update imports order * fix: region tag mismatch * fix: Add required ENV vars * fix: using `v1beta1` instead of `v1` --------- Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com>
1 parent 9990873 commit 0586ab2

9 files changed

+337
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
16+
def create_content_cache() -> str:
17+
# [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
18+
from google import genai
19+
from google.genai.types import Content, CreateCachedContentConfig, HttpOptions, Part
20+
21+
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
22+
23+
system_instruction = """
24+
You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts.
25+
Now look at these research papers, and answer the following questions.
26+
"""
27+
28+
contents = [
29+
Content(
30+
role="user",
31+
parts=[
32+
Part.from_uri(
33+
file_uri="gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf",
34+
mime_type="application/pdf",
35+
),
36+
Part.from_uri(
37+
file_uri="gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf",
38+
mime_type="application/pdf",
39+
),
40+
],
41+
)
42+
]
43+
44+
content_cache = client.caches.create(
45+
model="gemini-1.5-pro-002",
46+
config=CreateCachedContentConfig(
47+
contents=contents,
48+
system_instruction=system_instruction,
49+
display_name="example-cache",
50+
ttl="86400s",
51+
),
52+
)
53+
54+
print(content_cache.name)
55+
print(content_cache.usage_metadata)
56+
# Example response:
57+
# projects/111111111111/locations/us-central1/cachedContents/1111111111111111111
58+
# CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167,
59+
# text_count=153, total_token_count=43130, video_duration_seconds=None)
60+
# [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
61+
return content_cache.name
62+
63+
64+
if __name__ == "__main__":
65+
create_content_cache()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
16+
def delete_context_caches(cache_name: str) -> str:
17+
# [START googlegenaisdk_contentcache_delete]
18+
from google import genai
19+
from google.genai.types import HttpOptions
20+
21+
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
22+
23+
# Delete content cache using name
24+
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
25+
client.caches.delete(name=cache_name)
26+
print("Deleted Cache", cache_name)
27+
# Example response
28+
# Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111
29+
# [END googlegenaisdk_contentcache_delete]
30+
return cache_name
31+
32+
33+
if __name__ == "__main__":
34+
cache_name = input("Cache Name: ")
35+
delete_context_caches(cache_name)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
16+
def list_context_caches() -> str:
17+
# [START googlegenaisdk_contentcache_list]
18+
from google import genai
19+
from google.genai.types import HttpOptions
20+
21+
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
22+
23+
content_cache_list = client.caches.list()
24+
25+
# Access individual properties of a ContentCache object(s)
26+
for content_cache in content_cache_list:
27+
print(f"Cache `{content_cache.name}` for model `{content_cache.model}`")
28+
print(f"Last updated at: {content_cache.update_time}")
29+
print(f"Expires at: {content_cache.expire_time}")
30+
31+
# Example response:
32+
# * Cache `projects/111111111111/locations/us-central1/cachedContents/1111111111111111111` for
33+
# model `projects/111111111111/locations/us-central1/publishers/google/models/gemini-XXX-pro-XXX`
34+
# * Last updated at: 2025-02-13 14:46:42.620490+00:00
35+
# * CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, text_count=153, total_token_count=43130, video_duration_seconds=None)
36+
# ...
37+
# [END googlegenaisdk_contentcache_list]
38+
return [content_cache.name for content_cache in content_cache_list]
39+
40+
41+
if __name__ == "__main__":
42+
list_context_caches()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
16+
def update_content_cache(cache_name: str) -> str:
17+
# [START googlegenaisdk_contentcache_update]
18+
from datetime import datetime as dt
19+
from datetime import timezone as tz
20+
from datetime import timedelta
21+
22+
from google import genai
23+
from google.genai.types import HttpOptions, UpdateCachedContentConfig
24+
25+
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
26+
27+
# Get content cache by name
28+
# cache_name = "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111"
29+
content_cache = client.caches.get(name=cache_name)
30+
print("Expire time", content_cache.expire_time)
31+
# Example response
32+
# Expire time 2025-02-20 15:50:18.434482+00:00
33+
34+
# Update expire time using TTL
35+
content_cache = client.caches.update(
36+
name=cache_name, config=UpdateCachedContentConfig(ttl="36000s")
37+
)
38+
time_diff = content_cache.expire_time - dt.now(tz.utc)
39+
print("Expire time(after update):", content_cache.expire_time)
40+
print("Expire time(in seconds):", time_diff.seconds)
41+
# Example response
42+
# Expire time(after update): 2025-02-14 01:51:42.571696+00:00
43+
# Expire time(in seconds): 35999
44+
45+
# Update expire time using specific time stamp
46+
next_week_utc = dt.now(tz.utc) + timedelta(days=7)
47+
content_cache = client.caches.update(
48+
name=cache_name, config=UpdateCachedContentConfig(expireTime=next_week_utc)
49+
)
50+
print("Expire time(after update):", content_cache.expire_time)
51+
# Example response
52+
# Expire time(after update): 2025-02-20 15:51:42.614968+00:00
53+
# [END googlegenaisdk_contentcache_update]
54+
return cache_name
55+
56+
57+
if __name__ == "__main__":
58+
cache_name = input("Cache Name: ")
59+
update_content_cache(cache_name)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
16+
def generate_content(cache_name: str) -> str:
17+
# [START googlegenaisdk_contentcache_use_with_txt]
18+
from google import genai
19+
from google.genai.types import GenerateContentConfig, HttpOptions
20+
21+
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
22+
23+
# Use content cache to generate text response
24+
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
25+
response = client.models.generate_content(
26+
model='gemini-1.5-pro-002',
27+
contents='Summarize the pdfs',
28+
config=GenerateContentConfig(
29+
cached_content=cache_name,
30+
),
31+
)
32+
print(response.text)
33+
# Example response
34+
# The Gemini family of multimodal models from Google DeepMind demonstrates remarkable capabilities across various
35+
# modalities, including image, audio, video, and text....
36+
# [END googlegenaisdk_contentcache_use_with_txt]
37+
return response.text
38+
39+
40+
if __name__ == "__main__":
41+
cache_name = input("Cache Name: ")
42+
generate_content(cache_name)

genai/content_cache/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-api-core==2.24.0
2+
pytest==8.2.0

genai/content_cache/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.2.0
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import os
16+
17+
import contentcache_create_with_txt_gcs_pdf
18+
import contentcache_delete
19+
import contentcache_list
20+
import contentcache_update
21+
import contentcache_use_with_txt
22+
23+
24+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
25+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
26+
# The project name is included in the CICD pipeline
27+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
28+
29+
30+
def test_content_cache() -> None:
31+
# Create a Cache
32+
cache_name = contentcache_create_with_txt_gcs_pdf.create_content_cache()
33+
assert cache_name
34+
35+
# List cache
36+
assert contentcache_list.list_context_caches()
37+
38+
# Update cache
39+
assert contentcache_update.update_content_cache(cache_name)
40+
41+
# Use cache
42+
assert contentcache_use_with_txt.generate_content(cache_name)
43+
44+
# Delete cache
45+
assert contentcache_delete.delete_context_caches(cache_name)
46+
47+
48+
if __name__ == "__main__":
49+
test_content_cache()

0 commit comments

Comments
 (0)