openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.26.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

README.md

270lines · modeblame

3c6d4cd6Greg Brockman5 years ago1# OpenAI Python Library
2
3The OpenAI Python library provides convenient access to the OpenAI API
4from applications written in the Python language. It includes a
5pre-defined set of classes for API resources that initialize
6themselves dynamically from API responses which makes it compatible
7with a wide range of versions of the OpenAI API.
8
bf51385aLogan Kilpatrick3 years ago9You 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 ago10
11## Installation
12
13You don't need this source code unless you want to modify the package. If you just
14want to use the package, just run:
15
16```sh
17pip install --upgrade openai
18```
19
20Install from source with:
21
22```sh
23python setup.py install
24```
25
ede08829Jakub Roztocil3 years ago26### Optional dependencies
27
777c1c3dJosh Bode3 years ago28Install dependencies for [`openai.embeddings_utils`](openai/embeddings_utils.py):
ede08829Jakub Roztocil3 years ago29
30```sh
31pip install openai[embeddings]
32```
33
34Install support for [Weights & Biases](https://wandb.me/openai-docs):
35
36```
37pip install openai[wandb]
38```
39
40Data 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
43pip install openai[datalib]
44````
45
d53d9efbRachel Lim5 years ago46## Usage
47
48The 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
51export OPENAI_API_KEY='sk-...'
52```
53
54Or set `openai.api_key` to its value:
55
56```python
57import openai
58openai.api_key = "sk-..."
59
60# list engines
61engines = openai.Engine.list()
62
63# print the first engine's id
64print(engines.data[0].id)
65
66# create a completion
67completion = openai.Completion.create(engine="ada", prompt="Hello world")
68
69# print the completion
70print(completion.choices[0].text)
71```
72
3edecbeehallacy3 years ago73
74### Params
4a33f3f1Agnieszka Stec3 years ago75All 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 ago76
f288b001Sorin Suciu4 years ago77### Microsoft Azure Endpoints
78
ec4943f9Christian Mürtz3 years ago79In 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 ago80In addition, the deployment name must be passed as the engine parameter.
81
82```python
83import openai
84openai.api_type = "azure"
85openai.api_key = "..."
86openai.api_base = "https://example-endpoint.openai.azure.com"
ec4943f9Christian Mürtz3 years ago87openai.api_version = "2022-12-01"
f288b001Sorin Suciu4 years ago88
89# create a completion
53e5ba4bt-asutedjo4 years ago90completion = openai.Completion.create(engine="deployment-name", prompt="Hello world")
f288b001Sorin Suciu4 years ago91
92# print the completion
93print(completion.choices[0].text)
94```
62b51ca0Boris Dayma4 years ago95
ec4943f9Christian Mürtz3 years ago96Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations.
4a33f3f1Agnieszka Stec3 years ago97For 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 ago98* [Using Azure completions](https://github.com/openai/openai-cookbook/tree/main/examples/azure/completions.ipynb)
7884a7b9Ted Sanders3 years ago99* [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 ago101
53e5ba4bt-asutedjo4 years ago102### Microsoft Azure Active Directory Authentication
103
ec4943f9Christian Mürtz3 years ago104In 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 ago105
106
107```python
108from azure.identity import DefaultAzureCredential
109import openai
110
111# Request credential
112default_credential = DefaultAzureCredential()
ec4943f9Christian Mürtz3 years ago113token = default_credential.get_token("https://cognitiveservices.azure.com/.default")
53e5ba4bt-asutedjo4 years ago114
115# Setup parameters
116openai.api_type = "azure_ad"
117openai.api_key = token.token
118openai.api_base = "https://example-endpoint.openai.azure.com/"
ec4943f9Christian Mürtz3 years ago119openai.api_version = "2022-12-01"
53e5ba4bt-asutedjo4 years ago120
121# ...
122```
d53d9efbRachel Lim5 years ago123### Command-line interface
124
125This library additionally provides an `openai` command-line utility
126which makes it easy to interact with the API from your terminal. Run
127`openai api -h` for usage.
128
0e21703eTed Sanders4 years ago129```sh
d53d9efbRachel Lim5 years ago130# list engines
131openai api engines.list
132
133# create a completion
134openai api completions.create -e ada -p "Hello world"
dc33cb9dMichelle Pokrass3 years ago135
136# generate images via DALL·E API
137openai api image.create -p "two dogs playing chess, cartoon" -n 1
d53d9efbRachel Lim5 years ago138```
139
0e21703eTed Sanders4 years ago140## Example code
141
7884a7b9Ted Sanders3 years ago142Examples 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
153Prior 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 ago154
155### Embeddings
156
157In 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
159To get an embedding for a text string, you can use the embeddings method as follows in Python:
160
161```python
162import openai
163openai.api_key = "sk-..." # supply your API key however you choose
164
165# choose text to embed
166text_string = "sample text"
167
168# choose an embedding
f4be8f2fhallacy4 years ago169model_id = "text-similarity-davinci-001"
0e21703eTed Sanders4 years ago170
171# compute the embedding of the text
f4be8f2fhallacy4 years ago172embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding']
0e21703eTed Sanders4 years ago173```
174
7884a7b9Ted Sanders3 years ago175An 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 ago176
177Examples of how to use embeddings are shared in the following Jupyter notebooks:
178
7884a7b9Ted Sanders3 years ago179- [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 ago186
187For 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 ago189### Fine-tuning
0e21703eTed Sanders4 years ago190
4a33f3f1Agnieszka Stec3 years ago191Fine-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 ago192
4a33f3f1Agnieszka Stec3 years ago193Examples of fine-tuning are shared in the following Jupyter notebooks:
0e21703eTed Sanders4 years ago194
4a33f3f1Agnieszka Stec3 years ago195- [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 ago197- [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 ago200
62b51ca0Boris Dayma4 years ago201Sync 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
204openai wandb sync
205```
206
4a33f3f1Agnieszka Stec3 years ago207For 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 ago208
3c00e856hallacy3 years ago209### Moderation
210
211OpenAI 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
214import openai
215openai.api_key = "sk-..." # supply your API key however you choose
216
217moderation_resp = openai.Moderation.create(input="Here is some perfectly innocuous text that follows all OpenAI content policies.")
218```
219
220See the [moderation guide](https://beta.openai.com/docs/guides/moderation) for more details.
221
dc33cb9dMichelle Pokrass3 years ago222## Image generation (DALL·E)
223
224```python
225import openai
226openai.api_key = "sk-..." # supply your API key however you choose
227
228image_resp = openai.Image.create(prompt="two dogs playing chess, oil painting", n=4, size="512x512")
229
230```
231
0abf6413Andrew Chen Wang3 years ago232## Async API
233
234Async support is available in the API by prepending `a` to a network-bound method:
235
236```python
237import openai
238openai.api_key = "sk-..." # supply your API key however you choose
239
240async def create_completion():
241completion_resp = await openai.Completion.acreate(prompt="This is a test", engine="davinci")
242
243```
244
245To 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
247of your program/event loop:
248
249```python
250import openai
251from aiohttp import ClientSession
252
253openai.aiosession.set(ClientSession())
254# At the end of your program, close the http session
255await openai.aiosession.get().close()
256```
257
dc33cb9dMichelle Pokrass3 years ago258See the [usage guide](https://beta.openai.com/docs/guides/images) for more details.
259
3c6d4cd6Greg Brockman5 years ago260## Requirements
261
62f8d40fMadeleine Thompson4 years ago262- Python 3.7.1+
3c6d4cd6Greg Brockman5 years ago263
34a12097Chris3 years ago264In general, we want to support the versions of Python that our
265customers are using. If you run into problems with any version
fb6232f7Logan Kilpatrick3 years ago266issues, please let us know at on our [support page](https://help.openai.com/en/).
3c6d4cd6Greg Brockman5 years ago267
268## Credit
269
270This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python).