From 19b599f57bc3a56363236747eadaf5c029834eeb Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Wed, 25 Sep 2024 15:22:41 +0200 Subject: [PATCH 1/9] Add a sample for updating context cache with an expiration date --- .../context_caching/context_caching_test.py | 15 ++++++ .../update_context_cache_with_expire_time.py | 50 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 generative_ai/context_caching/update_context_cache_with_expire_time.py diff --git a/generative_ai/context_caching/context_caching_test.py b/generative_ai/context_caching/context_caching_test.py index 3cf679ccb17..bb58140d6ea 100644 --- a/generative_ai/context_caching/context_caching_test.py +++ b/generative_ai/context_caching/context_caching_test.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from datetime import datetime as dt import os from typing import Generator @@ -21,6 +22,7 @@ import get_context_cache import pytest import update_context_cache +import update_context_cache_with_expire_time import use_context_cache PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") @@ -51,3 +53,16 @@ def test_get_context_cache(cache_id: str) -> None: def test_update_context_cache(cache_id: str) -> None: response = update_context_cache.update_context_cache(cache_id) assert response + + +def test_update_context_cache_with_expire_time(cache_id: str) -> None: + response = update_context_cache_with_expire_time.update_context_cache_with_expire_time(cache_id) + assert response + + create_time = dt.fromisoformat(str(response.create_time).replace("Z", "+00:00")) + update_time = dt.fromisoformat(str(response.update_time).replace("Z", "+00:00")) + expire_time = dt.fromisoformat(str(response.expire_time).replace("Z", "+00:00")) + + assert update_time >= create_time, "updateTime should be equal to or after createTime" + assert expire_time > update_time, "expireTime should be after updateTime" + assert 364 <= (expire_time - update_time).days <= 366, "expireTime should be roughly one year after updateTime" diff --git a/generative_ai/context_caching/update_context_cache_with_expire_time.py b/generative_ai/context_caching/update_context_cache_with_expire_time.py new file mode 100644 index 00000000000..eec6f2283a0 --- /dev/null +++ b/generative_ai/context_caching/update_context_cache_with_expire_time.py @@ -0,0 +1,50 @@ +# 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 google.cloud.aiplatform_v1beta1.types.cached_content import CachedContent + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + + +def update_context_cache_with_expire_time(cache_id: str) -> CachedContent: + # [START generativeaionvertexai_gemini_update_context_cache_with_expire_time] + import vertexai + # from datetime import datetime as dt, timezone as tz + import datetime as dt + + from vertexai.preview import caching + + # TODO(developer): Update and un-comment below lines + # PROJECT_ID = "your-project-id" + # cache_id = "your-cache-id" + + vertexai.init(project=PROJECT_ID, location="us-central1") + + cached_content = caching.CachedContent(cached_content_name=cache_id) + + # Update the expiration to a specific time in the future + next_year_utc = dt.datetime.now(dt.timezone.utc) + dt.timedelta(days=365) + cached_content.update(expire_time=next_year_utc) + cached_content.refresh() + + print("Expire time:", cached_content.expire_time) + # Example output: + # Expire time: 2025-09-25 17:16:45.864520+00:00 + # [END generativeaionvertexai_gemini_update_context_cache_with_expire_time] + return cached_content + + +if __name__ == "__main__": + update_context_cache_with_expire_time("1234567890") From b8dd2914ded0e0cf49d6b53f811ccdf92309ed99 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Wed, 25 Sep 2024 17:18:48 +0200 Subject: [PATCH 2/9] refresh CI checks From 2a5460ae83c1faa370ee9798dfc75ff7ca496ab7 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Wed, 25 Sep 2024 19:12:24 +0200 Subject: [PATCH 3/9] refresh CI builds From e4324f475258f942798033e1de927fe9ba0a8585 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Wed, 25 Sep 2024 20:02:16 +0200 Subject: [PATCH 4/9] refresh CI builds From e5976f5cf0a70d5ba91678cfc15b46ec328bd6df Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 26 Sep 2024 17:03:42 +0200 Subject: [PATCH 5/9] * Make the import block more readable * Update 'TODO' line to follow the runnable-snippets-plan standard. --- .../update_context_cache_with_expire_time.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/generative_ai/context_caching/update_context_cache_with_expire_time.py b/generative_ai/context_caching/update_context_cache_with_expire_time.py index eec6f2283a0..78886d8c455 100644 --- a/generative_ai/context_caching/update_context_cache_with_expire_time.py +++ b/generative_ai/context_caching/update_context_cache_with_expire_time.py @@ -21,8 +21,9 @@ def update_context_cache_with_expire_time(cache_id: str) -> CachedContent: # [START generativeaionvertexai_gemini_update_context_cache_with_expire_time] import vertexai - # from datetime import datetime as dt, timezone as tz - import datetime as dt + from datetime import datetime as dt + from datetime import timezone as tz + from datetime import timedelta from vertexai.preview import caching @@ -35,12 +36,12 @@ def update_context_cache_with_expire_time(cache_id: str) -> CachedContent: cached_content = caching.CachedContent(cached_content_name=cache_id) # Update the expiration to a specific time in the future - next_year_utc = dt.datetime.now(dt.timezone.utc) + dt.timedelta(days=365) + next_year_utc = dt.now(tz.utc) + timedelta(days=365) cached_content.update(expire_time=next_year_utc) cached_content.refresh() print("Expire time:", cached_content.expire_time) - # Example output: + # Example response: # Expire time: 2025-09-25 17:16:45.864520+00:00 # [END generativeaionvertexai_gemini_update_context_cache_with_expire_time] return cached_content From c6a43433680eac7c9e7f6cbc35b7824fd986e7f0 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Wed, 2 Oct 2024 13:52:12 +0200 Subject: [PATCH 6/9] merge 2 'update_content_cache' samples (ttl & expire_time) into 1 --- ...aching_test.py => test_context_caching.py} | 16 ------ .../context_caching/update_context_cache.py | 13 +++-- .../update_context_cache_with_expire_time.py | 51 ------------------- 3 files changed, 10 insertions(+), 70 deletions(-) rename generative_ai/context_caching/{context_caching_test.py => test_context_caching.py} (65%) delete mode 100644 generative_ai/context_caching/update_context_cache_with_expire_time.py diff --git a/generative_ai/context_caching/context_caching_test.py b/generative_ai/context_caching/test_context_caching.py similarity index 65% rename from generative_ai/context_caching/context_caching_test.py rename to generative_ai/context_caching/test_context_caching.py index bb58140d6ea..3735cebcd30 100644 --- a/generative_ai/context_caching/context_caching_test.py +++ b/generative_ai/context_caching/test_context_caching.py @@ -11,8 +11,6 @@ # 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. - -from datetime import datetime as dt import os from typing import Generator @@ -22,7 +20,6 @@ import get_context_cache import pytest import update_context_cache -import update_context_cache_with_expire_time import use_context_cache PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") @@ -53,16 +50,3 @@ def test_get_context_cache(cache_id: str) -> None: def test_update_context_cache(cache_id: str) -> None: response = update_context_cache.update_context_cache(cache_id) assert response - - -def test_update_context_cache_with_expire_time(cache_id: str) -> None: - response = update_context_cache_with_expire_time.update_context_cache_with_expire_time(cache_id) - assert response - - create_time = dt.fromisoformat(str(response.create_time).replace("Z", "+00:00")) - update_time = dt.fromisoformat(str(response.update_time).replace("Z", "+00:00")) - expire_time = dt.fromisoformat(str(response.expire_time).replace("Z", "+00:00")) - - assert update_time >= create_time, "updateTime should be equal to or after createTime" - assert expire_time > update_time, "expireTime should be after updateTime" - assert 364 <= (expire_time - update_time).days <= 366, "expireTime should be roughly one year after updateTime" diff --git a/generative_ai/context_caching/update_context_cache.py b/generative_ai/context_caching/update_context_cache.py index 28a154a4657..5c75aa60aaf 100644 --- a/generative_ai/context_caching/update_context_cache.py +++ b/generative_ai/context_caching/update_context_cache.py @@ -19,7 +19,9 @@ def update_context_cache(cache_id: str) -> str: # [START generativeaionvertexai_gemini_update_context_cache] import vertexai - import datetime + from datetime import datetime as dt + from datetime import timezone as tz + from datetime import timedelta from vertexai.preview import caching @@ -31,8 +33,13 @@ def update_context_cache(cache_id: str) -> str: cached_content = caching.CachedContent(cached_content_name=cache_id) - # Update the expiration time by 1 hour - cached_content.update(ttl=datetime.timedelta(hours=1)) + # Update the context cache to expire in 3 hours + cached_content.update(ttl=timedelta(hours=3)) + cached_content.refresh() + + # We can also set the cache expiration to a specific time in the future + next_week_utc = dt.now(tz.utc) + timedelta(days=7) + cached_content.update(expire_time=next_week_utc) cached_content.refresh() print(cached_content.expire_time) diff --git a/generative_ai/context_caching/update_context_cache_with_expire_time.py b/generative_ai/context_caching/update_context_cache_with_expire_time.py deleted file mode 100644 index 78886d8c455..00000000000 --- a/generative_ai/context_caching/update_context_cache_with_expire_time.py +++ /dev/null @@ -1,51 +0,0 @@ -# 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 google.cloud.aiplatform_v1beta1.types.cached_content import CachedContent - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def update_context_cache_with_expire_time(cache_id: str) -> CachedContent: - # [START generativeaionvertexai_gemini_update_context_cache_with_expire_time] - import vertexai - from datetime import datetime as dt - from datetime import timezone as tz - from datetime import timedelta - - from vertexai.preview import caching - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - # cache_id = "your-cache-id" - - vertexai.init(project=PROJECT_ID, location="us-central1") - - cached_content = caching.CachedContent(cached_content_name=cache_id) - - # Update the expiration to a specific time in the future - next_year_utc = dt.now(tz.utc) + timedelta(days=365) - cached_content.update(expire_time=next_year_utc) - cached_content.refresh() - - print("Expire time:", cached_content.expire_time) - # Example response: - # Expire time: 2025-09-25 17:16:45.864520+00:00 - # [END generativeaionvertexai_gemini_update_context_cache_with_expire_time] - return cached_content - - -if __name__ == "__main__": - update_context_cache_with_expire_time("1234567890") From 4c53bdabf967988141a14c664c3f40c13158ba17 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 3 Oct 2024 15:01:45 +0200 Subject: [PATCH 7/9] =?UTF-8?q?Update=20update=5Fcontext=5Fcache=20?= =?UTF-8?q?=E2=80=94=20make=20developer=20comment=20more=20clear?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sampath Kumar --- generative_ai/context_caching/update_context_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generative_ai/context_caching/update_context_cache.py b/generative_ai/context_caching/update_context_cache.py index 5c75aa60aaf..131cea899b4 100644 --- a/generative_ai/context_caching/update_context_cache.py +++ b/generative_ai/context_caching/update_context_cache.py @@ -33,7 +33,7 @@ def update_context_cache(cache_id: str) -> str: cached_content = caching.CachedContent(cached_content_name=cache_id) - # Update the context cache to expire in 3 hours + # Option1: Update the context cache using ttl(time to live) cached_content.update(ttl=timedelta(hours=3)) cached_content.refresh() From d81022c6c337e47d73e1da8d70e97d86f5b60763 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 3 Oct 2024 15:02:32 +0200 Subject: [PATCH 8/9] =?UTF-8?q?Update=20update=5Fcontext=5Fcache=20?= =?UTF-8?q?=E2=80=94=20make=20developer=20comment=20more=20clear?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sampath Kumar --- generative_ai/context_caching/update_context_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generative_ai/context_caching/update_context_cache.py b/generative_ai/context_caching/update_context_cache.py index 131cea899b4..9cb21993827 100644 --- a/generative_ai/context_caching/update_context_cache.py +++ b/generative_ai/context_caching/update_context_cache.py @@ -37,7 +37,7 @@ def update_context_cache(cache_id: str) -> str: cached_content.update(ttl=timedelta(hours=3)) cached_content.refresh() - # We can also set the cache expiration to a specific time in the future + # Option2: Update the context cache using specific time next_week_utc = dt.now(tz.utc) + timedelta(days=7) cached_content.update(expire_time=next_week_utc) cached_content.refresh() From 5c1ba465330b48835a5b14d0ea9cd0ea419be443 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 3 Oct 2024 15:59:18 +0200 Subject: [PATCH 9/9] minor fix for the 'Option1...' comment (uppercase the abbreviation) --- generative_ai/context_caching/update_context_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generative_ai/context_caching/update_context_cache.py b/generative_ai/context_caching/update_context_cache.py index 9cb21993827..15527418b6a 100644 --- a/generative_ai/context_caching/update_context_cache.py +++ b/generative_ai/context_caching/update_context_cache.py @@ -33,7 +33,7 @@ def update_context_cache(cache_id: str) -> str: cached_content = caching.CachedContent(cached_content_name=cache_id) - # Option1: Update the context cache using ttl(time to live) + # Option1: Update the context cache using TTL (Time to live) cached_content.update(ttl=timedelta(hours=3)) cached_content.refresh()