From 026ee6caac005f1db65a52c1d262fe0beafc7045 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Mon, 23 Sep 2024 18:36:54 +0200 Subject: [PATCH 1/6] Gen AI: Add sample for the 'get list of content caches' section --- .../context_caching/context_caching_test.py | 10 ++- .../get_list_of_content_caches.py | 65 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 generative_ai/context_caching/get_list_of_content_caches.py diff --git a/generative_ai/context_caching/context_caching_test.py b/generative_ai/context_caching/context_caching_test.py index 3cf679ccb17..608d23c78ea 100644 --- a/generative_ai/context_caching/context_caching_test.py +++ b/generative_ai/context_caching/context_caching_test.py @@ -16,10 +16,12 @@ from typing import Generator +import pytest + import create_context_cache import delete_context_cache import get_context_cache -import pytest +import get_list_of_content_caches import update_context_cache import use_context_cache @@ -48,6 +50,12 @@ def test_get_context_cache(cache_id: str) -> None: assert response +def test_get_list_of_context_caches(cache_id: str) -> None: + response = get_list_of_content_caches.get_list_of_context_caches() + cache_id_is_in_response = any([cc for cc in response if cc.name == cache_id]) + assert cache_id_is_in_response + + def test_update_context_cache(cache_id: str) -> None: response = update_context_cache.update_context_cache(cache_id) assert response diff --git a/generative_ai/context_caching/get_list_of_content_caches.py b/generative_ai/context_caching/get_list_of_content_caches.py new file mode 100644 index 00000000000..86db0a9e8e9 --- /dev/null +++ b/generative_ai/context_caching/get_list_of_content_caches.py @@ -0,0 +1,65 @@ +# Copyright 2024 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. +import os + +from typing import List + +from google.cloud.aiplatform_v1beta1.types.cached_content import CachedContent + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + + +def get_list_of_context_caches() -> List[CachedContent]: + # [START generativeaionvertexai_gemini_get_list_of_context_caches] + import json + + from datetime import datetime as dt, timezone as tz + + import vertexai + + from vertexai.preview import caching + + # TODO(developer): Update project id and location + vertexai.init(project=PROJECT_ID, location="us-central1") + + cached_content_list = caching.CachedContent.list() + # Access individual properties of a CachedContent object + for cc in cached_content_list: + expire_time_in_seconds = int((cc.expire_time - dt.now(tz.utc)).total_seconds()) + model_name = cc.model_name.split("/")[-1] + print( + f"Cached content '{cc.display_name}' for model '{model_name}' expires in {expire_time_in_seconds} s." + ) + # Example output: + # Cached content 'scientific-articles' for model 'gemini-1.5-pro-001' expires in 3000 s. + + # or convert the ContentCache object to a dictionary: + for cc in cached_content_list: + print(json.dumps(cc.to_dict(), indent=2)) + # Example output: + # { + # "name": "projects/[PROJECT_NUMBER]/locations/us-central1/cachedContents/4101407070023057408", + # "model": "projects/[PROJECT_ID]/locations/us-central1/publishers/google/models/gemini-1.5-pro-001", + # "createTime": "2024-09-16T12:41:09.998635Z", + # "updateTime": "2024-09-16T12:41:09.998635Z", + # "expireTime": "2024-09-16T13:41:09.989729Z", + # "displayName": "scientific-articles" + # } + + # [END generativeaionvertexai_gemini_get_list_of_context_caches] + return cached_content_list + + +if __name__ == "__main__": + get_list_of_context_caches() From bc5df0e3f439c91230b3fe6c57710c4584ef6e57 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Mon, 23 Sep 2024 19:24:42 +0200 Subject: [PATCH 2/6] review comments: make sample simpler --- .../context_caching/get_list_of_content_caches.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/generative_ai/context_caching/get_list_of_content_caches.py b/generative_ai/context_caching/get_list_of_content_caches.py index 86db0a9e8e9..3fc993e6dc1 100644 --- a/generative_ai/context_caching/get_list_of_content_caches.py +++ b/generative_ai/context_caching/get_list_of_content_caches.py @@ -24,8 +24,6 @@ def get_list_of_context_caches() -> List[CachedContent]: # [START generativeaionvertexai_gemini_get_list_of_context_caches] import json - from datetime import datetime as dt, timezone as tz - import vertexai from vertexai.preview import caching @@ -36,13 +34,11 @@ def get_list_of_context_caches() -> List[CachedContent]: cached_content_list = caching.CachedContent.list() # Access individual properties of a CachedContent object for cc in cached_content_list: - expire_time_in_seconds = int((cc.expire_time - dt.now(tz.utc)).total_seconds()) - model_name = cc.model_name.split("/")[-1] print( - f"Cached content '{cc.display_name}' for model '{model_name}' expires in {expire_time_in_seconds} s." + f"Cached content '{cc.display_name}' for model '{cc.model_name}' expires at {cc.expire_time}." ) # Example output: - # Cached content 'scientific-articles' for model 'gemini-1.5-pro-001' expires in 3000 s. + # Cached content 'scientific-articles' for model '.../gemini-1.5-pro-001' expires at 2024-09-23 18:01:47.242036+00:00. # or convert the ContentCache object to a dictionary: for cc in cached_content_list: @@ -55,6 +51,11 @@ def get_list_of_context_caches() -> List[CachedContent]: # "updateTime": "2024-09-16T12:41:09.998635Z", # "expireTime": "2024-09-16T13:41:09.989729Z", # "displayName": "scientific-articles" + # "usageMetadata": { + # "totalTokenCount": 43130, + # "textCount": 153, + # "imageCount": 167 + # } # } # [END generativeaionvertexai_gemini_get_list_of_context_caches] From e907d4d912fe48ea31be26e5c375c6d06017adb8 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Mon, 23 Sep 2024 19:29:42 +0200 Subject: [PATCH 3/6] review comments: rename the sample to follow standard naming practice --- generative_ai/context_caching/context_caching_test.py | 4 ++-- .../{get_list_of_content_caches.py => list_content_caches.py} | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename generative_ai/context_caching/{get_list_of_content_caches.py => list_content_caches.py} (96%) diff --git a/generative_ai/context_caching/context_caching_test.py b/generative_ai/context_caching/context_caching_test.py index 608d23c78ea..6e73ccb5314 100644 --- a/generative_ai/context_caching/context_caching_test.py +++ b/generative_ai/context_caching/context_caching_test.py @@ -21,7 +21,7 @@ import create_context_cache import delete_context_cache import get_context_cache -import get_list_of_content_caches +import list_content_caches import update_context_cache import use_context_cache @@ -51,7 +51,7 @@ def test_get_context_cache(cache_id: str) -> None: def test_get_list_of_context_caches(cache_id: str) -> None: - response = get_list_of_content_caches.get_list_of_context_caches() + response = list_content_caches.list_content_caches() cache_id_is_in_response = any([cc for cc in response if cc.name == cache_id]) assert cache_id_is_in_response diff --git a/generative_ai/context_caching/get_list_of_content_caches.py b/generative_ai/context_caching/list_content_caches.py similarity index 96% rename from generative_ai/context_caching/get_list_of_content_caches.py rename to generative_ai/context_caching/list_content_caches.py index 3fc993e6dc1..3428bb54ef7 100644 --- a/generative_ai/context_caching/get_list_of_content_caches.py +++ b/generative_ai/context_caching/list_content_caches.py @@ -20,7 +20,7 @@ PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -def get_list_of_context_caches() -> List[CachedContent]: +def list_content_caches() -> List[CachedContent]: # [START generativeaionvertexai_gemini_get_list_of_context_caches] import json @@ -63,4 +63,4 @@ def get_list_of_context_caches() -> List[CachedContent]: if __name__ == "__main__": - get_list_of_context_caches() + list_content_caches() From 4b0c443d095758a58ddb9f85539796fe48f0dc2b Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Tue, 24 Sep 2024 13:05:41 +0200 Subject: [PATCH 4/6] lint failure: rollback import statements re-arrangement --- generative_ai/context_caching/context_caching_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generative_ai/context_caching/context_caching_test.py b/generative_ai/context_caching/context_caching_test.py index 6e73ccb5314..7cd8b556c64 100644 --- a/generative_ai/context_caching/context_caching_test.py +++ b/generative_ai/context_caching/context_caching_test.py @@ -16,12 +16,12 @@ from typing import Generator -import pytest import create_context_cache import delete_context_cache import get_context_cache import list_content_caches +import pytest import update_context_cache import use_context_cache From 2b1627e201419b18479ffe6cdde544c5c51fd9a8 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 26 Sep 2024 16:10:23 +0200 Subject: [PATCH 5/6] Update comments to follow the runnable-snippets-plan standard --- generative_ai/context_caching/list_content_caches.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/generative_ai/context_caching/list_content_caches.py b/generative_ai/context_caching/list_content_caches.py index 3428bb54ef7..75f233ca296 100644 --- a/generative_ai/context_caching/list_content_caches.py +++ b/generative_ai/context_caching/list_content_caches.py @@ -28,7 +28,8 @@ def list_content_caches() -> List[CachedContent]: from vertexai.preview import caching - # TODO(developer): Update project id and location + # TODO(developer): Update & uncomment line below + # PROJECT_ID = "your-project-id" vertexai.init(project=PROJECT_ID, location="us-central1") cached_content_list = caching.CachedContent.list() @@ -37,13 +38,13 @@ def list_content_caches() -> List[CachedContent]: print( f"Cached content '{cc.display_name}' for model '{cc.model_name}' expires at {cc.expire_time}." ) - # Example output: + # Example response: # Cached content 'scientific-articles' for model '.../gemini-1.5-pro-001' expires at 2024-09-23 18:01:47.242036+00:00. # or convert the ContentCache object to a dictionary: for cc in cached_content_list: print(json.dumps(cc.to_dict(), indent=2)) - # Example output: + # Example response: # { # "name": "projects/[PROJECT_NUMBER]/locations/us-central1/cachedContents/4101407070023057408", # "model": "projects/[PROJECT_ID]/locations/us-central1/publishers/google/models/gemini-1.5-pro-001", From 287d400a5cf97e2e30827065de823669cbcc7a2a Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 26 Sep 2024 17:22:29 +0200 Subject: [PATCH 6/6] refresh CI builds