Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
466 lines (358 loc) · 12.3 KB
/
Copy path__init__.py
File metadata and controls
466 lines (358 loc) · 12.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details.
from __future__ import annotations
import os as _os
import typing as _t
from typing_extensions import override
from . import types
from ._types import NOT_GIVEN, Omit, NoneType, NotGiven, Transport, ProxiesTypes, omit, not_given
from ._utils import file_from_path
from ._client import Client, OpenAI, Stream, Timeout, Transport, AsyncClient, AsyncOpenAI, AsyncStream, RequestOptions
from ._httpx2 import (
DefaultHttpx2Client as DefaultHttpx2Client,
DefaultAsyncHttpx2Client as DefaultAsyncHttpx2Client,
normalize_httpx_url as _normalize_httpx_url,
)
from ._models import BaseModel
from ._version import __title__, __version__
from ._response import APIResponse as APIResponse, AsyncAPIResponse as AsyncAPIResponse
from ._constants import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES, DEFAULT_CONNECTION_LIMITS
from ._exceptions import (
APIError,
OAuthError as OAuthError,
OpenAIError,
ConflictError,
NotFoundError,
APIStatusError,
RateLimitError,
APITimeoutError,
BadRequestError,
APIConnectionError,
AuthenticationError,
InternalServerError,
PermissionDeniedError,
LengthFinishReasonError as LengthFinishReasonError,
WebSocketQueueFullError,
UnprocessableEntityError,
APIResponseValidationError,
InvalidWebhookSignatureError as InvalidWebhookSignatureError,
ContentFilterFinishReasonError as ContentFilterFinishReasonError,
WebSocketConnectionClosedError,
)
from ._base_client import DefaultHttpxClient, DefaultAioHttpClient, DefaultAsyncHttpxClient
from ._utils._logs import setup_logging as _setup_logging
from ._data_residency import DataResidency
from ._legacy_response import HttpxBinaryResponseContent as HttpxBinaryResponseContent
from .types.websocket_reconnection import ReconnectingEvent, ReconnectingOverrides
__all__ = [
"types",
"__version__",
"__title__",
"NoneType",
"Transport",
"ProxiesTypes",
"NotGiven",
"NOT_GIVEN",
"not_given",
"Omit",
"omit",
"OpenAIError",
"APIError",
"APIStatusError",
"APITimeoutError",
"APIConnectionError",
"APIResponseValidationError",
"BadRequestError",
"AuthenticationError",
"OAuthError",
"PermissionDeniedError",
"NotFoundError",
"ConflictError",
"UnprocessableEntityError",
"RateLimitError",
"InternalServerError",
"LengthFinishReasonError",
"ContentFilterFinishReasonError",
"InvalidWebhookSignatureError",
"Timeout",
"RequestOptions",
"DataResidency",
"Client",
"AsyncClient",
"Stream",
"AsyncStream",
"OpenAI",
"AsyncOpenAI",
"BedrockOpenAI",
"AsyncBedrockOpenAI",
"file_from_path",
"BaseModel",
"DEFAULT_TIMEOUT",
"DEFAULT_MAX_RETRIES",
"DEFAULT_CONNECTION_LIMITS",
"DefaultHttpxClient",
"DefaultAsyncHttpxClient",
"DefaultAioHttpClient",
"DefaultHttpx2Client",
"DefaultAsyncHttpx2Client",
"ReconnectingEvent",
"ReconnectingOverrides",
"WebSocketQueueFullError",
"WebSocketConnectionClosedError",
]
if not _t.TYPE_CHECKING:
from ._utils._resources_proxy import resources as resources
from .lib import (
azure as _azure,
bedrock as _bedrock,
pydantic_function_tool as pydantic_function_tool,
pydantic_responses_function_tool as pydantic_responses_function_tool,
)
from .version import VERSION as VERSION
from .lib.azure import AzureOpenAI as AzureOpenAI, AsyncAzureOpenAI as AsyncAzureOpenAI
from .lib.bedrock import BedrockOpenAI as BedrockOpenAI, AsyncBedrockOpenAI as AsyncBedrockOpenAI
from .lib._old_api import *
_COMPUTER_EXPORTS = (
"ComputerToolset",
"AsyncComputerToolset",
"ComputerDriver",
"AsyncComputerDriver",
)
_STREAMING_EXPORTS = (
"AssistantEventHandler",
"AsyncAssistantEventHandler",
)
if _t.TYPE_CHECKING:
from .lib.computer import (
ComputerDriver as ComputerDriver,
ComputerToolset as ComputerToolset,
AsyncComputerDriver as AsyncComputerDriver,
AsyncComputerToolset as AsyncComputerToolset,
)
from .lib.streaming import (
AssistantEventHandler as AssistantEventHandler,
AsyncAssistantEventHandler as AsyncAssistantEventHandler,
)
else:
def __getattr__(name: str) -> _t.Any:
if name in _COMPUTER_EXPORTS:
from importlib import import_module
value = getattr(import_module(".lib.computer", __name__), name)
globals()[name] = value
return value
if name in _STREAMING_EXPORTS:
from importlib import import_module
value = getattr(import_module(".lib.streaming", __name__), name)
globals()[name] = value
return value
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def __dir__() -> list[str]:
return sorted(set(globals()) | set(_COMPUTER_EXPORTS) | set(_STREAMING_EXPORTS))
_setup_logging()
# Update the __module__ attribute for exported symbols so that
# error messages point to this module instead of the module
# it was originally defined in, e.g.
# openai._exceptions.NotFoundError -> openai.NotFoundError
__locals = locals()
for __name in __all__:
if not __name.startswith("__"):
try:
__locals[__name].__module__ = "openai"
except (TypeError, AttributeError):
# Some of our exported symbols are builtins which we can't set attributes for.
pass
__all__ += ["ComputerToolset", "AsyncComputerToolset", "ComputerDriver", "AsyncComputerDriver"]
# ------ Module level client ------
import typing as _t
import httpx2 as _httpx
from ._base_client import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES
api_key: str | None = None
admin_api_key: str | None = None
organization: str | None = None
project: str | None = None
webhook_secret: str | None = None
base_url: str | _httpx.URL | None = None
timeout: float | Timeout | None = DEFAULT_TIMEOUT
max_retries: int = DEFAULT_MAX_RETRIES
default_headers: _t.Mapping[str, str] | None = None
default_query: _t.Mapping[str, object] | None = None
http_client: _httpx.Client | None = None
_ApiType = _t.Literal["openai", "azure", "amazon-bedrock"]
api_type: _ApiType | None = _t.cast(_ApiType, _os.environ.get("OPENAI_API_TYPE"))
api_version: str | None = _os.environ.get("OPENAI_API_VERSION")
azure_endpoint: str | None = _os.environ.get("AZURE_OPENAI_ENDPOINT")
azure_ad_token: str | None = None
azure_ad_token_provider: _azure.AzureADTokenProvider | None = None
_bedrock_api_key: str | None = None
bedrock_token_provider: _bedrock.BedrockTokenProvider | None = None
class _ModuleClient(OpenAI):
@property
@override
def api_key(self) -> str | None:
return api_key
@api_key.setter
def api_key(self, value: str | None) -> None: # ty: ignore[invalid-property-type-override]
global api_key # ty: ignore[invalid-property-type-override]
api_key = value
@property
@override
def admin_api_key(self) -> str | None:
return admin_api_key
@admin_api_key.setter
def admin_api_key(self, value: str | None) -> None:
global admin_api_key
admin_api_key = value
@property
@override
def organization(self) -> str | None:
return organization
@organization.setter
def organization(self, value: str | None) -> None:
global organization
organization = value
@property
@override
def project(self) -> str | None:
return project
@project.setter
def project(self, value: str | None) -> None:
global project
project = value
@property
@override
def webhook_secret(self) -> str | None:
return webhook_secret
@webhook_secret.setter
def webhook_secret(self, value: str | None) -> None:
global webhook_secret
webhook_secret = value
@property
@override
def base_url(self) -> _httpx.URL:
if base_url is not None:
return _normalize_httpx_url(base_url)
return super().base_url
@base_url.setter
def base_url(self, url: _httpx.URL | str) -> None:
# The module proxy forwards to the existing base property setter.
super().base_url = url # ty: ignore[invalid-assignment]
@property
@override
def timeout(self) -> float | Timeout | None:
return timeout
@timeout.setter
def timeout(self, value: float | Timeout | None) -> None:
global timeout
timeout = value
@property
@override
def max_retries(self) -> int:
return max_retries
@max_retries.setter
def max_retries(self, value: int) -> None:
global max_retries
max_retries = value
@property
@override
def _custom_headers(self) -> _t.Mapping[str, str] | None:
return default_headers
@_custom_headers.setter
def _custom_headers(self, value: _t.Mapping[str, str] | None) -> None:
global default_headers
default_headers = value
@property
@override
def _custom_query(self) -> _t.Mapping[str, object] | None:
return default_query
@_custom_query.setter
def _custom_query(self, value: _t.Mapping[str, object] | None) -> None:
global default_query
default_query = value
@property
@override
def _client(self) -> _httpx.Client:
return http_client or super()._client
@_client.setter
def _client(self, value: _httpx.Client) -> None:
global http_client
http_client = value
class _AzureModuleClient(_ModuleClient, AzureOpenAI): ...
class _BedrockModuleClient(_ModuleClient, BedrockOpenAI):
@property
@override
def api_key(
self,
) -> str | None:
from .lib._module_runtime import BedrockModuleClient_api_key
return BedrockModuleClient_api_key(self)
@api_key.setter
def api_key(
self,
value: str | None,
) -> None:
from .lib._module_runtime import BedrockModuleClient_api_key_setter
return BedrockModuleClient_api_key_setter(self, value)
@override
def _refresh_api_key(
self,
) -> str:
from typing import Any as _RuntimeAny, cast as _runtime_cast
from .lib._module_runtime import BedrockModuleClient_refresh_api_key
return BedrockModuleClient_refresh_api_key(_runtime_cast(_RuntimeAny, super()).__thisclass__, self)
@override
def _legacy_auth_configuration(
self,
) -> _bedrock._LegacyAuthConfiguration:
from typing import Any as _RuntimeAny, cast as _runtime_cast
from .lib._module_runtime import BedrockModuleClient_legacy_auth_configuration
return BedrockModuleClient_legacy_auth_configuration(_runtime_cast(_RuntimeAny, super()).__thisclass__, self)
class _AmbiguousModuleClientUsageError(OpenAIError):
def __init__(
self,
) -> None:
from typing import Any as _RuntimeAny, cast as _runtime_cast
from .lib._module_runtime import AmbiguousModuleClientUsageError_init
return AmbiguousModuleClientUsageError_init(_runtime_cast(_RuntimeAny, super()).__thisclass__, self)
def _has_openai_credentials() -> bool:
from .lib._module_runtime import has_openai_credentials
return has_openai_credentials()
def _has_azure_credentials() -> bool:
from .lib._module_runtime import has_azure_credentials
return has_azure_credentials()
def _has_azure_ad_credentials() -> bool:
from .lib._module_runtime import has_azure_ad_credentials
return has_azure_ad_credentials()
_client: OpenAI | None = None
def _load_client() -> OpenAI:
from .lib._module_runtime import load_client
return load_client()
def _reset_client() -> None:
global _client
_client = None
from ._module_client import (
beta as beta,
chat as chat,
live as live,
admin as admin,
audio as audio,
evals as evals,
files as files,
images as images,
models as models,
safety as safety,
skills as skills,
videos as videos,
batches as batches,
uploads as uploads,
realtime as realtime,
webhooks as webhooks,
decisions as decisions,
responses as responses,
containers as containers,
embeddings as embeddings,
completions as completions,
fine_tuning as fine_tuning,
moderations as moderations,
conversations as conversations,
vector_stores as vector_stores,
content_provenance_checks as content_provenance_checks,
)