Skip to content

Commit cd664b2

Browse files
refactor: (GenAI) Improved Context Cache Samples (Group A) (#12571)
## Description I have modified the following files: - `create_context_cache.py` - `delete_context_cache.py` - `get_context_cache.py` - `update_context_cache.py` - `use_context_cache.py` ❓However, as a developer and user, I can not find a single file `list_context_cache.py` ❗I have begun developing this file, but I have encountered an issue: the method required for listing context caches is not yet implemented and cannot be used. I have detailed the problem in the attached [document](https://docs.google.com/document/d/1AvNZYbIhrxO414CmPFRbyL24f-3fpncZQOOaaGy-Kcg/edit?usp=sharing). Proposed region_tag: `generativeaionvertexai_gemini_list_context_cache` Could you please review the issue and prioritize the implementation of this method? Having this method available would greatly facilitate the development process. ## Checklist - [x] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample - [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample - [ ] Please **merge** this PR for me once it is approved
1 parent 28839c5 commit cd664b2

File tree

7 files changed

+72
-33
lines changed

7 files changed

+72
-33
lines changed

generative_ai/classify_news_items.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START aiplatform_sdk_classify_news_items]
16-
1715

1816
def classify_news_items() -> str:
1917
"""Text Classification Example with a Large Language Model"""
@@ -59,9 +57,12 @@ def classify_news_items() -> str:
5957
)
6058

6159
print(response.text)
60+
# Example response:
61+
# business
6262
# [END generativeaionvertexai_classification]
6363

6464
return response.text
6565

6666

67-
# [END aiplatform_sdk_classify_news_items]
67+
if __name__ == "__main__":
68+
classify_news_items()

generative_ai/context_caching/context_caching_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@
2929

3030
@pytest.fixture(scope="module")
3131
def cache_id() -> Generator[str, None, None]:
32-
cached_content_name = create_context_cache.create_context_cache(PROJECT_ID)
32+
cached_content_name = create_context_cache.create_context_cache()
3333
yield cached_content_name
34-
delete_context_cache.delete_context_cache(PROJECT_ID, cached_content_name)
34+
delete_context_cache.delete_context_cache(cached_content_name)
3535

3636

3737
def test_create_context_cache(cache_id: str) -> None:
3838
assert cache_id
3939

4040

4141
def test_use_context_cache(cache_id: str) -> None:
42-
response = use_context_cache.use_context_cache(PROJECT_ID, cache_id)
42+
response = use_context_cache.use_context_cache(cache_id)
4343
assert response
4444

4545

4646
def test_get_context_cache(cache_id: str) -> None:
47-
response = get_context_cache.get_context_cache(PROJECT_ID, cache_id)
47+
response = get_context_cache.get_context_cache(cache_id)
4848
assert response
4949

5050

5151
def test_update_context_cache(cache_id: str) -> None:
52-
response = update_context_cache.update_context_cache(PROJECT_ID, cache_id)
52+
response = update_context_cache.update_context_cache(cache_id)
5353
assert response

generative_ai/context_caching/create_context_cache.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
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
1415

16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
1517

16-
def create_context_cache(project_id: str) -> str:
18+
19+
def create_context_cache() -> str:
1720
# [START generativeaionvertexai_gemini_create_context_cache]
1821
import vertexai
1922
import datetime
@@ -22,9 +25,9 @@ def create_context_cache(project_id: str) -> str:
2225
from vertexai.preview import caching
2326

2427
# TODO(developer): Update and un-comment below line
25-
# project_id = "PROJECT_ID"
28+
# PROJECT_ID = "your-project-id"
2629

27-
vertexai.init(project=project_id, location="us-central1")
30+
vertexai.init(project=PROJECT_ID, location="us-central1")
2831

