Skip to content

Commit 6b6acdc

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: GA cache python SDK
FUTURE_COPYBARA_INTEGRATE_REVIEW=#4861 from googleapis:release-please--branches--main 039f2cb PiperOrigin-RevId: 718130866
1 parent c2e7ce4 commit 6b6acdc

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

google/cloud/aiplatform/compat/types/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
annotation_spec as annotation_spec_v1,
119119
artifact as artifact_v1,
120120
batch_prediction_job as batch_prediction_job_v1,
121+
cached_content as cached_content_v1,
121122
completion_stats as completion_stats_v1,
122123
context as context_v1,
123124
custom_job as custom_job_v1,

tests/unit/vertexai/test_caching.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import json
2323
import mock
2424
import pytest
25-
from vertexai.preview import caching
25+
from vertexai.caching import _caching
2626
from google.cloud.aiplatform import initializer
2727
import vertexai
2828
from google.cloud.aiplatform_v1beta1.types.cached_content import (
@@ -141,7 +141,7 @@ def list_cached_contents(self, request):
141141

142142
@pytest.mark.usefixtures("google_auth_mock")
143143
class TestCaching:
144-
"""Unit tests for caching.CachedContent."""
144+
"""Unit tests for _caching.CachedContent."""
145145

146146
def setup_method(self):
147147
vertexai.init(
@@ -156,7 +156,7 @@ def test_constructor_with_full_resource_name(self, mock_get_cached_content):
156156
full_resource_name = (
157157
"projects/123/locations/europe-west1/cachedContents/contents-id"
158158
)
159-
cache = caching.CachedContent(
159+
cache = _caching.CachedContent(
160160
cached_content_name=full_resource_name,
161161
)
162162

@@ -166,7 +166,7 @@ def test_constructor_with_full_resource_name(self, mock_get_cached_content):
166166
def test_constructor_with_only_content_id(self, mock_get_cached_content):
167167
partial_resource_name = "contents-id"
168168

169-
cache = caching.CachedContent(
169+
cache = _caching.CachedContent(
170170
cached_content_name=partial_resource_name,
171171
)
172172

@@ -179,7 +179,7 @@ def test_constructor_with_only_content_id(self, mock_get_cached_content):
179179
def test_get_with_content_id(self, mock_get_cached_content):
180180
partial_resource_name = "contents-id"
181181

182-
cache = caching.CachedContent.get(
182+
cache = _caching.CachedContent.get(
183183
cached_content_name=partial_resource_name,
184184
)
185185

@@ -192,7 +192,7 @@ def test_get_with_content_id(self, mock_get_cached_content):
192192
def test_create_with_real_payload(
193193
self, mock_create_cached_content, mock_get_cached_content
194194
):
195-
cache = caching.CachedContent.create(
195+
cache = _caching.CachedContent.create(
196196
model_name="model-name",
197197
system_instruction=GapicContent(
198198
role="system", parts=[GapicPart(text="system instruction")]
@@ -219,7 +219,7 @@ def test_create_with_real_payload(
219219
def test_create_with_real_payload_and_wrapped_type(
220220
self, mock_create_cached_content, mock_get_cached_content
221221
):
222-
cache = caching.CachedContent.create(
222+
cache = _caching.CachedContent.create(
223223
model_name="model-name",
224224
system_instruction="Please answer my questions with cool",
225225
tools=[],
@@ -239,15 +239,15 @@ def test_create_with_real_payload_and_wrapped_type(
239239
assert cache.display_name == _TEST_DISPLAY_NAME
240240

241241
def test_list(self, mock_list_cached_contents):
242-
cached_contents = caching.CachedContent.list()
242+
cached_contents = _caching.CachedContent.list()
243243
for i, cached_content in enumerate(cached_contents):
244244
assert cached_content.name == f"cached_content{i + 1}_from_list_request"
245245
assert cached_content.model_name == f"model-name{i + 1}"
246246

247247
def test_print_a_cached_content(
248248
self, mock_create_cached_content, mock_get_cached_content
249249
):
250-
cached_content = caching.CachedContent.create(
250+
cached_content = _caching.CachedContent.create(
251251
model_name="model-name",
252252
system_instruction="Please answer my questions with cool",
253253
tools=[],

vertexai/caching/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2023 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+
# http://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+
#
15+
"""Classes for working with the Gemini models."""
16+
17+
# We just want to re-export certain classes
18+
# pylint: disable=g-multiple-import,g-importing-member
19+
from vertexai.caching._caching import (
20+
CachedContent,
21+
)
22+
23+
__all__ = [
24+
"CachedContent",
25+
]

vertexai/generative_models/_generative_models.py

+39-39
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,45 @@ def start_chat(
11611161
response_validation=response_validation,
11621162
)
11631163

1164+
@classmethod
1165+
def from_cached_content(
1166+
cls,
1167+
cached_content: Union[str, "caching.CachedContent"],
1168+
*,
1169+
generation_config: Optional[GenerationConfigType] = None,
1170+
safety_settings: Optional[SafetySettingsType] = None,
1171+
) -> "_GenerativeModel":
1172+
"""Creates a model from cached content.
1173+
1174+
Creates a model instance with an existing cached content. The cached
1175+
content becomes the prefix of the requesting contents.
1176+
1177+
Args:
1178+
cached_content: The cached content resource name or object.
1179+
generation_config: The generation config to use for this model.
1180+
safety_settings: The safety settings to use for this model.
1181+
1182+
Returns:
1183+
A model instance with the cached content wtih cached content as
1184+
prefix of all its requests.
1185+
"""
1186+
if isinstance(cached_content, str):
1187+
from vertexai.caching import _caching
1188+
1189+
cached_content = _caching.CachedContent.get(cached_content)
1190+
model_name = cached_content.model_name
1191+
model = cls(
1192+
model_name=model_name,
1193+
generation_config=generation_config,
1194+
safety_settings=safety_settings,
1195+
tools=None,
1196+
tool_config=None,
1197+
system_instruction=None,
1198+
)
1199+
model._cached_content = cached_content
1200+
1201+
return model
1202+
11641203

11651204
_SUCCESSFUL_FINISH_REASONS = [
11661205
gapic_content_types.Candidate.FinishReason.STOP,
@@ -3515,42 +3554,3 @@ def start_chat(
35153554
response_validation=response_validation,
35163555
responder=responder,
35173556
)
3518-
3519-
@classmethod
3520-
def from_cached_content(
3521-
cls,
3522-
cached_content: Union[str, "caching.CachedContent"],
3523-
*,
3524-
generation_config: Optional[GenerationConfigType] = None,
3525-
safety_settings: Optional[SafetySettingsType] = None,
3526-
) -> "_GenerativeModel":
3527-
"""Creates a model from cached content.
3528-
3529-
Creates a model instance with an existing cached content. The cached
3530-
content becomes the prefix of the requesting contents.
3531-
3532-
Args:
3533-
cached_content: The cached content resource name or object.
3534-
generation_config: The generation config to use for this model.
3535-
safety_settings: The safety settings to use for this model.
3536-
3537-
Returns:
3538-
A model instance with the cached content wtih cached content as
3539-
prefix of all its requests.
3540-
"""
3541-
if isinstance(cached_content, str):
3542-
from vertexai.preview import caching
3543-
3544-
cached_content = caching.CachedContent.get(cached_content)
3545-
model_name = cached_content.model_name
3546-
model = cls(
3547-
model_name=model_name,
3548-
generation_config=generation_config,
3549-
safety_settings=safety_settings,
3550-
tools=None,
3551-
tool_config=None,
3552-
system_instruction=None,
3553-
)
3554-
model._cached_content = cached_content
3555-
3556-
return model

0 commit comments

Comments
 (0)