Content-Length: 377046 | pFad | https://github.com/googleapis/python-aiplatform/commit/8f53902b933f3abf0c9a222f45c2fa7ada727505

6D feat: GenAI - ContextCaching - allow from_cached_content to take the … · googleapis/python-aiplatform@8f53902 · GitHub
Skip to content

Commit 8f53902

Browse files
Zhenyi Qicopybara-github
Zhenyi Qi
authored andcommitted
feat: GenAI - ContextCaching - allow from_cached_content to take the cached_content resource name
PiperOrigin-RevId: 652913296
1 parent 3ec043e commit 8f53902

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

tests/unit/vertexai/test_generative_models.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,37 @@ def test_generative_model_from_cached_content(
483483
== "cached-content-id-in-from-cached-content-test"
484484
)
485485

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+
486517
@mock.patch.object(
487518
target=prediction_service.PredictionServiceClient,
488519
attribute="generate_content",

vertexai/generative_models/_generative_models.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2687,11 +2687,29 @@ def start_chat(
26872687
@classmethod
26882688
def from_cached_content(
26892689
cls,
2690-
cached_content: "caching.CachedContent",
2690+
cached_content: Union[str, "caching.CachedContent"],
26912691
*,
26922692
generation_config: Optional[GenerationConfigType] = None,
26932693
safety_settings: Optional[SafetySettingsType] = None,
26942694
) -> "_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)
26952713
model_name = cached_content.model_name
26962714
model = cls(
26972715
model_name=model_name,

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/googleapis/python-aiplatform/commit/8f53902b933f3abf0c9a222f45c2fa7ada727505

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy