openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
src/openai/_client.py
1500lines · 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, 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 | Omit, |
| 15 | Headers, |
| 16 | Timeout, |
| 17 | NotGiven, |
| 18 | Transport, |
| 19 | ProxiesTypes, |
| 20 | RequestOptions, |
| 21 | not_given, |
| 22 | ) |
| 23 | from ._utils import ( |
| 24 | is_given, |
| 25 | is_mapping, |
| 26 | is_mapping_t, |
| 27 | get_async_library, |
| 28 | ) |
| 29 | from ._compat import cached_property |
| 30 | from ._models import SecurityOptions |
| 31 | from ._version import __version__ |
| 32 | from ._streaming import Stream as Stream, AsyncStream as AsyncStream |
| 33 | from ._exceptions import APIStatusError |
| 34 | from ._base_client import ( |
| 35 | DEFAULT_MAX_RETRIES, |
| 36 | SyncAPIClient, |
| 37 | AsyncAPIClient, |
| 38 | ) |
| 39 | |
| 40 | if TYPE_CHECKING: |
| 41 | from .resources import ( |
| 42 | beta, |
| 43 | chat, |
| 44 | admin, |
| 45 | audio, |
| 46 | evals, |
| 47 | files, |
| 48 | images, |
| 49 | models, |
| 50 | skills, |
| 51 | videos, |
| 52 | batches, |
| 53 | uploads, |
| 54 | realtime, |
| 55 | responses, |
| 56 | containers, |
| 57 | embeddings, |
| 58 | completions, |
| 59 | fine_tuning, |
| 60 | moderations, |
| 61 | conversations, |
| 62 | vector_stores, |
| 63 | ) |
| 64 | from .resources.files import Files, AsyncFiles |
| 65 | from .resources.images import Images, AsyncImages |
| 66 | from .resources.models import Models, AsyncModels |
| 67 | from .resources.videos import Videos, AsyncVideos |
| 68 | from .resources.batches import Batches, AsyncBatches |
| 69 | from .resources.beta.beta import Beta, AsyncBeta |
| 70 | from .resources.chat.chat import Chat, AsyncChat |
| 71 | from .resources.embeddings import Embeddings, AsyncEmbeddings |
| 72 | from .resources.admin.admin import Admin, AsyncAdmin |
| 73 | from .resources.audio.audio import Audio, AsyncAudio |
| 74 | from .resources.completions import Completions, AsyncCompletions |
| 75 | from .resources.evals.evals import Evals, AsyncEvals |
| 76 | from .resources.moderations import Moderations, AsyncModerations |
| 77 | from .resources.skills.skills import Skills, AsyncSkills |
| 78 | from .resources.uploads.uploads import Uploads, AsyncUploads |
| 79 | from .resources.realtime.realtime import Realtime, AsyncRealtime |
| 80 | from .resources.webhooks.webhooks import Webhooks, AsyncWebhooks |
| 81 | from .resources.responses.responses import Responses, AsyncResponses |
| 82 | from .resources.containers.containers import Containers, AsyncContainers |
| 83 | from .resources.fine_tuning.fine_tuning import FineTuning, AsyncFineTuning |
| 84 | from .resources.conversations.conversations import Conversations, AsyncConversations |
| 85 | from .resources.vector_stores.vector_stores import VectorStores, AsyncVectorStores |
| 86 | |
| 87 | __all__ = ["Timeout", "Transport", "ProxiesTypes", "RequestOptions", "OpenAI", "AsyncOpenAI", "Client", "AsyncClient"] |
| 88 | |
| 89 | |
| 90 | class OpenAI(SyncAPIClient): |
| 91 | # client options |
| 92 | api_key: str | None |
| 93 | admin_api_key: str | None |
| 94 | organization: str | None |
| 95 | project: str | None |
| 96 | webhook_secret: str | None |
| 97 | |
| 98 | websocket_base_url: str | httpx.URL | None |
| 99 | """Base URL for WebSocket connections. |
| 100 | |
| 101 | If not specified, the default base URL will be used, with 'wss://' replacing the |
| 102 | 'http://' or 'https://' scheme. For example: 'http://example.com' becomes |
| 103 | 'wss://example.com' |
| 104 | """ |
| 105 | |
| 106 | def __init__( |
| 107 | self, |
| 108 | *, |
| 109 | api_key: str | None = None, |
| 110 | admin_api_key: str | None = None, |
| 111 | organization: str | None = None, |
| 112 | project: str | None = None, |
| 113 | webhook_secret: str | None = None, |
| 114 | base_url: str | httpx.URL | None = None, |
| 115 | websocket_base_url: str | httpx.URL | None = None, |
| 116 | timeout: float | Timeout | None | NotGiven = not_given, |
| 117 | max_retries: int = DEFAULT_MAX_RETRIES, |
| 118 | default_headers: Mapping[str, str] | None = None, |
| 119 | default_query: Mapping[str, object] | None = None, |
| 120 | # Configure a custom httpx client. |
| 121 | # We provide a `DefaultHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`. |
| 122 | # See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details. |
| 123 | http_client: httpx.Client | None = None, |
| 124 | # Enable or disable schema validation for data returned by the API. |
| 125 | # When enabled an error APIResponseValidationError is raised |
| 126 | # if the API responds with invalid data for the expected schema. |
| 127 | # |
| 128 | # This parameter may be removed or changed in the future. |
| 129 | # If you rely on this feature, please open a GitHub issue |
| 130 | # outlining your use-case to help us decide if it should be |
| 131 | # part of our public interface in the future. |
| 132 | _strict_response_validation: bool = False, |
| 133 | ) -> None: |
| 134 | """Construct a new synchronous OpenAI client instance. |
| 135 | |
| 136 | This automatically infers the following arguments from their corresponding environment variables if they are not provided: |
| 137 | - `api_key` from `OPENAI_API_KEY` |
| 138 | - `admin_api_key` from `OPENAI_ADMIN_KEY` |
| 139 | - `organization` from `OPENAI_ORG_ID` |
| 140 | - `project` from `OPENAI_PROJECT_ID` |
| 141 | - `webhook_secret` from `OPENAI_WEBHOOK_SECRET` |
| 142 | """ |
| 143 | if api_key is None: |
| 144 | api_key = os.environ.get("OPENAI_API_KEY") |
| 145 | self.api_key = api_key |
| 146 | |
| 147 | if admin_api_key is None: |
| 148 | admin_api_key = os.environ.get("OPENAI_ADMIN_KEY") |
| 149 | self.admin_api_key = admin_api_key |
| 150 | |
| 151 | if organization is None: |
| 152 | organization = os.environ.get("OPENAI_ORG_ID") |
| 153 | self.organization = organization |
| 154 | |
| 155 | if project is None: |
| 156 | project = os.environ.get("OPENAI_PROJECT_ID") |
| 157 | self.project = project |
| 158 | |
| 159 | if webhook_secret is None: |
| 160 | webhook_secret = os.environ.get("OPENAI_WEBHOOK_SECRET") |
| 161 | self.webhook_secret = webhook_secret |
| 162 | |
| 163 | self.websocket_base_url = websocket_base_url |
| 164 | |
| 165 | if base_url is None: |
| 166 | base_url = os.environ.get("OPENAI_BASE_URL") |
| 167 | if base_url is None: |
| 168 | base_url = f"https://api.openai.com/v1" |
| 169 | |
| 170 | custom_headers_env = os.environ.get("OPENAI_CUSTOM_HEADERS") |
| 171 | if custom_headers_env is not None: |
| 172 | parsed: dict[str, str] = {} |
| 173 | for line in custom_headers_env.split("\n"): |
| 174 | colon = line.find(":") |
| 175 | if colon >= 0: |
| 176 | parsed[line[:colon].strip()] = line[colon + 1 :].strip() |
| 177 | default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})} |
| 178 | |
| 179 | super().__init__( |
| 180 | version=__version__, |
| 181 | base_url=base_url, |
| 182 | max_retries=max_retries, |
| 183 | timeout=timeout, |
| 184 | http_client=http_client, |
| 185 | custom_headers=default_headers, |
| 186 | custom_query=default_query, |
| 187 | _strict_response_validation=_strict_response_validation, |
| 188 | ) |
| 189 | |
| 190 | self._default_stream_cls = Stream |
| 191 | |
| 192 | @cached_property |
| 193 | def completions(self) -> Completions: |
| 194 | """ |
| 195 | Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. |
| 196 | """ |
| 197 | from .resources.completions import Completions |
| 198 | |
| 199 | return Completions(self) |
| 200 | |
| 201 | @cached_property |
| 202 | def chat(self) -> Chat: |
| 203 | from .resources.chat import Chat |
| 204 | |
| 205 | return Chat(self) |
| 206 | |
| 207 | @cached_property |
| 208 | def embeddings(self) -> Embeddings: |
| 209 | """ |
| 210 | Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. |
| 211 | """ |
| 212 | from .resources.embeddings import Embeddings |
| 213 | |
| 214 | return Embeddings(self) |
| 215 | |
| 216 | @cached_property |
| 217 | def files(self) -> Files: |
| 218 | """ |
| 219 | Files are used to upload documents that can be used with features like Assistants and Fine-tuning. |
| 220 | """ |
| 221 | from .resources.files import Files |
| 222 | |
| 223 | return Files(self) |
| 224 | |
| 225 | @cached_property |
| 226 | def images(self) -> Images: |
| 227 | """Given a prompt and/or an input image, the model will generate a new image.""" |
| 228 | from .resources.images import Images |
| 229 | |
| 230 | return Images(self) |
| 231 | |
| 232 | @cached_property |
| 233 | def audio(self) -> Audio: |
| 234 | from .resources.audio import Audio |
| 235 | |
| 236 | return Audio(self) |
| 237 | |
| 238 | @cached_property |
| 239 | def moderations(self) -> Moderations: |
| 240 | """ |
| 241 | Given text and/or image inputs, classifies if those inputs are potentially harmful. |
| 242 | """ |
| 243 | from .resources.moderations import Moderations |
| 244 | |
| 245 | return Moderations(self) |
| 246 | |
| 247 | @cached_property |
| 248 | def models(self) -> Models: |
| 249 | """List and describe the various models available in the API.""" |
| 250 | from .resources.models import Models |
| 251 | |
| 252 | return Models(self) |
| 253 | |
| 254 | @cached_property |
| 255 | def fine_tuning(self) -> FineTuning: |
| 256 | from .resources.fine_tuning import FineTuning |
| 257 | |
| 258 | return FineTuning(self) |
| 259 | |
| 260 | @cached_property |
| 261 | def vector_stores(self) -> VectorStores: |
| 262 | from .resources.vector_stores import VectorStores |
| 263 | |
| 264 | return VectorStores(self) |
| 265 | |
| 266 | @cached_property |
| 267 | def webhooks(self) -> Webhooks: |
| 268 | from .resources.webhooks import Webhooks |
| 269 | |
| 270 | return Webhooks(self) |
| 271 | |
| 272 | @cached_property |
| 273 | def beta(self) -> Beta: |
| 274 | from .resources.beta import Beta |
| 275 | |
| 276 | return Beta(self) |
| 277 | |
| 278 | @cached_property |
| 279 | def batches(self) -> Batches: |
| 280 | """Create large batches of API requests to run asynchronously.""" |
| 281 | from .resources.batches import Batches |
| 282 | |
| 283 | return Batches(self) |
| 284 | |
| 285 | @cached_property |
| 286 | def uploads(self) -> Uploads: |
| 287 | """Use Uploads to upload large files in multiple parts.""" |
| 288 | from .resources.uploads import Uploads |
| 289 | |
| 290 | return Uploads(self) |
| 291 | |
| 292 | @cached_property |
| 293 | def admin(self) -> Admin: |
| 294 | from .resources.admin import Admin |
| 295 | |
| 296 | return Admin(self) |
| 297 | |
| 298 | @cached_property |
| 299 | def responses(self) -> Responses: |
| 300 | from .resources.responses import Responses |
| 301 | |
| 302 | return Responses(self) |
| 303 | |
| 304 | @cached_property |
| 305 | def realtime(self) -> Realtime: |
| 306 | from .resources.realtime import Realtime |
| 307 | |
| 308 | return Realtime(self) |
| 309 | |
| 310 | @cached_property |
| 311 | def conversations(self) -> Conversations: |
| 312 | """Manage conversations and conversation items.""" |
| 313 | from .resources.conversations import Conversations |
| 314 | |
| 315 | return Conversations(self) |
| 316 | |
| 317 | @cached_property |
| 318 | def evals(self) -> Evals: |
| 319 | """Manage and run evals in the OpenAI platform.""" |
| 320 | from .resources.evals import Evals |
| 321 | |
| 322 | return Evals(self) |
| 323 | |
| 324 | @cached_property |
| 325 | def containers(self) -> Containers: |
| 326 | from .resources.containers import Containers |
| 327 | |
| 328 | return Containers(self) |
| 329 | |
| 330 | @cached_property |
| 331 | def skills(self) -> Skills: |
| 332 | from .resources.skills import Skills |
| 333 | |
| 334 | return Skills(self) |
| 335 | |
| 336 | @cached_property |
| 337 | def videos(self) -> Videos: |
| 338 | from .resources.videos import Videos |
| 339 | |
| 340 | return Videos(self) |
| 341 | |
| 342 | @cached_property |
| 343 | def with_raw_response(self) -> OpenAIWithRawResponse: |
| 344 | return OpenAIWithRawResponse(self) |
| 345 | |
| 346 | @cached_property |
| 347 | def with_streaming_response(self) -> OpenAIWithStreamedResponse: |
| 348 | return OpenAIWithStreamedResponse(self) |
| 349 | |
| 350 | @property |
| 351 | @override |
| 352 | def qs(self) -> Querystring: |
| 353 | return Querystring(array_format="brackets") |
| 354 | |
| 355 | @override |
| 356 | def _auth_headers(self, security: SecurityOptions) -> dict[str, str]: |
| 357 | return { |
| 358 | **(self._bearer_auth if security.get("bearer_auth", False) else {}), |
| 359 | **(self._admin_api_key_auth if security.get("admin_api_key_auth", False) else {}), |
| 360 | } |
| 361 | |
| 362 | @property |
| 363 | def _bearer_auth(self) -> dict[str, str]: |
| 364 | api_key = self.api_key |
| 365 | if api_key is None: |
| 366 | return {} |
| 367 | return {"Authorization": f"Bearer {api_key}"} |
| 368 | |
| 369 | @property |
| 370 | def _admin_api_key_auth(self) -> dict[str, str]: |
| 371 | admin_api_key = self.admin_api_key |
| 372 | if admin_api_key is None: |
| 373 | return {} |
| 374 | return {"Authorization": f"Bearer {admin_api_key}"} |
| 375 | |
| 376 | @property |
| 377 | @override |
| 378 | def default_headers(self) -> dict[str, str | Omit]: |
| 379 | return { |
| 380 | **super().default_headers, |
| 381 | "X-Stainless-Async": "false", |
| 382 | "OpenAI-Organization": self.organization if self.organization is not None else Omit(), |
| 383 | "OpenAI-Project": self.project if self.project is not None else Omit(), |
| 384 | **self._custom_headers, |
| 385 | } |
| 386 | |
| 387 | @override |
| 388 | def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None: |
| 389 | if headers.get("Authorization") or isinstance(custom_headers.get("Authorization"), Omit): |
| 390 | return |
| 391 | |
| 392 | raise TypeError( |
| 393 | '"Could not resolve authentication method. Expected either api_key or admin_api_key to be set. Or for one of the `Authorization` or `Authorization` headers to be explicitly omitted"' |
| 394 | ) |
| 395 | |
| 396 | def copy( |
| 397 | self, |
| 398 | *, |
| 399 | api_key: str | None = None, |
| 400 | admin_api_key: str | None = None, |
| 401 | organization: str | None = None, |
| 402 | project: str | None = None, |
| 403 | webhook_secret: str | None = None, |
| 404 | websocket_base_url: str | httpx.URL | None = None, |
| 405 | base_url: str | httpx.URL | None = None, |
| 406 | timeout: float | Timeout | None | NotGiven = not_given, |
| 407 | http_client: httpx.Client | None = None, |
| 408 | max_retries: int | NotGiven = not_given, |
| 409 | default_headers: Mapping[str, str] | None = None, |
| 410 | set_default_headers: Mapping[str, str] | None = None, |
| 411 | default_query: Mapping[str, object] | None = None, |
| 412 | set_default_query: Mapping[str, object] | None = None, |
| 413 | _extra_kwargs: Mapping[str, Any] = {}, |
| 414 | ) -> Self: |
| 415 | """ |
| 416 | Create a new client instance re-using the same options given to the current client with optional overriding. |
| 417 | """ |
| 418 | if default_headers is not None and set_default_headers is not None: |
| 419 | raise ValueError("The `default_headers` and `set_default_headers` arguments are mutually exclusive") |
| 420 | |
| 421 | if default_query is not None and set_default_query is not None: |
| 422 | raise ValueError("The `default_query` and `set_default_query` arguments are mutually exclusive") |
| 423 | |
| 424 | headers = self._custom_headers |
| 425 | if default_headers is not None: |
| 426 | headers = {**headers, **default_headers} |
| 427 | elif set_default_headers is not None: |
| 428 | headers = set_default_headers |
| 429 | |
| 430 | params = self._custom_query |
| 431 | if default_query is not None: |
| 432 | params = {**params, **default_query} |
| 433 | elif set_default_query is not None: |
| 434 | params = set_default_query |
| 435 | |
| 436 | http_client = http_client or self._client |
| 437 | return self.__class__( |
| 438 | api_key=api_key or self.api_key, |
| 439 | admin_api_key=admin_api_key or self.admin_api_key, |
| 440 | organization=organization or self.organization, |
| 441 | project=project or self.project, |
| 442 | webhook_secret=webhook_secret or self.webhook_secret, |
| 443 | websocket_base_url=websocket_base_url or self.websocket_base_url, |
| 444 | base_url=base_url or self.base_url, |
| 445 | timeout=self.timeout if isinstance(timeout, NotGiven) else timeout, |
| 446 | http_client=http_client, |
| 447 | max_retries=max_retries if is_given(max_retries) else self.max_retries, |
| 448 | default_headers=headers, |
| 449 | default_query=params, |
| 450 | **_extra_kwargs, |
| 451 | ) |
| 452 | |
| 453 | # Alias for `copy` for nicer inline usage, e.g. |
| 454 | # client.with_options(timeout=10).foo.create(...) |
| 455 | with_options = copy |
| 456 | |
| 457 | @override |
| 458 | def _make_status_error( |
| 459 | self, |
| 460 | err_msg: str, |
| 461 | *, |
| 462 | body: object, |
| 463 | response: httpx.Response, |
| 464 | ) -> APIStatusError: |
| 465 | data = body.get("error", body) if is_mapping(body) else body |
| 466 | if response.status_code == 400: |
| 467 | return _exceptions.BadRequestError(err_msg, response=response, body=data) |
| 468 | |
| 469 | if response.status_code == 401: |
| 470 | return _exceptions.AuthenticationError(err_msg, response=response, body=data) |
| 471 | |
| 472 | if response.status_code == 403: |
| 473 | return _exceptions.PermissionDeniedError(err_msg, response=response, body=data) |
| 474 | |
| 475 | if response.status_code == 404: |
| 476 | return _exceptions.NotFoundError(err_msg, response=response, body=data) |
| 477 | |
| 478 | if response.status_code == 409: |
| 479 | return _exceptions.ConflictError(err_msg, response=response, body=data) |
| 480 | |
| 481 | if response.status_code == 422: |
| 482 | return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data) |
| 483 | |
| 484 | if response.status_code == 429: |
| 485 | return _exceptions.RateLimitError(err_msg, response=response, body=data) |
| 486 | |
| 487 | if response.status_code >= 500: |
| 488 | return _exceptions.InternalServerError(err_msg, response=response, body=data) |
| 489 | return APIStatusError(err_msg, response=response, body=data) |
| 490 | |
| 491 | |
| 492 | class AsyncOpenAI(AsyncAPIClient): |
| 493 | # client options |
| 494 | api_key: str | None |
| 495 | admin_api_key: str | None |
| 496 | organization: str | None |
| 497 | project: str | None |
| 498 | webhook_secret: str | None |
| 499 | |
| 500 | websocket_base_url: str | httpx.URL | None |
| 501 | """Base URL for WebSocket connections. |
| 502 | |
| 503 | If not specified, the default base URL will be used, with 'wss://' replacing the |
| 504 | 'http://' or 'https://' scheme. For example: 'http://example.com' becomes |
| 505 | 'wss://example.com' |
| 506 | """ |
| 507 | |
| 508 | def __init__( |
| 509 | self, |
| 510 | *, |
| 511 | api_key: str | None = None, |
| 512 | admin_api_key: str | None = None, |
| 513 | organization: str | None = None, |
| 514 | project: str | None = None, |
| 515 | webhook_secret: str | None = None, |
| 516 | base_url: str | httpx.URL | None = None, |
| 517 | websocket_base_url: str | httpx.URL | None = None, |
| 518 | timeout: float | Timeout | None | NotGiven = not_given, |
| 519 | max_retries: int = DEFAULT_MAX_RETRIES, |
| 520 | default_headers: Mapping[str, str] | None = None, |
| 521 | default_query: Mapping[str, object] | None = None, |
| 522 | # Configure a custom httpx client. |
| 523 | # We provide a `DefaultAsyncHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`. |
| 524 | # See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details. |
| 525 | http_client: httpx.AsyncClient | None = None, |
| 526 | # Enable or disable schema validation for data returned by the API. |
| 527 | # When enabled an error APIResponseValidationError is raised |
| 528 | # if the API responds with invalid data for the expected schema. |
| 529 | # |
| 530 | # This parameter may be removed or changed in the future. |
| 531 | # If you rely on this feature, please open a GitHub issue |
| 532 | # outlining your use-case to help us decide if it should be |
| 533 | # part of our public interface in the future. |
| 534 | _strict_response_validation: bool = False, |
| 535 | ) -> None: |
| 536 | """Construct a new async AsyncOpenAI client instance. |
| 537 | |
| 538 | This automatically infers the following arguments from their corresponding environment variables if they are not provided: |
| 539 | - `api_key` from `OPENAI_API_KEY` |
| 540 | - `admin_api_key` from `OPENAI_ADMIN_KEY` |
| 541 | - `organization` from `OPENAI_ORG_ID` |
| 542 | - `project` from `OPENAI_PROJECT_ID` |
| 543 | - `webhook_secret` from `OPENAI_WEBHOOK_SECRET` |
| 544 | """ |
| 545 | if api_key is None: |
| 546 | api_key = os.environ.get("OPENAI_API_KEY") |
| 547 | self.api_key = api_key |
| 548 | |
| 549 | if admin_api_key is None: |
| 550 | admin_api_key = os.environ.get("OPENAI_ADMIN_KEY") |
| 551 | self.admin_api_key = admin_api_key |
| 552 | |
| 553 | if organization is None: |
| 554 | organization = os.environ.get("OPENAI_ORG_ID") |
| 555 | self.organization = organization |
| 556 | |
| 557 | if project is None: |
| 558 | project = os.environ.get("OPENAI_PROJECT_ID") |
| 559 | self.project = project |
| 560 | |
| 561 | if webhook_secret is None: |
| 562 | webhook_secret = os.environ.get("OPENAI_WEBHOOK_SECRET") |
| 563 | self.webhook_secret = webhook_secret |
| 564 | |
| 565 | self.websocket_base_url = websocket_base_url |
| 566 | |
| 567 | if base_url is None: |
| 568 | base_url = os.environ.get("OPENAI_BASE_URL") |
| 569 | if base_url is None: |
| 570 | base_url = f"https://api.openai.com/v1" |
| 571 | |
| 572 | custom_headers_env = os.environ.get("OPENAI_CUSTOM_HEADERS") |
| 573 | if custom_headers_env is not None: |
| 574 | parsed: dict[str, str] = {} |
| 575 | for line in custom_headers_env.split("\n"): |
| 576 | colon = line.find(":") |
| 577 | if colon >= 0: |
| 578 | parsed[line[:colon].strip()] = line[colon + 1 :].strip() |
| 579 | default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})} |
| 580 | |
| 581 | super().__init__( |
| 582 | version=__version__, |
| 583 | base_url=base_url, |
| 584 | max_retries=max_retries, |
| 585 | timeout=timeout, |
| 586 | http_client=http_client, |
| 587 | custom_headers=default_headers, |
| 588 | custom_query=default_query, |
| 589 | _strict_response_validation=_strict_response_validation, |
| 590 | ) |
| 591 | |
| 592 | self._default_stream_cls = AsyncStream |
| 593 | |
| 594 | @cached_property |
| 595 | def completions(self) -> AsyncCompletions: |
| 596 | """ |
| 597 | Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. |
| 598 | """ |
| 599 | from .resources.completions import AsyncCompletions |
| 600 | |
| 601 | return AsyncCompletions(self) |
| 602 | |
| 603 | @cached_property |
| 604 | def chat(self) -> AsyncChat: |
| 605 | from .resources.chat import AsyncChat |
| 606 | |
| 607 | return AsyncChat(self) |
| 608 | |
| 609 | @cached_property |
| 610 | def embeddings(self) -> AsyncEmbeddings: |
| 611 | """ |
| 612 | Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. |
| 613 | """ |
| 614 | from .resources.embeddings import AsyncEmbeddings |
| 615 | |
| 616 | return AsyncEmbeddings(self) |
| 617 | |
| 618 | @cached_property |
| 619 | def files(self) -> AsyncFiles: |
| 620 | """ |
| 621 | Files are used to upload documents that can be used with features like Assistants and Fine-tuning. |
| 622 | """ |
| 623 | from .resources.files import AsyncFiles |
| 624 | |
| 625 | return AsyncFiles(self) |
| 626 | |
| 627 | @cached_property |
| 628 | def images(self) -> AsyncImages: |
| 629 | """Given a prompt and/or an input image, the model will generate a new image.""" |
| 630 | from .resources.images import AsyncImages |
| 631 | |
| 632 | return AsyncImages(self) |
| 633 | |
| 634 | @cached_property |
| 635 | def audio(self) -> AsyncAudio: |
| 636 | from .resources.audio import AsyncAudio |
| 637 | |
| 638 | return AsyncAudio(self) |
| 639 | |
| 640 | @cached_property |
| 641 | def moderations(self) -> AsyncModerations: |
| 642 | """ |
| 643 | Given text and/or image inputs, classifies if those inputs are potentially harmful. |
| 644 | """ |
| 645 | from .resources.moderations import AsyncModerations |
| 646 | |
| 647 | return AsyncModerations(self) |
| 648 | |
| 649 | @cached_property |
| 650 | def models(self) -> AsyncModels: |
| 651 | """List and describe the various models available in the API.""" |
| 652 | from .resources.models import AsyncModels |
| 653 | |
| 654 | return AsyncModels(self) |
| 655 | |
| 656 | @cached_property |
| 657 | def fine_tuning(self) -> AsyncFineTuning: |
| 658 | from .resources.fine_tuning import AsyncFineTuning |
| 659 | |
| 660 | return AsyncFineTuning(self) |
| 661 | |
| 662 | @cached_property |
| 663 | def vector_stores(self) -> AsyncVectorStores: |
| 664 | from .resources.vector_stores import AsyncVectorStores |
| 665 | |
| 666 | return AsyncVectorStores(self) |
| 667 | |
| 668 | @cached_property |
| 669 | def webhooks(self) -> AsyncWebhooks: |
| 670 | from .resources.webhooks import AsyncWebhooks |
| 671 | |
| 672 | return AsyncWebhooks(self) |
| 673 | |
| 674 | @cached_property |
| 675 | def beta(self) -> AsyncBeta: |
| 676 | from .resources.beta import AsyncBeta |
| 677 | |
| 678 | return AsyncBeta(self) |
| 679 | |
| 680 | @cached_property |
| 681 | def batches(self) -> AsyncBatches: |
| 682 | """Create large batches of API requests to run asynchronously.""" |
| 683 | from .resources.batches import AsyncBatches |
| 684 | |
| 685 | return AsyncBatches(self) |
| 686 | |
| 687 | @cached_property |
| 688 | def uploads(self) -> AsyncUploads: |
| 689 | """Use Uploads to upload large files in multiple parts.""" |
| 690 | from .resources.uploads import AsyncUploads |
| 691 | |
| 692 | return AsyncUploads(self) |
| 693 | |
| 694 | @cached_property |
| 695 | def admin(self) -> AsyncAdmin: |
| 696 | from .resources.admin import AsyncAdmin |
| 697 | |
| 698 | return AsyncAdmin(self) |
| 699 | |
| 700 | @cached_property |
| 701 | def responses(self) -> AsyncResponses: |
| 702 | from .resources.responses import AsyncResponses |
| 703 | |
| 704 | return AsyncResponses(self) |
| 705 | |
| 706 | @cached_property |
| 707 | def realtime(self) -> AsyncRealtime: |
| 708 | from .resources.realtime import AsyncRealtime |
| 709 | |
| 710 | return AsyncRealtime(self) |
| 711 | |
| 712 | @cached_property |
| 713 | def conversations(self) -> AsyncConversations: |
| 714 | """Manage conversations and conversation items.""" |
| 715 | from .resources.conversations import AsyncConversations |
| 716 | |
| 717 | return AsyncConversations(self) |
| 718 | |
| 719 | @cached_property |
| 720 | def evals(self) -> AsyncEvals: |
| 721 | """Manage and run evals in the OpenAI platform.""" |
| 722 | from .resources.evals import AsyncEvals |
| 723 | |
| 724 | return AsyncEvals(self) |
| 725 | |
| 726 | @cached_property |
| 727 | def containers(self) -> AsyncContainers: |
| 728 | from .resources.containers import AsyncContainers |
| 729 | |
| 730 | return AsyncContainers(self) |
| 731 | |
| 732 | @cached_property |
| 733 | def skills(self) -> AsyncSkills: |
| 734 | from .resources.skills import AsyncSkills |
| 735 | |
| 736 | return AsyncSkills(self) |
| 737 | |
| 738 | @cached_property |
| 739 | def videos(self) -> AsyncVideos: |
| 740 | from .resources.videos import AsyncVideos |
| 741 | |
| 742 | return AsyncVideos(self) |
| 743 | |
| 744 | @cached_property |
| 745 | def with_raw_response(self) -> AsyncOpenAIWithRawResponse: |
| 746 | return AsyncOpenAIWithRawResponse(self) |
| 747 | |
| 748 | @cached_property |
| 749 | def with_streaming_response(self) -> AsyncOpenAIWithStreamedResponse: |
| 750 | return AsyncOpenAIWithStreamedResponse(self) |
| 751 | |
| 752 | @property |
| 753 | @override |
| 754 | def qs(self) -> Querystring: |
| 755 | return Querystring(array_format="brackets") |
| 756 | |
| 757 | @override |
| 758 | def _auth_headers(self, security: SecurityOptions) -> dict[str, str]: |
| 759 | return { |
| 760 | **(self._bearer_auth if security.get("bearer_auth", False) else {}), |
| 761 | **(self._admin_api_key_auth if security.get("admin_api_key_auth", False) else {}), |
| 762 | } |
| 763 | |
| 764 | @property |
| 765 | def _bearer_auth(self) -> dict[str, str]: |
| 766 | api_key = self.api_key |
| 767 | if api_key is None: |
| 768 | return {} |
| 769 | return {"Authorization": f"Bearer {api_key}"} |
| 770 | |
| 771 | @property |
| 772 | def _admin_api_key_auth(self) -> dict[str, str]: |
| 773 | admin_api_key = self.admin_api_key |
| 774 | if admin_api_key is None: |
| 775 | return {} |
| 776 | return {"Authorization": f"Bearer {admin_api_key}"} |
| 777 | |
| 778 | @property |
| 779 | @override |
| 780 | def default_headers(self) -> dict[str, str | Omit]: |
| 781 | return { |
| 782 | **super().default_headers, |
| 783 | "X-Stainless-Async": f"async:{get_async_library()}", |
| 784 | "OpenAI-Organization": self.organization if self.organization is not None else Omit(), |
| 785 | "OpenAI-Project": self.project if self.project is not None else Omit(), |
| 786 | **self._custom_headers, |
| 787 | } |
| 788 | |
| 789 | @override |
| 790 | def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None: |
| 791 | if headers.get("Authorization") or isinstance(custom_headers.get("Authorization"), Omit): |
| 792 | return |
| 793 | |
| 794 | raise TypeError( |
| 795 | '"Could not resolve authentication method. Expected either api_key or admin_api_key to be set. Or for one of the `Authorization` or `Authorization` headers to be explicitly omitted"' |
| 796 | ) |
| 797 | |
| 798 | def copy( |
| 799 | self, |
| 800 | *, |
| 801 | api_key: str | None = None, |
| 802 | admin_api_key: str | None = None, |
| 803 | organization: str | None = None, |
| 804 | project: str | None = None, |
| 805 | webhook_secret: str | None = None, |
| 806 | websocket_base_url: str | httpx.URL | None = None, |
| 807 | base_url: str | httpx.URL | None = None, |
| 808 | timeout: float | Timeout | None | NotGiven = not_given, |
| 809 | http_client: httpx.AsyncClient | None = None, |
| 810 | max_retries: int | NotGiven = not_given, |
| 811 | default_headers: Mapping[str, str] | None = None, |
| 812 | set_default_headers: Mapping[str, str] | None = None, |
| 813 | default_query: Mapping[str, object] | None = None, |
| 814 | set_default_query: Mapping[str, object] | None = None, |
| 815 | _extra_kwargs: Mapping[str, Any] = {}, |
| 816 | ) -> Self: |
| 817 | """ |
| 818 | Create a new client instance re-using the same options given to the current client with optional overriding. |
| 819 | """ |
| 820 | if default_headers is not None and set_default_headers is not None: |
| 821 | raise ValueError("The `default_headers` and `set_default_headers` arguments are mutually exclusive") |
| 822 | |
| 823 | if default_query is not None and set_default_query is not None: |
| 824 | raise ValueError("The `default_query` and `set_default_query` arguments are mutually exclusive") |
| 825 | |
| 826 | headers = self._custom_headers |
| 827 | if default_headers is not None: |
| 828 | headers = {**headers, **default_headers} |
| 829 | elif set_default_headers is not None: |
| 830 | headers = set_default_headers |
| 831 | |
| 832 | params = self._custom_query |
| 833 | if default_query is not None: |
| 834 | params = {**params, **default_query} |
| 835 | elif set_default_query is not None: |
| 836 | params = set_default_query |
| 837 | |
| 838 | http_client = http_client or self._client |
| 839 | return self.__class__( |
| 840 | api_key=api_key or self.api_key, |
| 841 | admin_api_key=admin_api_key or self.admin_api_key, |
| 842 | organization=organization or self.organization, |
| 843 | project=project or self.project, |
| 844 | webhook_secret=webhook_secret or self.webhook_secret, |
| 845 | websocket_base_url=websocket_base_url or self.websocket_base_url, |
| 846 | base_url=base_url or self.base_url, |
| 847 | timeout=self.timeout if isinstance(timeout, NotGiven) else timeout, |
| 848 | http_client=http_client, |
| 849 | max_retries=max_retries if is_given(max_retries) else self.max_retries, |
| 850 | default_headers=headers, |
| 851 | default_query=params, |
| 852 | **_extra_kwargs, |
| 853 | ) |
| 854 | |
| 855 | # Alias for `copy` for nicer inline usage, e.g. |
| 856 | # client.with_options(timeout=10).foo.create(...) |
| 857 | with_options = copy |
| 858 | |
| 859 | @override |
| 860 | def _make_status_error( |
| 861 | self, |
| 862 | err_msg: str, |
| 863 | *, |
| 864 | body: object, |
| 865 | response: httpx.Response, |
| 866 | ) -> APIStatusError: |
| 867 | data = body.get("error", body) if is_mapping(body) else body |
| 868 | if response.status_code == 400: |
| 869 | return _exceptions.BadRequestError(err_msg, response=response, body=data) |
| 870 | |
| 871 | if response.status_code == 401: |
| 872 | return _exceptions.AuthenticationError(err_msg, response=response, body=data) |
| 873 | |
| 874 | if response.status_code == 403: |
| 875 | return _exceptions.PermissionDeniedError(err_msg, response=response, body=data) |
| 876 | |
| 877 | if response.status_code == 404: |
| 878 | return _exceptions.NotFoundError(err_msg, response=response, body=data) |
| 879 | |
| 880 | if response.status_code == 409: |
| 881 | return _exceptions.ConflictError(err_msg, response=response, body=data) |
| 882 | |
| 883 | if response.status_code == 422: |
| 884 | return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data) |
| 885 | |
| 886 | if response.status_code == 429: |
| 887 | return _exceptions.RateLimitError(err_msg, response=response, body=data) |
| 888 | |
| 889 | if response.status_code >= 500: |
| 890 | return _exceptions.InternalServerError(err_msg, response=response, body=data) |
| 891 | return APIStatusError(err_msg, response=response, body=data) |
| 892 | |
| 893 | |
| 894 | class OpenAIWithRawResponse: |
| 895 | _client: OpenAI |
| 896 | |
| 897 | def __init__(self, client: OpenAI) -> None: |
| 898 | self._client = client |
| 899 | |
| 900 | @cached_property |
| 901 | def completions(self) -> completions.CompletionsWithRawResponse: |
| 902 | """ |
| 903 | Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. |
| 904 | """ |
| 905 | from .resources.completions import CompletionsWithRawResponse |
| 906 | |
| 907 | return CompletionsWithRawResponse(self._client.completions) |
| 908 | |
| 909 | @cached_property |
| 910 | def chat(self) -> chat.ChatWithRawResponse: |
| 911 | from .resources.chat import ChatWithRawResponse |
| 912 | |
| 913 | return ChatWithRawResponse(self._client.chat) |
| 914 | |
| 915 | @cached_property |
| 916 | def embeddings(self) -> embeddings.EmbeddingsWithRawResponse: |
| 917 | """ |
| 918 | Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. |
| 919 | """ |
| 920 | from .resources.embeddings import EmbeddingsWithRawResponse |
| 921 | |
| 922 | return EmbeddingsWithRawResponse(self._client.embeddings) |
| 923 | |
| 924 | @cached_property |
| 925 | def files(self) -> files.FilesWithRawResponse: |
| 926 | """ |
| 927 | Files are used to upload documents that can be used with features like Assistants and Fine-tuning. |
| 928 | """ |
| 929 | from .resources.files import FilesWithRawResponse |
| 930 | |
| 931 | return FilesWithRawResponse(self._client.files) |
| 932 | |
| 933 | @cached_property |
| 934 | def images(self) -> images.ImagesWithRawResponse: |
| 935 | """Given a prompt and/or an input image, the model will generate a new image.""" |
| 936 | from .resources.images import ImagesWithRawResponse |
| 937 | |
| 938 | return ImagesWithRawResponse(self._client.images) |
| 939 | |
| 940 | @cached_property |
| 941 | def audio(self) -> audio.AudioWithRawResponse: |
| 942 | from .resources.audio import AudioWithRawResponse |
| 943 | |
| 944 | return AudioWithRawResponse(self._client.audio) |
| 945 | |
| 946 | @cached_property |
| 947 | def moderations(self) -> moderations.ModerationsWithRawResponse: |
| 948 | """ |
| 949 | Given text and/or image inputs, classifies if those inputs are potentially harmful. |
| 950 | """ |
| 951 | from .resources.moderations import ModerationsWithRawResponse |
| 952 | |
| 953 | return ModerationsWithRawResponse(self._client.moderations) |
| 954 | |
| 955 | @cached_property |
| 956 | def models(self) -> models.ModelsWithRawResponse: |
| 957 | """List and describe the various models available in the API.""" |
| 958 | from .resources.models import ModelsWithRawResponse |
| 959 | |
| 960 | return ModelsWithRawResponse(self._client.models) |
| 961 | |
| 962 | @cached_property |
| 963 | def fine_tuning(self) -> fine_tuning.FineTuningWithRawResponse: |
| 964 | from .resources.fine_tuning import FineTuningWithRawResponse |
| 965 | |
| 966 | return FineTuningWithRawResponse(self._client.fine_tuning) |
| 967 | |
| 968 | @cached_property |
| 969 | def vector_stores(self) -> vector_stores.VectorStoresWithRawResponse: |
| 970 | from .resources.vector_stores import VectorStoresWithRawResponse |
| 971 | |
| 972 | return VectorStoresWithRawResponse(self._client.vector_stores) |
| 973 | |
| 974 | @cached_property |
| 975 | def beta(self) -> beta.BetaWithRawResponse: |
| 976 | from .resources.beta import BetaWithRawResponse |
| 977 | |
| 978 | return BetaWithRawResponse(self._client.beta) |
| 979 | |
| 980 | @cached_property |
| 981 | def batches(self) -> batches.BatchesWithRawResponse: |
| 982 | """Create large batches of API requests to run asynchronously.""" |
| 983 | from .resources.batches import BatchesWithRawResponse |
| 984 | |
| 985 | return BatchesWithRawResponse(self._client.batches) |
| 986 | |
| 987 | @cached_property |
| 988 | def uploads(self) -> uploads.UploadsWithRawResponse: |
| 989 | """Use Uploads to upload large files in multiple parts.""" |
| 990 | from .resources.uploads import UploadsWithRawResponse |
| 991 | |
| 992 | return UploadsWithRawResponse(self._client.uploads) |
| 993 | |
| 994 | @cached_property |
| 995 | def admin(self) -> admin.AdminWithRawResponse: |
| 996 | from .resources.admin import AdminWithRawResponse |
| 997 | |
| 998 | return AdminWithRawResponse(self._client.admin) |
| 999 | |
| 1000 | @cached_property |
| 1001 | def responses(self) -> responses.ResponsesWithRawResponse: |
| 1002 | from .resources.responses import ResponsesWithRawResponse |
| 1003 | |
| 1004 | return ResponsesWithRawResponse(self._client.responses) |
| 1005 | |
| 1006 | @cached_property |
| 1007 | def realtime(self) -> realtime.RealtimeWithRawResponse: |
| 1008 | from .resources.realtime import RealtimeWithRawResponse |
| 1009 | |
| 1010 | return RealtimeWithRawResponse(self._client.realtime) |
| 1011 | |
| 1012 | @cached_property |
| 1013 | def conversations(self) -> conversations.ConversationsWithRawResponse: |
| 1014 | """Manage conversations and conversation items.""" |
| 1015 | from .resources.conversations import ConversationsWithRawResponse |
| 1016 | |
| 1017 | return ConversationsWithRawResponse(self._client.conversations) |
| 1018 | |
| 1019 | @cached_property |
| 1020 | def evals(self) -> evals.EvalsWithRawResponse: |
| 1021 | """Manage and run evals in the OpenAI platform.""" |
| 1022 | from .resources.evals import EvalsWithRawResponse |
| 1023 | |
| 1024 | return EvalsWithRawResponse(self._client.evals) |
| 1025 | |
| 1026 | @cached_property |
| 1027 | def containers(self) -> containers.ContainersWithRawResponse: |
| 1028 | from .resources.containers import ContainersWithRawResponse |
| 1029 | |
| 1030 | return ContainersWithRawResponse(self._client.containers) |
| 1031 | |
| 1032 | @cached_property |
| 1033 | def skills(self) -> skills.SkillsWithRawResponse: |
| 1034 | from .resources.skills import SkillsWithRawResponse |
| 1035 | |
| 1036 | return SkillsWithRawResponse(self._client.skills) |
| 1037 | |
| 1038 | @cached_property |
| 1039 | def videos(self) -> videos.VideosWithRawResponse: |
| 1040 | from .resources.videos import VideosWithRawResponse |
| 1041 | |
| 1042 | return VideosWithRawResponse(self._client.videos) |
| 1043 | |
| 1044 | |
| 1045 | class AsyncOpenAIWithRawResponse: |
| 1046 | _client: AsyncOpenAI |
| 1047 | |
| 1048 | def __init__(self, client: AsyncOpenAI) -> None: |
| 1049 | self._client = client |
| 1050 | |
| 1051 | @cached_property |
| 1052 | def completions(self) -> completions.AsyncCompletionsWithRawResponse: |
| 1053 | """ |
| 1054 | Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. |
| 1055 | """ |
| 1056 | from .resources.completions import AsyncCompletionsWithRawResponse |
| 1057 | |
| 1058 | return AsyncCompletionsWithRawResponse(self._client.completions) |
| 1059 | |
| 1060 | @cached_property |
| 1061 | def chat(self) -> chat.AsyncChatWithRawResponse: |
| 1062 | from .resources.chat import AsyncChatWithRawResponse |
| 1063 | |
| 1064 | return AsyncChatWithRawResponse(self._client.chat) |
| 1065 | |
| 1066 | @cached_property |
| 1067 | def embeddings(self) -> embeddings.AsyncEmbeddingsWithRawResponse: |
| 1068 | """ |
| 1069 | Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. |
| 1070 | """ |
| 1071 | from .resources.embeddings import AsyncEmbeddingsWithRawResponse |
| 1072 | |
| 1073 | return AsyncEmbeddingsWithRawResponse(self._client.embeddings) |
| 1074 | |
| 1075 | @cached_property |
| 1076 | def files(self) -> files.AsyncFilesWithRawResponse: |
| 1077 | """ |
| 1078 | Files are used to upload documents that can be used with features like Assistants and Fine-tuning. |
| 1079 | """ |
| 1080 | from .resources.files import AsyncFilesWithRawResponse |
| 1081 | |
| 1082 | return AsyncFilesWithRawResponse(self._client.files) |
| 1083 | |
| 1084 | @cached_property |
| 1085 | def images(self) -> images.AsyncImagesWithRawResponse: |
| 1086 | """Given a prompt and/or an input image, the model will generate a new image.""" |
| 1087 | from .resources.images import AsyncImagesWithRawResponse |
| 1088 | |
| 1089 | return AsyncImagesWithRawResponse(self._client.images) |
| 1090 | |
| 1091 | @cached_property |
| 1092 | def audio(self) -> audio.AsyncAudioWithRawResponse: |
| 1093 | from .resources.audio import AsyncAudioWithRawResponse |
| 1094 | |
| 1095 | return AsyncAudioWithRawResponse(self._client.audio) |
| 1096 | |
| 1097 | @cached_property |
| 1098 | def moderations(self) -> moderations.AsyncModerationsWithRawResponse: |
| 1099 | """ |
| 1100 | Given text and/or image inputs, classifies if those inputs are potentially harmful. |
| 1101 | """ |
| 1102 | from .resources.moderations import AsyncModerationsWithRawResponse |
| 1103 | |
| 1104 | return AsyncModerationsWithRawResponse(self._client.moderations) |
| 1105 | |
| 1106 | @cached_property |
| 1107 | def models(self) -> models.AsyncModelsWithRawResponse: |
| 1108 | """List and describe the various models available in the API.""" |
| 1109 | from .resources.models import AsyncModelsWithRawResponse |
| 1110 | |
| 1111 | return AsyncModelsWithRawResponse(self._client.models) |
| 1112 | |
| 1113 | @cached_property |
| 1114 | def fine_tuning(self) -> fine_tuning.AsyncFineTuningWithRawResponse: |
| 1115 | from .resources.fine_tuning import AsyncFineTuningWithRawResponse |
| 1116 | |
| 1117 | return AsyncFineTuningWithRawResponse(self._client.fine_tuning) |
| 1118 | |
| 1119 | @cached_property |
| 1120 | def vector_stores(self) -> vector_stores.AsyncVectorStoresWithRawResponse: |
| 1121 | from .resources.vector_stores import AsyncVectorStoresWithRawResponse |
| 1122 | |
| 1123 | return AsyncVectorStoresWithRawResponse(self._client.vector_stores) |
| 1124 | |
| 1125 | @cached_property |
| 1126 | def beta(self) -> beta.AsyncBetaWithRawResponse: |
| 1127 | from .resources.beta import AsyncBetaWithRawResponse |
| 1128 | |
| 1129 | return AsyncBetaWithRawResponse(self._client.beta) |
| 1130 | |
| 1131 | @cached_property |
| 1132 | def batches(self) -> batches.AsyncBatchesWithRawResponse: |
| 1133 | """Create large batches of API requests to run asynchronously.""" |
| 1134 | from .resources.batches import AsyncBatchesWithRawResponse |
| 1135 | |
| 1136 | return AsyncBatchesWithRawResponse(self._client.batches) |
| 1137 | |
| 1138 | @cached_property |
| 1139 | def uploads(self) -> uploads.AsyncUploadsWithRawResponse: |
| 1140 | """Use Uploads to upload large files in multiple parts.""" |
| 1141 | from .resources.uploads import AsyncUploadsWithRawResponse |
| 1142 | |
| 1143 | return AsyncUploadsWithRawResponse(self._client.uploads) |
| 1144 | |
| 1145 | @cached_property |
| 1146 | def admin(self) -> admin.AsyncAdminWithRawResponse: |
| 1147 | from .resources.admin import AsyncAdminWithRawResponse |
| 1148 | |
| 1149 | return AsyncAdminWithRawResponse(self._client.admin) |
| 1150 | |
| 1151 | @cached_property |
| 1152 | def responses(self) -> responses.AsyncResponsesWithRawResponse: |
| 1153 | from .resources.responses import AsyncResponsesWithRawResponse |
| 1154 | |
| 1155 | return AsyncResponsesWithRawResponse(self._client.responses) |
| 1156 | |
| 1157 | @cached_property |
| 1158 | def realtime(self) -> realtime.AsyncRealtimeWithRawResponse: |
| 1159 | from .resources.realtime import AsyncRealtimeWithRawResponse |
| 1160 | |
| 1161 | return AsyncRealtimeWithRawResponse(self._client.realtime) |
| 1162 | |
| 1163 | @cached_property |
| 1164 | def conversations(self) -> conversations.AsyncConversationsWithRawResponse: |
| 1165 | """Manage conversations and conversation items.""" |
| 1166 | from .resources.conversations import AsyncConversationsWithRawResponse |
| 1167 | |
| 1168 | return AsyncConversationsWithRawResponse(self._client.conversations) |
| 1169 | |
| 1170 | @cached_property |
| 1171 | def evals(self) -> evals.AsyncEvalsWithRawResponse: |
| 1172 | """Manage and run evals in the OpenAI platform.""" |
| 1173 | from .resources.evals import AsyncEvalsWithRawResponse |
| 1174 | |
| 1175 | return AsyncEvalsWithRawResponse(self._client.evals) |
| 1176 | |
| 1177 | @cached_property |
| 1178 | def containers(self) -> containers.AsyncContainersWithRawResponse: |
| 1179 | from .resources.containers import AsyncContainersWithRawResponse |
| 1180 | |
| 1181 | return AsyncContainersWithRawResponse(self._client.containers) |
| 1182 | |
| 1183 | @cached_property |
| 1184 | def skills(self) -> skills.AsyncSkillsWithRawResponse: |
| 1185 | from .resources.skills import AsyncSkillsWithRawResponse |
| 1186 | |
| 1187 | return AsyncSkillsWithRawResponse(self._client.skills) |
| 1188 | |
| 1189 | @cached_property |
| 1190 | def videos(self) -> videos.AsyncVideosWithRawResponse: |
| 1191 | from .resources.videos import AsyncVideosWithRawResponse |
| 1192 | |
| 1193 | return AsyncVideosWithRawResponse(self._client.videos) |
| 1194 | |
| 1195 | |
| 1196 | class OpenAIWithStreamedResponse: |
| 1197 | _client: OpenAI |
| 1198 | |
| 1199 | def __init__(self, client: OpenAI) -> None: |
| 1200 | self._client = client |
| 1201 | |
| 1202 | @cached_property |
| 1203 | def completions(self) -> completions.CompletionsWithStreamingResponse: |
| 1204 | """ |
| 1205 | Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. |
| 1206 | """ |
| 1207 | from .resources.completions import CompletionsWithStreamingResponse |
| 1208 | |
| 1209 | return CompletionsWithStreamingResponse(self._client.completions) |
| 1210 | |
| 1211 | @cached_property |
| 1212 | def chat(self) -> chat.ChatWithStreamingResponse: |
| 1213 | from .resources.chat import ChatWithStreamingResponse |
| 1214 | |
| 1215 | return ChatWithStreamingResponse(self._client.chat) |
| 1216 | |
| 1217 | @cached_property |
| 1218 | def embeddings(self) -> embeddings.EmbeddingsWithStreamingResponse: |
| 1219 | """ |
| 1220 | Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. |
| 1221 | """ |
| 1222 | from .resources.embeddings import EmbeddingsWithStreamingResponse |
| 1223 | |
| 1224 | return EmbeddingsWithStreamingResponse(self._client.embeddings) |
| 1225 | |
| 1226 | @cached_property |
| 1227 | def files(self) -> files.FilesWithStreamingResponse: |
| 1228 | """ |
| 1229 | Files are used to upload documents that can be used with features like Assistants and Fine-tuning. |
| 1230 | """ |
| 1231 | from .resources.files import FilesWithStreamingResponse |
| 1232 | |
| 1233 | return FilesWithStreamingResponse(self._client.files) |
| 1234 | |
| 1235 | @cached_property |
| 1236 | def images(self) -> images.ImagesWithStreamingResponse: |
| 1237 | """Given a prompt and/or an input image, the model will generate a new image.""" |
| 1238 | from .resources.images import ImagesWithStreamingResponse |
| 1239 | |
| 1240 | return ImagesWithStreamingResponse(self._client.images) |
| 1241 | |
| 1242 | @cached_property |
| 1243 | def audio(self) -> audio.AudioWithStreamingResponse: |
| 1244 | from .resources.audio import AudioWithStreamingResponse |
| 1245 | |
| 1246 | return AudioWithStreamingResponse(self._client.audio) |
| 1247 | |
| 1248 | @cached_property |
| 1249 | def moderations(self) -> moderations.ModerationsWithStreamingResponse: |
| 1250 | """ |
| 1251 | Given text and/or image inputs, classifies if those inputs are potentially harmful. |
| 1252 | """ |
| 1253 | from .resources.moderations import ModerationsWithStreamingResponse |
| 1254 | |
| 1255 | return ModerationsWithStreamingResponse(self._client.moderations) |
| 1256 | |
| 1257 | @cached_property |
| 1258 | def models(self) -> models.ModelsWithStreamingResponse: |
| 1259 | """List and describe the various models available in the API.""" |
| 1260 | from .resources.models import ModelsWithStreamingResponse |
| 1261 | |
| 1262 | return ModelsWithStreamingResponse(self._client.models) |
| 1263 | |
| 1264 | @cached_property |
| 1265 | def fine_tuning(self) -> fine_tuning.FineTuningWithStreamingResponse: |
| 1266 | from .resources.fine_tuning import FineTuningWithStreamingResponse |
| 1267 | |
| 1268 | return FineTuningWithStreamingResponse(self._client.fine_tuning) |
| 1269 | |
| 1270 | @cached_property |
| 1271 | def vector_stores(self) -> vector_stores.VectorStoresWithStreamingResponse: |
| 1272 | from .resources.vector_stores import VectorStoresWithStreamingResponse |
| 1273 | |
| 1274 | return VectorStoresWithStreamingResponse(self._client.vector_stores) |
| 1275 | |
| 1276 | @cached_property |
| 1277 | def beta(self) -> beta.BetaWithStreamingResponse: |
| 1278 | from .resources.beta import BetaWithStreamingResponse |
| 1279 | |
| 1280 | return BetaWithStreamingResponse(self._client.beta) |
| 1281 | |
| 1282 | @cached_property |
| 1283 | def batches(self) -> batches.BatchesWithStreamingResponse: |
| 1284 | """Create large batches of API requests to run asynchronously.""" |
| 1285 | from .resources.batches import BatchesWithStreamingResponse |
| 1286 | |
| 1287 | return BatchesWithStreamingResponse(self._client.batches) |
| 1288 | |
| 1289 | @cached_property |
| 1290 | def uploads(self) -> uploads.UploadsWithStreamingResponse: |
| 1291 | """Use Uploads to upload large files in multiple parts.""" |
| 1292 | from .resources.uploads import UploadsWithStreamingResponse |
| 1293 | |
| 1294 | return UploadsWithStreamingResponse(self._client.uploads) |
| 1295 | |
| 1296 | @cached_property |
| 1297 | def admin(self) -> admin.AdminWithStreamingResponse: |
| 1298 | from .resources.admin import AdminWithStreamingResponse |
| 1299 | |
| 1300 | return AdminWithStreamingResponse(self._client.admin) |
| 1301 | |
| 1302 | @cached_property |
| 1303 | def responses(self) -> responses.ResponsesWithStreamingResponse: |
| 1304 | from .resources.responses import ResponsesWithStreamingResponse |
| 1305 | |
| 1306 | return ResponsesWithStreamingResponse(self._client.responses) |
| 1307 | |
| 1308 | @cached_property |
| 1309 | def realtime(self) -> realtime.RealtimeWithStreamingResponse: |
| 1310 | from .resources.realtime import RealtimeWithStreamingResponse |
| 1311 | |
| 1312 | return RealtimeWithStreamingResponse(self._client.realtime) |
| 1313 | |
| 1314 | @cached_property |
| 1315 | def conversations(self) -> conversations.ConversationsWithStreamingResponse: |
| 1316 | """Manage conversations and conversation items.""" |
| 1317 | from .resources.conversations import ConversationsWithStreamingResponse |
| 1318 | |
| 1319 | return ConversationsWithStreamingResponse(self._client.conversations) |
| 1320 | |
| 1321 | @cached_property |
| 1322 | def evals(self) -> evals.EvalsWithStreamingResponse: |
| 1323 | """Manage and run evals in the OpenAI platform.""" |
| 1324 | from .resources.evals import EvalsWithStreamingResponse |
| 1325 | |
| 1326 | return EvalsWithStreamingResponse(self._client.evals) |
| 1327 | |
| 1328 | @cached_property |
| 1329 | def containers(self) -> containers.ContainersWithStreamingResponse: |
| 1330 | from .resources.containers import ContainersWithStreamingResponse |
| 1331 | |
| 1332 | return ContainersWithStreamingResponse(self._client.containers) |
| 1333 | |
| 1334 | @cached_property |
| 1335 | def skills(self) -> skills.SkillsWithStreamingResponse: |
| 1336 | from .resources.skills import SkillsWithStreamingResponse |
| 1337 | |
| 1338 | return SkillsWithStreamingResponse(self._client.skills) |
| 1339 | |
| 1340 | @cached_property |
| 1341 | def videos(self) -> videos.VideosWithStreamingResponse: |
| 1342 | from .resources.videos import VideosWithStreamingResponse |
| 1343 | |
| 1344 | return VideosWithStreamingResponse(self._client.videos) |
| 1345 | |
| 1346 | |
| 1347 | class AsyncOpenAIWithStreamedResponse: |
| 1348 | _client: AsyncOpenAI |
| 1349 | |
| 1350 | def __init__(self, client: AsyncOpenAI) -> None: |
| 1351 | self._client = client |
| 1352 | |
| 1353 | @cached_property |
| 1354 | def completions(self) -> completions.AsyncCompletionsWithStreamingResponse: |
| 1355 | """ |
| 1356 | Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. |
| 1357 | """ |
| 1358 | from .resources.completions import AsyncCompletionsWithStreamingResponse |
| 1359 | |
| 1360 | return AsyncCompletionsWithStreamingResponse(self._client.completions) |
| 1361 | |
| 1362 | @cached_property |
| 1363 | def chat(self) -> chat.AsyncChatWithStreamingResponse: |
| 1364 | from .resources.chat import AsyncChatWithStreamingResponse |
| 1365 | |
| 1366 | return AsyncChatWithStreamingResponse(self._client.chat) |
| 1367 | |
| 1368 | @cached_property |
| 1369 | def embeddings(self) -> embeddings.AsyncEmbeddingsWithStreamingResponse: |
| 1370 | """ |
| 1371 | Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. |
| 1372 | """ |
| 1373 | from .resources.embeddings import AsyncEmbeddingsWithStreamingResponse |
| 1374 | |
| 1375 | return AsyncEmbeddingsWithStreamingResponse(self._client.embeddings) |
| 1376 | |
| 1377 | @cached_property |
| 1378 | def files(self) -> files.AsyncFilesWithStreamingResponse: |
| 1379 | """ |
| 1380 | Files are used to upload documents that can be used with features like Assistants and Fine-tuning. |
| 1381 | """ |
| 1382 | from .resources.files import AsyncFilesWithStreamingResponse |
| 1383 | |
| 1384 | return AsyncFilesWithStreamingResponse(self._client.files) |
| 1385 | |
| 1386 | @cached_property |
| 1387 | def images(self) -> images.AsyncImagesWithStreamingResponse: |
| 1388 | """Given a prompt and/or an input image, the model will generate a new image.""" |
| 1389 | from .resources.images import AsyncImagesWithStreamingResponse |
| 1390 | |
| 1391 | return AsyncImagesWithStreamingResponse(self._client.images) |
| 1392 | |
| 1393 | @cached_property |
| 1394 | def audio(self) -> audio.AsyncAudioWithStreamingResponse: |
| 1395 | from .resources.audio import AsyncAudioWithStreamingResponse |
| 1396 | |
| 1397 | return AsyncAudioWithStreamingResponse(self._client.audio) |
| 1398 | |
| 1399 | @cached_property |
| 1400 | def moderations(self) -> moderations.AsyncModerationsWithStreamingResponse: |
| 1401 | """ |
| 1402 | Given text and/or image inputs, classifies if those inputs are potentially harmful. |
| 1403 | """ |
| 1404 | from .resources.moderations import AsyncModerationsWithStreamingResponse |
| 1405 | |
| 1406 | return AsyncModerationsWithStreamingResponse(self._client.moderations) |
| 1407 | |
| 1408 | @cached_property |
| 1409 | def models(self) -> models.AsyncModelsWithStreamingResponse: |
| 1410 | """List and describe the various models available in the API.""" |
| 1411 | from .resources.models import AsyncModelsWithStreamingResponse |
| 1412 | |
| 1413 | return AsyncModelsWithStreamingResponse(self._client.models) |
| 1414 | |
| 1415 | @cached_property |
| 1416 | def fine_tuning(self) -> fine_tuning.AsyncFineTuningWithStreamingResponse: |
| 1417 | from .resources.fine_tuning import AsyncFineTuningWithStreamingResponse |
| 1418 | |
| 1419 | return AsyncFineTuningWithStreamingResponse(self._client.fine_tuning) |
| 1420 | |
| 1421 | @cached_property |
| 1422 | def vector_stores(self) -> vector_stores.AsyncVectorStoresWithStreamingResponse: |
| 1423 | from .resources.vector_stores import AsyncVectorStoresWithStreamingResponse |
| 1424 | |
| 1425 | return AsyncVectorStoresWithStreamingResponse(self._client.vector_stores) |
| 1426 | |
| 1427 | @cached_property |
| 1428 | def beta(self) -> beta.AsyncBetaWithStreamingResponse: |
| 1429 | from .resources.beta import AsyncBetaWithStreamingResponse |
| 1430 | |
| 1431 | return AsyncBetaWithStreamingResponse(self._client.beta) |
| 1432 | |
| 1433 | @cached_property |
| 1434 | def batches(self) -> batches.AsyncBatchesWithStreamingResponse: |
| 1435 | """Create large batches of API requests to run asynchronously.""" |
| 1436 | from .resources.batches import AsyncBatchesWithStreamingResponse |
| 1437 | |
| 1438 | return AsyncBatchesWithStreamingResponse(self._client.batches) |
| 1439 | |
| 1440 | @cached_property |
| 1441 | def uploads(self) -> uploads.AsyncUploadsWithStreamingResponse: |
| 1442 | """Use Uploads to upload large files in multiple parts.""" |
| 1443 | from .resources.uploads import AsyncUploadsWithStreamingResponse |
| 1444 | |
| 1445 | return AsyncUploadsWithStreamingResponse(self._client.uploads) |
| 1446 | |
| 1447 | @cached_property |
| 1448 | def admin(self) -> admin.AsyncAdminWithStreamingResponse: |
| 1449 | from .resources.admin import AsyncAdminWithStreamingResponse |
| 1450 | |
| 1451 | return AsyncAdminWithStreamingResponse(self._client.admin) |
| 1452 | |
| 1453 | @cached_property |
| 1454 | def responses(self) -> responses.AsyncResponsesWithStreamingResponse: |
| 1455 | from .resources.responses import AsyncResponsesWithStreamingResponse |
| 1456 | |
| 1457 | return AsyncResponsesWithStreamingResponse(self._client.responses) |
| 1458 | |
| 1459 | @cached_property |
| 1460 | def realtime(self) -> realtime.AsyncRealtimeWithStreamingResponse: |
| 1461 | from .resources.realtime import AsyncRealtimeWithStreamingResponse |
| 1462 | |
| 1463 | return AsyncRealtimeWithStreamingResponse(self._client.realtime) |
| 1464 | |
| 1465 | @cached_property |
| 1466 | def conversations(self) -> conversations.AsyncConversationsWithStreamingResponse: |
| 1467 | """Manage conversations and conversation items.""" |
| 1468 | from .resources.conversations import AsyncConversationsWithStreamingResponse |
| 1469 | |
| 1470 | return AsyncConversationsWithStreamingResponse(self._client.conversations) |
| 1471 | |
| 1472 | @cached_property |
| 1473 | def evals(self) -> evals.AsyncEvalsWithStreamingResponse: |
| 1474 | """Manage and run evals in the OpenAI platform.""" |
| 1475 | from .resources.evals import AsyncEvalsWithStreamingResponse |
| 1476 | |
| 1477 | return AsyncEvalsWithStreamingResponse(self._client.evals) |
| 1478 | |
| 1479 | @cached_property |
| 1480 | def containers(self) -> containers.AsyncContainersWithStreamingResponse: |
| 1481 | from .resources.containers import AsyncContainersWithStreamingResponse |
| 1482 | |
| 1483 | return AsyncContainersWithStreamingResponse(self._client.containers) |
| 1484 | |
| 1485 | @cached_property |
| 1486 | def skills(self) -> skills.AsyncSkillsWithStreamingResponse: |
| 1487 | from .resources.skills import AsyncSkillsWithStreamingResponse |
| 1488 | |
| 1489 | return AsyncSkillsWithStreamingResponse(self._client.skills) |
| 1490 | |
| 1491 | @cached_property |
| 1492 | def videos(self) -> videos.AsyncVideosWithStreamingResponse: |
| 1493 | from .resources.videos import AsyncVideosWithStreamingResponse |
| 1494 | |
| 1495 | return AsyncVideosWithStreamingResponse(self._client.videos) |
| 1496 | |
| 1497 | |
| 1498 | Client = OpenAI |
| 1499 | |
| 1500 | AsyncClient = AsyncOpenAI |