Skip to content

Commit a22e7ae

Browse files
committed
feat(genai): Add Content Cache example using Gemini 1.5 models
Example include how to create, update, delete and use Content Cache.
1 parent 12d510d commit a22e7ae

9 files changed

+332
-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 HttpOptions, Part, Content, CreateCachedContentConfig
20+
21+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
from distlib.compat import raw_input
15+
16+
17+
def delete_context_caches(cache_name: str) -> str:
18+
# [START googlegenaisdk_contentcache_delete]
19+
from google import genai
20+
from google.genai.types import HttpOptions
21+
22+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
23+
24+
# Delete content cache using name
25+
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
26+
client.caches.delete(name=cache_name)
27+
print("Deleted Cache", cache_name)
28+
# Example response
29+
# Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111
30+
# [END googlegenaisdk_contentcache_delete]
31+
return cache_name
32+
33+
34+
if __name__ == "__main__":
35+
cache_name = raw_input("Cache Name: ")
36+
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="v1"))
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
from distlib.compat import raw_input
15+
16+
17+
def update_content_cache(cache_name: str) -> str:
18+
# [START googlegenaisdk_contentcache_update]
19+
from datetime import datetime as dt
20+
from datetime import timezone as tz
21+
from datetime import timedelta
22+
23+
from google import genai
24+
from google.genai.types import HttpOptions, UpdateCachedContentConfig
25+
26+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
27+
28+
# Get content cache by name
29+
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
30+
content_cache = client.caches.get(name=cache_name)
31+
print("Expire time", content_cache.expire_time)
32+
# Example response
33+
# Expire time 2025-02-20 15:50:18.434482+00:00
34+
35+
# Update expire time using TTL
36+
content_cache = client.caches.update(
37+
name=cache_name, config=UpdateCachedContentConfig(ttl="36000s")
38+
)
39+
time_diff = content_cache.expire_time - dt.now(tz.utc)
40+
print("Expire time(after update):", content_cache.expire_time)
41+
print("Expire time(in seconds):", time_diff.seconds)
42+
# Example response
43+
# Expire time(after update): 2025-02-14 01:51:42.571696+00:00
44+
# Expire time(in seconds): 35999
45+
46+
# Update expire time using specific time stamp
47+
next_week_utc = dt.now(tz.utc) + timedelta(days=7)
48+
content_cache = client.caches.update(
49+
name=cache_name, config=UpdateCachedContentConfig(expireTime=next_week_utc)
50+
)
51+
print("Expire time(after update):", content_cache.expire_time)
52+
# Example response
53+
# Expire time(after update): 2025-02-20 15:51:42.614968+00:00
54+
# [END googlegenaisdk_contentcache_update]
55+
return cache_name
56+
57+
58+
if __name__ == "__main__":
59+
cache_name = raw_input("Cache Name: ")
60+
update_content_cache(cache_name)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
from distlib.compat import raw_input
15+
16+
17+
def generate_content(cache_name: str) -> str:
18+
# [START googlegenaisdk_contentcache_use_with_txt]
19+
from google import genai
20+
from google.genai.types import HttpOptions, GenerateContentConfig
21+
22+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
23+
24+
# Use content cache to generate text response
25+
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
26+
response = client.models.generate_content(
27+
model='gemini-1.5-pro-002',
28+
contents='Summarize the pdfs',
29+
config=GenerateContentConfig(
30+
cached_content=cache_name,
31+
),
32+
)
33+
print(response.text)
34+
# Example response
35+
# The Gemini family of multimodal models from Google DeepMind demonstrates remarkable capabilities across various
36+
# modalities, including image, audio, video, and text....
37+
# [END googlegenaisdk_contentcache_usage_with_txt]
38+
return response.text
39+
40+
41+
if __name__ == "__main__":
42+
cache_name = raw_input("Cache Name: ")
43+
generate_content(cache_name)
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
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 contentcache_create_with_txt_gcs_pdf
16+
import contentcache_delete
17+
import contentcache_list
18+
import contentcache_update
19+
import contentcache_use_with_txt
20+
21+
22+
def test_content_cache() -> None:
23+
# Create a Cache
24+
cache_name = contentcache_create_with_txt_gcs_pdf.create_content_cache()
25+
assert cache_name
26+
27+
# List cache
28+
assert contentcache_list.list_context_caches()
29+
30+
# Update cache
31+
assert contentcache_update.update_content_cache(cache_name)
32+
33+
# Use cache
34+
assert contentcache_use_with_txt.generate_content(cache_name)
35+
36+
# Delete cache
37+
assert contentcache_delete.delete_context_caches(cache_name)
38+
39+
40+
if __name__ == "__main__":
41+
test_content_cache()

0 commit comments

Comments
 (0)