Skip to content

Commit ea56a14

Browse files
committed
Bump for 5f87e2c
1 parent 7df33cb commit ea56a14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3678
-1135
lines changed

.stats.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
configured_endpoints: 16

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2022 openai
189+
Copyright 2022 OpenAI
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Open AI Python API Library
1+
# OpenAI Python API Library
22

3-
[![PyPI version](https://img.shields.io/pypi/v/openai.svg)](https://pypi.org/project/openai/)
3+
[![PyPI version](https://img.shields.io/pypi/v/OpenAI.svg)](https://pypi.org/project/OpenAI/)
44

5-
The Open AI Python library provides convenient access to the Open AI REST API from any Python 3.7+
5+
The OpenAI Python library provides convenient access to the OpenAI REST API from any Python 3.7+
66
application. It includes type definitions for all request params and response fields,
77
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
88

@@ -13,7 +13,7 @@ The API documentation can be found [here](https://beta.openai.com/docs/).
1313
## Installation
1414

1515
```sh
16-
pip install openai
16+
pip install OpenAI
1717
```
1818

1919
## Usage
@@ -26,18 +26,17 @@ openai = OpenAI(
2626
api_key="my api key",
2727
)
2828

29-
completion = openai.completions.create({
30-
"model": "text-davinci-002",
31-
"prompt": "Say this is a test",
32-
"max_tokens": 6,
33-
"temperature": 0,
34-
})
35-
29+
completion = openai.completions.create(
30+
model="text-davinci-002",
31+
prompt="Say this is a test",
32+
max_tokens=6,
33+
temperature=0,
34+
)
3635
print(completion.choices)
3736
```
3837

3938
While you can provide an `api_key` keyword argument, we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
40-
and adding `OPENAI_API_KEY="my api key"` to your `.env` file so that your API keys are not stored in source control.
39+
and adding `OPENAI_API_KEY="my api key"` to your `.env` file so that your API Key is not stored in source control.
4140

4241
## Async Usage
4342

@@ -51,34 +50,35 @@ openai = AsyncOpenAI(
5150
api_key="my api key",
5251
)
5352

53+
5454
async def main():
55-
completion = await openai.completions.create({
56-
"model": "text-davinci-002",
57-
"prompt": "Say this is a test",
58-
"max_tokens": 6,
59-
"temperature": 0,
60-
})
55+
completion = await openai.completions.create(
56+
model="text-davinci-002",
57+
prompt="Say this is a test",
58+
max_tokens=6,
59+
temperature=0,
60+
)
6161
print(completion.choices)
6262

63+
6364
asyncio.run(main())
6465
```
6566

6667
Functionality between the synchronous and asynchronous clients are otherwise identical.
6768

6869
## Using Types
6970

70-
Request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict), while responses are [Pydantic](https://pydantic-docs.helpmanual.io/) models. This helps provide autocomplete and documentation within your editor.
71+
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict), while responses are [Pydantic](https://pydantic-docs.helpmanual.io/) models. This helps provide autocomplete and documentation within your editor.
7172

7273
If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `"basic"`.
7374

7475
## Pagination
7576

76-
List methods in the Open AI API are paginated.
77+
List methods in the OpenAI API are paginated.
7778

7879
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
7980

8081
```py
81-
from typing import List
8282
import openai
8383

8484
openai = OpenAI()
@@ -95,44 +95,35 @@ Or, asynchronously:
9595

9696
```python
9797
import asyncio
98-
from typing import List
9998
import openai
10099

101100
openai = AsyncOpenAI()
102101

102+
103103
async def main() -> None:
104104
all_files = []
105105
# Iterate through items across all pages, issuing requests as needed.
106106
async for file in openai.files.list():
107107
all_files.append(file)
108-
return all_files
108+
print(all_files)
109+
109110

110111
asyncio.run(main())
111112
```
112113

113114
Alternatively, you can use the `.has_next_page()`, `.next_page_params()`,
114115
or `.get_next_page()` methods for more granular control working with pages:
115116

116-
```python
117-
first_page = await client.cards.list({"page_size": 2})
118-
if first_page.has_next_page():
119-
print("will fetch next page, with params", first_page.next_page_params())
120-
next_page = await first_page.get_next_page()
121-
print(f"number of items we just fetched: {len(next_page.data)}")
117+
## Nested params
122118

123-
# Remove `await` for non-async usage.
124-
```
119+
Nested parameters are dictionaries, typed using `TypedDict`, for example:
125120

126-
Or just work directly with the returned data:
127-
128-
```python
129-
first_page = await client.cards.list()
121+
```py
122+
from openai import OpenAI
130123

131-
print(f"page number: {first_page.page}") # => "page number: 1"
132-
for card in first_page.data:
133-
print(card.token)
124+
openai = OpenAI()
134125

135-
# Remove `await` for non-async usage.
126+
openai.files.list()
136127
```
137128

138129
## Handling errors
@@ -150,12 +141,12 @@ from openai import OpenAI
150141
openai = OpenAI()
151142

152143
try:
153-
openai.fine_tunes.create({
154-
"training_file": "file-XGinujblHPwGLSztz8cPS8XY"
155-
})
144+
openai.fine_tunes.create(
145+
training_file="file-XGinujblHPwGLSztz8cPS8XY",
146+
)
156147
except openai.APIConnectionError as e:
157148
print("The server could not be reached")
158-
print(e.__cause__) # an underlying Exception, likely raised within httpx.
149+
print(e.__cause__) # an underlying Exception, likely raised within httpx.
159150
except openai.RateLimitError as e:
160151
print("A 429 status code was received; we should back off a bit.")
161152
except openai.APIStatusError as e:
@@ -195,10 +186,10 @@ openai = OpenAI(
195186
)
196187

197188
# Or, configure per-request:
198-
openai.embeddings.create({
199-
"model": "text-similarity-babbage-001",
200-
"input": "The food was delicious and the waiter...",
201-
}, max_retries=5);
189+
openai.with_options(max_retries=5).embeddings.create(
190+
model="text-similarity-babbage-001",
191+
input="The food was delicious and the waiter...",
192+
)
202193
```
203194

204195
### Timeouts
@@ -221,11 +212,11 @@ openai = OpenAI(
221212
)
222213

223214
# Override per-request:
224-
openai.edits.create({
225-
"model": "text-davinci-edit-001",
226-
"input": "What day of the wek is it?",
227-
"instruction": "Fix the spelling mistakes",
228-
}, timeout=5 * 1000)
215+
openai.with_options(timeout=5 * 1000).edits.create(
216+
model="text-davinci-edit-001",
217+
input="What day of the wek is it?",
218+
instruction="Fix the spelling mistakes",
219+
)
229220
```
230221

231222
On timeout, an `APITimeoutError` is thrown.
@@ -260,4 +251,4 @@ bugs, or suggestions.
260251

261252
## Requirements
262253

263-
Python 3.7 or higher.
254+
Python 3.7 or higher.

0 commit comments

Comments
 (0)