| 118 | |
| 119 | |
| 120 | class OpenAIClient(ModelClient): |
| 121 | __doc__ = r"""A component wrapper for the OpenAI API client. |
| 122 | |
| 123 | Supports both embedding and chat completion APIs, including multimodal capabilities. |
| 124 | |
| 125 | Users can: |
| 126 | 1. Simplify use of ``Embedder`` and ``Generator`` components by passing `OpenAIClient()` as the `model_client`. |
| 127 | 2. Use this as a reference to create their own API client or extend this class by copying and modifying the code. |
| 128 | |
| 129 | Note: |
| 130 | We recommend avoiding `response_format` to enforce output data type or `tools` and `tool_choice` in `model_kwargs` when calling the API. |
| 131 | OpenAI's internal formatting and added prompts are unknown. Instead: |
| 132 | - Use :ref:`OutputParser<components-output_parsers>` for response parsing and formatting. |
| 133 | |
| 134 | For multimodal inputs, provide images in `model_kwargs["images"]` as a path, URL, or list of them. |
| 135 | The model must support vision capabilities (e.g., `gpt-4o`, `gpt-4o-mini`, `o1`, `o1-mini`). |
| 136 | |
| 137 | For image generation, use `model_type=ModelType.IMAGE_GENERATION` and provide: |
| 138 | - model: `"dall-e-3"` or `"dall-e-2"` |
| 139 | - prompt: Text description of the image to generate |
| 140 | - size: `"1024x1024"`, `"1024x1792"`, or `"1792x1024"` for DALL-E 3; `"256x256"`, `"512x512"`, or `"1024x1024"` for DALL-E 2 |
| 141 | - quality: `"standard"` or `"hd"` (DALL-E 3 only) |
| 142 | - n: Number of images to generate (1 for DALL-E 3, 1-10 for DALL-E 2) |
| 143 | - response_format: `"url"` or `"b64_json"` |
| 144 | |
| 145 | Args: |
| 146 | api_key (Optional[str], optional): OpenAI API key. Defaults to `None`. |
| 147 | chat_completion_parser (Callable[[Completion], Any], optional): A function to parse the chat completion into a `str`. Defaults to `None`. |
| 148 | The default parser is `get_first_message_content`. |
| 149 | base_url (str): The API base URL to use when initializing the client. |
| 150 | Defaults to `"https://api.openai.com"`, but can be customized for third-party API providers or self-hosted models. |
| 151 | env_api_key_name (str): The environment variable name for the API key. Defaults to `"OPENAI_API_KEY"`. |
| 152 | |
| 153 | References: |
| 154 | - OpenAI API Overview: https://platform.openai.com/docs/introduction |
| 155 | - Embeddings Guide: https://platform.openai.com/docs/guides/embeddings |
| 156 | - Chat Completion Models: https://platform.openai.com/docs/guides/text-generation |
| 157 | - Vision Models: https://platform.openai.com/docs/guides/vision |
| 158 | - Image Generation: https://platform.openai.com/docs/guides/images |
| 159 | """ |
| 160 | |
| 161 | def __init__( |
| 162 | self, |
| 163 | api_key: Optional[str] = None, |
| 164 | chat_completion_parser: Callable[[Completion], Any] = None, |
| 165 | input_type: Literal["text", "messages"] = "text", |
| 166 | base_url: Optional[str] = None, |
| 167 | env_base_url_name: str = "OPENAI_BASE_URL", |
| 168 | env_api_key_name: str = "OPENAI_API_KEY", |
| 169 | ): |
| 170 | r"""It is recommended to set the OPENAI_API_KEY environment variable instead of passing it as an argument. |
| 171 | |
| 172 | Args: |
| 173 | api_key (Optional[str], optional): OpenAI API key. Defaults to None. |
| 174 | base_url (str): The API base URL to use when initializing the client. |
| 175 | env_api_key_name (str): The environment variable name for the API key. Defaults to `"OPENAI_API_KEY"`. |
| 176 | """ |
| 177 | super().__init__() |
no outgoing calls