|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | + |
| 16 | +from typing import List |
| 17 | + |
| 18 | +from google.cloud.aiplatform_v1beta1.types.cached_content import CachedContent |
| 19 | + |
| 20 | +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 21 | + |
| 22 | + |
| 23 | +def list_content_caches() -> List[CachedContent]: |
| 24 | + # [START generativeaionvertexai_gemini_get_list_of_context_caches] |
| 25 | + import json |
| 26 | + |
| 27 | + import vertexai |
| 28 | + |
| 29 | + from vertexai.preview import caching |
| 30 | + |
| 31 | + # TODO(developer): Update & uncomment line below |
| 32 | + # PROJECT_ID = "your-project-id" |
| 33 | + vertexai.init(project=PROJECT_ID, location="us-central1") |
| 34 | + |
| 35 | + cached_content_list = caching.CachedContent.list() |
| 36 | + # Access individual properties of a CachedContent object |
| 37 | + for cc in cached_content_list: |
| 38 | + print( |
| 39 | + f"Cached content '{cc.display_name}' for model '{cc.model_name}' expires at {cc.expire_time}." |
| 40 | + ) |
| 41 | + # Example response: |
| 42 | + # Cached content 'scientific-articles' for model '.../gemini-1.5-pro-001' expires at 2024-09-23 18:01:47.242036+00:00. |
| 43 | + |
| 44 | + # or convert the ContentCache object to a dictionary: |
| 45 | + for cc in cached_content_list: |
| 46 | + print(json.dumps(cc.to_dict(), indent=2)) |
| 47 | + # Example response: |
| 48 | + # { |
| 49 | + # "name": "projects/[PROJECT_NUMBER]/locations/us-central1/cachedContents/4101407070023057408", |
| 50 | + # "model": "projects/[PROJECT_ID]/locations/us-central1/publishers/google/models/gemini-1.5-pro-001", |
| 51 | + # "createTime": "2024-09-16T12:41:09.998635Z", |
| 52 | + # "updateTime": "2024-09-16T12:41:09.998635Z", |
| 53 | + # "expireTime": "2024-09-16T13:41:09.989729Z", |
| 54 | + # "displayName": "scientific-articles" |
| 55 | + # "usageMetadata": { |
| 56 | + # "totalTokenCount": 43130, |
| 57 | + # "textCount": 153, |
| 58 | + # "imageCount": 167 |
| 59 | + # } |
| 60 | + # } |
| 61 | + |
| 62 | + # [END generativeaionvertexai_gemini_get_list_of_context_caches] |
| 63 | + return cached_content_list |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + list_content_caches() |
0 commit comments