Repository: GoogleCloudPlatform/python-docs-samples (language: python)

Issue:
feat(generative-ai): Add context caching samples (1)
## Description

Fixes b346529113

Add context caching samples:
- Create context cache




Discussion / hints:
<!-- probot comment [9602930]-->
Here is the summary of changes.
<details>
  <summary>You are about to add 1 region tag.</summary>

  - [generative_ai/context_caching/create_context_cache.py:17](https://github.com/gericdong/edong-python-docs-samples/blob/ccbc7b142e2ea9d737c2fa3303a04dcfdb4f5798/generative_ai/context_caching/create_context_cache.py#L17), tag `generativeaionvertexai_gemini_create_context_cache`

</details>

---
This comment is generated by [snippet-bot](https://github.com/apps/snippet-bot).
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add `snippet-bot:force-run` label or use the checkbox below:
- [ ] Refresh this comment

I think #11875 will fix the problem in 3.11 build, but I don't know what's going on in 3.8.

Fix patch:
diff --git a/generative_ai/context_caching/delete_context_cache.py b/generative_ai/context_caching/delete_context_cache.py
new file mode 100644
index 000000000000..51c19faa3ba0
--- /dev/null
+++ b/generative_ai/context_caching/delete_context_cache.py
@@ -0,0 +1,30 @@
+# 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.
+
+
+def delete_context_cache(project_id: str, cache_id: str) -> None:
+    # [START generativeaionvertexai_gemini_delete_context_cache]
+    import vertexai
+
+    from vertexai.preview import caching
+
+    # TODO(developer): Update and un-comment below lines
+    # project_id = "PROJECT_ID"
+    # cache_id = "CACHE_ID"
+
+    vertexai.init(project=project_id, location="us-central1")
+
+    cached_content = caching.CachedContent(cached_content_name=cache_id)
+    cached_content.delete()
+    # [END generativeaionvertexai_gemini_delete_context_cache]
diff --git a/generative_ai/context_caching/get_context_cache.py b/generative_ai/context_caching/get_context_cache.py
new file mode 100644
index 000000000000..f9c41f064150
--- /dev/null
+++ b/generative_ai/context_caching/get_context_cache.py
@@ -0,0 +1,33 @@
+# 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.
+
+
+def get_context_cache(project_id: str, cache_id: str) -> str:
+    # [START generativeaionvertexai_gemini_get_context_cache]
+    import vertexai
+
+    from vertexai.preview import caching
+
+    # TODO(developer): Update and un-comment below lines
+    # project_id = "PROJECT_ID"
+    # cache_id = "CACHE_ID"
+
+    vertexai.init(project=project_id, location="us-central1")
+
+    cached_content = caching.CachedContent(cached_content_name=cache_id)
+
+    print(cached_content.name)
+    # [END generativeaionvertexai_gemini_get_context_cache]
+
+    return cached_content.name
diff --git a/generative_ai/context_caching/update_context_cache.py b/generative_ai/context_caching/update_context_cache.py
new file mode 100644
index 000000000000..4e4422c160bb
--- /dev/null
+++ b/generative_ai/context_caching/update_context_cache.py
@@ -0,0 +1,38 @@
+# 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.
+
+
+def update_context_cache(project_id: str, cache_id: str) -> str:
+    # [START generativeaionvertexai_gemini_update_context_cache]
+    import vertexai
+    import datetime
+
+    from vertexai.preview import caching
+
+    # TODO(developer): Update and un-comment below lines
+    # project_id = "PROJECT_ID"
+    # cache_id = "CACHE_ID"
+
+    vertexai.init(project=project_id, location="us-central1")
+
+    cached_content = caching.CachedContent(cached_content_name=cache_id)
+
+    # Update the expiration time by 1 hour
+    cached_content.update(ttl=datetime.timedelta(hours=1))
+
+    cached_content.refresh()
+    print(cached_content.expire_time)
+    # [END generativeaionvertexai_gemini_update_context_cache]
+
+    return cached_content.expire_time
diff --git a/generative_ai/context_caching/use_context_cache.py b/generative_ai/context_caching/use_context_cache.py
new file mode 100644
index 000000000000..f04d17acbef8
--- /dev/null
+++ b/generative_ai/context_caching/use_context_cache.py
@@ -0,0 +1,38 @@
+# 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.
+
+
+def use_context_cache(project_id: str, cache_id: str) -> str:
+    # [START generativeaionvertexai_gemini_use_context_cache]
+    import vertexai
+
+    from vertexai.preview.generative_models import GenerativeModel
+    from vertexai.preview import caching
+
+    # TODO(developer): Update and un-comment below lines
+    # project_id = "PROJECT_ID"
+    # cache_id = "CACHE_ID"
+
+    vertexai.init(project=project_id, location="us-central1")
+
+    cached_content = caching.CachedContent(cached_content_name=cache_id)
+
+    model = GenerativeModel.from_cached_content(cached_content=cached_content)
+
+    response = model.generate_content("What are the papers about?")
+
+    print(response.text)
+    # [END generativeaionvertexai_gemini_use_context_cache]
+
+    return response.text


Test patch:
diff --git a/generative_ai/context_caching/context_caching_test.py b/generative_ai/context_caching/context_caching_test.py
index 4a5aae7ff097..f08df993c28f 100644
--- a/generative_ai/context_caching/context_caching_test.py
+++ b/generative_ai/context_caching/context_caching_test.py
@@ -17,7 +17,11 @@
 from typing import Generator
 
 import create_context_cache
+import delete_context_cache
+import get_context_cache
 import pytest
+import update_context_cache
+import use_context_cache
 
 PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
 REGION = "us-central1"
@@ -27,7 +31,23 @@
 def cache_id() -> Generator[str, None, None]:
     cached_content_name = create_context_cache.create_context_cache(PROJECT_ID)
     yield cached_content_name
+    delete_context_cache.delete_context_cache(PROJECT_ID, cached_content_name)
 
 
 def test_create_context_cache(cache_id: str) -> None:
     assert cache_id
+
+
+def test_use_context_cache(cache_id: str) -> None:
+    response = use_context_cache.use_context_cache(PROJECT_ID, cache_id)
+    assert response
+
+
+def test_get_context_cache(cache_id: str) -> None:
+    response = get_context_cache.get_context_cache(PROJECT_ID, cache_id)
+    assert response
+
+
+def test_update_context_cache(cache_id: str) -> None:
+    response = update_context_cache.update_context_cache(PROJECT_ID, cache_id)
+    assert response