openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
README.md
272lines · modecode
| 1 | # OpenAI Python Library |
| 2 | |
| 3 | The OpenAI Python library provides convenient access to the OpenAI API |
| 4 | from applications written in the Python language. It includes a |
| 5 | pre-defined set of classes for API resources that initialize |
| 6 | themselves dynamically from API responses which makes it compatible |
| 7 | with a wide range of versions of the OpenAI API. |
| 8 | |
| 9 | ## Documentation |
| 10 | |
| 11 | See the [OpenAI API docs](https://beta.openai.com/docs/api-reference?lang=python). |
| 12 | |
| 13 | ## Installation |
| 14 | |
| 15 | You don't need this source code unless you want to modify the package. If you just |
| 16 | want to use the package, just run: |
| 17 | |
| 18 | ```sh |
| 19 | pip install --upgrade openai |
| 20 | ``` |
| 21 | |
| 22 | Install from source with: |
| 23 | |
| 24 | ```sh |
| 25 | python setup.py install |
| 26 | ``` |
| 27 | |
| 28 | ### Optional dependencies |
| 29 | |
| 30 | Install dependencies for [`openapi.embeddings_utils`](openai/embeddings_utils.py): |
| 31 | |
| 32 | ```sh |
| 33 | pip install openai[embeddings] |
| 34 | ``` |
| 35 | |
| 36 | Install support for [Weights & Biases](https://wandb.me/openai-docs): |
| 37 | |
| 38 | ``` |
| 39 | pip install openai[wandb] |
| 40 | ``` |
| 41 | |
| 42 | Data libraries like `numpy` and `pandas` are not installed by default due to their size. They’re needed for some functionality of this library, but generally not for talking to the API. If you encounter a `MissingDependencyError`, install them with: |
| 43 | |
| 44 | ```sh |
| 45 | pip install openai[datalib] |
| 46 | ```` |
| 47 | |
| 48 | ## Usage |
| 49 | |
| 50 | The library needs to be configured with your account's secret key which is available on the [website](https://beta.openai.com/account/api-keys). Either set it as the `OPENAI_API_KEY` environment variable before using the library: |
| 51 | |
| 52 | ```bash |
| 53 | export OPENAI_API_KEY='sk-...' |
| 54 | ``` |
| 55 | |
| 56 | Or set `openai.api_key` to its value: |
| 57 | |
| 58 | ```python |
| 59 | import openai |
| 60 | openai.api_key = "sk-..." |
| 61 | |
| 62 | # list engines |
| 63 | engines = openai.Engine.list() |
| 64 | |
| 65 | # print the first engine's id |
| 66 | print(engines.data[0].id) |
| 67 | |
| 68 | # create a completion |
| 69 | completion = openai.Completion.create(engine="ada", prompt="Hello world") |
| 70 | |
| 71 | # print the completion |
| 72 | print(completion.choices[0].text) |
| 73 | ``` |
| 74 | |
| 75 | |
| 76 | ### Params |
| 77 | All endpoints have a `.create` method that support a `request_timeout` param. This param takes a `Union[float, Tuple[float, float]]` and will raise a `openai.error.TimeoutError` error if the request exceeds that time in seconds (See: https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts). |
| 78 | |
| 79 | ### Microsoft Azure Endpoints |
| 80 | |
| 81 | In order to use the library with Microsoft Azure endpoints, you need to set the `api_type`, `api_base` and `api_version` in addition to the `api_key`. The `api_type` must be set to 'azure' and the others correspond to the properties of your endpoint. |
| 82 | In addition, the deployment name must be passed as the engine parameter. |
| 83 | |
| 84 | ```python |
| 85 | import openai |
| 86 | openai.api_type = "azure" |
| 87 | openai.api_key = "..." |
| 88 | openai.api_base = "https://example-endpoint.openai.azure.com" |
| 89 | openai.api_version = "2022-12-01" |
| 90 | |
| 91 | # create a completion |
| 92 | completion = openai.Completion.create(engine="deployment-name", prompt="Hello world") |
| 93 | |
| 94 | # print the completion |
| 95 | print(completion.choices[0].text) |
| 96 | ``` |
| 97 | |
| 98 | Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations. |
| 99 | For a detailed example on how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebooks: |
| 100 | * [Using Azure completions](https://github.com/openai/openai-cookbook/tree/main/examples/azure/completions.ipynb) |
| 101 | * [Using Azure fine-tuning](https://github.com/openai/openai-cookbook/tree/main/examples/azure/finetuning.ipynb) |
| 102 | * [Using Azure embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb) |
| 103 | |
| 104 | ### Microsoft Azure Active Directory Authentication |
| 105 | |
| 106 | In order to use Microsoft Active Directory to authenticate to your Azure endpoint, you need to set the `api_type` to "azure_ad" and pass the acquired credential token to `api_key`. The rest of the parameters need to be set as specified in the previous section. |
| 107 | |
| 108 | |
| 109 | ```python |
| 110 | from azure.identity import DefaultAzureCredential |
| 111 | import openai |
| 112 | |
| 113 | # Request credential |
| 114 | default_credential = DefaultAzureCredential() |
| 115 | token = default_credential.get_token("https://cognitiveservices.azure.com/.default") |
| 116 | |
| 117 | # Setup parameters |
| 118 | openai.api_type = "azure_ad" |
| 119 | openai.api_key = token.token |
| 120 | openai.api_base = "https://example-endpoint.openai.azure.com/" |
| 121 | openai.api_version = "2022-12-01" |
| 122 | |
| 123 | # ... |
| 124 | ``` |
| 125 | ### Command-line interface |
| 126 | |
| 127 | This library additionally provides an `openai` command-line utility |
| 128 | which makes it easy to interact with the API from your terminal. Run |
| 129 | `openai api -h` for usage. |
| 130 | |
| 131 | ```sh |
| 132 | # list engines |
| 133 | openai api engines.list |
| 134 | |
| 135 | # create a completion |
| 136 | openai api completions.create -e ada -p "Hello world" |
| 137 | |
| 138 | # generate images via DALL·E API |
| 139 | openai api image.create -p "two dogs playing chess, cartoon" -n 1 |
| 140 | ``` |
| 141 | |
| 142 | ## Example code |
| 143 | |
| 144 | Examples of how to use this Python library to accomplish various tasks can be found in the [OpenAI Cookbook](https://github.com/openai/openai-cookbook/). It contains code examples for: |
| 145 | |
| 146 | * Classification using fine-tuning |
| 147 | * Clustering |
| 148 | * Code search |
| 149 | * Customizing embeddings |
| 150 | * Question answering from a corpus of documents |
| 151 | * Recommendations |
| 152 | * Visualization of embeddings |
| 153 | * And more |
| 154 | |
| 155 | Prior to July 2022, this OpenAI Python library hosted code examples in its examples folder, but since then all examples have been migrated to the [OpenAI Cookbook](https://github.com/openai/openai-cookbook/). |
| 156 | |
| 157 | ### Embeddings |
| 158 | |
| 159 | In the OpenAI Python library, an embedding represents a text string as a fixed-length vector of floating point numbers. Embeddings are designed to measure the similarity or relevance between text strings. |
| 160 | |
| 161 | To get an embedding for a text string, you can use the embeddings method as follows in Python: |
| 162 | |
| 163 | ```python |
| 164 | import openai |
| 165 | openai.api_key = "sk-..." # supply your API key however you choose |
| 166 | |
| 167 | # choose text to embed |
| 168 | text_string = "sample text" |
| 169 | |
| 170 | # choose an embedding |
| 171 | model_id = "text-similarity-davinci-001" |
| 172 | |
| 173 | # compute the embedding of the text |
| 174 | embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding'] |
| 175 | ``` |
| 176 | |
| 177 | An example of how to call the embeddings method is shown in this [get embeddings notebook](https://github.com/openai/openai-cookbook/blob/main/examples/Get_embeddings.ipynb). |
| 178 | |
| 179 | Examples of how to use embeddings are shared in the following Jupyter notebooks: |
| 180 | |
| 181 | - [Classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Classification_using_embeddings.ipynb) |
| 182 | - [Clustering using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Clustering.ipynb) |
| 183 | - [Code search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Code_search.ipynb) |
| 184 | - [Semantic text search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Semantic_text_search_using_embeddings.ipynb) |
| 185 | - [User and product embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/User_and_product_embeddings.ipynb) |
| 186 | - [Zero-shot classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Zero-shot_classification_with_embeddings.ipynb) |
| 187 | - [Recommendation using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Recommendation_using_embeddings.ipynb) |
| 188 | |
| 189 | For more information on embeddings and the types of embeddings OpenAI offers, read the [embeddings guide](https://beta.openai.com/docs/guides/embeddings) in the OpenAI documentation. |
| 190 | |
| 191 | ### Fine tuning |
| 192 | |
| 193 | Fine tuning a model on training data can both improve the results (by giving the model more examples to learn from) and reduce the cost/latency of API calls (chiefly through reducing the need to include training examples in prompts). |
| 194 | |
| 195 | Examples of fine tuning are shared in the following Jupyter notebooks: |
| 196 | |
| 197 | - [Classification with fine tuning](https://github.com/openai/openai-cookbook/blob/main/examples/Fine-tuned_classification.ipynb) (a simple notebook that shows the steps required for fine tuning) |
| 198 | - Fine tuning a model that answers questions about the 2020 Olympics |
| 199 | - [Step 1: Collecting data](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb) |
| 200 | - [Step 2: Creating a synthetic Q&A dataset](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb) |
| 201 | - [Step 3: Train a fine-tuning model specialized for Q&A](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb) |
| 202 | |
| 203 | Sync your fine-tunes to [Weights & Biases](https://wandb.me/openai-docs) to track experiments, models, and datasets in your central dashboard with: |
| 204 | |
| 205 | ```bash |
| 206 | openai wandb sync |
| 207 | ``` |
| 208 | |
| 209 | For more information on fine tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation. |
| 210 | |
| 211 | ### Moderation |
| 212 | |
| 213 | OpenAI provides a Moderation endpoint that can be used to check whether content complies with the OpenAI [content policy](https://beta.openai.com/docs/usage-policies) |
| 214 | |
| 215 | ```python |
| 216 | import openai |
| 217 | openai.api_key = "sk-..." # supply your API key however you choose |
| 218 | |
| 219 | moderation_resp = openai.Moderation.create(input="Here is some perfectly innocuous text that follows all OpenAI content policies.") |
| 220 | ``` |
| 221 | |
| 222 | See the [moderation guide](https://beta.openai.com/docs/guides/moderation) for more details. |
| 223 | |
| 224 | ## Image generation (DALL·E) |
| 225 | |
| 226 | ```python |
| 227 | import openai |
| 228 | openai.api_key = "sk-..." # supply your API key however you choose |
| 229 | |
| 230 | image_resp = openai.Image.create(prompt="two dogs playing chess, oil painting", n=4, size="512x512") |
| 231 | |
| 232 | ``` |
| 233 | |
| 234 | ## Async API |
| 235 | |
| 236 | Async support is available in the API by prepending `a` to a network-bound method: |
| 237 | |
| 238 | ```python |
| 239 | import openai |
| 240 | openai.api_key = "sk-..." # supply your API key however you choose |
| 241 | |
| 242 | async def create_completion(): |
| 243 | completion_resp = await openai.Completion.acreate(prompt="This is a test", engine="davinci") |
| 244 | |
| 245 | ``` |
| 246 | |
| 247 | To make async requests more efficient, you can pass in your own |
| 248 | ``aiohttp.ClientSession``, but you must manually close the client session at the end |
| 249 | of your program/event loop: |
| 250 | |
| 251 | ```python |
| 252 | import openai |
| 253 | from aiohttp import ClientSession |
| 254 | |
| 255 | openai.aiosession.set(ClientSession()) |
| 256 | # At the end of your program, close the http session |
| 257 | await openai.aiosession.get().close() |
| 258 | ``` |
| 259 | |
| 260 | See the [usage guide](https://beta.openai.com/docs/guides/images) for more details. |
| 261 | |
| 262 | ## Requirements |
| 263 | |
| 264 | - Python 3.7.1+ |
| 265 | |
| 266 | In general, we want to support the versions of Python that our |
| 267 | customers are using. If you run into problems with any version |
| 268 | issues, please let us know at support@openai.com. |
| 269 | |
| 270 | ## Credit |
| 271 | |
| 272 | This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python). |
| 273 | |