Skip to content

Commit d548c11

Browse files
Zhenyi Qicopybara-github
Zhenyi Qi
authored andcommitted
feat: GenAI - Context Caching - also print model_name and expire_time.
PiperOrigin-RevId: 644166997
1 parent 97b91db commit d548c11

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

tests/unit/vertexai/test_caching.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
#
1717

1818

19+
from contextlib import redirect_stdout
1920
import datetime
20-
import pytest
21+
import io
22+
import json
2123
import mock
24+
import pytest
2225
from vertexai.preview import caching
2326
from google.cloud.aiplatform import initializer
2427
import vertexai
@@ -50,6 +53,33 @@ def create_cached_content(self, request):
5053
response = GapicCachedContent(
5154
name=f"{request.parent}/cachedContents/{_CREATED_CONTENT_ID}",
5255
model=f"{request.cached_content.model}",
56+
create_time=datetime.datetime(
57+
year=2024,
58+
month=2,
59+
day=1,
60+
hour=1,
61+
minute=1,
62+
second=1,
63+
tzinfo=datetime.timezone.utc,
64+
),
65+
update_time=datetime.datetime(
66+
year=2024,
67+
month=2,
68+
day=1,
69+
hour=1,
70+
minute=1,
71+
second=1,
72+
tzinfo=datetime.timezone.utc,
73+
),
74+
expire_time=datetime.datetime(
75+
year=2024,
76+
month=2,
77+
day=1,
78+
hour=2,
79+
minute=1,
80+
second=1,
81+
tzinfo=datetime.timezone.utc,
82+
),
5383
)
5484
return response
5585

@@ -207,3 +237,32 @@ def test_list(self, mock_list_cached_contents):
207237
for i, cached_content in enumerate(cached_contents):
208238
assert cached_content.name == f"cached_content{i + 1}_from_list_request"
209239
assert cached_content.model_name == f"model-name{i + 1}"
240+
241+
def test_print_a_cached_content(
242+
self, mock_create_cached_content, mock_get_cached_content
243+
):
244+
cached_content = caching.CachedContent.create(
245+
model_name="model-name",
246+
system_instruction="Please answer my questions with cool",
247+
tools=[],
248+
tool_config=GapicToolConfig(),
249+
contents=["user content"],
250+
ttl=datetime.timedelta(days=1),
251+
)
252+
f = io.StringIO()
253+
with redirect_stdout(f):
254+
print(cached_content)
255+
output = f.getvalue()
256+
assert (
257+
json.dumps(
258+
{
259+
"name": f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/{_CREATED_CONTENT_ID}",
260+
"model": "projects/test-project/locations/us-central1/publishers/google/models/model-name",
261+
"createTime": "2024-02-01T01:01:01Z",
262+
"updateTime": "2024-02-01T01:01:01Z",
263+
"expireTime": "2024-02-01T02:01:01Z",
264+
},
265+
indent=2,
266+
)
267+
in output
268+
)

vertexai/caching/_caching.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#
1515

1616
import datetime
17-
from typing import Optional, List
17+
import json
18+
from typing import List, Optional
19+
from typing_extensions import override
1820

1921
from google.cloud.aiplatform import base as aiplatform_base
2022
from google.cloud.aiplatform import initializer as aiplatform_initializer
@@ -269,6 +271,10 @@ def delete(self):
269271
"""Deletes the current cached content resource."""
270272
self._delete()
271273

274+
@override
275+
def __repr__(self) -> str:
276+
return f"{object.__repr__(self)}: {json.dumps(self.to_dict(), indent=2)}"
277+
272278
@classmethod
273279
def list(cls) -> List["CachedContent"]:
274280
"""Lists the active cached content resources."""

0 commit comments

Comments
 (0)