openai/openai-python

Public

mirrored fromhttps://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 · modecode

1# 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
9You 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/).
10
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
26### Optional dependencies
27
28Install dependencies for [`openai.embeddings_utils`](openai/embeddings_utils.py):
29
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
46## 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
73
74### Params
75All 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).
76
77### Microsoft Azure Endpoints
78
79In 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.
80In 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"
87openai.api_version = "2022-12-01"
88
89# create a completion
90completion = openai.Completion.create(engine="deployment-name", prompt="Hello world")
91
92# print the completion
93print(completion.choices[0].text)
94```
95
96Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations.
97For a detailed example of how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebooks:
98* [Using Azure completions](https://github.com/openai/openai-cookbook/tree/main/examples/azure/completions.ipynb)
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)
101
102### Microsoft Azure Active Directory Authentication
103
104In 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.
105
106
107```python
108from azure.identity import DefaultAzureCredential
109import openai
110
111# Request credential
112default_credential = DefaultAzureCredential()
113token = default_credential.get_token("https://cognitiveservices.azure.com/.default")
114
115# Setup parameters
116openai.api_type = "azure_ad"
117openai.api_key = token.token
118openai.api_base = "https://example-endpoint.openai.azure.com/"
119openai.api_version = "2022-12-01"
120
121# ...
122```
123### 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
129```sh
130# list engines
131openai api engines.list
132
133# create a completion
134openai api completions.create -e ada -p "Hello world"
135
136# generate images via DALL·E API
137openai api image.create -p "two dogs playing chess, cartoon" -n 1
138```
139
140## Example code
141
142Examples 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/).
154
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
169model_id = "text-similarity-davinci-001"
170
171# compute the embedding of the text
172embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding']
173```
174
175An 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).
176
177Examples of how to use embeddings are shared in the following Jupyter notebooks:
178
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)
186
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
189### Fine-tuning
190
191Fine-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).
192
193Examples of fine-tuning are shared in the following Jupyter notebooks:
194
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
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)
200
201Sync 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
207For more information on fine-tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation.
208
209### 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
222## 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
232## 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():
241 completion_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
258See the [usage guide](https://beta.openai.com/docs/guides/images) for more details.
259
260## Requirements
261
262- Python 3.7.1+
263
264In general, we want to support the versions of Python that our
265customers are using. If you run into problems with any version
266issues, please let us know at on our [support page](https://help.openai.com/en/).
267
268## Credit
269
270This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python).
271