File size: 9,244 Bytes
3193ef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


from contextlib import redirect_stdout
import datetime
import io
import json
import mock
import pytest
from vertexai.preview import caching
from google.cloud.aiplatform import initializer
import vertexai
from google.cloud.aiplatform_v1beta1.types.cached_content import (
    CachedContent as GapicCachedContent,
)
from google.cloud.aiplatform_v1beta1.types.content import (
    Content as GapicContent,
    Part as GapicPart,
)
from google.cloud.aiplatform_v1beta1.types.tool import (
    ToolConfig as GapicToolConfig,
)
from google.cloud.aiplatform_v1beta1.services import (
    gen_ai_cache_service,
)


_TEST_PROJECT = "test-project"
_TEST_LOCATION = "us-central1"
_CREATED_CONTENT_ID = "contents-id-mocked"
_TEST_DISPLAY_NAME = "test-display-name"


@pytest.fixture
def mock_create_cached_content():
    """Mocks GenAiCacheServiceClient.create_cached_content()."""

    def create_cached_content(self, request):
        response = GapicCachedContent(
            name=f"{request.parent}/cachedContents/{_CREATED_CONTENT_ID}",
            model=f"{request.cached_content.model}",
            display_name=f"{request.cached_content.display_name}",
            create_time=datetime.datetime(
                year=2024,
                month=2,
                day=1,
                hour=1,
                minute=1,
                second=1,
                tzinfo=datetime.timezone.utc,
            ),
            update_time=datetime.datetime(
                year=2024,
                month=2,
                day=1,
                hour=1,
                minute=1,
                second=1,
                tzinfo=datetime.timezone.utc,
            ),
            expire_time=datetime.datetime(
                year=2024,
                month=2,
                day=1,
                hour=2,
                minute=1,
                second=1,
                tzinfo=datetime.timezone.utc,
            ),
        )
        return response

    with mock.patch.object(
        gen_ai_cache_service.client.GenAiCacheServiceClient,
        "create_cached_content",
        new=create_cached_content,
    ) as create_cached_content:
        yield create_cached_content


@pytest.fixture
def mock_get_cached_content():
    """Mocks GenAiCacheServiceClient.get_cached_content()."""

    def get_cached_content(self, name, retry=None):
        del self, retry
        response = GapicCachedContent(
            name=f"{name}",
            model="model-name",
        )
        return response

    with mock.patch.object(
        gen_ai_cache_service.client.GenAiCacheServiceClient,
        "get_cached_content",
        new=get_cached_content,
    ) as get_cached_content:
        yield get_cached_content


@pytest.fixture
def mock_list_cached_contents():
    """Mocks GenAiCacheServiceClient.get_cached_content()."""

    def list_cached_contents(self, request):
        del self, request
        response = [
            GapicCachedContent(
                name="cached_content1_from_list_request",
                model="model-name1",
            ),
            GapicCachedContent(
                name="cached_content2_from_list_request",
                model="model-name2",
            ),
        ]
        return response

    with mock.patch.object(
        gen_ai_cache_service.client.GenAiCacheServiceClient,
        "list_cached_contents",
        new=list_cached_contents,
    ) as list_cached_contents:
        yield list_cached_contents


@pytest.mark.usefixtures("google_auth_mock")
class TestCaching:
    """Unit tests for caching.CachedContent."""

    def setup_method(self):
        vertexai.init(
            project=_TEST_PROJECT,
            location=_TEST_LOCATION,
        )

    def teardown_method(self):
        initializer.global_pool.shutdown(wait=True)

    def test_constructor_with_full_resource_name(self, mock_get_cached_content):
        full_resource_name = (
            "projects/123/locations/europe-west1/cachedContents/contents-id"
        )
        cache = caching.CachedContent(
            cached_content_name=full_resource_name,
        )

        assert cache.name == "contents-id"
        assert cache.resource_name == full_resource_name

    def test_constructor_with_only_content_id(self, mock_get_cached_content):
        partial_resource_name = "contents-id"

        cache = caching.CachedContent(
            cached_content_name=partial_resource_name,
        )

        assert cache.name == "contents-id"
        assert cache.resource_name == (
            f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/contents-id"
        )
        assert cache.model_name == "model-name"

    def test_get_with_content_id(self, mock_get_cached_content):
        partial_resource_name = "contents-id"

        cache = caching.CachedContent.get(
            cached_content_name=partial_resource_name,
        )

        assert cache.name == "contents-id"
        assert cache.resource_name == (
            f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/contents-id"
        )
        assert cache.model_name == "model-name"

    def test_create_with_real_payload(
        self, mock_create_cached_content, mock_get_cached_content
    ):
        cache = caching.CachedContent.create(
            model_name="model-name",
            system_instruction=GapicContent(
                role="system", parts=[GapicPart(text="system instruction")]
            ),
            tools=[],
            tool_config=GapicToolConfig(),
            contents=[GapicContent(role="user")],
            ttl=datetime.timedelta(days=1),
            display_name=_TEST_DISPLAY_NAME,
        )

        # parent is automantically set to align with the current project and location.
        # _CREATED_CONTENT_ID is from the mock
        assert cache.resource_name == (
            f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/{_CREATED_CONTENT_ID}"
        )
        assert cache.name == _CREATED_CONTENT_ID
        assert (
            cache.model_name
            == f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/publishers/google/models/model-name"
        )
        assert cache.display_name == _TEST_DISPLAY_NAME

    def test_create_with_real_payload_and_wrapped_type(
        self, mock_create_cached_content, mock_get_cached_content
    ):
        cache = caching.CachedContent.create(
            model_name="model-name",
            system_instruction="Please answer my questions with cool",
            tools=[],
            tool_config=GapicToolConfig(),
            contents=["user content"],
            ttl=datetime.timedelta(days=1),
            display_name=_TEST_DISPLAY_NAME,
        )

        # parent is automantically set to align with the current project and location.
        # _CREATED_CONTENT_ID is from the mock
        assert (
            cache.model_name
            == f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/publishers/google/models/model-name"
        )
        assert cache.name == _CREATED_CONTENT_ID
        assert cache.display_name == _TEST_DISPLAY_NAME

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

    def test_print_a_cached_content(
        self, mock_create_cached_content, mock_get_cached_content
    ):
        cached_content = caching.CachedContent.create(
            model_name="model-name",
            system_instruction="Please answer my questions with cool",
            tools=[],
            tool_config=GapicToolConfig(),
            contents=["user content"],
            ttl=datetime.timedelta(days=1),
            display_name=_TEST_DISPLAY_NAME,
        )
        f = io.StringIO()
        with redirect_stdout(f):
            print(cached_content)
        output = f.getvalue()
        assert (
            json.dumps(
                {
                    "name": f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/{_CREATED_CONTENT_ID}",
                    "model": "projects/test-project/locations/us-central1/publishers/google/models/model-name",
                    "createTime": "2024-02-01T01:01:01Z",
                    "updateTime": "2024-02-01T01:01:01Z",
                    "expireTime": "2024-02-01T02:01:01Z",
                    "displayName": "test-display-name",
                },
                indent=2,
            )
            in output
        )