File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
vertexai/generative_models Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -483,6 +483,37 @@ def test_generative_model_from_cached_content(
483
483
== "cached-content-id-in-from-cached-content-test"
484
484
)
485
485
486
+ def test_generative_model_from_cached_content_with_resource_name (
487
+ self , mock_get_cached_content_fixture
488
+ ):
489
+ project_location_prefix = (
490
+ f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } /"
491
+ )
492
+
493
+ model = preview_generative_models .GenerativeModel .from_cached_content (
494
+ cached_content = "cached-content-id-in-from-cached-content-test"
495
+ )
496
+
497
+ assert (
498
+ model ._prediction_resource_name
499
+ == project_location_prefix
500
+ + "publishers/google/models/"
501
+ + "gemini-pro-from-mock-get-cached-content"
502
+ )
503
+ assert (
504
+ model ._cached_content .model_name
505
+ == "gemini-pro-from-mock-get-cached-content"
506
+ )
507
+ assert (
508
+ model ._cached_content .resource_name
509
+ == f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } /"
510
+ "cachedContents/cached-content-id-in-from-cached-content-test"
511
+ )
512
+ assert (
513
+ model ._cached_content .name
514
+ == "cached-content-id-in-from-cached-content-test"
515
+ )
516
+
486
517
@mock .patch .object (
487
518
target = prediction_service .PredictionServiceClient ,
488
519
attribute = "generate_content" ,
Original file line number Diff line number Diff line change @@ -2687,11 +2687,29 @@ def start_chat(
2687
2687
@classmethod
2688
2688
def from_cached_content (
2689
2689
cls ,
2690
- cached_content : "caching.CachedContent" ,
2690
+ cached_content : Union [ str , "caching.CachedContent" ] ,
2691
2691
* ,
2692
2692
generation_config : Optional [GenerationConfigType ] = None ,
2693
2693
safety_settings : Optional [SafetySettingsType ] = None ,
2694
2694
) -> "_GenerativeModel" :
2695
+ """Creates a model from cached content.
2696
+
2697
+ Creates a model instance with an existing cached content. The cached
2698
+ content becomes the prefix of the requesting contents.
2699
+
2700
+ Args:
2701
+ cached_content: The cached content resource name or object.
2702
+ generation_config: The generation config to use for this model.
2703
+ safety_settings: The safety settings to use for this model.
2704
+
2705
+ Returns:
2706
+ A model instance with the cached content wtih cached content as
2707
+ prefix of all its requests.
2708
+ """
2709
+ if isinstance (cached_content , str ):
2710
+ from vertexai .preview import caching
2711
+
2712
+ cached_content = caching .CachedContent .get (cached_content )
2695
2713
model_name = cached_content .model_name
2696
2714
model = cls (
2697
2715
model_name = model_name ,
You can’t perform that action at this time.
0 commit comments