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