Skip to content

Commit d626e91

Browse files
feat(Gen-AI): Updates for the for list_context_caches sample (GoogleCloudPlatform#12667)
* fix post-review comments for 'list_content_caches' sample * rename the test file for context caching to follow the new structure * refresh CI checks * refresh CI checks * refresh CI checks * Update list_content_caches - use non-abstract values in example response when possible Co-authored-by: Sampath Kumar <[email protected]> * Update list_content_caches - use non-abstract values in example response when possible Co-authored-by: Sampath Kumar <[email protected]> * Update list_content_caches - use non-abstract values in example response when possible Co-authored-by: Sampath Kumar <[email protected]> * update region tag name to follow the standard schema * update region tag name following an offline discussion * refresh CI checks * don't use deprecated 'typings' --------- Co-authored-by: Sampath Kumar <[email protected]>
1 parent 6d805a8 commit d626e91

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

generative_ai/context_caching/list_content_caches.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import os
15-
16-
from typing import List
14+
from __future__ import annotations
1715

18-
from google.cloud.aiplatform_v1beta1.types.cached_content import CachedContent
16+
import os
1917

2018
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2119

2220

23-
def list_content_caches() -> List[CachedContent]:
24-
# [START generativeaionvertexai_gemini_get_list_of_context_caches]
25-
import json
26-
21+
def list_content_caches() -> list[str]:
22+
# [START generativeaionvertexai_context_caching_list]
2723
import vertexai
2824

2925
from vertexai.preview import caching
@@ -32,35 +28,19 @@ def list_content_caches() -> List[CachedContent]:
3228
# PROJECT_ID = "your-project-id"
3329
vertexai.init(project=PROJECT_ID, location="us-central1")
3430

35-
cached_content_list = caching.CachedContent.list()
31+
cache_list = caching.CachedContent.list()
3632
# Access individual properties of a CachedContent object
37-
for cc in cached_content_list:
38-
print(
39-
f"Cached content '{cc.display_name}' for model '{cc.model_name}' expires at {cc.expire_time}."
40-
)
41-
# Example response:
42-
# Cached content 'scientific-articles' for model '.../gemini-1.5-pro-001' expires at 2024-09-23 18:01:47.242036+00:00.
43-
44-
# or convert the ContentCache object to a dictionary:
45-
for cc in cached_content_list:
46-
print(json.dumps(cc.to_dict(), indent=2))
33+
for cached_content in cache_list:
34+
print(f"Cache '{cached_content.name}' for model '{cached_content.model_name}'")
35+
print(f"Last updated at: {cached_content.update_time}")
36+
print(f"Expires at: {cached_content.expire_time}")
4737
# Example response:
48-
# {
49-
# "name": "projects/[PROJECT_NUMBER]/locations/us-central1/cachedContents/4101407070023057408",
50-
# "model": "projects/[PROJECT_ID]/locations/us-central1/publishers/google/models/gemini-1.5-pro-001",
51-
# "createTime": "2024-09-16T12:41:09.998635Z",
52-
# "updateTime": "2024-09-16T12:41:09.998635Z",
53-
# "expireTime": "2024-09-16T13:41:09.989729Z",
54-
# "displayName": "scientific-articles"
55-
# "usageMetadata": {
56-
# "totalTokenCount": 43130,
57-
# "textCount": 153,
58-
# "imageCount": 167
59-
# }
60-
# }
38+
# Cached content 'example-cache' for model '.../gemini-1.5-pro-001'
39+
# Last updated at: 2024-09-16T12:41:09.998635Z
40+
# Expires at: 2024-09-16T13:41:09.989729Z
6141

62-
# [END generativeaionvertexai_gemini_get_list_of_context_caches]
63-
return cached_content_list
42+
# [END generativeaionvertexai_context_caching_list]
43+
return [cached_content.name for cached_content in cache_list]
6444

6545

6646
if __name__ == "__main__":

generative_ai/context_caching/test_context_caching.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def test_get_context_cache(cache_id: str) -> None:
5151

5252
def test_get_list_of_context_caches(cache_id: str) -> None:
5353
response = list_content_caches.list_content_caches()
54-
cache_id_is_in_response = any([cc for cc in response if cc.name == cache_id])
55-
assert cache_id_is_in_response
54+
assert cache_id in response
5655

5756

5857
def test_update_context_cache(cache_id: str) -> None:

0 commit comments

Comments
 (0)