openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.26.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

README.md

272lines · 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
9## Documentation
10
11See the [OpenAI API docs](https://beta.openai.com/docs/api-reference?lang=python).
12
13## Installation
14
15You don't need this source code unless you want to modify the package. If you just
16want to use the package, just run:
17
18```sh
19pip install --upgrade openai
20```
21
22Install from source with:
23
24```sh
25python setup.py install
26```
27
28### Optional dependencies
29
30Install dependencies for [`openapi.embeddings_utils`](openai/embeddings_utils.py):
31
32```sh
33pip install openai[embeddings]
34```
35
36Install support for [Weights & Biases](https://wandb.me/openai-docs):
37
38```
39pip install openai[wandb]
40```
41
42Data 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
45pip install openai[datalib]
46````
47
48## Usage
49
50The 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
53export OPENAI_API_KEY='sk-...'
54```
55
56Or set `openai.api_key` to its value:
57
58```python
59import openai
60openai.api_key = "sk-..."
61
62# list engines
63engines = openai.Engine.list()
64
65# print the first engine's id
66print(engines.data[0].id)
67
68# create a completion
69completion = openai.Completion.create(engine="ada", prompt="Hello world")
70
71# print the completion
72print(completion.choices[0].text)
73```
74
75
76### Params
77All 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
81In 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.
82In addition, the deployment name must be passed as the engine parameter.
83
84```python
85import openai
86openai.api_type = "azure"
87openai.api_key = "..."
88openai.api_base = "https://example-endpoint.openai.azure.com"
89openai.api_version = "2022-12-01"
90
91# create a completion
92completion = openai.Completion.create(engine="deployment-name", prompt="Hello world")
93
94# print the completion
95print(completion.choices[0].text)
96```
97
98Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations.
99For 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
106In 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
110from azure.identity import DefaultAzureCredential
111import openai
112
113# Request credential
114default_credential = DefaultAzureCredential()
115token = default_credential.get_token("https://cognitiveservices.azure.com/.default")
116
117# Setup parameters
118openai.api_type = "azure_ad"
119openai.api_key = token.token
120openai.api_base = "https://example-endpoint.openai.azure.com/"
121openai.api_version = "2022-12-01"
122
123# ...
124```
125### Command-line interface
126
127This library additionally provides an `openai` command-line utility
128which makes it easy to interact with the API from your terminal. Run
129`openai api -h` for usage.
130
131```sh
132# list engines
133openai api engines.list
134
135# create a completion
136openai api completions.create -e ada -p "Hello world"
137
138# generate images via DALL·E API
139openai api image.create -p "two dogs playing chess, cartoon" -n 1
140```
141
142## Example code
143
144Examples 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
155Prior 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
159In 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
161To get an embedding for a text string, you can use the embeddings method as follows in Python:
162
163```python
164import openai
165openai.api_key = "sk-..." # supply your API key however you choose
166
167# choose text to embed
168text_string = "sample text"
169
170# choose an embedding
171model_id = "text-similarity-davinci-001"
172
173# compute the embedding of the text
174embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding']
175```
176
177An 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
179Examples 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
189For 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
193Fine 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
195Examples 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
203Sync 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
206openai wandb sync
207```
208
209For 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
213OpenAI 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
216import openai
217openai.api_key = "sk-..." # supply your API key however you choose
218
219moderation_resp = openai.Moderation.create(input="Here is some perfectly innocuous text that follows all OpenAI content policies.")
220```
221
222See the [moderation guide](https://beta.openai.com/docs/guides/moderation) for more details.
223
224## Image generation (DALL·E)
225
226```python
227import openai
228openai.api_key = "sk-..." # supply your API key however you choose
229
230image_resp = openai.Image.create(prompt="two dogs playing chess, oil painting", n=4, size="512x512")
231
232```
233
234## Async API
235
236Async support is available in the API by prepending `a` to a network-bound method:
237
238```python
239import openai
240openai.api_key = "sk-..." # supply your API key however you choose
241
242async def create_completion():
243 completion_resp = await openai.Completion.acreate(prompt="This is a test", engine="davinci")
244
245```
246
247To 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
249of your program/event loop:
250
251```python
252import openai
253from aiohttp import ClientSession
254
255openai.aiosession.set(ClientSession())
256# At the end of your program, close the http session
257await openai.aiosession.get().close()
258```
259
260See the [usage guide](https://beta.openai.com/docs/guides/images) for more details.
261
262## Requirements
263
264- Python 3.7.1+
265
266In general, we want to support the versions of Python that our
267customers are using. If you run into problems with any version
268issues, please let us know at support@openai.com.
269
270## Credit
271
272This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python).
273