openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.63.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/lib/test_azure.py

241lines · modeblame

1ca831cfKrista Pratico1 years ago1import logging
6b01ba6aRobert Craigie1 years ago2from typing import Union, cast
3from typing_extensions import Literal, Protocol
08b8179aDavid Schnurr2 years ago4
6b01ba6aRobert Craigie1 years ago5import httpx
08b8179aDavid Schnurr2 years ago6import pytest
6b01ba6aRobert Craigie1 years ago7from respx import MockRouter
08b8179aDavid Schnurr2 years ago8
1ca831cfKrista Pratico1 years ago9from openai._utils import SensitiveHeadersFilter, is_dict
08b8179aDavid Schnurr2 years ago10from openai._models import FinalRequestOptions
11from openai.lib.azure import AzureOpenAI, AsyncAzureOpenAI
12
13Client = Union[AzureOpenAI, AsyncAzureOpenAI]
14
15
16sync_client = AzureOpenAI(
17api_version="2023-07-01",
18api_key="example API key",
19azure_endpoint="https://example-resource.azure.openai.com",
20)
21
22async_client = AsyncAzureOpenAI(
23api_version="2023-07-01",
24api_key="example API key",
25azure_endpoint="https://example-resource.azure.openai.com",
26)
27
28
6b01ba6aRobert Craigie1 years ago29class MockRequestCall(Protocol):
30request: httpx.Request
31
32
08b8179aDavid Schnurr2 years ago33@pytest.mark.parametrize("client", [sync_client, async_client])
34def test_implicit_deployment_path(client: Client) -> None:
35req = client._build_request(
36FinalRequestOptions.construct(
37method="post",
38url="/chat/completions",
39json_data={"model": "my-deployment-model"},
40)
41)
42assert (
43req.url
44== "https://example-resource.azure.openai.com/openai/deployments/my-deployment-model/chat/completions?api-version=2023-07-01"
45)
97a6895bStainless Bot2 years ago46
47
48@pytest.mark.parametrize(
49"client,method",
50[
51(sync_client, "copy"),
52(sync_client, "with_options"),
53(async_client, "copy"),
54(async_client, "with_options"),
55],
56)
57def test_client_copying(client: Client, method: Literal["copy", "with_options"]) -> None:
58if method == "copy":
59copied = client.copy()
60else:
61copied = client.with_options()
62
63assert copied._custom_query == {"api-version": "2023-07-01"}
64
65
66@pytest.mark.parametrize(
67"client",
68[sync_client, async_client],
69)
70def test_client_copying_override_options(client: Client) -> None:
71copied = client.copy(
72api_version="2022-05-01",
73)
74assert copied._custom_query == {"api-version": "2022-05-01"}
6b01ba6aRobert Craigie1 years ago75
76
77@pytest.mark.respx()
78def test_client_token_provider_refresh_sync(respx_mock: MockRouter) -> None:
79respx_mock.post(
80"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
81).mock(
82side_effect=[
83httpx.Response(500, json={"error": "server error"}),
84httpx.Response(200, json={"foo": "bar"}),
85]
86)
87
88counter = 0
89
90def token_provider() -> str:
91nonlocal counter
92
93counter += 1
94
95if counter == 1:
96return "first"
97
98return "second"
99
100client = AzureOpenAI(
101api_version="2024-02-01",
102azure_ad_token_provider=token_provider,
103azure_endpoint="https://example-resource.azure.openai.com",
104)
105client.chat.completions.create(messages=[], model="gpt-4")
106
107calls = cast("list[MockRequestCall]", respx_mock.calls)
108
109assert len(calls) == 2
110
111assert calls[0].request.headers.get("Authorization") == "Bearer first"
112assert calls[1].request.headers.get("Authorization") == "Bearer second"
113
114
115@pytest.mark.asyncio
116@pytest.mark.respx()
117async def test_client_token_provider_refresh_async(respx_mock: MockRouter) -> None:
118respx_mock.post(
119"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
120).mock(
121side_effect=[
122httpx.Response(500, json={"error": "server error"}),
123httpx.Response(200, json={"foo": "bar"}),
124]
125)
126
127counter = 0
128
129def token_provider() -> str:
130nonlocal counter
131
132counter += 1
133
134if counter == 1:
135return "first"
136
137return "second"
138
139client = AsyncAzureOpenAI(
140api_version="2024-02-01",
141azure_ad_token_provider=token_provider,
142azure_endpoint="https://example-resource.azure.openai.com",
143)
144
145await client.chat.completions.create(messages=[], model="gpt-4")
146
147calls = cast("list[MockRequestCall]", respx_mock.calls)
148
149assert len(calls) == 2
150
151assert calls[0].request.headers.get("Authorization") == "Bearer first"
152assert calls[1].request.headers.get("Authorization") == "Bearer second"
1ca831cfKrista Pratico1 years ago153
154
155class TestAzureLogging:
156@pytest.fixture(autouse=True)
157def logger_with_filter(self) -> logging.Logger:
158logger = logging.getLogger("openai")
159logger.setLevel(logging.DEBUG)
160logger.addFilter(SensitiveHeadersFilter())
161return logger
162
163@pytest.mark.respx()
164def test_azure_api_key_redacted(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None:
165respx_mock.post(
166"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago167).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago168
169client = AzureOpenAI(
170api_version="2024-06-01",
171api_key="example_api_key",
172azure_endpoint="https://example-resource.azure.openai.com",
173)
174
175with caplog.at_level(logging.DEBUG):
176client.chat.completions.create(messages=[], model="gpt-4")
177
178for record in caplog.records:
179if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
180assert record.args["headers"]["api-key"] == "<redacted>"
181
182@pytest.mark.respx()
183def test_azure_bearer_token_redacted(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None:
184respx_mock.post(
185"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago186).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago187
188client = AzureOpenAI(
189api_version="2024-06-01",
190azure_ad_token="example_token",
191azure_endpoint="https://example-resource.azure.openai.com",
192)
193
194with caplog.at_level(logging.DEBUG):
195client.chat.completions.create(messages=[], model="gpt-4")
196
197for record in caplog.records:
198if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
199assert record.args["headers"]["Authorization"] == "<redacted>"
200
201@pytest.mark.asyncio
202@pytest.mark.respx()
203async def test_azure_api_key_redacted_async(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None:
204respx_mock.post(
205"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago206).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago207
208client = AsyncAzureOpenAI(
209api_version="2024-06-01",
210api_key="example_api_key",
211azure_endpoint="https://example-resource.azure.openai.com",
212)
213
214with caplog.at_level(logging.DEBUG):
215await client.chat.completions.create(messages=[], model="gpt-4")
216
217for record in caplog.records:
218if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
219assert record.args["headers"]["api-key"] == "<redacted>"
220
221@pytest.mark.asyncio
222@pytest.mark.respx()
300f58bbstainless-app[bot]1 years ago223async def test_azure_bearer_token_redacted_async(
224self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture
225) -> None:
1ca831cfKrista Pratico1 years ago226respx_mock.post(
227"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago228).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago229
230client = AsyncAzureOpenAI(
231api_version="2024-06-01",
232azure_ad_token="example_token",
233azure_endpoint="https://example-resource.azure.openai.com",
234)
235
236with caplog.at_level(logging.DEBUG):
237await client.chat.completions.create(messages=[], model="gpt-4")
238
239for record in caplog.records:
240if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
241assert record.args["headers"]["Authorization"] == "<redacted>"