You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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+
6
6
application. It includes type definitions for all request params and response fields,
7
7
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
8
8
@@ -13,7 +13,7 @@ The API documentation can be found [here](https://beta.openai.com/docs/).
13
13
## Installation
14
14
15
15
```sh
16
-
pip install openai
16
+
pip install OpenAI
17
17
```
18
18
19
19
## Usage
@@ -26,18 +26,17 @@ openai = OpenAI(
26
26
api_key="my api key",
27
27
)
28
28
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
+
)
36
35
print(completion.choices)
37
36
```
38
37
39
38
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.
41
40
42
41
## Async Usage
43
42
@@ -51,34 +50,35 @@ openai = AsyncOpenAI(
51
50
api_key="my api key",
52
51
)
53
52
53
+
54
54
asyncdefmain():
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
+
)
61
61
print(completion.choices)
62
62
63
+
63
64
asyncio.run(main())
64
65
```
65
66
66
67
Functionality between the synchronous and asynchronous clients are otherwise identical.
67
68
68
69
## Using Types
69
70
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.
71
72
72
73
If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `"basic"`.
73
74
74
75
## Pagination
75
76
76
-
List methods in the Open AI API are paginated.
77
+
List methods in the OpenAI API are paginated.
77
78
78
79
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
79
80
80
81
```py
81
-
from typing import List
82
82
import openai
83
83
84
84
openai = OpenAI()
@@ -95,44 +95,35 @@ Or, asynchronously:
95
95
96
96
```python
97
97
import asyncio
98
-
from typing import List
99
98
import openai
100
99
101
100
openai = AsyncOpenAI()
102
101
102
+
103
103
asyncdefmain() -> None:
104
104
all_files = []
105
105
# Iterate through items across all pages, issuing requests as needed.
106
106
asyncforfilein openai.files.list():
107
107
all_files.append(file)
108
-
return all_files
108
+
print(all_files)
109
+
109
110
110
111
asyncio.run(main())
111
112
```
112
113
113
114
Alternatively, you can use the `.has_next_page()`, `.next_page_params()`,
114
115
or `.get_next_page()` methods for more granular control working with pages:
0 commit comments