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
278
279
280
281
|
# 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.
#
import os
from typing import Optional, Union
import google.auth
import pydantic
from ._api_client import ApiClient, HttpOptions, HttpOptionsDict
from ._replay_api_client import ReplayApiClient
from .batches import AsyncBatches, Batches
from .caches import AsyncCaches, Caches
from .chats import AsyncChats, Chats
from .files import AsyncFiles, Files
from .live import AsyncLive
from .models import AsyncModels, Models
from .tunings import AsyncTunings, Tunings
class AsyncClient:
"""Client for making asynchronous (non-blocking) requests."""
def __init__(self, api_client: ApiClient):
self._api_client = api_client
self._models = AsyncModels(self._api_client)
self._tunings = AsyncTunings(self._api_client)
self._caches = AsyncCaches(self._api_client)
self._batches = AsyncBatches(self._api_client)
self._files = AsyncFiles(self._api_client)
self._live = AsyncLive(self._api_client)
@property
def models(self) -> AsyncModels:
return self._models
@property
def tunings(self) -> AsyncTunings:
return self._tunings
@property
def caches(self) -> AsyncCaches:
return self._caches
@property
def batches(self) -> AsyncBatches:
return self._batches
@property
def chats(self) -> AsyncChats:
return AsyncChats(modules=self.models)
@property
def files(self) -> AsyncFiles:
return self._files
@property
def live(self) -> AsyncLive:
return self._live
class DebugConfig(pydantic.BaseModel):
"""Configuration options that change client network behavior when testing."""
client_mode: Optional[str] = pydantic.Field(
default_factory=lambda: os.getenv('GOOGLE_GENAI_CLIENT_MODE', None)
)
replays_directory: Optional[str] = pydantic.Field(
default_factory=lambda: os.getenv('GOOGLE_GENAI_REPLAYS_DIRECTORY', None)
)
replay_id: Optional[str] = pydantic.Field(
default_factory=lambda: os.getenv('GOOGLE_GENAI_REPLAY_ID', None)
)
class Client:
"""Client for making synchronous requests.
Use this client to make a request to the Gemini Developer API or Vertex AI
API and then wait for the response.
Attributes:
api_key: The `API key <https://ai.google.dev/gemini-api/docs/api-key>`_ to
use for authentication. Applies to the Gemini Developer API only.
vertexai: Indicates whether the client should use the Vertex AI
API endpoints. Defaults to False (uses Gemini Developer API endpoints).
Applies to the Vertex AI API only.
credentials: The credentials to use for authentication when calling the
Vertex AI APIs. Credentials can be obtained from environment variables and
default credentials. For more information, see
`Set up Application Default Credentials
<https://cloud.google.com/docs/authentication/provide-credentials-adc>`_.
Applies to the Vertex AI API only.
project: The `Google Cloud project ID <https://cloud.google.com/vertex-ai/docs/start/cloud-environment>`_ to
use for quota. Can be obtained from environment variables (for example,
``GOOGLE_CLOUD_PROJECT``). Applies to the Vertex AI API only.
location: The `location <https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations>`_
to send API requests to (for example, ``us-central1``). Can be obtained
from environment variables. Applies to the Vertex AI API only.
debug_config: Config settings that control network behavior of the client.
This is typically used when running test code.
http_options: Http options to use for the client. Response_payload can't be
set when passing to the client constructor.
Usage for the Gemini Developer API:
.. code-block:: python
from google import genai
client = genai.Client(api_key='my-api-key')
Usage for the Vertex AI API:
.. code-block:: python
from google import genai
client = genai.Client(
vertexai=True, project='my-project-id', location='us-central1'
)
"""
def __init__(
self,
*,
vertexai: Optional[bool] = None,
api_key: Optional[str] = None,
credentials: Optional[google.auth.credentials.Credentials] = None,
project: Optional[str] = None,
location: Optional[str] = None,
debug_config: Optional[DebugConfig] = None,
http_options: Optional[Union[HttpOptions, HttpOptionsDict]] = None,
):
"""Initializes the client.
Args:
vertexai (bool):
Indicates whether the client should use the Vertex AI
API endpoints. Defaults to False (uses Gemini Developer API
endpoints). Applies to the Vertex AI API only.
api_key (str):
The `API key
<https://ai.google.dev/gemini-api/docs/api-key>`_ to use for
authentication. Applies to the Gemini Developer API only.
credentials (google.auth.credentials.Credentials):
The credentials to
use for authentication when calling the Vertex AI APIs. Credentials
can be obtained from environment variables and default credentials.
For more information, see `Set up Application Default Credentials
<https://cloud.google.com/docs/authentication/provide-credentials-adc>`_.
Applies to the Vertex AI API only.
project (str):
The `Google Cloud project ID
<https://cloud.google.com/vertex-ai/docs/start/cloud-environment>`_ to
use for quota. Can be obtained from environment variables (for
example, ``GOOGLE_CLOUD_PROJECT``). Applies to the Vertex AI API only.
location (str):
The `location
<https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations>`_
to send API requests to (for example, ``us-central1``). Can be
obtained from environment variables. Applies to the Vertex AI API
only.
debug_config (DebugConfig):
Config settings that control network
behavior of the client. This is typically used when running test code.
http_options (Union[HttpOptions, HttpOptionsDict]):
Http options to use for the client. Response_payload can't be
set when passing to the client constructor.
"""
self._debug_config = debug_config or DebugConfig()
# Throw ValueError if response_payload is set in http_options due to
# unpredictable behavior when running multiple coroutines through
# client.aio.
if http_options and 'response_payload' in http_options:
raise ValueError(
'Setting response_payload in http_options is not supported.'
)
self._api_client = self._get_api_client(
vertexai=vertexai,
api_key=api_key,
credentials=credentials,
project=project,
location=location,
debug_config=self._debug_config,
http_options=http_options,
)
self._aio = AsyncClient(self._api_client)
self._models = Models(self._api_client)
self._tunings = Tunings(self._api_client)
self._caches = Caches(self._api_client)
self._batches = Batches(self._api_client)
self._files = Files(self._api_client)
@staticmethod
def _get_api_client(
vertexai: Optional[bool] = None,
api_key: Optional[str] = None,
credentials: Optional[google.auth.credentials.Credentials] = None,
project: Optional[str] = None,
location: Optional[str] = None,
debug_config: Optional[DebugConfig] = None,
http_options: Optional[HttpOptions] = None,
):
if debug_config and debug_config.client_mode in [
'record',
'replay',
'auto',
]:
return ReplayApiClient(
mode=debug_config.client_mode,
replay_id=debug_config.replay_id,
replays_directory=debug_config.replays_directory,
vertexai=vertexai,
api_key=api_key,
credentials=credentials,
project=project,
location=location,
http_options=http_options,
)
return ApiClient(
vertexai=vertexai,
api_key=api_key,
credentials=credentials,
project=project,
location=location,
http_options=http_options,
)
@property
def chats(self) -> Chats:
return Chats(modules=self.models)
@property
def aio(self) -> AsyncClient:
return self._aio
@property
def models(self) -> Models:
return self._models
@property
def tunings(self) -> Tunings:
return self._tunings
@property
def caches(self) -> Caches:
return self._caches
@property
def batches(self) -> Batches:
return self._batches
@property
def files(self) -> Files:
return self._files
@property
def vertexai(self) -> bool:
"""Returns whether the client is using the Vertex AI API."""
return self._api_client.vertexai or False
|