openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.27.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

README.md

294lines · 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://platform.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 models
61models = openai.Model.list()
62
63# print the first model's id
64print(models.data[0].id)
65
66# create a completion
67completion = openai.Completion.create(model="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 models
131openai api models.list
132
133# create a completion
134openai api completions.create -m ada -p "Hello world"
135
136# create a chat completion
137openai api chat_completions.create -m gpt-3.5-turbo -g user "Hello world"
138
139# generate images via DALL·E API
140openai api image.create -p "two dogs playing chess, cartoon" -n 1
141```
142
143## Example code
144
145Examples 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:
146
147* Classification using fine-tuning
148* Clustering
149* Code search
150* Customizing embeddings
151* Question answering from a corpus of documents
152* Recommendations
153* Visualization of embeddings
154* And more
155
156Prior 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/).
157
158### Chat
159
160Conversational models such as `gpt-3.5-turbo` can be called using the chat completions endpoint.
161
162```python
163import openai
164openai.api_key = "sk-..." # supply your API key however you choose
165
166completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world!"}])
167print(completion.choices[0].message.content)
168```
169
170### Embeddings
171
172In 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.
173
174To get an embedding for a text string, you can use the embeddings method as follows in Python:
175
176```python
177import openai
178openai.api_key = "sk-..." # supply your API key however you choose
179
180# choose text to embed
181text_string = "sample text"
182
183# choose an embedding
184model_id = "text-similarity-davinci-001"
185
186# compute the embedding of the text
187embedding = openai.Embedding.create(input=text_string, model=model_id)['data'][0]['embedding']
188```
189
190An 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).
191
192Examples of how to use embeddings are shared in the following Jupyter notebooks:
193
194- [Classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Classification_using_embeddings.ipynb)
195- [Clustering using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Clustering.ipynb)
196- [Code search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Code_search.ipynb)
197- [Semantic text search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Semantic_text_search_using_embeddings.ipynb)
198- [User and product embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/User_and_product_embeddings.ipynb)
199- [Zero-shot classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Zero-shot_classification_with_embeddings.ipynb)
200- [Recommendation using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Recommendation_using_embeddings.ipynb)
201
202For 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.
203
204### Fine-tuning
205
206Fine-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).
207
208Examples of fine-tuning are shared in the following Jupyter notebooks:
209
210- [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)
211- Fine-tuning a model that answers questions about the 2020 Olympics
212 - [Step 1: Collecting data](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb)
213 - [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)
214 - [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)
215
216Sync your fine-tunes to [Weights & Biases](https://wandb.me/openai-docs) to track experiments, models, and datasets in your central dashboard with:
217
218```bash
219openai wandb sync
220```
221
222For more information on fine-tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation.
223
224### Moderation
225
226OpenAI provides a Moderation endpoint that can be used to check whether content complies with the OpenAI [content policy](https://platform.openai.com/docs/usage-policies)
227
228```python
229import openai
230openai.api_key = "sk-..." # supply your API key however you choose
231
232moderation_resp = openai.Moderation.create(input="Here is some perfectly innocuous text that follows all OpenAI content policies.")
233```
234
235See the [moderation guide](https://platform.openai.com/docs/guides/moderation) for more details.
236
237## Image generation (DALL·E)
238
239```python
240import openai
241openai.api_key = "sk-..." # supply your API key however you choose
242
243image_resp = openai.Image.create(prompt="two dogs playing chess, oil painting", n=4, size="512x512")
244
245```
246
247## Audio transcription (Whisper)
248```python
249import openai
250openai.api_key = "sk-..." # supply your API key however you choose
251f = open("path/to/file.mp3", "rb")
252transcript = openai.Audio.transcribe("whisper-1", f)
253
254```
255
256## Async API
257
258Async support is available in the API by prepending `a` to a network-bound method:
259
260```python
261import openai
262openai.api_key = "sk-..." # supply your API key however you choose
263
264async def create_completion():
265 completion_resp = await openai.Completion.acreate(prompt="This is a test", model="davinci")
266
267```
268
269To make async requests more efficient, you can pass in your own
270``aiohttp.ClientSession``, but you must manually close the client session at the end
271of your program/event loop:
272
273```python
274import openai
275from aiohttp import ClientSession
276
277openai.aiosession.set(ClientSession())
278# At the end of your program, close the http session
279await openai.aiosession.get().close()
280```
281
282See the [usage guide](https://platform.openai.com/docs/guides/images) for more details.
283
284## Requirements
285
286- Python 3.7.1+
287
288In general, we want to support the versions of Python that our
289customers are using. If you run into problems with any version
290issues, please let us know at on our [support page](https://help.openai.com/en/).
291
292## Credit
293
294This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python).
295