google-genai 1.33.0__py3-none-any.whl → 1.53.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/genai/_api_client.py +361 -208
- google/genai/_common.py +260 -69
- google/genai/_extra_utils.py +142 -12
- google/genai/_live_converters.py +691 -2746
- google/genai/_local_tokenizer_loader.py +0 -9
- google/genai/_operations_converters.py +186 -99
- google/genai/_replay_api_client.py +48 -51
- google/genai/_tokens_converters.py +169 -489
- google/genai/_transformers.py +193 -90
- google/genai/batches.py +1014 -1307
- google/genai/caches.py +458 -1107
- google/genai/client.py +101 -0
- google/genai/documents.py +532 -0
- google/genai/errors.py +58 -4
- google/genai/file_search_stores.py +1296 -0
- google/genai/files.py +108 -358
- google/genai/live.py +90 -32
- google/genai/live_music.py +24 -27
- google/genai/local_tokenizer.py +36 -3
- google/genai/models.py +2308 -3375
- google/genai/operations.py +129 -21
- google/genai/pagers.py +7 -1
- google/genai/tokens.py +2 -12
- google/genai/tunings.py +770 -436
- google/genai/types.py +4341 -1218
- google/genai/version.py +1 -1
- {google_genai-1.33.0.dist-info → google_genai-1.53.0.dist-info}/METADATA +359 -201
- google_genai-1.53.0.dist-info/RECORD +41 -0
- google_genai-1.33.0.dist-info/RECORD +0 -39
- {google_genai-1.33.0.dist-info → google_genai-1.53.0.dist-info}/WHEEL +0 -0
- {google_genai-1.33.0.dist-info → google_genai-1.53.0.dist-info}/licenses/LICENSE +0 -0
- {google_genai-1.33.0.dist-info → google_genai-1.53.0.dist-info}/top_level.txt +0 -0
google/genai/client.py
CHANGED
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
#
|
|
15
15
|
|
|
16
|
+
import asyncio
|
|
16
17
|
import os
|
|
18
|
+
from types import TracebackType
|
|
17
19
|
from typing import Optional, Union
|
|
18
20
|
|
|
19
21
|
import google.auth
|
|
@@ -25,6 +27,7 @@ from ._replay_api_client import ReplayApiClient
|
|
|
25
27
|
from .batches import AsyncBatches, Batches
|
|
26
28
|
from .caches import AsyncCaches, Caches
|
|
27
29
|
from .chats import AsyncChats, Chats
|
|
30
|
+
from .file_search_stores import AsyncFileSearchStores, FileSearchStores
|
|
28
31
|
from .files import AsyncFiles, Files
|
|
29
32
|
from .live import AsyncLive
|
|
30
33
|
from .models import AsyncModels, Models
|
|
@@ -45,6 +48,7 @@ class AsyncClient:
|
|
|
45
48
|
self._caches = AsyncCaches(self._api_client)
|
|
46
49
|
self._batches = AsyncBatches(self._api_client)
|
|
47
50
|
self._files = AsyncFiles(self._api_client)
|
|
51
|
+
self._file_search_stores = AsyncFileSearchStores(self._api_client)
|
|
48
52
|
self._live = AsyncLive(self._api_client)
|
|
49
53
|
self._tokens = AsyncTokens(self._api_client)
|
|
50
54
|
self._operations = AsyncOperations(self._api_client)
|
|
@@ -61,6 +65,10 @@ class AsyncClient:
|
|
|
61
65
|
def caches(self) -> AsyncCaches:
|
|
62
66
|
return self._caches
|
|
63
67
|
|
|
68
|
+
@property
|
|
69
|
+
def file_search_stores(self) -> AsyncFileSearchStores:
|
|
70
|
+
return self._file_search_stores
|
|
71
|
+
|
|
64
72
|
@property
|
|
65
73
|
def batches(self) -> AsyncBatches:
|
|
66
74
|
return self._batches
|
|
@@ -85,6 +93,50 @@ class AsyncClient:
|
|
|
85
93
|
def operations(self) -> AsyncOperations:
|
|
86
94
|
return self._operations
|
|
87
95
|
|
|
96
|
+
async def aclose(self) -> None:
|
|
97
|
+
"""Closes the async client explicitly.
|
|
98
|
+
|
|
99
|
+
However, it doesn't close the sync client, which can be closed using the
|
|
100
|
+
Client.close() method or using the context manager.
|
|
101
|
+
|
|
102
|
+
Usage:
|
|
103
|
+
.. code-block:: python
|
|
104
|
+
|
|
105
|
+
from google.genai import Client
|
|
106
|
+
|
|
107
|
+
async_client = Client(
|
|
108
|
+
vertexai=True, project='my-project-id', location='us-central1'
|
|
109
|
+
).aio
|
|
110
|
+
response_1 = await async_client.models.generate_content(
|
|
111
|
+
model='gemini-2.0-flash',
|
|
112
|
+
contents='Hello World',
|
|
113
|
+
)
|
|
114
|
+
response_2 = await async_client.models.generate_content(
|
|
115
|
+
model='gemini-2.0-flash',
|
|
116
|
+
contents='Hello World',
|
|
117
|
+
)
|
|
118
|
+
# Close the client to release resources.
|
|
119
|
+
await async_client.aclose()
|
|
120
|
+
"""
|
|
121
|
+
await self._api_client.aclose()
|
|
122
|
+
|
|
123
|
+
async def __aenter__(self) -> 'AsyncClient':
|
|
124
|
+
return self
|
|
125
|
+
|
|
126
|
+
async def __aexit__(
|
|
127
|
+
self,
|
|
128
|
+
exc_type: Optional[Exception],
|
|
129
|
+
exc_value: Optional[Exception],
|
|
130
|
+
traceback: Optional[TracebackType],
|
|
131
|
+
) -> None:
|
|
132
|
+
await self.aclose()
|
|
133
|
+
|
|
134
|
+
def __del__(self) -> None:
|
|
135
|
+
try:
|
|
136
|
+
asyncio.get_running_loop().create_task(self.aclose())
|
|
137
|
+
except Exception:
|
|
138
|
+
pass
|
|
139
|
+
|
|
88
140
|
|
|
89
141
|
class DebugConfig(pydantic.BaseModel):
|
|
90
142
|
"""Configuration options that change client network behavior when testing."""
|
|
@@ -230,6 +282,7 @@ class Client:
|
|
|
230
282
|
self._models = Models(self._api_client)
|
|
231
283
|
self._tunings = Tunings(self._api_client)
|
|
232
284
|
self._caches = Caches(self._api_client)
|
|
285
|
+
self._file_search_stores = FileSearchStores(self._api_client)
|
|
233
286
|
self._batches = Batches(self._api_client)
|
|
234
287
|
self._files = Files(self._api_client)
|
|
235
288
|
self._tokens = Tokens(self._api_client)
|
|
@@ -291,6 +344,10 @@ class Client:
|
|
|
291
344
|
def caches(self) -> Caches:
|
|
292
345
|
return self._caches
|
|
293
346
|
|
|
347
|
+
@property
|
|
348
|
+
def file_search_stores(self) -> FileSearchStores:
|
|
349
|
+
return self._file_search_stores
|
|
350
|
+
|
|
294
351
|
@property
|
|
295
352
|
def batches(self) -> Batches:
|
|
296
353
|
return self._batches
|
|
@@ -311,3 +368,47 @@ class Client:
|
|
|
311
368
|
def vertexai(self) -> bool:
|
|
312
369
|
"""Returns whether the client is using the Vertex AI API."""
|
|
313
370
|
return self._api_client.vertexai or False
|
|
371
|
+
|
|
372
|
+
def close(self) -> None:
|
|
373
|
+
"""Closes the synchronous client explicitly.
|
|
374
|
+
|
|
375
|
+
However, it doesn't close the async client, which can be closed using the
|
|
376
|
+
Client.aio.aclose() method or using the async context manager.
|
|
377
|
+
|
|
378
|
+
Usage:
|
|
379
|
+
.. code-block:: python
|
|
380
|
+
|
|
381
|
+
from google.genai import Client
|
|
382
|
+
|
|
383
|
+
client = Client(
|
|
384
|
+
vertexai=True, project='my-project-id', location='us-central1'
|
|
385
|
+
)
|
|
386
|
+
response_1 = client.models.generate_content(
|
|
387
|
+
model='gemini-2.0-flash',
|
|
388
|
+
contents='Hello World',
|
|
389
|
+
)
|
|
390
|
+
response_2 = client.models.generate_content(
|
|
391
|
+
model='gemini-2.0-flash',
|
|
392
|
+
contents='Hello World',
|
|
393
|
+
)
|
|
394
|
+
# Close the client to release resources.
|
|
395
|
+
client.close()
|
|
396
|
+
"""
|
|
397
|
+
self._api_client.close()
|
|
398
|
+
|
|
399
|
+
def __enter__(self) -> 'Client':
|
|
400
|
+
return self
|
|
401
|
+
|
|
402
|
+
def __exit__(
|
|
403
|
+
self,
|
|
404
|
+
exc_type: Optional[Exception],
|
|
405
|
+
exc_value: Optional[Exception],
|
|
406
|
+
traceback: Optional[TracebackType],
|
|
407
|
+
) -> None:
|
|
408
|
+
self.close()
|
|
409
|
+
|
|
410
|
+
def __del__(self) -> None:
|
|
411
|
+
try:
|
|
412
|
+
self.close()
|
|
413
|
+
except Exception:
|
|
414
|
+
pass
|
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
# Copyright 2025 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
|
+
|
|
16
|
+
# Code generated by the Google Gen AI SDK generator DO NOT EDIT.
|
|
17
|
+
|
|
18
|
+
from functools import partial
|
|
19
|
+
import json
|
|
20
|
+
import logging
|
|
21
|
+
from typing import Any, Optional, Union
|
|
22
|
+
from urllib.parse import urlencode
|
|
23
|
+
|
|
24
|
+
from . import _api_module
|
|
25
|
+
from . import _common
|
|
26
|
+
from . import types
|
|
27
|
+
from ._common import get_value_by_path as getv
|
|
28
|
+
from ._common import set_value_by_path as setv
|
|
29
|
+
from .pagers import AsyncPager, Pager
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
logger = logging.getLogger('google_genai.documents')
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _DeleteDocumentConfig_to_mldev(
|
|
36
|
+
from_object: Union[dict[str, Any], object],
|
|
37
|
+
parent_object: Optional[dict[str, Any]] = None,
|
|
38
|
+
) -> dict[str, Any]:
|
|
39
|
+
to_object: dict[str, Any] = {}
|
|
40
|
+
|
|
41
|
+
if getv(from_object, ['force']) is not None:
|
|
42
|
+
setv(parent_object, ['_query', 'force'], getv(from_object, ['force']))
|
|
43
|
+
|
|
44
|
+
return to_object
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _DeleteDocumentParameters_to_mldev(
|
|
48
|
+
from_object: Union[dict[str, Any], object],
|
|
49
|
+
parent_object: Optional[dict[str, Any]] = None,
|
|
50
|
+
) -> dict[str, Any]:
|
|
51
|
+
to_object: dict[str, Any] = {}
|
|
52
|
+
if getv(from_object, ['name']) is not None:
|
|
53
|
+
setv(to_object, ['_url', 'name'], getv(from_object, ['name']))
|
|
54
|
+
|
|
55
|
+
if getv(from_object, ['config']) is not None:
|
|
56
|
+
_DeleteDocumentConfig_to_mldev(getv(from_object, ['config']), to_object)
|
|
57
|
+
|
|
58
|
+
return to_object
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _GetDocumentParameters_to_mldev(
|
|
62
|
+
from_object: Union[dict[str, Any], object],
|
|
63
|
+
parent_object: Optional[dict[str, Any]] = None,
|
|
64
|
+
) -> dict[str, Any]:
|
|
65
|
+
to_object: dict[str, Any] = {}
|
|
66
|
+
if getv(from_object, ['name']) is not None:
|
|
67
|
+
setv(to_object, ['_url', 'name'], getv(from_object, ['name']))
|
|
68
|
+
|
|
69
|
+
return to_object
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _ListDocumentsConfig_to_mldev(
|
|
73
|
+
from_object: Union[dict[str, Any], object],
|
|
74
|
+
parent_object: Optional[dict[str, Any]] = None,
|
|
75
|
+
) -> dict[str, Any]:
|
|
76
|
+
to_object: dict[str, Any] = {}
|
|
77
|
+
|
|
78
|
+
if getv(from_object, ['page_size']) is not None:
|
|
79
|
+
setv(
|
|
80
|
+
parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
if getv(from_object, ['page_token']) is not None:
|
|
84
|
+
setv(
|
|
85
|
+
parent_object,
|
|
86
|
+
['_query', 'pageToken'],
|
|
87
|
+
getv(from_object, ['page_token']),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
return to_object
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _ListDocumentsParameters_to_mldev(
|
|
94
|
+
from_object: Union[dict[str, Any], object],
|
|
95
|
+
parent_object: Optional[dict[str, Any]] = None,
|
|
96
|
+
) -> dict[str, Any]:
|
|
97
|
+
to_object: dict[str, Any] = {}
|
|
98
|
+
if getv(from_object, ['parent']) is not None:
|
|
99
|
+
setv(to_object, ['_url', 'parent'], getv(from_object, ['parent']))
|
|
100
|
+
|
|
101
|
+
if getv(from_object, ['config']) is not None:
|
|
102
|
+
_ListDocumentsConfig_to_mldev(getv(from_object, ['config']), to_object)
|
|
103
|
+
|
|
104
|
+
return to_object
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _ListDocumentsResponse_from_mldev(
|
|
108
|
+
from_object: Union[dict[str, Any], object],
|
|
109
|
+
parent_object: Optional[dict[str, Any]] = None,
|
|
110
|
+
) -> dict[str, Any]:
|
|
111
|
+
to_object: dict[str, Any] = {}
|
|
112
|
+
if getv(from_object, ['sdkHttpResponse']) is not None:
|
|
113
|
+
setv(
|
|
114
|
+
to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
if getv(from_object, ['nextPageToken']) is not None:
|
|
118
|
+
setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
|
|
119
|
+
|
|
120
|
+
if getv(from_object, ['documents']) is not None:
|
|
121
|
+
setv(
|
|
122
|
+
to_object,
|
|
123
|
+
['documents'],
|
|
124
|
+
[item for item in getv(from_object, ['documents'])],
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
return to_object
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class Documents(_api_module.BaseModule):
|
|
131
|
+
|
|
132
|
+
def get(
|
|
133
|
+
self, *, name: str, config: Optional[types.GetDocumentConfigOrDict] = None
|
|
134
|
+
) -> types.Document:
|
|
135
|
+
"""Gets metadata about a Document.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
name (str): The resource name of the Document.
|
|
139
|
+
Example: ragStores/rag-store-foo/documents/documents-bar
|
|
140
|
+
config (GetDocumentConfig | None): Optional parameters for the request.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
The Document.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
parameter_model = types._GetDocumentParameters(
|
|
147
|
+
name=name,
|
|
148
|
+
config=config,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
request_url_dict: Optional[dict[str, str]]
|
|
152
|
+
if self._api_client.vertexai:
|
|
153
|
+
raise ValueError(
|
|
154
|
+
'This method is only supported in the Gemini Developer client.'
|
|
155
|
+
)
|
|
156
|
+
else:
|
|
157
|
+
request_dict = _GetDocumentParameters_to_mldev(parameter_model)
|
|
158
|
+
request_url_dict = request_dict.get('_url')
|
|
159
|
+
if request_url_dict:
|
|
160
|
+
path = '{name}'.format_map(request_url_dict)
|
|
161
|
+
else:
|
|
162
|
+
path = '{name}'
|
|
163
|
+
|
|
164
|
+
query_params = request_dict.get('_query')
|
|
165
|
+
if query_params:
|
|
166
|
+
path = f'{path}?{urlencode(query_params)}'
|
|
167
|
+
# TODO: remove the hack that pops config.
|
|
168
|
+
request_dict.pop('config', None)
|
|
169
|
+
|
|
170
|
+
http_options: Optional[types.HttpOptions] = None
|
|
171
|
+
if (
|
|
172
|
+
parameter_model.config is not None
|
|
173
|
+
and parameter_model.config.http_options is not None
|
|
174
|
+
):
|
|
175
|
+
http_options = parameter_model.config.http_options
|
|
176
|
+
|
|
177
|
+
request_dict = _common.convert_to_dict(request_dict)
|
|
178
|
+
request_dict = _common.encode_unserializable_types(request_dict)
|
|
179
|
+
|
|
180
|
+
response = self._api_client.request('get', path, request_dict, http_options)
|
|
181
|
+
|
|
182
|
+
response_dict = {} if not response.body else json.loads(response.body)
|
|
183
|
+
|
|
184
|
+
return_value = types.Document._from_response(
|
|
185
|
+
response=response_dict, kwargs=parameter_model.model_dump()
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
self._api_client._verify_response(return_value)
|
|
189
|
+
return return_value
|
|
190
|
+
|
|
191
|
+
def delete(
|
|
192
|
+
self,
|
|
193
|
+
*,
|
|
194
|
+
name: str,
|
|
195
|
+
config: Optional[types.DeleteDocumentConfigOrDict] = None,
|
|
196
|
+
) -> None:
|
|
197
|
+
"""Deletes a Document.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
name (str): The resource name of the Document.
|
|
201
|
+
Example: ragStores/rag-store-foo/documents/documents-bar
|
|
202
|
+
config (DeleteDocumentConfig | None): Optional parameters for the request.
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
None
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
parameter_model = types._DeleteDocumentParameters(
|
|
209
|
+
name=name,
|
|
210
|
+
config=config,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
request_url_dict: Optional[dict[str, str]]
|
|
214
|
+
if self._api_client.vertexai:
|
|
215
|
+
raise ValueError(
|
|
216
|
+
'This method is only supported in the Gemini Developer client.'
|
|
217
|
+
)
|
|
218
|
+
else:
|
|
219
|
+
request_dict = _DeleteDocumentParameters_to_mldev(parameter_model)
|
|
220
|
+
request_url_dict = request_dict.get('_url')
|
|
221
|
+
if request_url_dict:
|
|
222
|
+
path = '{name}'.format_map(request_url_dict)
|
|
223
|
+
else:
|
|
224
|
+
path = '{name}'
|
|
225
|
+
|
|
226
|
+
query_params = request_dict.get('_query')
|
|
227
|
+
if query_params:
|
|
228
|
+
path = f'{path}?{urlencode(query_params)}'
|
|
229
|
+
# TODO: remove the hack that pops config.
|
|
230
|
+
request_dict.pop('config', None)
|
|
231
|
+
|
|
232
|
+
http_options: Optional[types.HttpOptions] = None
|
|
233
|
+
if (
|
|
234
|
+
parameter_model.config is not None
|
|
235
|
+
and parameter_model.config.http_options is not None
|
|
236
|
+
):
|
|
237
|
+
http_options = parameter_model.config.http_options
|
|
238
|
+
|
|
239
|
+
request_dict = _common.convert_to_dict(request_dict)
|
|
240
|
+
request_dict = _common.encode_unserializable_types(request_dict)
|
|
241
|
+
|
|
242
|
+
response = self._api_client.request(
|
|
243
|
+
'delete', path, request_dict, http_options
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
def _list(
|
|
247
|
+
self,
|
|
248
|
+
*,
|
|
249
|
+
parent: str,
|
|
250
|
+
config: Optional[types.ListDocumentsConfigOrDict] = None,
|
|
251
|
+
) -> types.ListDocumentsResponse:
|
|
252
|
+
parameter_model = types._ListDocumentsParameters(
|
|
253
|
+
parent=parent,
|
|
254
|
+
config=config,
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
request_url_dict: Optional[dict[str, str]]
|
|
258
|
+
if self._api_client.vertexai:
|
|
259
|
+
raise ValueError(
|
|
260
|
+
'This method is only supported in the Gemini Developer client.'
|
|
261
|
+
)
|
|
262
|
+
else:
|
|
263
|
+
request_dict = _ListDocumentsParameters_to_mldev(parameter_model)
|
|
264
|
+
request_url_dict = request_dict.get('_url')
|
|
265
|
+
if request_url_dict:
|
|
266
|
+
path = '{parent}/documents'.format_map(request_url_dict)
|
|
267
|
+
else:
|
|
268
|
+
path = '{parent}/documents'
|
|
269
|
+
|
|
270
|
+
query_params = request_dict.get('_query')
|
|
271
|
+
if query_params:
|
|
272
|
+
path = f'{path}?{urlencode(query_params)}'
|
|
273
|
+
# TODO: remove the hack that pops config.
|
|
274
|
+
request_dict.pop('config', None)
|
|
275
|
+
|
|
276
|
+
http_options: Optional[types.HttpOptions] = None
|
|
277
|
+
if (
|
|
278
|
+
parameter_model.config is not None
|
|
279
|
+
and parameter_model.config.http_options is not None
|
|
280
|
+
):
|
|
281
|
+
http_options = parameter_model.config.http_options
|
|
282
|
+
|
|
283
|
+
request_dict = _common.convert_to_dict(request_dict)
|
|
284
|
+
request_dict = _common.encode_unserializable_types(request_dict)
|
|
285
|
+
|
|
286
|
+
response = self._api_client.request('get', path, request_dict, http_options)
|
|
287
|
+
|
|
288
|
+
response_dict = {} if not response.body else json.loads(response.body)
|
|
289
|
+
|
|
290
|
+
if not self._api_client.vertexai:
|
|
291
|
+
response_dict = _ListDocumentsResponse_from_mldev(response_dict)
|
|
292
|
+
|
|
293
|
+
return_value = types.ListDocumentsResponse._from_response(
|
|
294
|
+
response=response_dict, kwargs=parameter_model.model_dump()
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
self._api_client._verify_response(return_value)
|
|
298
|
+
return return_value
|
|
299
|
+
|
|
300
|
+
def list(
|
|
301
|
+
self,
|
|
302
|
+
*,
|
|
303
|
+
parent: str,
|
|
304
|
+
config: Optional[types.ListDocumentsConfigOrDict] = None,
|
|
305
|
+
) -> Pager[types.Document]:
|
|
306
|
+
"""Lists documents.
|
|
307
|
+
|
|
308
|
+
Args:
|
|
309
|
+
parent (str): The name of the RagStore containing the Documents.
|
|
310
|
+
config (ListDocumentsConfig): Optional configuration for the list request.
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
A Pager object that contains one page of documents. When iterating over
|
|
314
|
+
the pager, it automatically fetches the next page if there are more.
|
|
315
|
+
Usage:
|
|
316
|
+
.. code-block:: python
|
|
317
|
+
for document in client.documents.list(parent='rag_store_name'):
|
|
318
|
+
print(f"document: {document.name} - {document.display_name}")
|
|
319
|
+
"""
|
|
320
|
+
|
|
321
|
+
list_request = partial(self._list, parent=parent)
|
|
322
|
+
return Pager(
|
|
323
|
+
'documents',
|
|
324
|
+
list_request,
|
|
325
|
+
self._list(parent=parent, config=config),
|
|
326
|
+
config,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class AsyncDocuments(_api_module.BaseModule):
|
|
331
|
+
|
|
332
|
+
async def get(
|
|
333
|
+
self, *, name: str, config: Optional[types.GetDocumentConfigOrDict] = None
|
|
334
|
+
) -> types.Document:
|
|
335
|
+
"""Gets metadata about a Document.
|
|
336
|
+
|
|
337
|
+
Args:
|
|
338
|
+
name (str): The resource name of the Document.
|
|
339
|
+
Example: ragStores/rag-store-foo/documents/documents-bar
|
|
340
|
+
config (GetDocumentConfig | None): Optional parameters for the request.
|
|
341
|
+
|
|
342
|
+
Returns:
|
|
343
|
+
The Document.
|
|
344
|
+
"""
|
|
345
|
+
|
|
346
|
+
parameter_model = types._GetDocumentParameters(
|
|
347
|
+
name=name,
|
|
348
|
+
config=config,
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
request_url_dict: Optional[dict[str, str]]
|
|
352
|
+
if self._api_client.vertexai:
|
|
353
|
+
raise ValueError(
|
|
354
|
+
'This method is only supported in the Gemini Developer client.'
|
|
355
|
+
)
|
|
356
|
+
else:
|
|
357
|
+
request_dict = _GetDocumentParameters_to_mldev(parameter_model)
|
|
358
|
+
request_url_dict = request_dict.get('_url')
|
|
359
|
+
if request_url_dict:
|
|
360
|
+
path = '{name}'.format_map(request_url_dict)
|
|
361
|
+
else:
|
|
362
|
+
path = '{name}'
|
|
363
|
+
|
|
364
|
+
query_params = request_dict.get('_query')
|
|
365
|
+
if query_params:
|
|
366
|
+
path = f'{path}?{urlencode(query_params)}'
|
|
367
|
+
# TODO: remove the hack that pops config.
|
|
368
|
+
request_dict.pop('config', None)
|
|
369
|
+
|
|
370
|
+
http_options: Optional[types.HttpOptions] = None
|
|
371
|
+
if (
|
|
372
|
+
parameter_model.config is not None
|
|
373
|
+
and parameter_model.config.http_options is not None
|
|
374
|
+
):
|
|
375
|
+
http_options = parameter_model.config.http_options
|
|
376
|
+
|
|
377
|
+
request_dict = _common.convert_to_dict(request_dict)
|
|
378
|
+
request_dict = _common.encode_unserializable_types(request_dict)
|
|
379
|
+
|
|
380
|
+
response = await self._api_client.async_request(
|
|
381
|
+
'get', path, request_dict, http_options
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
response_dict = {} if not response.body else json.loads(response.body)
|
|
385
|
+
|
|
386
|
+
return_value = types.Document._from_response(
|
|
387
|
+
response=response_dict, kwargs=parameter_model.model_dump()
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
self._api_client._verify_response(return_value)
|
|
391
|
+
return return_value
|
|
392
|
+
|
|
393
|
+
async def delete(
|
|
394
|
+
self,
|
|
395
|
+
*,
|
|
396
|
+
name: str,
|
|
397
|
+
config: Optional[types.DeleteDocumentConfigOrDict] = None,
|
|
398
|
+
) -> None:
|
|
399
|
+
"""Deletes a Document.
|
|
400
|
+
|
|
401
|
+
Args:
|
|
402
|
+
name (str): The resource name of the Document.
|
|
403
|
+
Example: ragStores/rag-store-foo/documents/documents-bar
|
|
404
|
+
config (DeleteDocumentConfig | None): Optional parameters for the request.
|
|
405
|
+
|
|
406
|
+
Returns:
|
|
407
|
+
None
|
|
408
|
+
"""
|
|
409
|
+
|
|
410
|
+
parameter_model = types._DeleteDocumentParameters(
|
|
411
|
+
name=name,
|
|
412
|
+
config=config,
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
request_url_dict: Optional[dict[str, str]]
|
|
416
|
+
if self._api_client.vertexai:
|
|
417
|
+
raise ValueError(
|
|
418
|
+
'This method is only supported in the Gemini Developer client.'
|
|
419
|
+
)
|
|
420
|
+
else:
|
|
421
|
+
request_dict = _DeleteDocumentParameters_to_mldev(parameter_model)
|
|
422
|
+
request_url_dict = request_dict.get('_url')
|
|
423
|
+
if request_url_dict:
|
|
424
|
+
path = '{name}'.format_map(request_url_dict)
|
|
425
|
+
else:
|
|
426
|
+
path = '{name}'
|
|
427
|
+
|
|
428
|
+
query_params = request_dict.get('_query')
|
|
429
|
+
if query_params:
|
|
430
|
+
path = f'{path}?{urlencode(query_params)}'
|
|
431
|
+
# TODO: remove the hack that pops config.
|
|
432
|
+
request_dict.pop('config', None)
|
|
433
|
+
|
|
434
|
+
http_options: Optional[types.HttpOptions] = None
|
|
435
|
+
if (
|
|
436
|
+
parameter_model.config is not None
|
|
437
|
+
and parameter_model.config.http_options is not None
|
|
438
|
+
):
|
|
439
|
+
http_options = parameter_model.config.http_options
|
|
440
|
+
|
|
441
|
+
request_dict = _common.convert_to_dict(request_dict)
|
|
442
|
+
request_dict = _common.encode_unserializable_types(request_dict)
|
|
443
|
+
|
|
444
|
+
response = await self._api_client.async_request(
|
|
445
|
+
'delete', path, request_dict, http_options
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
async def _list(
|
|
449
|
+
self,
|
|
450
|
+
*,
|
|
451
|
+
parent: str,
|
|
452
|
+
config: Optional[types.ListDocumentsConfigOrDict] = None,
|
|
453
|
+
) -> types.ListDocumentsResponse:
|
|
454
|
+
parameter_model = types._ListDocumentsParameters(
|
|
455
|
+
parent=parent,
|
|
456
|
+
config=config,
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
request_url_dict: Optional[dict[str, str]]
|
|
460
|
+
if self._api_client.vertexai:
|
|
461
|
+
raise ValueError(
|
|
462
|
+
'This method is only supported in the Gemini Developer client.'
|
|
463
|
+
)
|
|
464
|
+
else:
|
|
465
|
+
request_dict = _ListDocumentsParameters_to_mldev(parameter_model)
|
|
466
|
+
request_url_dict = request_dict.get('_url')
|
|
467
|
+
if request_url_dict:
|
|
468
|
+
path = '{parent}/documents'.format_map(request_url_dict)
|
|
469
|
+
else:
|
|
470
|
+
path = '{parent}/documents'
|
|
471
|
+
|
|
472
|
+
query_params = request_dict.get('_query')
|
|
473
|
+
if query_params:
|
|
474
|
+
path = f'{path}?{urlencode(query_params)}'
|
|
475
|
+
# TODO: remove the hack that pops config.
|
|
476
|
+
request_dict.pop('config', None)
|
|
477
|
+
|
|
478
|
+
http_options: Optional[types.HttpOptions] = None
|
|
479
|
+
if (
|
|
480
|
+
parameter_model.config is not None
|
|
481
|
+
and parameter_model.config.http_options is not None
|
|
482
|
+
):
|
|
483
|
+
http_options = parameter_model.config.http_options
|
|
484
|
+
|
|
485
|
+
request_dict = _common.convert_to_dict(request_dict)
|
|
486
|
+
request_dict = _common.encode_unserializable_types(request_dict)
|
|
487
|
+
|
|
488
|
+
response = await self._api_client.async_request(
|
|
489
|
+
'get', path, request_dict, http_options
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
response_dict = {} if not response.body else json.loads(response.body)
|
|
493
|
+
|
|
494
|
+
if not self._api_client.vertexai:
|
|
495
|
+
response_dict = _ListDocumentsResponse_from_mldev(response_dict)
|
|
496
|
+
|
|
497
|
+
return_value = types.ListDocumentsResponse._from_response(
|
|
498
|
+
response=response_dict, kwargs=parameter_model.model_dump()
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
self._api_client._verify_response(return_value)
|
|
502
|
+
return return_value
|
|
503
|
+
|
|
504
|
+
async def list(
|
|
505
|
+
self,
|
|
506
|
+
*,
|
|
507
|
+
parent: str,
|
|
508
|
+
config: Optional[types.ListDocumentsConfigOrDict] = None,
|
|
509
|
+
) -> AsyncPager[types.Document]:
|
|
510
|
+
"""Lists documents asynchronously.
|
|
511
|
+
|
|
512
|
+
Args:
|
|
513
|
+
parent (str): The name of the RagStore containing the Documents.
|
|
514
|
+
config (ListDocumentsConfig): Optional configuration for the list request.
|
|
515
|
+
|
|
516
|
+
Returns:
|
|
517
|
+
A Pager object that contains one page of documents. When iterating over
|
|
518
|
+
the pager, it automatically fetches the next page if there are more.
|
|
519
|
+
Usage:
|
|
520
|
+
.. code-block:: python
|
|
521
|
+
async for document in await
|
|
522
|
+
client.aio.documents.list(parent='rag_store_name'):
|
|
523
|
+
print(f"document: {document.name} - {document.display_name}")
|
|
524
|
+
"""
|
|
525
|
+
|
|
526
|
+
list_request = partial(self._list, parent=parent)
|
|
527
|
+
return AsyncPager(
|
|
528
|
+
'documents',
|
|
529
|
+
list_request,
|
|
530
|
+
await self._list(parent=parent, config=config),
|
|
531
|
+
config,
|
|
532
|
+
)
|