2932
system_instruction = """
3033
You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts.
@@ -51,6 +54,12 @@ def create_context_cache(project_id: str) -> str:
5154
)
5255

5356
print(cached_content.name)
57+
# Example response:
58+
# 1234567890
5459
# [END generativeaionvertexai_gemini_create_context_cache]
5560

5661
return cached_content.name
62+
63+
64+
if __name__ == "__main__":
65+
create_context_cache()

generative_ai/context_caching/delete_context_cache.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,27 @@
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
1415

16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
1517

16-
def delete_context_cache(project_id: str, cache_id: str) -> None:
18+
19+
def delete_context_cache(cache_id: str) -> None:
1720
# [START generativeaionvertexai_gemini_delete_context_cache]
1821
import vertexai
1922

2023
from vertexai.preview import caching
2124

2225
# TODO(developer): Update and un-comment below lines
23-
# project_id = "PROJECT_ID"
24-
# cache_id = "CACHE_ID"
26+
# PROJECT_ID = "your-project-id"
27+
# cache_id = "your-cache-id"
2528

26-
vertexai.init(project=project_id, location="us-central1")
29+
vertexai.init(project=PROJECT_ID, location="us-central1")
2730

2831
cached_content = caching.CachedContent(cached_content_name=cache_id)
2932
cached_content.delete()
3033
# [END generativeaionvertexai_gemini_delete_context_cache]
34+
35+
36+
if __name__ == "__main__":
37+
delete_context_cache("1234567890")

generative_ai/context_caching/get_context_cache.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@
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
1415

16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
1517

16-
def get_context_cache(project_id: str, cache_id: str) -> str:
18+
19+
def get_context_cache(cache_id: str) -> str:
1720
# [START generativeaionvertexai_gemini_get_context_cache]
1821
import vertexai
1922

2023
from vertexai.preview import caching
2124

2225
# TODO(developer): Update and un-comment below lines
23-
# project_id = "PROJECT_ID"
24-
# cache_id = "CACHE_ID"
26+
# PROJECT_ID = "your-project-id"
27+
# cache_id = "your-cache-id"
2528

26-
vertexai.init(project=project_id, location="us-central1")
29+
vertexai.init(project=PROJECT_ID, location="us-central1")
2730

2831
cached_content = caching.CachedContent(cached_content_name=cache_id)
2932

30-
print(cached_content.name)
33+
print(cached_content.resource_name)
34+
# Example response:
35+
# projects/[PROJECT_ID]/locations/us-central1/cachedContents/1234567890
3136
# [END generativeaionvertexai_gemini_get_context_cache]
37+
return cached_content.resource_name
38+
3239

33-
return cached_content.name
40+
if __name__ == "__main__":
41+
get_context_cache("1234567890")

generative_ai/context_caching/update_context_cache.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,36 @@
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
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
1817

1918

20-
def update_context_cache(project_id: str, cache_id: str) -> str:
19+
def update_context_cache(cache_id: str) -> str:
2120
# [START generativeaionvertexai_gemini_update_context_cache]
2221
import vertexai
2322
import datetime
2423

2524
from vertexai.preview import caching
2625

2726
# TODO(developer): Update and un-comment below lines
28-
# project_id = "PROJECT_ID"
29-
# cache_id = "CACHE_ID"
27+
# PROJECT_ID = "your-project-id"
28+
# cache_id = "your-cache-id"
3029

3130
vertexai.init(project=PROJECT_ID, location="us-central1")
3231

3332
cached_content = caching.CachedContent(cached_content_name=cache_id)
3433

35-
# Update the expiration time by 2 hour
36-
cached_content.update(ttl=datetime.timedelta(hours=2))
37-
34+
# Update the expiration time by 1 hour
35+
cached_content.update(ttl=datetime.timedelta(hours=1))
3836
cached_content.refresh()
37+
3938
print(cached_content.expire_time)
39+
# Example response:
40+
# 2024-09-11 17:16:45.864520+00:00
4041
# [END generativeaionvertexai_gemini_update_context_cache]
41-
4242
return cached_content.expire_time
43+
44+
45+
if __name__ == "__main__":
46+
update_context_cache("1234567890")

generative_ai/context_caching/use_context_cache.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@
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
1415

16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
1517

16-
def use_context_cache(project_id: str, cache_id: str) -> str:
18+
19+
def use_context_cache(cache_id: str) -> str:
1720
# [START generativeaionvertexai_gemini_use_context_cache]
1821
import vertexai
1922

2023
from vertexai.preview.generative_models import GenerativeModel
2124
from vertexai.preview import caching
2225

2326
# TODO(developer): Update and un-comment below lines
24-
# project_id = "PROJECT_ID"
25-
# cache_id = "CACHE_ID"
27+
# PROJECT_ID = "your-project-id"
28+
# cache_id = "your-cache-id"
2629

27-
vertexai.init(project=project_id, location="us-central1")
30+
vertexai.init(project=PROJECT_ID, location="us-central1")
2831

2932
cached_content = caching.CachedContent(cached_content_name=cache_id)
3033

@@ -33,6 +36,13 @@ def use_context_cache(project_id: str, cache_id: str) -> str:
3336
response = model.generate_content("What are the papers about?")
3437

3538
print(response.text)
39+
# Example response:
40+
# The provided text is about a new family of multimodal models called Gemini, developed by Google.
41+
# ...
3642
# [END generativeaionvertexai_gemini_use_context_cache]
3743

3844
return response.text
45+
46+
47+
if __name__ == "__main__":
48+
use_context_cache("1234567890")

0 commit comments

Comments
 (0)