Content-Length: 449231 | pFad | http://github.com/googleapis/python-aiplatform/commit/c551cc63cc133b2c434e19ec510b6252bed7b7f5

A2 feat: allow customers to set kms_key_name for context caching create … · googleapis/python-aiplatform@c551cc6 · GitHub
Skip to content

Commit c551cc6

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: allow customers to set kms_key_name for context caching create method
PiperOrigin-RevId: 758332266
1 parent 64dfdbc commit c551cc6

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

tests/unit/vertexai/test_caching.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,34 @@ def test_create_with_real_payload(
216216
)
217217
assert cache.display_name == _TEST_DISPLAY_NAME
218218

219+
def test_create_with_encrypted_real_payload(
220+
self, mock_create_cached_content, mock_get_cached_content
221+
):
222+
cache = CachedContent.create(
223+
model_name="model-name",
224+
system_instruction=GapicContent(
225+
role="system", parts=[GapicPart(text="system instruction")]
226+
),
227+
tools=[],
228+
tool_config=GapicToolConfig(),
229+
contents=[GapicContent(role="user")],
230+
ttl=datetime.timedelta(days=1),
231+
display_name=_TEST_DISPLAY_NAME,
232+
kms_key_name=f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/keyRings/test-key-ring/cryptoKeys/test-key",
233+
)
234+
235+
# parent is automantically set to align with the current project and location.
236+
# _CREATED_CONTENT_ID is from the mock
237+
assert cache.resource_name == (
238+
f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/{_CREATED_CONTENT_ID}"
239+
)
240+
assert cache.name == _CREATED_CONTENT_ID
241+
assert (
242+
cache.model_name
243+
== f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/publishers/google/models/model-name"
244+
)
245+
assert cache.display_name == _TEST_DISPLAY_NAME
246+
219247
def test_create_with_real_payload_and_wrapped_type(
220248
self, mock_create_cached_content, mock_get_cached_content
221249
):

vertexai/caching/_caching.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
UpdateCachedContentRequest,
4040
)
4141
from google.cloud.aiplatform_v1 import types as types_v1
42+
from google.cloud.aiplatform_v1beta1.types import EncryptionSpec
4243
from vertexai.generative_models import _generative_models
4344
from vertexai.generative_models._generative_models import (
4445
Content,
@@ -60,6 +61,7 @@ def _prepare_create_request(
6061
expire_time: Optional[datetime.datetime] = None,
6162
ttl: Optional[datetime.timedelta] = None,
6263
display_name: Optional[str] = None,
64+
kms_key_name: Optional[str] = None,
6365
) -> CreateCachedContentRequest:
6466
"""Prepares the request create_cached_content RPC."""
6567
(
@@ -103,6 +105,7 @@ def _prepare_create_request(
103105
expire_time=expire_time,
104106
ttl=ttl,
105107
display_name=display_name,
108+
encryption_spec=EncryptionSpec(kms_key_name=kms_key_name),
106109
),
107110
)
108111
serialized_message_v1beta1 = type(request_v1beta1).serialize(request_v1beta1)
@@ -175,6 +178,7 @@ def create(
175178
expire_time: Optional[datetime.datetime] = None,
176179
ttl: Optional[datetime.timedelta] = None,
177180
display_name: Optional[str] = None,
181+
kms_key_name: Optional[str] = None,
178182
) -> "CachedContent":
179183
"""Creates a new cached content through the gen ai cache service.
180184
@@ -213,6 +217,14 @@ def create(
213217
default TTL on the API side will be used (currently 1 hour).
214218
display_name:
215219
The user-generated meaningful display name of the cached content.
220+
kms_key_name:
221+
Optional. Customer-managed encryption key. See
222+
https://cloud.google.com/vertex-ai/docs/general/cmek for more
223+
details. If this is set, then all created CachedContent objects
224+
will be encrypted with the provided encryption key.
225+
Allowed formats:
226+
227+
projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}
216228
Returns:
217229
A CachedContent object with only name and model_name specified.
218230
Raises:
@@ -237,6 +249,7 @@ def create(
237249
expire_time=expire_time,
238250
ttl=ttl,
239251
display_name=display_name,
252+
kms_key_name=kms_key_name,
240253
)
241254
client = cls._instantiate_client(location=location)
242255
cached_content_resource = client.create_cached_content(request)

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: http://github.com/googleapis/python-aiplatform/commit/c551cc63cc133b2c434e19ec510b6252bed7b7f5

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy