Skip to content

Commit 8ef366e

Browse files
logankilpatrickmegamanics
authored andcommitted
Revamp README to make examples front and center (openai#603)
* Revamp README * Add completions guides * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md
1 parent 9946a36 commit 8ef366e

File tree

1 file changed

+100
-161
lines changed

1 file changed

+100
-161
lines changed

README.md

Lines changed: 100 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ You can find usage examples for the OpenAI Python library in our [API reference]
1010

1111
## Installation
1212

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:
13+
To start, ensure you have Python 3.7.1 or newer. If you just
14+
want to use the package, run:
1515

1616
```sh
1717
pip install --upgrade openai
1818
```
1919

20-
Install from source with:
20+
After you have installed the package, import it at the top of a file:
21+
22+
```python
23+
import openai
24+
```
25+
26+
To install this package from source to make modifications to it, run the following command from the root of the repository:
2127

2228
```sh
2329
python setup.py install
@@ -33,7 +39,7 @@ pip install openai[embeddings]
3339

3440
Install support for [Weights & Biases](https://wandb.me/openai-docs):
3541

36-
```
42+
```sh
3743
pip install openai[wandb]
3844
```
3945

@@ -54,168 +60,48 @@ export OPENAI_API_KEY='sk-...'
5460
Or set `openai.api_key` to its value:
5561

5662
```python
57-
import openai
5863
openai.api_key = "sk-..."
59-
60-
# list models
61-
models = openai.Model.list()
62-
63-
# print the first model's id
64-
print(models.data[0].id)
65-
66-
# create a chat completion
67-
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
68-
69-
# print the chat completion
70-
print(chat_completion.choices[0].message.content)
71-
```
72-
73-
### Params
74-
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.Timeout` 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-
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.
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"
87-
openai.api_version = "2023-05-15"
88-
89-
# create a chat completion
90-
chat_completion = openai.ChatCompletion.create(deployment_id="deployment-name", model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
91-
92-
# print the completion
93-
print(chat_completion.choices[0].message.content)
94-
```
95-
96-
Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations.
97-
For a detailed example of how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebooks:
98-
99-
- [Using Azure completions](https://github.com/openai/openai-cookbook/tree/main/examples/azure/completions.ipynb)
100-
- [Using Azure fine-tuning](https://github.com/openai/openai-cookbook/tree/main/examples/azure/finetuning.ipynb)
101-
- [Using Azure embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb)
102-
103-
### Microsoft Azure Active Directory Authentication
104-
105-
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.
106-
107-
```python
108-
from azure.identity import DefaultAzureCredential
109-
import openai
110-
111-
# Request credential
112-
default_credential = DefaultAzureCredential()
113-
token = default_credential.get_token("https://cognitiveservices.azure.com/.default")
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/"
119-
openai.api_version = "2023-05-15"
120-
121-
# ...
12264
```
12365

124-
### Command-line interface
66+
Examples of how to use this library to accomplish various tasks can be found in the [OpenAI Cookbook](https://github.com/openai/openai-cookbook/). It contains code examples for: classification using fine-tuning, clustering, code search, customizing embeddings, question answering from a corpus of documents. recommendations, visualization of embeddings, and more.
12567

126-
This library additionally provides an `openai` command-line utility
127-
which makes it easy to interact with the API from your terminal. Run
128-
`openai api -h` for usage.
68+
Most endpoints support a `request_timeout` param. This param takes a `Union[float, Tuple[float, float]]` and will raise an `openai.error.Timeout` error if the request exceeds that time in seconds (See: https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts).
12969

130-
```sh
131-
# list models
132-
openai api models.list
133-
134-
# create a chat completion (gpt-3.5-turbo, gpt-4, etc.)
135-
openai api chat_completions.create -m gpt-3.5-turbo -g user "Hello world"
136-
137-
# create a completion (text-davinci-003, text-davinci-002, ada, babbage, curie, davinci, etc.)
138-
openai api completions.create -m ada -p "Hello world"
139-
140-
# generate images via DALL·E API
141-
openai api image.create -p "two dogs playing chess, cartoon" -n 1
70+
### Chat completions
14271

143-
# using openai through a proxy
144-
openai --proxy=http://proxy.com api models.list
145-
```
146-
147-
## Example code
148-
149-
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:
150-
151-
- Classification using fine-tuning
152-
- Clustering
153-
- Code search
154-
- Customizing embeddings
155-
- Question answering from a corpus of documents
156-
- Recommendations
157-
- Visualization of embeddings
158-
- And more
159-
160-
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/).
161-
162-
### Chat Completions
163-
164-
Conversational models such as `gpt-3.5-turbo` can be called using the chat completions endpoint.
72+
Chat models such as `gpt-3.5-turbo` and `gpt-4` can be called using the [chat completions endpoint](https://platform.openai.com/docs/api-reference/chat/create).
16573

16674
```python
167-
import openai
168-
openai.api_key = "sk-..." # supply your API key however you choose
169-
17075
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
17176
print(completion.choices[0].message.content)
17277
```
17378

79+
You can learn more in our [chat completions guide](https://platform.openai.com/docs/guides/gpt/chat-completions-api).
80+
17481
### Completions
17582

17683
Text models such as `babbage-002` or `davinci-002` (and our [legacy completions models](https://platform.openai.com/docs/deprecations/deprecation-history)) can be called using the completions endpoint.
17784

17885
```python
179-
import openai
180-
openai.api_key = "sk-..." # supply your API key however you choose
181-
18286
completion = openai.Completion.create(model="davinci-002", prompt="Hello world")
18387
print(completion.choices[0].text)
18488
```
18589

186-
### Embeddings
90+
You can learn more in our [completions guide](https://platform.openai.com/docs/guides/gpt/completions-api).
18791

188-
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.
92+
### Embeddings
18993

190-
To get an embedding for a text string, you can use the embeddings method as follows in Python:
94+
Embeddings are designed to measure the similarity or relevance between text strings. To get an embedding for a text string, you can use following:
19195

19296
```python
193-
import openai
194-
openai.api_key = "sk-..." # supply your API key however you choose
195-
196-
# choose text to embed
19797
text_string = "sample text"
19898

199-
# choose an embedding
20099
model_id = "text-embedding-ada-002"
201100

202-
# compute the embedding of the text
203101
embedding = openai.Embedding.create(input=text_string, model=model_id)['data'][0]['embedding']
204102
```
205103

206-
An example of how to call the embeddings method is shown in this [embeddings guide](https://platform.openai.com/docs/guides/embeddings/embeddings).
207-
208-
Examples of how to use embeddings are shared in the following Jupyter notebooks:
209-
210-
- [Classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Classification_using_embeddings.ipynb)
211-
- [Clustering using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Clustering.ipynb)
212-
- [Code search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Code_search.ipynb)
213-
- [Semantic text search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Semantic_text_search_using_embeddings.ipynb)
214-
- [User and product embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/User_and_product_embeddings.ipynb)
215-
- [Zero-shot classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Zero-shot_classification_with_embeddings.ipynb)
216-
- [Recommendation using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Recommendation_using_embeddings.ipynb)
217-
218-
For more information on embeddings and the types of embeddings OpenAI offers, read the [embeddings guide](https://platform.openai.com/docs/guides/embeddings) in the OpenAI documentation.
104+
You can learn more in our [embeddings guide](https://platform.openai.com/docs/guides/embeddings/embeddings).
219105

220106
### Fine-tuning
221107

@@ -241,76 +127,129 @@ openai.FineTuningJob.list_events(id="ft-abc123", limit=10)
241127
openai.Model.delete("ft:gpt-3.5-turbo:acemeco:suffix:abc123")
242128
```
243129

244-
For more information on fine-tuning, read the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) in the OpenAI documentation.
130+
You can learn more in our [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning).
245131

246132
### Moderation
247133

248-
OpenAI 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)
134+
OpenAI provides a free Moderation endpoint that can be used to check whether content complies with the OpenAI [content policy](https://platform.openai.com/docs/usage-policies).
249135

250136
```python
251-
import openai
252-
openai.api_key = "sk-..." # supply your API key however you choose
253-
254137
moderation_resp = openai.Moderation.create(input="Here is some perfectly innocuous text that follows all OpenAI content policies.")
255138
```
256139

257-
See the [moderation guide](https://platform.openai.com/docs/guides/moderation) for more details.
140+
You can learn more in our [moderation guide](https://platform.openai.com/docs/guides/moderation).
258141

259-
## Image generation (DALL·E)
142+
### Image generation (DALL·E)
260143

261-
```python
262-
import openai
263-
openai.api_key = "sk-..." # supply your API key however you choose
144+
DALL·E is a generative image model that can create new images based on a prompt.
264145

146+
```python
265147
image_resp = openai.Image.create(prompt="two dogs playing chess, oil painting", n=4, size="512x512")
266-
267148
```
268149

269-
## Audio transcription (Whisper)
150+
You can learn more in our [image generation guide](https://platform.openai.com/docs/guides/images).
151+
152+
### Audio (Whisper)
153+
154+
The speech to text API provides two endpoints, transcriptions and translations, based on our state-of-the-art [open source large-v2 Whisper model](https://github.com/openai/whisper).
270155

271156
```python
272-
import openai
273-
openai.api_key = "sk-..." # supply your API key however you choose
274157
f = open("path/to/file.mp3", "rb")
275158
transcript = openai.Audio.transcribe("whisper-1", f)
276159

160+
transcript = openai.Audio.translate("whisper-1", f)
277161
```
278162

279-
## Async API
163+
You can learn more in our [speech to text guide](https://platform.openai.com/docs/guides/speech-to-text).
164+
165+
### Async API
280166

281167
Async support is available in the API by prepending `a` to a network-bound method:
282168

283169
```python
284-
import openai
285-
openai.api_key = "sk-..." # supply your API key however you choose
286-
287170
async def create_chat_completion():
288171
chat_completion_resp = await openai.ChatCompletion.acreate(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
289-
290172
```
291173

292174
To make async requests more efficient, you can pass in your own
293175
`aiohttp.ClientSession`, but you must manually close the client session at the end
294176
of your program/event loop:
295177

296178
```python
297-
import openai
298179
from aiohttp import ClientSession
299-
300180
openai.aiosession.set(ClientSession())
181+
301182
# At the end of your program, close the http session
302183
await openai.aiosession.get().close()
303184
```
304185

305-
See the [usage guide](https://platform.openai.com/docs/guides/images) for more details.
186+
### Command-line interface
187+
188+
This library additionally provides an `openai` command-line utility
189+
which makes it easy to interact with the API from your terminal. Run
190+
`openai api -h` for usage.
191+
192+
```sh
193+
# list models
194+
openai api models.list
195+
196+
# create a chat completion (gpt-3.5-turbo, gpt-4, etc.)
197+
openai api chat_completions.create -m gpt-3.5-turbo -g user "Hello world"
198+
199+
# create a completion (text-davinci-003, text-davinci-002, ada, babbage, curie, davinci, etc.)
200+
openai api completions.create -m ada -p "Hello world"
201+
202+
# generate images via DALL·E API
203+
openai api image.create -p "two dogs playing chess, cartoon" -n 1
204+
205+
# using openai through a proxy
206+
openai --proxy=http://proxy.com api models.list
207+
```
208+
209+
### Microsoft Azure Endpoints
306210

307-
## Requirements
211+
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.
212+
In addition, the deployment name must be passed as the engine parameter.
308213

309-
- Python 3.7.1+
214+
```python
215+
import openai
216+
openai.api_type = "azure"
217+
openai.api_key = "..."
218+
openai.api_base = "https://example-endpoint.openai.azure.com"
219+
openai.api_version = "2023-05-15"
220+
221+
# create a chat completion
222+
chat_completion = openai.ChatCompletion.create(deployment_id="deployment-name", model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
223+
224+
# print the completion
225+
print(chat_completion.choices[0].message.content)
226+
```
227+
228+
Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations.
229+
For a detailed example of how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebooks:
310230

311-
In general, we want to support the versions of Python that our
312-
customers are using. If you run into problems with any version
313-
issues, please let us know on our [support page](https://help.openai.com/en/).
231+
- [Using Azure completions](https://github.com/openai/openai-cookbook/tree/main/examples/azure/completions.ipynb)
232+
- [Using Azure fine-tuning](https://github.com/openai/openai-cookbook/tree/main/examples/azure/finetuning.ipynb)
233+
- [Using Azure embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb)
234+
235+
### Microsoft Azure Active Directory Authentication
236+
237+
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.
238+
239+
```python
240+
from azure.identity import DefaultAzureCredential
241+
import openai
242+
243+
# Request credential
244+
default_credential = DefaultAzureCredential()
245+
token = default_credential.get_token("https://cognitiveservices.azure.com/.default")
246+
247+
# Setup parameters
248+
openai.api_type = "azure_ad"
249+
openai.api_key = token.token
250+
openai.api_base = "https://example-endpoint.openai.azure.com/"
251+
openai.api_version = "2023-05-15"
252+
```
314253

315254
## Credit
316255

0 commit comments

Comments
 (0)