openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
README.md
270lines · modeblame
3c6d4cd6Greg Brockman5 years ago | 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 | | |
bf51385aLogan Kilpatrick3 years ago | 9 | You can find usage examples for the OpenAI Python library in our [API reference](https://beta.openai.com/docs/api-reference?lang=python) and the [OpenAI Cookbook](https://github.com/openai/openai-cookbook/). |
3c6d4cd6Greg Brockman5 years ago | 10 | |
| 11 | ## Installation | |
| 12 | | |
| 13 | You don't need this source code unless you want to modify the package. If you just | |
| 14 | want to use the package, just run: | |
| 15 | | |
| 16 | ```sh | |
| 17 | pip install --upgrade openai | |
| 18 | ``` | |
| 19 | | |
| 20 | Install from source with: | |
| 21 | | |
| 22 | ```sh | |
| 23 | python setup.py install | |
| 24 | ``` | |
| 25 | | |
ede08829Jakub Roztocil3 years ago | 26 | ### Optional dependencies |
| 27 | | |
777c1c3dJosh Bode3 years ago | 28 | Install dependencies for [`openai.embeddings_utils`](openai/embeddings_utils.py): |
ede08829Jakub Roztocil3 years ago | 29 | |
| 30 | ```sh | |
| 31 | pip install openai[embeddings] | |
| 32 | ``` | |
| 33 | | |
| 34 | Install support for [Weights & Biases](https://wandb.me/openai-docs): | |
| 35 | | |
| 36 | ``` | |
| 37 | pip install openai[wandb] | |
| 38 | ``` | |
| 39 | | |
| 40 | 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: | |
| 41 | | |
| 42 | ```sh | |
| 43 | pip install openai[datalib] | |
| 44 | ```` | |
| 45 | | |
d53d9efbRachel Lim5 years ago | 46 | ## Usage |
| 47 | | |
| 48 | 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: | |
| 49 | | |
| 50 | ```bash | |
| 51 | export OPENAI_API_KEY='sk-...' | |
| 52 | ``` | |
| 53 | | |
| 54 | Or set `openai.api_key` to its value: | |
| 55 | | |
| 56 | ```python | |
| 57 | import openai | |
| 58 | openai.api_key = "sk-..." | |
| 59 | | |
| 60 | # list engines | |
| 61 | engines = openai.Engine.list() | |
| 62 | | |
| 63 | # print the first engine's id | |
| 64 | print(engines.data[0].id) | |
| 65 | | |
| 66 | # create a completion | |
| 67 | completion = openai.Completion.create(engine="ada", prompt="Hello world") | |
| 68 | | |
| 69 | # print the completion | |
| 70 | print(completion.choices[0].text) | |
| 71 | ``` | |
| 72 | | |
3edecbeehallacy3 years ago | 73 | |
| 74 | ### Params | |
4a33f3f1Agnieszka Stec3 years ago | 75 | All endpoints have a `.create` method that supports a `request_timeout` param. This param takes a `Union[float, Tuple[float, float]]` and will raise an `openai.error.TimeoutError` error if the request exceeds that time in seconds (See: https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts). |
3edecbeehallacy3 years ago | 76 | |
f288b001Sorin Suciu4 years ago | 77 | ### Microsoft Azure Endpoints |
| 78 | | |
ec4943f9Christian Mürtz3 years ago | 79 | 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. |
f288b001Sorin Suciu4 years ago | 80 | In addition, the deployment name must be passed as the engine parameter. |
| 81 | | |
| 82 | ```python | |
| 83 | import openai | |
| 84 | openai.api_type = "azure" | |
| 85 | openai.api_key = "..." | |
| 86 | openai.api_base = "https://example-endpoint.openai.azure.com" | |
ec4943f9Christian Mürtz3 years ago | 87 | openai.api_version = "2022-12-01" |
f288b001Sorin Suciu4 years ago | 88 | |
| 89 | # create a completion | |
53e5ba4bt-asutedjo4 years ago | 90 | completion = openai.Completion.create(engine="deployment-name", prompt="Hello world") |
f288b001Sorin Suciu4 years ago | 91 | |
| 92 | # print the completion | |
| 93 | print(completion.choices[0].text) | |
| 94 | ``` | |
62b51ca0Boris Dayma4 years ago | 95 | |
ec4943f9Christian Mürtz3 years ago | 96 | Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations. |
4a33f3f1Agnieszka Stec3 years ago | 97 | For a detailed example of how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebooks: |
ec4943f9Christian Mürtz3 years ago | 98 | * [Using Azure completions](https://github.com/openai/openai-cookbook/tree/main/examples/azure/completions.ipynb) |
7884a7b9Ted Sanders3 years ago | 99 | * [Using Azure fine-tuning](https://github.com/openai/openai-cookbook/tree/main/examples/azure/finetuning.ipynb) |
| 100 | * [Using Azure embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb) | |
f288b001Sorin Suciu4 years ago | 101 | |
53e5ba4bt-asutedjo4 years ago | 102 | ### Microsoft Azure Active Directory Authentication |
| 103 | | |
ec4943f9Christian Mürtz3 years ago | 104 | 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. |
53e5ba4bt-asutedjo4 years ago | 105 | |
| 106 | | |
| 107 | ```python | |
| 108 | from azure.identity import DefaultAzureCredential | |
| 109 | import openai | |
| 110 | | |
| 111 | # Request credential | |
| 112 | default_credential = DefaultAzureCredential() | |
ec4943f9Christian Mürtz3 years ago | 113 | token = default_credential.get_token("https://cognitiveservices.azure.com/.default") |
53e5ba4bt-asutedjo4 years ago | 114 | |
| 115 | # Setup parameters | |
| 116 | openai.api_type = "azure_ad" | |
| 117 | openai.api_key = token.token | |
| 118 | openai.api_base = "https://example-endpoint.openai.azure.com/" | |
ec4943f9Christian Mürtz3 years ago | 119 | openai.api_version = "2022-12-01" |
53e5ba4bt-asutedjo4 years ago | 120 | |
| 121 | # ... | |
| 122 | ``` | |
d53d9efbRachel Lim5 years ago | 123 | ### Command-line interface |
| 124 | | |
| 125 | This library additionally provides an `openai` command-line utility | |
| 126 | which makes it easy to interact with the API from your terminal. Run | |
| 127 | `openai api -h` for usage. | |
| 128 | | |
0e21703eTed Sanders4 years ago | 129 | ```sh |
d53d9efbRachel Lim5 years ago | 130 | # list engines |
| 131 | openai api engines.list | |
| 132 | | |
| 133 | # create a completion | |
| 134 | openai api completions.create -e ada -p "Hello world" | |
dc33cb9dMichelle Pokrass3 years ago | 135 | |
| 136 | # generate images via DALL·E API | |
| 137 | openai api image.create -p "two dogs playing chess, cartoon" -n 1 | |
d53d9efbRachel Lim5 years ago | 138 | ``` |
| 139 | | |
0e21703eTed Sanders4 years ago | 140 | ## Example code |
| 141 | | |
7884a7b9Ted Sanders3 years ago | 142 | 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: |
| 143 | | |
| 144 | * Classification using fine-tuning | |
| 145 | * Clustering | |
| 146 | * Code search | |
| 147 | * Customizing embeddings | |
| 148 | * Question answering from a corpus of documents | |
| 149 | * Recommendations | |
| 150 | * Visualization of embeddings | |
| 151 | * And more | |
| 152 | | |
| 153 | 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/). | |
0e21703eTed Sanders4 years ago | 154 | |
| 155 | ### Embeddings | |
| 156 | | |
| 157 | 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. | |
| 158 | | |
| 159 | To get an embedding for a text string, you can use the embeddings method as follows in Python: | |
| 160 | | |
| 161 | ```python | |
| 162 | import openai | |
| 163 | openai.api_key = "sk-..." # supply your API key however you choose | |
| 164 | | |
| 165 | # choose text to embed | |
| 166 | text_string = "sample text" | |
| 167 | | |
| 168 | # choose an embedding | |
f4be8f2fhallacy4 years ago | 169 | model_id = "text-similarity-davinci-001" |
0e21703eTed Sanders4 years ago | 170 | |
| 171 | # compute the embedding of the text | |
f4be8f2fhallacy4 years ago | 172 | embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding'] |
0e21703eTed Sanders4 years ago | 173 | ``` |
| 174 | | |
7884a7b9Ted Sanders3 years ago | 175 | 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). |
0e21703eTed Sanders4 years ago | 176 | |
| 177 | Examples of how to use embeddings are shared in the following Jupyter notebooks: | |
| 178 | | |
7884a7b9Ted Sanders3 years ago | 179 | - [Classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Classification_using_embeddings.ipynb) |
| 180 | - [Clustering using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Clustering.ipynb) | |
| 181 | - [Code search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Code_search.ipynb) | |
| 182 | - [Semantic text search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Semantic_text_search_using_embeddings.ipynb) | |
| 183 | - [User and product embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/User_and_product_embeddings.ipynb) | |
| 184 | - [Zero-shot classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Zero-shot_classification_with_embeddings.ipynb) | |
| 185 | - [Recommendation using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Recommendation_using_embeddings.ipynb) | |
0e21703eTed Sanders4 years ago | 186 | |
| 187 | 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. | |
| 188 | | |
4a33f3f1Agnieszka Stec3 years ago | 189 | ### Fine-tuning |
0e21703eTed Sanders4 years ago | 190 | |
4a33f3f1Agnieszka Stec3 years ago | 191 | 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). |
0e21703eTed Sanders4 years ago | 192 | |
4a33f3f1Agnieszka Stec3 years ago | 193 | Examples of fine-tuning are shared in the following Jupyter notebooks: |
0e21703eTed Sanders4 years ago | 194 | |
4a33f3f1Agnieszka Stec3 years ago | 195 | - [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) |
| 196 | - Fine-tuning a model that answers questions about the 2020 Olympics | |
7884a7b9Ted Sanders3 years ago | 197 | - [Step 1: Collecting data](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb) |
| 198 | - [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) | |
| 199 | - [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) | |
0e21703eTed Sanders4 years ago | 200 | |
62b51ca0Boris Dayma4 years ago | 201 | Sync your fine-tunes to [Weights & Biases](https://wandb.me/openai-docs) to track experiments, models, and datasets in your central dashboard with: |
| 202 | | |
| 203 | ```bash | |
| 204 | openai wandb sync | |
| 205 | ``` | |
| 206 | | |
4a33f3f1Agnieszka Stec3 years ago | 207 | For more information on fine-tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation. |
0e21703eTed Sanders4 years ago | 208 | |
3c00e856hallacy3 years ago | 209 | ### Moderation |
| 210 | | |
| 211 | 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) | |
| 212 | | |
| 213 | ```python | |
| 214 | import openai | |
| 215 | openai.api_key = "sk-..." # supply your API key however you choose | |
| 216 | | |
| 217 | moderation_resp = openai.Moderation.create(input="Here is some perfectly innocuous text that follows all OpenAI content policies.") | |
| 218 | ``` | |
| 219 | | |
| 220 | See the [moderation guide](https://beta.openai.com/docs/guides/moderation) for more details. | |
| 221 | | |
dc33cb9dMichelle Pokrass3 years ago | 222 | ## Image generation (DALL·E) |
| 223 | | |
| 224 | ```python | |
| 225 | import openai | |
| 226 | openai.api_key = "sk-..." # supply your API key however you choose | |
| 227 | | |
| 228 | image_resp = openai.Image.create(prompt="two dogs playing chess, oil painting", n=4, size="512x512") | |
| 229 | | |
| 230 | ``` | |
| 231 | | |
0abf6413Andrew Chen Wang3 years ago | 232 | ## Async API |
| 233 | | |
| 234 | Async support is available in the API by prepending `a` to a network-bound method: | |
| 235 | | |
| 236 | ```python | |
| 237 | import openai | |
| 238 | openai.api_key = "sk-..." # supply your API key however you choose | |
| 239 | | |
| 240 | async def create_completion(): | |
| 241 | completion_resp = await openai.Completion.acreate(prompt="This is a test", engine="davinci") | |
| 242 | | |
| 243 | ``` | |
| 244 | | |
| 245 | To make async requests more efficient, you can pass in your own | |
| 246 | ``aiohttp.ClientSession``, but you must manually close the client session at the end | |
| 247 | of your program/event loop: | |
| 248 | | |
| 249 | ```python | |
| 250 | import openai | |
| 251 | from aiohttp import ClientSession | |
| 252 | | |
| 253 | openai.aiosession.set(ClientSession()) | |
| 254 | # At the end of your program, close the http session | |
| 255 | await openai.aiosession.get().close() | |
| 256 | ``` | |
| 257 | | |
dc33cb9dMichelle Pokrass3 years ago | 258 | See the [usage guide](https://beta.openai.com/docs/guides/images) for more details. |
| 259 | | |
3c6d4cd6Greg Brockman5 years ago | 260 | ## Requirements |
| 261 | | |
62f8d40fMadeleine Thompson4 years ago | 262 | - Python 3.7.1+ |
3c6d4cd6Greg Brockman5 years ago | 263 | |
34a12097Chris3 years ago | 264 | In general, we want to support the versions of Python that our |
| 265 | customers are using. If you run into problems with any version | |
fb6232f7Logan Kilpatrick3 years ago | 266 | issues, please let us know at on our [support page](https://help.openai.com/en/). |
3c6d4cd6Greg Brockman5 years ago | 267 | |
| 268 | ## Credit | |
| 269 | | |
| 270 | This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python). |