openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
src/openai/_client.py
1092lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import TYPE_CHECKING, Any, Union, Mapping |
| 7 | from typing_extensions import Self, override |
| 8 | |
| 9 | import httpx |
| 10 | |
| 11 | from . import _exceptions |
| 12 | from ._qs import Querystring |
| 13 | from ._types import ( |
| 14 | NOT_GIVEN, |
| 15 | Omit, |
| 16 | Timeout, |
| 17 | NotGiven, |
| 18 | Transport, |
| 19 | ProxiesTypes, |
| 20 | RequestOptions, |
| 21 | ) |
| 22 | from ._utils import ( |
| 23 | is_given, |
| 24 | is_mapping, |
| 25 | get_async_library, |
| 26 | ) |
| 27 | from ._compat import cached_property |
| 28 | from ._version import __version__ |
| 29 | from ._streaming import Stream as Stream, AsyncStream as AsyncStream |
| 30 | from ._exceptions import OpenAIError, APIStatusError |
| 31 | from ._base_client import ( |
| 32 | DEFAULT_MAX_RETRIES, |
| 33 | SyncAPIClient, |
| 34 | AsyncAPIClient, |
| 35 | ) |
| 36 | |
| 37 | if TYPE_CHECKING: |
| 38 | from .resources import ( |
| 39 | beta, |
| 40 | chat, |
| 41 | audio, |
| 42 | evals, |
| 43 | files, |
| 44 | images, |
| 45 | models, |
| 46 | batches, |
| 47 | uploads, |
| 48 | responses, |
| 49 | containers, |
| 50 | embeddings, |
| 51 | completions, |
| 52 | fine_tuning, |
| 53 | moderations, |
| 54 | vector_stores, |
| 55 | ) |
| 56 | from .resources.files import Files, AsyncFiles |
| 57 | from .resources.images import Images, AsyncImages |
| 58 | from .resources.models import Models, AsyncModels |
| 59 | from .resources.batches import Batches, AsyncBatches |
| 60 | from .resources.beta.beta import Beta, AsyncBeta |
| 61 | from .resources.chat.chat import Chat, AsyncChat |
| 62 | from .resources.embeddings import Embeddings, AsyncEmbeddings |
| 63 | from .resources.audio.audio import Audio, AsyncAudio |
| 64 | from .resources.completions import Completions, AsyncCompletions |
| 65 | from .resources.evals.evals import Evals, AsyncEvals |
| 66 | from .resources.moderations import Moderations, AsyncModerations |
| 67 | from .resources.uploads.uploads import Uploads, AsyncUploads |
| 68 | from .resources.responses.responses import Responses, AsyncResponses |
| 69 | from .resources.containers.containers import Containers, AsyncContainers |
| 70 | from .resources.fine_tuning.fine_tuning import FineTuning, AsyncFineTuning |
| 71 | from .resources.vector_stores.vector_stores import VectorStores, AsyncVectorStores |
| 72 | |
| 73 | __all__ = ["Timeout", "Transport", "ProxiesTypes", "RequestOptions", "OpenAI", "AsyncOpenAI", "Client", "AsyncClient"] |
| 74 | |
| 75 | |
| 76 | class OpenAI(SyncAPIClient): |
| 77 | # client options |
| 78 | api_key: str |
| 79 | organization: str | None |
| 80 | project: str | None |
| 81 | |
| 82 | websocket_base_url: str | httpx.URL | None |
| 83 | """Base URL for WebSocket connections. |
| 84 | |
| 85 | If not specified, the default base URL will be used, with 'wss://' replacing the |
| 86 | 'http://' or 'https://' scheme. For example: 'http://example.com' becomes |
| 87 | 'wss://example.com' |
| 88 | """ |
| 89 | |
| 90 | def __init__( |
| 91 | self, |
| 92 | *, |
| 93 | api_key: str | None = None, |
| 94 | organization: str | None = None, |
| 95 | project: str | None = None, |
| 96 | base_url: str | httpx.URL | None = None, |
| 97 | websocket_base_url: str | httpx.URL | None = None, |
| 98 | timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN, |
| 99 | max_retries: int = DEFAULT_MAX_RETRIES, |
| 100 | default_headers: Mapping[str, str] | None = None, |
| 101 | default_query: Mapping[str, object] | None = None, |
| 102 | # Configure a custom httpx client. |
| 103 | # We provide a `DefaultHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`. |
| 104 | # See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details. |
| 105 | http_client: httpx.Client | None = None, |
| 106 | # Enable or disable schema validation for data returned by the API. |
| 107 | # When enabled an error APIResponseValidationError is raised |
| 108 | # if the API responds with invalid data for the expected schema. |
| 109 | # |
| 110 | # This parameter may be removed or changed in the future. |
| 111 | # If you rely on this feature, please open a GitHub issue |
| 112 | # outlining your use-case to help us decide if it should be |
| 113 | # part of our public interface in the future. |
| 114 | _strict_response_validation: bool = False, |
| 115 | ) -> None: |
| 116 | """Construct a new synchronous OpenAI client instance. |
| 117 | |
| 118 | This automatically infers the following arguments from their corresponding environment variables if they are not provided: |
| 119 | - `api_key` from `OPENAI_API_KEY` |
| 120 | - `organization` from `OPENAI_ORG_ID` |
| 121 | - `project` from `OPENAI_PROJECT_ID` |
| 122 | """ |
| 123 | if api_key is None: |
| 124 | api_key = os.environ.get("OPENAI_API_KEY") |
| 125 | if api_key is None: |
| 126 | raise OpenAIError( |
| 127 | "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable" |
| 128 | ) |
| 129 | self.api_key = api_key |
| 130 | |
| 131 | if organization is None: |
| 132 | organization = os.environ.get("OPENAI_ORG_ID") |
| 133 | self.organization = organization |
| 134 | |
| 135 | if project is None: |
| 136 | project = os.environ.get("OPENAI_PROJECT_ID") |
| 137 | self.project = project |
| 138 | |
| 139 | self.websocket_base_url = websocket_base_url |
| 140 | |
| 141 | if base_url is None: |
| 142 | base_url = os.environ.get("OPENAI_BASE_URL") |
| 143 | if base_url is None: |
| 144 | base_url = f"https://api.openai.com/v1" |
| 145 | |
| 146 | super().__init__( |
| 147 | version=__version__, |
| 148 | base_url=base_url, |
| 149 | max_retries=max_retries, |
| 150 | timeout=timeout, |
| 151 | http_client=http_client, |
| 152 | custom_headers=default_headers, |
| 153 | custom_query=default_query, |
| 154 | _strict_response_validation=_strict_response_validation, |
| 155 | ) |
| 156 | |
| 157 | self._default_stream_cls = Stream |
| 158 | |
| 159 | @cached_property |
| 160 | def completions(self) -> Completions: |
| 161 | from .resources.completions import Completions |
| 162 | |
| 163 | return Completions(self) |
| 164 | |
| 165 | @cached_property |
| 166 | def chat(self) -> Chat: |
| 167 | from .resources.chat import Chat |
| 168 | |
| 169 | return Chat(self) |
| 170 | |
| 171 | @cached_property |
| 172 | def embeddings(self) -> Embeddings: |
| 173 | from .resources.embeddings import Embeddings |
| 174 | |
| 175 | return Embeddings(self) |
| 176 | |
| 177 | @cached_property |
| 178 | def files(self) -> Files: |
| 179 | from .resources.files import Files |
| 180 | |
| 181 | return Files(self) |
| 182 | |
| 183 | @cached_property |
| 184 | def images(self) -> Images: |
| 185 | from .resources.images import Images |
| 186 | |
| 187 | return Images(self) |
| 188 | |
| 189 | @cached_property |
| 190 | def audio(self) -> Audio: |
| 191 | from .resources.audio import Audio |
| 192 | |
| 193 | return Audio(self) |
| 194 | |
| 195 | @cached_property |
| 196 | def moderations(self) -> Moderations: |
| 197 | from .resources.moderations import Moderations |
| 198 | |
| 199 | return Moderations(self) |
| 200 | |
| 201 | @cached_property |
| 202 | def models(self) -> Models: |
| 203 | from .resources.models import Models |
| 204 | |
| 205 | return Models(self) |
| 206 | |
| 207 | @cached_property |
| 208 | def fine_tuning(self) -> FineTuning: |
| 209 | from .resources.fine_tuning import FineTuning |
| 210 | |
| 211 | return FineTuning(self) |
| 212 | |
| 213 | @cached_property |
| 214 | def vector_stores(self) -> VectorStores: |
| 215 | from .resources.vector_stores import VectorStores |
| 216 | |
| 217 | return VectorStores(self) |
| 218 | |
| 219 | @cached_property |
| 220 | def beta(self) -> Beta: |
| 221 | from .resources.beta import Beta |
| 222 | |
| 223 | return Beta(self) |
| 224 | |
| 225 | @cached_property |
| 226 | def batches(self) -> Batches: |
| 227 | from .resources.batches import Batches |
| 228 | |
| 229 | return Batches(self) |
| 230 | |
| 231 | @cached_property |
| 232 | def uploads(self) -> Uploads: |
| 233 | from .resources.uploads import Uploads |
| 234 | |
| 235 | return Uploads(self) |
| 236 | |
| 237 | @cached_property |
| 238 | def responses(self) -> Responses: |
| 239 | from .resources.responses import Responses |
| 240 | |
| 241 | return Responses(self) |
| 242 | |
| 243 | @cached_property |
| 244 | def evals(self) -> Evals: |
| 245 | from .resources.evals import Evals |
| 246 | |
| 247 | return Evals(self) |
| 248 | |
| 249 | @cached_property |
| 250 | def containers(self) -> Containers: |
| 251 | from .resources.containers import Containers |
| 252 | |
| 253 | return Containers(self) |
| 254 | |
| 255 | @cached_property |
| 256 | def with_raw_response(self) -> OpenAIWithRawResponse: |
| 257 | return OpenAIWithRawResponse(self) |
| 258 | |
| 259 | @cached_property |
| 260 | def with_streaming_response(self) -> OpenAIWithStreamedResponse: |
| 261 | return OpenAIWithStreamedResponse(self) |
| 262 | |
| 263 | @property |
| 264 | @override |
| 265 | def qs(self) -> Querystring: |
| 266 | return Querystring(array_format="brackets") |
| 267 | |
| 268 | @property |
| 269 | @override |
| 270 | def auth_headers(self) -> dict[str, str]: |
| 271 | api_key = self.api_key |
| 272 | return {"Authorization": f"Bearer {api_key}"} |
| 273 | |
| 274 | @property |
| 275 | @override |
| 276 | def default_headers(self) -> dict[str, str | Omit]: |
| 277 | return { |
| 278 | **super().default_headers, |
| 279 | "X-Stainless-Async": "false", |
| 280 | "OpenAI-Organization": self.organization if self.organization is not None else Omit(), |
| 281 | "OpenAI-Project": self.project if self.project is not None else Omit(), |
| 282 | **self._custom_headers, |
| 283 | } |
| 284 | |
| 285 | def copy( |
| 286 | self, |
| 287 | *, |
| 288 | api_key: str | None = None, |
| 289 | organization: str | None = None, |
| 290 | project: str | None = None, |
| 291 | websocket_base_url: str | httpx.URL | None = None, |
| 292 | base_url: str | httpx.URL | None = None, |
| 293 | timeout: float | Timeout | None | NotGiven = NOT_GIVEN, |
| 294 | http_client: httpx.Client | None = None, |
| 295 | max_retries: int | NotGiven = NOT_GIVEN, |
| 296 | default_headers: Mapping[str, str] | None = None, |
| 297 | set_default_headers: Mapping[str, str] | None = None, |
| 298 | default_query: Mapping[str, object] | None = None, |
| 299 | set_default_query: Mapping[str, object] | None = None, |
| 300 | _extra_kwargs: Mapping[str, Any] = {}, |
| 301 | ) -> Self: |
| 302 | """ |
| 303 | Create a new client instance re-using the same options given to the current client with optional overriding. |
| 304 | """ |
| 305 | if default_headers is not None and set_default_headers is not None: |
| 306 | raise ValueError("The `default_headers` and `set_default_headers` arguments are mutually exclusive") |
| 307 | |
| 308 | if default_query is not None and set_default_query is not None: |
| 309 | raise ValueError("The `default_query` and `set_default_query` arguments are mutually exclusive") |
| 310 | |
| 311 | headers = self._custom_headers |
| 312 | if default_headers is not None: |
| 313 | headers = {**headers, **default_headers} |
| 314 | elif set_default_headers is not None: |
| 315 | headers = set_default_headers |
| 316 | |
| 317 | params = self._custom_query |
| 318 | if default_query is not None: |
| 319 | params = {**params, **default_query} |
| 320 | elif set_default_query is not None: |
| 321 | params = set_default_query |
| 322 | |
| 323 | http_client = http_client or self._client |
| 324 | return self.__class__( |
| 325 | api_key=api_key or self.api_key, |
| 326 | organization=organization or self.organization, |
| 327 | project=project or self.project, |
| 328 | websocket_base_url=websocket_base_url or self.websocket_base_url, |
| 329 | base_url=base_url or self.base_url, |
| 330 | timeout=self.timeout if isinstance(timeout, NotGiven) else timeout, |
| 331 | http_client=http_client, |
| 332 | max_retries=max_retries if is_given(max_retries) else self.max_retries, |
| 333 | default_headers=headers, |
| 334 | default_query=params, |
| 335 | **_extra_kwargs, |
| 336 | ) |
| 337 | |
| 338 | # Alias for `copy` for nicer inline usage, e.g. |
| 339 | # client.with_options(timeout=10).foo.create(...) |
| 340 | with_options = copy |
| 341 | |
| 342 | @override |
| 343 | def _make_status_error( |
| 344 | self, |
| 345 | err_msg: str, |
| 346 | *, |
| 347 | body: object, |
| 348 | response: httpx.Response, |
| 349 | ) -> APIStatusError: |
| 350 | data = body.get("error", body) if is_mapping(body) else body |
| 351 | if response.status_code == 400: |
| 352 | return _exceptions.BadRequestError(err_msg, response=response, body=data) |
| 353 | |
| 354 | if response.status_code == 401: |
| 355 | return _exceptions.AuthenticationError(err_msg, response=response, body=data) |
| 356 | |
| 357 | if response.status_code == 403: |
| 358 | return _exceptions.PermissionDeniedError(err_msg, response=response, body=data) |
| 359 | |
| 360 | if response.status_code == 404: |
| 361 | return _exceptions.NotFoundError(err_msg, response=response, body=data) |
| 362 | |
| 363 | if response.status_code == 409: |
| 364 | return _exceptions.ConflictError(err_msg, response=response, body=data) |
| 365 | |
| 366 | if response.status_code == 422: |
| 367 | return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data) |
| 368 | |
| 369 | if response.status_code == 429: |
| 370 | return _exceptions.RateLimitError(err_msg, response=response, body=data) |
| 371 | |
| 372 | if response.status_code >= 500: |
| 373 | return _exceptions.InternalServerError(err_msg, response=response, body=data) |
| 374 | return APIStatusError(err_msg, response=response, body=data) |
| 375 | |
| 376 | |
| 377 | class AsyncOpenAI(AsyncAPIClient): |
| 378 | # client options |
| 379 | api_key: str |
| 380 | organization: str | None |
| 381 | project: str | None |
| 382 | |
| 383 | websocket_base_url: str | httpx.URL | None |
| 384 | """Base URL for WebSocket connections. |
| 385 | |
| 386 | If not specified, the default base URL will be used, with 'wss://' replacing the |
| 387 | 'http://' or 'https://' scheme. For example: 'http://example.com' becomes |
| 388 | 'wss://example.com' |
| 389 | """ |
| 390 | |
| 391 | def __init__( |
| 392 | self, |
| 393 | *, |
| 394 | api_key: str | None = None, |
| 395 | organization: str | None = None, |
| 396 | project: str | None = None, |
| 397 | base_url: str | httpx.URL | None = None, |
| 398 | websocket_base_url: str | httpx.URL | None = None, |
| 399 | timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN, |
| 400 | max_retries: int = DEFAULT_MAX_RETRIES, |
| 401 | default_headers: Mapping[str, str] | None = None, |
| 402 | default_query: Mapping[str, object] | None = None, |
| 403 | # Configure a custom httpx client. |
| 404 | # We provide a `DefaultAsyncHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`. |
| 405 | # See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details. |
| 406 | http_client: httpx.AsyncClient | None = None, |
| 407 | # Enable or disable schema validation for data returned by the API. |
| 408 | # When enabled an error APIResponseValidationError is raised |
| 409 | # if the API responds with invalid data for the expected schema. |
| 410 | # |
| 411 | # This parameter may be removed or changed in the future. |
| 412 | # If you rely on this feature, please open a GitHub issue |
| 413 | # outlining your use-case to help us decide if it should be |
| 414 | # part of our public interface in the future. |
| 415 | _strict_response_validation: bool = False, |
| 416 | ) -> None: |
| 417 | """Construct a new async AsyncOpenAI client instance. |
| 418 | |
| 419 | This automatically infers the following arguments from their corresponding environment variables if they are not provided: |
| 420 | - `api_key` from `OPENAI_API_KEY` |
| 421 | - `organization` from `OPENAI_ORG_ID` |
| 422 | - `project` from `OPENAI_PROJECT_ID` |
| 423 | """ |
| 424 | if api_key is None: |
| 425 | api_key = os.environ.get("OPENAI_API_KEY") |
| 426 | if api_key is None: |
| 427 | raise OpenAIError( |
| 428 | "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable" |
| 429 | ) |
| 430 | self.api_key = api_key |
| 431 | |
| 432 | if organization is None: |
| 433 | organization = os.environ.get("OPENAI_ORG_ID") |
| 434 | self.organization = organization |
| 435 | |
| 436 | if project is None: |
| 437 | project = os.environ.get("OPENAI_PROJECT_ID") |
| 438 | self.project = project |
| 439 | |
| 440 | self.websocket_base_url = websocket_base_url |
| 441 | |
| 442 | if base_url is None: |
| 443 | base_url = os.environ.get("OPENAI_BASE_URL") |
| 444 | if base_url is None: |
| 445 | base_url = f"https://api.openai.com/v1" |
| 446 | |
| 447 | super().__init__( |
| 448 | version=__version__, |
| 449 | base_url=base_url, |
| 450 | max_retries=max_retries, |
| 451 | timeout=timeout, |
| 452 | http_client=http_client, |
| 453 | custom_headers=default_headers, |
| 454 | custom_query=default_query, |
| 455 | _strict_response_validation=_strict_response_validation, |
| 456 | ) |
| 457 | |
| 458 | self._default_stream_cls = AsyncStream |
| 459 | |
| 460 | @cached_property |
| 461 | def completions(self) -> AsyncCompletions: |
| 462 | from .resources.completions import AsyncCompletions |
| 463 | |
| 464 | return AsyncCompletions(self) |
| 465 | |
| 466 | @cached_property |
| 467 | def chat(self) -> AsyncChat: |
| 468 | from .resources.chat import AsyncChat |
| 469 | |
| 470 | return AsyncChat(self) |
| 471 | |
| 472 | @cached_property |
| 473 | def embeddings(self) -> AsyncEmbeddings: |
| 474 | from .resources.embeddings import AsyncEmbeddings |
| 475 | |
| 476 | return AsyncEmbeddings(self) |
| 477 | |
| 478 | @cached_property |
| 479 | def files(self) -> AsyncFiles: |
| 480 | from .resources.files import AsyncFiles |
| 481 | |
| 482 | return AsyncFiles(self) |
| 483 | |
| 484 | @cached_property |
| 485 | def images(self) -> AsyncImages: |
| 486 | from .resources.images import AsyncImages |
| 487 | |
| 488 | return AsyncImages(self) |
| 489 | |
| 490 | @cached_property |
| 491 | def audio(self) -> AsyncAudio: |
| 492 | from .resources.audio import AsyncAudio |
| 493 | |
| 494 | return AsyncAudio(self) |
| 495 | |
| 496 | @cached_property |
| 497 | def moderations(self) -> AsyncModerations: |
| 498 | from .resources.moderations import AsyncModerations |
| 499 | |
| 500 | return AsyncModerations(self) |
| 501 | |
| 502 | @cached_property |
| 503 | def models(self) -> AsyncModels: |
| 504 | from .resources.models import AsyncModels |
| 505 | |
| 506 | return AsyncModels(self) |
| 507 | |
| 508 | @cached_property |
| 509 | def fine_tuning(self) -> AsyncFineTuning: |
| 510 | from .resources.fine_tuning import AsyncFineTuning |
| 511 | |
| 512 | return AsyncFineTuning(self) |
| 513 | |
| 514 | @cached_property |
| 515 | def vector_stores(self) -> AsyncVectorStores: |
| 516 | from .resources.vector_stores import AsyncVectorStores |
| 517 | |
| 518 | return AsyncVectorStores(self) |
| 519 | |
| 520 | @cached_property |
| 521 | def beta(self) -> AsyncBeta: |
| 522 | from .resources.beta import AsyncBeta |
| 523 | |
| 524 | return AsyncBeta(self) |
| 525 | |
| 526 | @cached_property |
| 527 | def batches(self) -> AsyncBatches: |
| 528 | from .resources.batches import AsyncBatches |
| 529 | |
| 530 | return AsyncBatches(self) |
| 531 | |
| 532 | @cached_property |
| 533 | def uploads(self) -> AsyncUploads: |
| 534 | from .resources.uploads import AsyncUploads |
| 535 | |
| 536 | return AsyncUploads(self) |
| 537 | |
| 538 | @cached_property |
| 539 | def responses(self) -> AsyncResponses: |
| 540 | from .resources.responses import AsyncResponses |
| 541 | |
| 542 | return AsyncResponses(self) |
| 543 | |
| 544 | @cached_property |
| 545 | def evals(self) -> AsyncEvals: |
| 546 | from .resources.evals import AsyncEvals |
| 547 | |
| 548 | return AsyncEvals(self) |
| 549 | |
| 550 | @cached_property |
| 551 | def containers(self) -> AsyncContainers: |
| 552 | from .resources.containers import AsyncContainers |
| 553 | |
| 554 | return AsyncContainers(self) |
| 555 | |
| 556 | @cached_property |
| 557 | def with_raw_response(self) -> AsyncOpenAIWithRawResponse: |
| 558 | return AsyncOpenAIWithRawResponse(self) |
| 559 | |
| 560 | @cached_property |
| 561 | def with_streaming_response(self) -> AsyncOpenAIWithStreamedResponse: |
| 562 | return AsyncOpenAIWithStreamedResponse(self) |
| 563 | |
| 564 | @property |
| 565 | @override |
| 566 | def qs(self) -> Querystring: |
| 567 | return Querystring(array_format="brackets") |
| 568 | |
| 569 | @property |
| 570 | @override |
| 571 | def auth_headers(self) -> dict[str, str]: |
| 572 | api_key = self.api_key |
| 573 | return {"Authorization": f"Bearer {api_key}"} |
| 574 | |
| 575 | @property |
| 576 | @override |
| 577 | def default_headers(self) -> dict[str, str | Omit]: |
| 578 | return { |
| 579 | **super().default_headers, |
| 580 | "X-Stainless-Async": f"async:{get_async_library()}", |
| 581 | "OpenAI-Organization": self.organization if self.organization is not None else Omit(), |
| 582 | "OpenAI-Project": self.project if self.project is not None else Omit(), |
| 583 | **self._custom_headers, |
| 584 | } |
| 585 | |
| 586 | def copy( |
| 587 | self, |
| 588 | *, |
| 589 | api_key: str | None = None, |
| 590 | organization: str | None = None, |
| 591 | project: str | None = None, |
| 592 | websocket_base_url: str | httpx.URL | None = None, |
| 593 | base_url: str | httpx.URL | None = None, |
| 594 | timeout: float | Timeout | None | NotGiven = NOT_GIVEN, |
| 595 | http_client: httpx.AsyncClient | None = None, |
| 596 | max_retries: int | NotGiven = NOT_GIVEN, |
| 597 | default_headers: Mapping[str, str] | None = None, |
| 598 | set_default_headers: Mapping[str, str] | None = None, |
| 599 | default_query: Mapping[str, object] | None = None, |
| 600 | set_default_query: Mapping[str, object] | None = None, |
| 601 | _extra_kwargs: Mapping[str, Any] = {}, |
| 602 | ) -> Self: |
| 603 | """ |
| 604 | Create a new client instance re-using the same options given to the current client with optional overriding. |
| 605 | """ |
| 606 | if default_headers is not None and set_default_headers is not None: |
| 607 | raise ValueError("The `default_headers` and `set_default_headers` arguments are mutually exclusive") |
| 608 | |
| 609 | if default_query is not None and set_default_query is not None: |
| 610 | raise ValueError("The `default_query` and `set_default_query` arguments are mutually exclusive") |
| 611 | |
| 612 | headers = self._custom_headers |
| 613 | if default_headers is not None: |
| 614 | headers = {**headers, **default_headers} |
| 615 | elif set_default_headers is not None: |
| 616 | headers = set_default_headers |
| 617 | |
| 618 | params = self._custom_query |
| 619 | if default_query is not None: |
| 620 | params = {**params, **default_query} |
| 621 | elif set_default_query is not None: |
| 622 | params = set_default_query |
| 623 | |
| 624 | http_client = http_client or self._client |
| 625 | return self.__class__( |
| 626 | api_key=api_key or self.api_key, |
| 627 | organization=organization or self.organization, |
| 628 | project=project or self.project, |
| 629 | websocket_base_url=websocket_base_url or self.websocket_base_url, |
| 630 | base_url=base_url or self.base_url, |
| 631 | timeout=self.timeout if isinstance(timeout, NotGiven) else timeout, |
| 632 | http_client=http_client, |
| 633 | max_retries=max_retries if is_given(max_retries) else self.max_retries, |
| 634 | default_headers=headers, |
| 635 | default_query=params, |
| 636 | **_extra_kwargs, |
| 637 | ) |
| 638 | |
| 639 | # Alias for `copy` for nicer inline usage, e.g. |
| 640 | # client.with_options(timeout=10).foo.create(...) |
| 641 | with_options = copy |
| 642 | |
| 643 | @override |
| 644 | def _make_status_error( |
| 645 | self, |
| 646 | err_msg: str, |
| 647 | *, |
| 648 | body: object, |
| 649 | response: httpx.Response, |
| 650 | ) -> APIStatusError: |
| 651 | data = body.get("error", body) if is_mapping(body) else body |
| 652 | if response.status_code == 400: |
| 653 | return _exceptions.BadRequestError(err_msg, response=response, body=data) |
| 654 | |
| 655 | if response.status_code == 401: |
| 656 | return _exceptions.AuthenticationError(err_msg, response=response, body=data) |
| 657 | |
| 658 | if response.status_code == 403: |
| 659 | return _exceptions.PermissionDeniedError(err_msg, response=response, body=data) |
| 660 | |
| 661 | if response.status_code == 404: |
| 662 | return _exceptions.NotFoundError(err_msg, response=response, body=data) |
| 663 | |
| 664 | if response.status_code == 409: |
| 665 | return _exceptions.ConflictError(err_msg, response=response, body=data) |
| 666 | |
| 667 | if response.status_code == 422: |
| 668 | return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data) |
| 669 | |
| 670 | if response.status_code == 429: |
| 671 | return _exceptions.RateLimitError(err_msg, response=response, body=data) |
| 672 | |
| 673 | if response.status_code >= 500: |
| 674 | return _exceptions.InternalServerError(err_msg, response=response, body=data) |
| 675 | return APIStatusError(err_msg, response=response, body=data) |
| 676 | |
| 677 | |
| 678 | class OpenAIWithRawResponse: |
| 679 | _client: OpenAI |
| 680 | |
| 681 | def __init__(self, client: OpenAI) -> None: |
| 682 | self._client = client |
| 683 | |
| 684 | @cached_property |
| 685 | def completions(self) -> completions.CompletionsWithRawResponse: |
| 686 | from .resources.completions import CompletionsWithRawResponse |
| 687 | |
| 688 | return CompletionsWithRawResponse(self._client.completions) |
| 689 | |
| 690 | @cached_property |
| 691 | def chat(self) -> chat.ChatWithRawResponse: |
| 692 | from .resources.chat import ChatWithRawResponse |
| 693 | |
| 694 | return ChatWithRawResponse(self._client.chat) |
| 695 | |
| 696 | @cached_property |
| 697 | def embeddings(self) -> embeddings.EmbeddingsWithRawResponse: |
| 698 | from .resources.embeddings import EmbeddingsWithRawResponse |
| 699 | |
| 700 | return EmbeddingsWithRawResponse(self._client.embeddings) |
| 701 | |
| 702 | @cached_property |
| 703 | def files(self) -> files.FilesWithRawResponse: |
| 704 | from .resources.files import FilesWithRawResponse |
| 705 | |
| 706 | return FilesWithRawResponse(self._client.files) |
| 707 | |
| 708 | @cached_property |
| 709 | def images(self) -> images.ImagesWithRawResponse: |
| 710 | from .resources.images import ImagesWithRawResponse |
| 711 | |
| 712 | return ImagesWithRawResponse(self._client.images) |
| 713 | |
| 714 | @cached_property |
| 715 | def audio(self) -> audio.AudioWithRawResponse: |
| 716 | from .resources.audio import AudioWithRawResponse |
| 717 | |
| 718 | return AudioWithRawResponse(self._client.audio) |
| 719 | |
| 720 | @cached_property |
| 721 | def moderations(self) -> moderations.ModerationsWithRawResponse: |
| 722 | from .resources.moderations import ModerationsWithRawResponse |
| 723 | |
| 724 | return ModerationsWithRawResponse(self._client.moderations) |
| 725 | |
| 726 | @cached_property |
| 727 | def models(self) -> models.ModelsWithRawResponse: |
| 728 | from .resources.models import ModelsWithRawResponse |
| 729 | |
| 730 | return ModelsWithRawResponse(self._client.models) |
| 731 | |
| 732 | @cached_property |
| 733 | def fine_tuning(self) -> fine_tuning.FineTuningWithRawResponse: |
| 734 | from .resources.fine_tuning import FineTuningWithRawResponse |
| 735 | |
| 736 | return FineTuningWithRawResponse(self._client.fine_tuning) |
| 737 | |
| 738 | @cached_property |
| 739 | def vector_stores(self) -> vector_stores.VectorStoresWithRawResponse: |
| 740 | from .resources.vector_stores import VectorStoresWithRawResponse |
| 741 | |
| 742 | return VectorStoresWithRawResponse(self._client.vector_stores) |
| 743 | |
| 744 | @cached_property |
| 745 | def beta(self) -> beta.BetaWithRawResponse: |
| 746 | from .resources.beta import BetaWithRawResponse |
| 747 | |
| 748 | return BetaWithRawResponse(self._client.beta) |
| 749 | |
| 750 | @cached_property |
| 751 | def batches(self) -> batches.BatchesWithRawResponse: |
| 752 | from .resources.batches import BatchesWithRawResponse |
| 753 | |
| 754 | return BatchesWithRawResponse(self._client.batches) |
| 755 | |
| 756 | @cached_property |
| 757 | def uploads(self) -> uploads.UploadsWithRawResponse: |
| 758 | from .resources.uploads import UploadsWithRawResponse |
| 759 | |
| 760 | return UploadsWithRawResponse(self._client.uploads) |
| 761 | |
| 762 | @cached_property |
| 763 | def responses(self) -> responses.ResponsesWithRawResponse: |
| 764 | from .resources.responses import ResponsesWithRawResponse |
| 765 | |
| 766 | return ResponsesWithRawResponse(self._client.responses) |
| 767 | |
| 768 | @cached_property |
| 769 | def evals(self) -> evals.EvalsWithRawResponse: |
| 770 | from .resources.evals import EvalsWithRawResponse |
| 771 | |
| 772 | return EvalsWithRawResponse(self._client.evals) |
| 773 | |
| 774 | @cached_property |
| 775 | def containers(self) -> containers.ContainersWithRawResponse: |
| 776 | from .resources.containers import ContainersWithRawResponse |
| 777 | |
| 778 | return ContainersWithRawResponse(self._client.containers) |
| 779 | |
| 780 | |
| 781 | class AsyncOpenAIWithRawResponse: |
| 782 | _client: AsyncOpenAI |
| 783 | |
| 784 | def __init__(self, client: AsyncOpenAI) -> None: |
| 785 | self._client = client |
| 786 | |
| 787 | @cached_property |
| 788 | def completions(self) -> completions.AsyncCompletionsWithRawResponse: |
| 789 | from .resources.completions import AsyncCompletionsWithRawResponse |
| 790 | |
| 791 | return AsyncCompletionsWithRawResponse(self._client.completions) |
| 792 | |
| 793 | @cached_property |
| 794 | def chat(self) -> chat.AsyncChatWithRawResponse: |
| 795 | from .resources.chat import AsyncChatWithRawResponse |
| 796 | |
| 797 | return AsyncChatWithRawResponse(self._client.chat) |
| 798 | |
| 799 | @cached_property |
| 800 | def embeddings(self) -> embeddings.AsyncEmbeddingsWithRawResponse: |
| 801 | from .resources.embeddings import AsyncEmbeddingsWithRawResponse |
| 802 | |
| 803 | return AsyncEmbeddingsWithRawResponse(self._client.embeddings) |
| 804 | |
| 805 | @cached_property |
| 806 | def files(self) -> files.AsyncFilesWithRawResponse: |
| 807 | from .resources.files import AsyncFilesWithRawResponse |
| 808 | |
| 809 | return AsyncFilesWithRawResponse(self._client.files) |
| 810 | |
| 811 | @cached_property |
| 812 | def images(self) -> images.AsyncImagesWithRawResponse: |
| 813 | from .resources.images import AsyncImagesWithRawResponse |
| 814 | |
| 815 | return AsyncImagesWithRawResponse(self._client.images) |
| 816 | |
| 817 | @cached_property |
| 818 | def audio(self) -> audio.AsyncAudioWithRawResponse: |
| 819 | from .resources.audio import AsyncAudioWithRawResponse |
| 820 | |
| 821 | return AsyncAudioWithRawResponse(self._client.audio) |
| 822 | |
| 823 | @cached_property |
| 824 | def moderations(self) -> moderations.AsyncModerationsWithRawResponse: |
| 825 | from .resources.moderations import AsyncModerationsWithRawResponse |
| 826 | |
| 827 | return AsyncModerationsWithRawResponse(self._client.moderations) |
| 828 | |
| 829 | @cached_property |
| 830 | def models(self) -> models.AsyncModelsWithRawResponse: |
| 831 | from .resources.models import AsyncModelsWithRawResponse |
| 832 | |
| 833 | return AsyncModelsWithRawResponse(self._client.models) |
| 834 | |
| 835 | @cached_property |
| 836 | def fine_tuning(self) -> fine_tuning.AsyncFineTuningWithRawResponse: |
| 837 | from .resources.fine_tuning import AsyncFineTuningWithRawResponse |
| 838 | |
| 839 | return AsyncFineTuningWithRawResponse(self._client.fine_tuning) |
| 840 | |
| 841 | @cached_property |
| 842 | def vector_stores(self) -> vector_stores.AsyncVectorStoresWithRawResponse: |
| 843 | from .resources.vector_stores import AsyncVectorStoresWithRawResponse |
| 844 | |
| 845 | return AsyncVectorStoresWithRawResponse(self._client.vector_stores) |
| 846 | |
| 847 | @cached_property |
| 848 | def beta(self) -> beta.AsyncBetaWithRawResponse: |
| 849 | from .resources.beta import AsyncBetaWithRawResponse |
| 850 | |
| 851 | return AsyncBetaWithRawResponse(self._client.beta) |
| 852 | |
| 853 | @cached_property |
| 854 | def batches(self) -> batches.AsyncBatchesWithRawResponse: |
| 855 | from .resources.batches import AsyncBatchesWithRawResponse |
| 856 | |
| 857 | return AsyncBatchesWithRawResponse(self._client.batches) |
| 858 | |
| 859 | @cached_property |
| 860 | def uploads(self) -> uploads.AsyncUploadsWithRawResponse: |
| 861 | from .resources.uploads import AsyncUploadsWithRawResponse |
| 862 | |
| 863 | return AsyncUploadsWithRawResponse(self._client.uploads) |
| 864 | |
| 865 | @cached_property |
| 866 | def responses(self) -> responses.AsyncResponsesWithRawResponse: |
| 867 | from .resources.responses import AsyncResponsesWithRawResponse |
| 868 | |
| 869 | return AsyncResponsesWithRawResponse(self._client.responses) |
| 870 | |
| 871 | @cached_property |
| 872 | def evals(self) -> evals.AsyncEvalsWithRawResponse: |
| 873 | from .resources.evals import AsyncEvalsWithRawResponse |
| 874 | |
| 875 | return AsyncEvalsWithRawResponse(self._client.evals) |
| 876 | |
| 877 | @cached_property |
| 878 | def containers(self) -> containers.AsyncContainersWithRawResponse: |
| 879 | from .resources.containers import AsyncContainersWithRawResponse |
| 880 | |
| 881 | return AsyncContainersWithRawResponse(self._client.containers) |
| 882 | |
| 883 | |
| 884 | class OpenAIWithStreamedResponse: |
| 885 | _client: OpenAI |
| 886 | |
| 887 | def __init__(self, client: OpenAI) -> None: |
| 888 | self._client = client |
| 889 | |
| 890 | @cached_property |
| 891 | def completions(self) -> completions.CompletionsWithStreamingResponse: |
| 892 | from .resources.completions import CompletionsWithStreamingResponse |
| 893 | |
| 894 | return CompletionsWithStreamingResponse(self._client.completions) |
| 895 | |
| 896 | @cached_property |
| 897 | def chat(self) -> chat.ChatWithStreamingResponse: |
| 898 | from .resources.chat import ChatWithStreamingResponse |
| 899 | |
| 900 | return ChatWithStreamingResponse(self._client.chat) |
| 901 | |
| 902 | @cached_property |
| 903 | def embeddings(self) -> embeddings.EmbeddingsWithStreamingResponse: |
| 904 | from .resources.embeddings import EmbeddingsWithStreamingResponse |
| 905 | |
| 906 | return EmbeddingsWithStreamingResponse(self._client.embeddings) |
| 907 | |
| 908 | @cached_property |
| 909 | def files(self) -> files.FilesWithStreamingResponse: |
| 910 | from .resources.files import FilesWithStreamingResponse |
| 911 | |
| 912 | return FilesWithStreamingResponse(self._client.files) |
| 913 | |
| 914 | @cached_property |
| 915 | def images(self) -> images.ImagesWithStreamingResponse: |
| 916 | from .resources.images import ImagesWithStreamingResponse |
| 917 | |
| 918 | return ImagesWithStreamingResponse(self._client.images) |
| 919 | |
| 920 | @cached_property |
| 921 | def audio(self) -> audio.AudioWithStreamingResponse: |
| 922 | from .resources.audio import AudioWithStreamingResponse |
| 923 | |
| 924 | return AudioWithStreamingResponse(self._client.audio) |
| 925 | |
| 926 | @cached_property |
| 927 | def moderations(self) -> moderations.ModerationsWithStreamingResponse: |
| 928 | from .resources.moderations import ModerationsWithStreamingResponse |
| 929 | |
| 930 | return ModerationsWithStreamingResponse(self._client.moderations) |
| 931 | |
| 932 | @cached_property |
| 933 | def models(self) -> models.ModelsWithStreamingResponse: |
| 934 | from .resources.models import ModelsWithStreamingResponse |
| 935 | |
| 936 | return ModelsWithStreamingResponse(self._client.models) |
| 937 | |
| 938 | @cached_property |
| 939 | def fine_tuning(self) -> fine_tuning.FineTuningWithStreamingResponse: |
| 940 | from .resources.fine_tuning import FineTuningWithStreamingResponse |
| 941 | |
| 942 | return FineTuningWithStreamingResponse(self._client.fine_tuning) |
| 943 | |
| 944 | @cached_property |
| 945 | def vector_stores(self) -> vector_stores.VectorStoresWithStreamingResponse: |
| 946 | from .resources.vector_stores import VectorStoresWithStreamingResponse |
| 947 | |
| 948 | return VectorStoresWithStreamingResponse(self._client.vector_stores) |
| 949 | |
| 950 | @cached_property |
| 951 | def beta(self) -> beta.BetaWithStreamingResponse: |
| 952 | from .resources.beta import BetaWithStreamingResponse |
| 953 | |
| 954 | return BetaWithStreamingResponse(self._client.beta) |
| 955 | |
| 956 | @cached_property |
| 957 | def batches(self) -> batches.BatchesWithStreamingResponse: |
| 958 | from .resources.batches import BatchesWithStreamingResponse |
| 959 | |
| 960 | return BatchesWithStreamingResponse(self._client.batches) |
| 961 | |
| 962 | @cached_property |
| 963 | def uploads(self) -> uploads.UploadsWithStreamingResponse: |
| 964 | from .resources.uploads import UploadsWithStreamingResponse |
| 965 | |
| 966 | return UploadsWithStreamingResponse(self._client.uploads) |
| 967 | |
| 968 | @cached_property |
| 969 | def responses(self) -> responses.ResponsesWithStreamingResponse: |
| 970 | from .resources.responses import ResponsesWithStreamingResponse |
| 971 | |
| 972 | return ResponsesWithStreamingResponse(self._client.responses) |
| 973 | |
| 974 | @cached_property |
| 975 | def evals(self) -> evals.EvalsWithStreamingResponse: |
| 976 | from .resources.evals import EvalsWithStreamingResponse |
| 977 | |
| 978 | return EvalsWithStreamingResponse(self._client.evals) |
| 979 | |
| 980 | @cached_property |
| 981 | def containers(self) -> containers.ContainersWithStreamingResponse: |
| 982 | from .resources.containers import ContainersWithStreamingResponse |
| 983 | |
| 984 | return ContainersWithStreamingResponse(self._client.containers) |
| 985 | |
| 986 | |
| 987 | class AsyncOpenAIWithStreamedResponse: |
| 988 | _client: AsyncOpenAI |
| 989 | |
| 990 | def __init__(self, client: AsyncOpenAI) -> None: |
| 991 | self._client = client |
| 992 | |
| 993 | @cached_property |
| 994 | def completions(self) -> completions.AsyncCompletionsWithStreamingResponse: |
| 995 | from .resources.completions import AsyncCompletionsWithStreamingResponse |
| 996 | |
| 997 | return AsyncCompletionsWithStreamingResponse(self._client.completions) |
| 998 | |
| 999 | @cached_property |
| 1000 | def chat(self) -> chat.AsyncChatWithStreamingResponse: |
| 1001 | from .resources.chat import AsyncChatWithStreamingResponse |
| 1002 | |
| 1003 | return AsyncChatWithStreamingResponse(self._client.chat) |
| 1004 | |
| 1005 | @cached_property |
| 1006 | def embeddings(self) -> embeddings.AsyncEmbeddingsWithStreamingResponse: |
| 1007 | from .resources.embeddings import AsyncEmbeddingsWithStreamingResponse |
| 1008 | |
| 1009 | return AsyncEmbeddingsWithStreamingResponse(self._client.embeddings) |
| 1010 | |
| 1011 | @cached_property |
| 1012 | def files(self) -> files.AsyncFilesWithStreamingResponse: |
| 1013 | from .resources.files import AsyncFilesWithStreamingResponse |
| 1014 | |
| 1015 | return AsyncFilesWithStreamingResponse(self._client.files) |
| 1016 | |
| 1017 | @cached_property |
| 1018 | def images(self) -> images.AsyncImagesWithStreamingResponse: |
| 1019 | from .resources.images import AsyncImagesWithStreamingResponse |
| 1020 | |
| 1021 | return AsyncImagesWithStreamingResponse(self._client.images) |
| 1022 | |
| 1023 | @cached_property |
| 1024 | def audio(self) -> audio.AsyncAudioWithStreamingResponse: |
| 1025 | from .resources.audio import AsyncAudioWithStreamingResponse |
| 1026 | |
| 1027 | return AsyncAudioWithStreamingResponse(self._client.audio) |
| 1028 | |
| 1029 | @cached_property |
| 1030 | def moderations(self) -> moderations.AsyncModerationsWithStreamingResponse: |
| 1031 | from .resources.moderations import AsyncModerationsWithStreamingResponse |
| 1032 | |
| 1033 | return AsyncModerationsWithStreamingResponse(self._client.moderations) |
| 1034 | |
| 1035 | @cached_property |
| 1036 | def models(self) -> models.AsyncModelsWithStreamingResponse: |
| 1037 | from .resources.models import AsyncModelsWithStreamingResponse |
| 1038 | |
| 1039 | return AsyncModelsWithStreamingResponse(self._client.models) |
| 1040 | |
| 1041 | @cached_property |
| 1042 | def fine_tuning(self) -> fine_tuning.AsyncFineTuningWithStreamingResponse: |
| 1043 | from .resources.fine_tuning import AsyncFineTuningWithStreamingResponse |
| 1044 | |
| 1045 | return AsyncFineTuningWithStreamingResponse(self._client.fine_tuning) |
| 1046 | |
| 1047 | @cached_property |
| 1048 | def vector_stores(self) -> vector_stores.AsyncVectorStoresWithStreamingResponse: |
| 1049 | from .resources.vector_stores import AsyncVectorStoresWithStreamingResponse |
| 1050 | |
| 1051 | return AsyncVectorStoresWithStreamingResponse(self._client.vector_stores) |
| 1052 | |
| 1053 | @cached_property |
| 1054 | def beta(self) -> beta.AsyncBetaWithStreamingResponse: |
| 1055 | from .resources.beta import AsyncBetaWithStreamingResponse |
| 1056 | |
| 1057 | return AsyncBetaWithStreamingResponse(self._client.beta) |
| 1058 | |
| 1059 | @cached_property |
| 1060 | def batches(self) -> batches.AsyncBatchesWithStreamingResponse: |
| 1061 | from .resources.batches import AsyncBatchesWithStreamingResponse |
| 1062 | |
| 1063 | return AsyncBatchesWithStreamingResponse(self._client.batches) |
| 1064 | |
| 1065 | @cached_property |
| 1066 | def uploads(self) -> uploads.AsyncUploadsWithStreamingResponse: |
| 1067 | from .resources.uploads import AsyncUploadsWithStreamingResponse |
| 1068 | |
| 1069 | return AsyncUploadsWithStreamingResponse(self._client.uploads) |
| 1070 | |
| 1071 | @cached_property |
| 1072 | def responses(self) -> responses.AsyncResponsesWithStreamingResponse: |
| 1073 | from .resources.responses import AsyncResponsesWithStreamingResponse |
| 1074 | |
| 1075 | return AsyncResponsesWithStreamingResponse(self._client.responses) |
| 1076 | |
| 1077 | @cached_property |
| 1078 | def evals(self) -> evals.AsyncEvalsWithStreamingResponse: |
| 1079 | from .resources.evals import AsyncEvalsWithStreamingResponse |
| 1080 | |
| 1081 | return AsyncEvalsWithStreamingResponse(self._client.evals) |
| 1082 | |
| 1083 | @cached_property |
| 1084 | def containers(self) -> containers.AsyncContainersWithStreamingResponse: |
| 1085 | from .resources.containers import AsyncContainersWithStreamingResponse |
| 1086 | |
| 1087 | return AsyncContainersWithStreamingResponse(self._client.containers) |
| 1088 | |
| 1089 | |
| 1090 | Client = OpenAI |
| 1091 | |
| 1092 | AsyncClient = AsyncOpenAI |
| 1093 | |