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
Copy file name to clipboardExpand all lines: README.md
+16-148
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,12 @@ The OpenAI Python library provides convenient access to the OpenAI REST API from
6
6
application. The library 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
9
-
It is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/).
10
-
11
9
## Documentation
12
10
13
11
The REST API documentation can be found [on platform.openai.com](https://platform.openai.com/docs). The full API of this library can be found in [api.md](api.md).
14
12
15
13
## Installation
16
14
17
-
> [!IMPORTANT]
18
-
> The SDK was rewritten in v1, which was released November 6th 2023. See the [v1 migration guide](https://github.com/openai/openai-python/discussions/742), which includes scripts to automatically update your code.
19
-
20
15
```sh
21
16
# install from PyPI
22
17
pip install openai
@@ -51,56 +46,6 @@ we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
51
46
to add `OPENAI_API_KEY="My API Key"` to your `.env` file
52
47
so that your API Key is not stored in source control.
53
48
54
-
### Polling Helpers
55
-
56
-
When interacting with the API some actions such as starting a Run and adding files to vector stores are asynchronous and take time to complete. The SDK includes
57
-
helper functions which will poll the status until it reaches a terminal state and then return the resulting object.
58
-
If an API method results in an action which could benefit from polling there will be a corresponding version of the
59
-
method ending in '\_and_poll'.
60
-
61
-
For instance to create a Run and poll until it reaches a terminal state you can run:
62
-
63
-
```python
64
-
run = client.beta.threads.runs.create_and_poll(
65
-
thread_id=thread.id,
66
-
assistant_id=assistant.id,
67
-
)
68
-
```
69
-
70
-
More information on the lifecycle of a Run can be found in the [Run Lifecycle Documentation](https://platform.openai.com/docs/assistants/how-it-works/run-lifecycle)
71
-
72
-
### Bulk Upload Helpers
73
-
74
-
When creating an interacting with vector stores, you can use the polling helpers to monitor the status of operations.
75
-
For convenience, we also provide a bulk upload helper to allow you to simultaneously upload several files at once.
> We highly recommend instantiating client instances instead of relying on the global client.
179
-
180
-
We also expose a global client instance that is accessible in a similar fashion to versions prior to v1.
181
-
182
-
```py
183
-
import openai
184
-
185
-
# optional; defaults to `os.environ['OPENAI_API_KEY']`
186
-
openai.api_key ='...'
187
-
188
-
# all client options can be configured just like the `OpenAI` instantiation counterpart
189
-
openai.base_url ="https://..."
190
-
openai.default_headers = {"x-foo": "true"}
191
-
192
-
completion = openai.chat.completions.create(
193
-
model="gpt-4",
111
+
stream =await client.chat.completions.create(
194
112
messages=[
195
113
{
196
114
"role": "user",
197
-
"content": "How do I output all files in a directory using Python?",
198
-
},
115
+
"content": "Say this is a test",
116
+
}
199
117
],
118
+
model="gpt-3.5-turbo",
119
+
stream=True,
200
120
)
201
-
print(completion.choices[0].message.content)
121
+
asyncfor chat_completion in stream:
122
+
print(chat_completion)
202
123
```
203
124
204
-
The API is the exact same as the standard client instance based API.
205
-
206
-
This is intended to be used within REPLs or notebooks for faster iteration, **not** in application code.
207
-
208
-
We recommend that you always instantiate a client (e.g., with `client = OpenAI()`) in application code because:
209
-
210
-
- It can be difficult to reason about where client options are configured
211
-
- It's not possible to change certain client options without potentially causing race conditions
212
-
- It's harder to mock for testing purposes
213
-
- It's not possible to control cleanup of network connections
214
-
215
125
## Using types
216
126
217
127
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:
@@ -579,48 +489,6 @@ client = OpenAI(
579
489
580
490
By default the library closes underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__). You can manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting.
581
491
582
-
## Microsoft Azure OpenAI
583
-
584
-
To use this library with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview), use the `AzureOpenAI`
585
-
class instead of the `OpenAI` class.
586
-
587
-
> [!IMPORTANT]
588
-
> The Azure API shape differs from the core API shape which means that the static types for responses / params
589
-
> won't always be correct.
590
-
591
-
```py
592
-
from openai import AzureOpenAI
593
-
594
-
# gets the API Key from environment variable AZURE_OPENAI_API_KEY
"content": "How do I output all files in a directory using Python?",
608
-
},
609
-
],
610
-
)
611
-
print(completion.to_json())
612
-
```
613
-
614
-
In addition to the options provided in the base `OpenAI` client, the following options are provided:
615
-
616
-
-`azure_endpoint` (or the `AZURE_OPENAI_ENDPOINT` environment variable)
617
-
-`azure_deployment`
618
-
-`api_version` (or the `OPENAI_API_VERSION` environment variable)
619
-
-`azure_ad_token` (or the `AZURE_OPENAI_AD_TOKEN` environment variable)
620
-
-`azure_ad_token_provider`
621
-
622
-
An example of using the client with Azure Active Directory can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py).
623
-
624
492
## Versioning
625
493
626
494
This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
0 commit comments