forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_module_client.py
More file actions
265 lines (187 loc) · 8.33 KB
/
Copy pathtest_module_client.py
File metadata and controls
265 lines (187 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
from __future__ import annotations
import os as _os
import httpx
import pytest
from httpx import URL
import openai
from openai import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES
def reset_state() -> None:
openai._reset_client()
openai.api_key = None
openai.admin_api_key = None
openai.organization = None
openai.project = None
openai.webhook_secret = None
openai.base_url = None
openai.timeout = DEFAULT_TIMEOUT
openai.max_retries = DEFAULT_MAX_RETRIES
openai.default_headers = None
openai.default_query = None
openai.http_client = None
openai.api_type = _os.environ.get("OPENAI_API_TYPE") # type: ignore
openai.api_version = None
openai.azure_endpoint = None
openai.azure_ad_token = None
openai.azure_ad_token_provider = None
openai._bedrock_api_key = None
openai.bedrock_token_provider = None
@pytest.fixture(autouse=True)
def reset_state_fixture() -> None:
reset_state()
def test_base_url_option() -> None:
assert openai.base_url is None
assert openai.completions._client.base_url == URL("https://api.openai.com/v1/")
openai.base_url = "http://foo.com"
assert openai.base_url == URL("http://foo.com")
assert openai.completions._client.base_url == URL("http://foo.com")
def test_timeout_option() -> None:
assert openai.timeout == openai.DEFAULT_TIMEOUT
assert openai.completions._client.timeout == openai.DEFAULT_TIMEOUT
openai.timeout = 3
assert openai.timeout == 3
assert openai.completions._client.timeout == 3
def test_max_retries_option() -> None:
assert openai.max_retries == openai.DEFAULT_MAX_RETRIES
assert openai.completions._client.max_retries == openai.DEFAULT_MAX_RETRIES
openai.max_retries = 1
assert openai.max_retries == 1
assert openai.completions._client.max_retries == 1
def test_default_headers_option() -> None:
assert openai.default_headers == None
openai.default_headers = {"Foo": "Bar"}
assert openai.default_headers["Foo"] == "Bar"
assert openai.completions._client.default_headers["Foo"] == "Bar"
def test_default_query_option() -> None:
assert openai.default_query is None
assert openai.completions._client._custom_query == {}
openai.default_query = {"Foo": {"nested": 1}}
assert openai.default_query["Foo"] == {"nested": 1}
assert openai.completions._client._custom_query["Foo"] == {"nested": 1}
def test_http_client_option() -> None:
assert openai.http_client is None
original_http_client = openai.completions._client._client
assert original_http_client is not None
new_client = httpx.Client()
openai.http_client = new_client
assert openai.completions._client._client is new_client
import contextlib
from typing import Iterator
from openai.lib.azure import AzureOpenAI
from openai.lib.bedrock import BedrockOpenAI
@contextlib.contextmanager
def fresh_env() -> Iterator[None]:
old = _os.environ.copy()
try:
_os.environ.clear()
yield
finally:
_os.environ.clear()
_os.environ.update(old)
def test_only_api_key_results_in_openai_api() -> None:
with fresh_env():
openai.api_type = None
openai.api_key = "example API key"
assert type(openai.completions._client).__name__ == "_ModuleClient"
def test_azure_api_key_env_without_api_version() -> None:
with fresh_env():
openai.api_type = None
_os.environ["AZURE_OPENAI_API_KEY"] = "example API key"
with pytest.raises(
ValueError,
match=r"Must provide either the `api_version` argument or the `OPENAI_API_VERSION` environment variable",
):
openai.completions._client # noqa: B018
def test_azure_api_key_and_version_env() -> None:
with fresh_env():
openai.api_type = None
_os.environ["AZURE_OPENAI_API_KEY"] = "example API key"
_os.environ["OPENAI_API_VERSION"] = "example-version"
with pytest.raises(
ValueError,
match=r"Must provide one of the `base_url` or `azure_endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable",
):
openai.completions._client # noqa: B018
def test_azure_api_key_version_and_endpoint_env() -> None:
with fresh_env():
openai.api_type = None
_os.environ["AZURE_OPENAI_API_KEY"] = "example API key"
_os.environ["OPENAI_API_VERSION"] = "example-version"
_os.environ["AZURE_OPENAI_ENDPOINT"] = "https://www.example"
openai.completions._client # noqa: B018
assert openai.api_type == "azure"
def test_azure_azure_ad_token_version_and_endpoint_env() -> None:
with fresh_env():
openai.api_type = None
_os.environ["AZURE_OPENAI_AD_TOKEN"] = "example AD token"
_os.environ["OPENAI_API_VERSION"] = "example-version"
_os.environ["AZURE_OPENAI_ENDPOINT"] = "https://www.example"
client = openai.completions._client
assert isinstance(client, AzureOpenAI)
assert client._azure_ad_token == "example AD token"
def test_azure_azure_ad_token_provider_version_and_endpoint_env() -> None:
with fresh_env():
openai.api_type = None
_os.environ["OPENAI_API_VERSION"] = "example-version"
_os.environ["AZURE_OPENAI_ENDPOINT"] = "https://www.example"
openai.azure_ad_token_provider = lambda: "token"
client = openai.completions._client
assert isinstance(client, AzureOpenAI)
assert client._azure_ad_token_provider is not None
assert client._azure_ad_token_provider() == "token"
def test_bedrock_token_and_region_env() -> None:
with fresh_env():
openai.api_type = "amazon-bedrock"
_os.environ["AWS_BEARER_TOKEN_BEDROCK"] = "example Bedrock token"
_os.environ["AWS_REGION"] = "us-west-2"
client = openai.responses._client
assert isinstance(client, BedrockOpenAI)
assert client.base_url == URL("https://bedrock-mantle.us-west-2.api.aws/openai/v1/")
def test_bedrock_api_type_env() -> None:
with fresh_env():
_os.environ["OPENAI_API_TYPE"] = "amazon-bedrock"
_os.environ["AWS_BEARER_TOKEN_BEDROCK"] = "example Bedrock token"
_os.environ["AWS_REGION"] = "us-west-2"
reset_state()
client = openai.responses._client
assert isinstance(client, BedrockOpenAI)
assert openai.api_type == "amazon-bedrock"
def test_bedrock_api_type_uses_bedrock_credentials() -> None:
with fresh_env():
openai.api_type = "amazon-bedrock"
_os.environ["OPENAI_API_KEY"] = "openai api key"
_os.environ["AWS_BEARER_TOKEN_BEDROCK"] = "example Bedrock token"
_os.environ["AWS_REGION"] = "us-west-2"
client = openai.responses._client
assert isinstance(client, BedrockOpenAI)
assert client.api_key == "example Bedrock token"
assert openai.api_key is None
def test_bedrock_api_type_uses_explicit_module_api_key() -> None:
with fresh_env():
openai.api_type = "amazon-bedrock"
openai.api_key = "explicit Bedrock token"
_os.environ["AWS_BEARER_TOKEN_BEDROCK"] = "env Bedrock token"
_os.environ["AWS_REGION"] = "us-west-2"
client = openai.responses._client
assert isinstance(client, BedrockOpenAI)
assert client.api_key == "explicit Bedrock token"
assert openai.api_key == "explicit Bedrock token"
def test_bedrock_module_api_key_overrides_cached_env_token_after_load() -> None:
with fresh_env():
openai.api_type = "amazon-bedrock"
_os.environ["AWS_BEARER_TOKEN_BEDROCK"] = "env Bedrock token"
_os.environ["AWS_REGION"] = "us-west-2"
client = openai.responses._client
assert isinstance(client, BedrockOpenAI)
assert client.api_key == "env Bedrock token"
openai.api_key = "new Bedrock token"
assert client.api_key == "new Bedrock token"
def test_bedrock_api_type_uses_token_provider_without_mutating_module_api_key() -> None:
with fresh_env():
openai.api_type = "amazon-bedrock"
openai.bedrock_token_provider = lambda: "provider Bedrock token"
_os.environ["AWS_REGION"] = "us-west-2"
client = openai.responses._client
assert isinstance(client, BedrockOpenAI)
assert client._refresh_api_key() == "provider Bedrock token"
assert openai.api_key is None