google-genai 0.0.1__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/__init__.py +20 -0
- google/genai/_api_client.py +467 -0
- google/genai/_automatic_function_calling_util.py +341 -0
- google/genai/_common.py +256 -0
- google/genai/_extra_utils.py +295 -0
- google/genai/_replay_api_client.py +478 -0
- google/genai/_test_api_client.py +149 -0
- google/genai/_transformers.py +438 -0
- google/genai/batches.py +1041 -0
- google/genai/caches.py +1830 -0
- google/genai/chats.py +184 -0
- google/genai/client.py +277 -0
- google/genai/errors.py +110 -0
- google/genai/files.py +1211 -0
- google/genai/live.py +629 -0
- google/genai/models.py +5307 -0
- google/genai/pagers.py +245 -0
- google/genai/tunings.py +1366 -0
- google/genai/types.py +7639 -0
- google_genai-0.0.1.dist-info/LICENSE +202 -0
- google_genai-0.0.1.dist-info/METADATA +763 -0
- google_genai-0.0.1.dist-info/RECORD +24 -0
- google_genai-0.0.1.dist-info/WHEEL +5 -0
- google_genai-0.0.1.dist-info/top_level.txt +1 -0
google/genai/pagers.py
ADDED
@@ -0,0 +1,245 @@
|
|
1
|
+
# Copyright 2024 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
|
+
"""Pagers for the GenAI List APIs."""
|
17
|
+
|
18
|
+
# pylint: disable=protected-access
|
19
|
+
|
20
|
+
import copy
|
21
|
+
from typing import Any, AsyncIterator, Awaitable, Callable, Generic, Iterator, Literal, TypeVar
|
22
|
+
|
23
|
+
T = TypeVar('T')
|
24
|
+
|
25
|
+
PagedItem = Literal[
|
26
|
+
'batch_jobs', 'models', 'tuning_jobs', 'files', 'cached_contents'
|
27
|
+
]
|
28
|
+
|
29
|
+
|
30
|
+
class _BasePager(Generic[T]):
|
31
|
+
"""Base pager class for iterating through paginated results."""
|
32
|
+
|
33
|
+
def __init__(
|
34
|
+
self,
|
35
|
+
name: PagedItem,
|
36
|
+
request: Callable[Any, Any],
|
37
|
+
response: Any,
|
38
|
+
config: Any,
|
39
|
+
):
|
40
|
+
self._name = name
|
41
|
+
self._request = request
|
42
|
+
|
43
|
+
self._page = getattr(response, self._name) or []
|
44
|
+
self._idx = 0
|
45
|
+
|
46
|
+
if not config:
|
47
|
+
request_config = {}
|
48
|
+
elif isinstance(config, dict):
|
49
|
+
request_config = copy.deepcopy(config)
|
50
|
+
else:
|
51
|
+
request_config = dict(config)
|
52
|
+
request_config['page_token'] = getattr(response, 'next_page_token')
|
53
|
+
self._config = request_config
|
54
|
+
|
55
|
+
self._page_size = request_config.get('page_size', len(self._page))
|
56
|
+
|
57
|
+
@property
|
58
|
+
def page(self) -> list[T]:
|
59
|
+
"""Returns the current page, which is a list of items.
|
60
|
+
|
61
|
+
The returned list of items is a subset of the entire list.
|
62
|
+
|
63
|
+
Usage:
|
64
|
+
|
65
|
+
.. code-block:: python
|
66
|
+
|
67
|
+
batch_jobs_pager = client.batches.list(config={'page_size': 5})
|
68
|
+
print(f"first page: {batch_jobs_pager.page}")
|
69
|
+
# first page: [BatchJob(name='projects/./locations/./batchPredictionJobs/1
|
70
|
+
"""
|
71
|
+
|
72
|
+
return self._page
|
73
|
+
|
74
|
+
@property
|
75
|
+
def name(self) -> str:
|
76
|
+
"""Returns the type of paged item (for example, ``batch_jobs``).
|
77
|
+
|
78
|
+
Usage:
|
79
|
+
|
80
|
+
.. code-block:: python
|
81
|
+
|
82
|
+
batch_jobs_pager = client.batches.list(config={'page_size': 5})
|
83
|
+
print(f"name: {batch_jobs_pager.name}")
|
84
|
+
# name: batch_jobs
|
85
|
+
"""
|
86
|
+
|
87
|
+
return self._name
|
88
|
+
|
89
|
+
@property
|
90
|
+
def page_size(self) -> int:
|
91
|
+
"""Returns the length of the page fetched each time by this pager.
|
92
|
+
|
93
|
+
The number of items in the page is less than or equal to the page length.
|
94
|
+
|
95
|
+
Usage:
|
96
|
+
|
97
|
+
.. code-block:: python
|
98
|
+
|
99
|
+
batch_jobs_pager = client.batches.list(config={'page_size': 5})
|
100
|
+
print(f"page_size: {batch_jobs_pager.page_size}")
|
101
|
+
# page_size: 5
|
102
|
+
"""
|
103
|
+
|
104
|
+
return self._page_size
|
105
|
+
|
106
|
+
@property
|
107
|
+
def config(self) -> dict[str, Any]:
|
108
|
+
"""Returns the configuration when making the API request for the next page.
|
109
|
+
|
110
|
+
A configuration is a set of optional parameters and arguments that can be
|
111
|
+
used to customize the API request. For example, the ``page_token`` parameter
|
112
|
+
contains the token to request the next page.
|
113
|
+
|
114
|
+
Usage:
|
115
|
+
|
116
|
+
.. code-block:: python
|
117
|
+
|
118
|
+
batch_jobs_pager = client.batches.list(config={'page_size': 5})
|
119
|
+
print(f"config: {batch_jobs_pager.config}")
|
120
|
+
# config: {'page_size': 5, 'page_token': 'AMEw9yO5jnsGnZJLHSKDFHJJu'}
|
121
|
+
"""
|
122
|
+
|
123
|
+
return self._config
|
124
|
+
|
125
|
+
def __len__(self) -> int:
|
126
|
+
"""Returns the total number of items in the current page."""
|
127
|
+
return len(self.page)
|
128
|
+
|
129
|
+
def __getitem__(self, index: int) -> T:
|
130
|
+
"""Returns the item at the given index."""
|
131
|
+
return self.page[index]
|
132
|
+
|
133
|
+
def _init_next_page(self, response: Any) -> None:
|
134
|
+
"""Initializes the next page from the response.
|
135
|
+
|
136
|
+
This is an internal method that should be called by subclasses after
|
137
|
+
fetching the next page.
|
138
|
+
|
139
|
+
Args:
|
140
|
+
response: The response object from the API request.
|
141
|
+
"""
|
142
|
+
self.__init__(self.name, self._request, response, self.config)
|
143
|
+
|
144
|
+
|
145
|
+
class Pager(_BasePager[T]):
|
146
|
+
"""Pager class for iterating through paginated results."""
|
147
|
+
|
148
|
+
def __next__(self) -> T:
|
149
|
+
"""Returns the next item."""
|
150
|
+
if self._idx >= len(self):
|
151
|
+
try:
|
152
|
+
self.next_page()
|
153
|
+
except IndexError:
|
154
|
+
raise StopIteration
|
155
|
+
|
156
|
+
item = self.page[self._idx]
|
157
|
+
self._idx += 1
|
158
|
+
return item
|
159
|
+
|
160
|
+
def __iter__(self) -> Iterator[T]:
|
161
|
+
"""Returns an iterator over the items."""
|
162
|
+
self._idx = 0
|
163
|
+
return self
|
164
|
+
|
165
|
+
def next_page(self) -> list[T]:
|
166
|
+
"""Fetches the next page of items. This makes a new API request.
|
167
|
+
|
168
|
+
Usage:
|
169
|
+
|
170
|
+
.. code-block:: python
|
171
|
+
|
172
|
+
batch_jobs_pager = client.batches.list(config={'page_size': 5})
|
173
|
+
print(f"current page: {batch_jobs_pager.page}")
|
174
|
+
batch_jobs_pager.next_page()
|
175
|
+
print(f"next page: {batch_jobs_pager.page}")
|
176
|
+
# current page: [BatchJob(name='projects/.../batchPredictionJobs/1
|
177
|
+
# next page: [BatchJob(name='projects/.../batchPredictionJobs/6
|
178
|
+
"""
|
179
|
+
|
180
|
+
if not self.config.get('page_token'):
|
181
|
+
raise IndexError('No more pages to fetch.')
|
182
|
+
|
183
|
+
response = self._request(config=self.config)
|
184
|
+
self._init_next_page(response)
|
185
|
+
return self.page
|
186
|
+
|
187
|
+
|
188
|
+
class AsyncPager(_BasePager[T]):
|
189
|
+
"""AsyncPager class for iterating through paginated results."""
|
190
|
+
|
191
|
+
def __init__(
|
192
|
+
self,
|
193
|
+
name: PagedItem,
|
194
|
+
request: Callable[Any, Awaitable[Any]],
|
195
|
+
response: Any,
|
196
|
+
config: Any,
|
197
|
+
):
|
198
|
+
super().__init__(name, request, response, config)
|
199
|
+
|
200
|
+
def __aiter__(self) -> AsyncIterator[T]:
|
201
|
+
"""Returns an async iterator over the items."""
|
202
|
+
self._idx = 0
|
203
|
+
return self
|
204
|
+
|
205
|
+
async def __anext__(self) -> Awaitable[T]:
|
206
|
+
"""Returns the next item asynchronously."""
|
207
|
+
if self._idx >= len(self):
|
208
|
+
try:
|
209
|
+
await self.next_page()
|
210
|
+
except IndexError:
|
211
|
+
raise StopAsyncIteration
|
212
|
+
|
213
|
+
item = self.page[self._idx]
|
214
|
+
self._idx += 1
|
215
|
+
return item
|
216
|
+
|
217
|
+
async def next_page(self) -> list[T]:
|
218
|
+
"""Fetches the next page of items asynchronously.
|
219
|
+
|
220
|
+
This makes a new API request.
|
221
|
+
|
222
|
+
Returns:
|
223
|
+
The next page of items.
|
224
|
+
|
225
|
+
Raises:
|
226
|
+
IndexError: No more pages to fetch.
|
227
|
+
|
228
|
+
Usage:
|
229
|
+
|
230
|
+
.. code-block:: python
|
231
|
+
|
232
|
+
batch_jobs_pager = await client.aio.batches.list(config={'page_size': 5})
|
233
|
+
print(f"current page: {batch_jobs_pager.page}")
|
234
|
+
await batch_jobs_pager.next_page()
|
235
|
+
print(f"next page: {batch_jobs_pager.page}")
|
236
|
+
# current page: [BatchJob(name='projects/.../batchPredictionJobs/1
|
237
|
+
# next page: [BatchJob(name='projects/.../batchPredictionJobs/6
|
238
|
+
"""
|
239
|
+
|
240
|
+
if not self.config.get('page_token'):
|
241
|
+
raise IndexError('No more pages to fetch.')
|
242
|
+
|
243
|
+
response = await self._request(config=self.config)
|
244
|
+
self._init_next_page(response)
|
245
|
+
return self.page
|