Skip to content

Commit 48f5cc7

Browse files
feat (Gen AI): Update Context-Cache sample to include expiration date info (#12631)
Update an existing code sample to shared details to using `expiration date` option Co-authored-by: Sampath Kumar <[email protected]>
1 parent ea2cb42 commit 48f5cc7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

generative_ai/context_caching/context_caching_test.py renamed to generative_ai/context_caching/test_context_caching.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
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-
1514
import os
1615

1716
from typing import Generator

generative_ai/context_caching/update_context_cache.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
def update_context_cache(cache_id: str) -> str:
2020
# [START generativeaionvertexai_gemini_update_context_cache]
2121
import vertexai
22-
import datetime
22+
from datetime import datetime as dt
23+
from datetime import timezone as tz
24+
from datetime import timedelta
2325

2426
from vertexai.preview import caching
2527

@@ -31,8 +33,13 @@ def update_context_cache(cache_id: str) -> str:
3133

3234
cached_content = caching.CachedContent(cached_content_name=cache_id)
3335

34-
# Update the expiration time by 1 hour
35-
cached_content.update(ttl=datetime.timedelta(hours=1))
36+
# Option1: Update the context cache using TTL (Time to live)
37+
cached_content.update(ttl=timedelta(hours=3))
38+
cached_content.refresh()
39+
40+
# Option2: Update the context cache using specific time
41+
next_week_utc = dt.now(tz.utc) + timedelta(days=7)
42+
cached_content.update(expire_time=next_week_utc)
3643
cached_content.refresh()
3744

3845
print(cached_content.expire_time)

0 commit comments

Comments
 (0)