openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.78.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/lib/test_azure.py

804lines · modeblame

c98d7400Krista Pratico1 years ago1from __future__ import annotations
2
1ca831cfKrista Pratico1 years ago3import logging
6b01ba6aRobert Craigie1 years ago4from typing import Union, cast
5from typing_extensions import Literal, Protocol
08b8179aDavid Schnurr2 years ago6
6b01ba6aRobert Craigie1 years ago7import httpx
08b8179aDavid Schnurr2 years ago8import pytest
6b01ba6aRobert Craigie1 years ago9from respx import MockRouter
08b8179aDavid Schnurr2 years ago10
1ca831cfKrista Pratico1 years ago11from openai._utils import SensitiveHeadersFilter, is_dict
08b8179aDavid Schnurr2 years ago12from openai._models import FinalRequestOptions
13from openai.lib.azure import AzureOpenAI, AsyncAzureOpenAI
14
15Client = Union[AzureOpenAI, AsyncAzureOpenAI]
16
17
18sync_client = AzureOpenAI(
19api_version="2023-07-01",
20api_key="example API key",
21azure_endpoint="https://example-resource.azure.openai.com",
22)
23
24async_client = AsyncAzureOpenAI(
25api_version="2023-07-01",
26api_key="example API key",
27azure_endpoint="https://example-resource.azure.openai.com",
28)
29
30
6b01ba6aRobert Craigie1 years ago31class MockRequestCall(Protocol):
32request: httpx.Request
33
34
08b8179aDavid Schnurr2 years ago35@pytest.mark.parametrize("client", [sync_client, async_client])
36def test_implicit_deployment_path(client: Client) -> None:
37req = client._build_request(
38FinalRequestOptions.construct(
39method="post",
40url="/chat/completions",
41json_data={"model": "my-deployment-model"},
42)
43)
44assert (
45req.url
46== "https://example-resource.azure.openai.com/openai/deployments/my-deployment-model/chat/completions?api-version=2023-07-01"
47)
97a6895bStainless Bot2 years ago48
49
50@pytest.mark.parametrize(
51"client,method",
52[
53(sync_client, "copy"),
54(sync_client, "with_options"),
55(async_client, "copy"),
56(async_client, "with_options"),
57],
58)
59def test_client_copying(client: Client, method: Literal["copy", "with_options"]) -> None:
60if method == "copy":
61copied = client.copy()
62else:
63copied = client.with_options()
64
65assert copied._custom_query == {"api-version": "2023-07-01"}
66
67
68@pytest.mark.parametrize(
69"client",
70[sync_client, async_client],
71)
72def test_client_copying_override_options(client: Client) -> None:
73copied = client.copy(
74api_version="2022-05-01",
75)
76assert copied._custom_query == {"api-version": "2022-05-01"}
6b01ba6aRobert Craigie1 years ago77
78
79@pytest.mark.respx()
80def test_client_token_provider_refresh_sync(respx_mock: MockRouter) -> None:
81respx_mock.post(
82"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
83).mock(
84side_effect=[
85httpx.Response(500, json={"error": "server error"}),
86httpx.Response(200, json={"foo": "bar"}),
87]
88)
89
90counter = 0
91
92def token_provider() -> str:
93nonlocal counter
94
95counter += 1
96
97if counter == 1:
98return "first"
99
100return "second"
101
102client = AzureOpenAI(
103api_version="2024-02-01",
104azure_ad_token_provider=token_provider,
105azure_endpoint="https://example-resource.azure.openai.com",
106)
107client.chat.completions.create(messages=[], model="gpt-4")
108
109calls = cast("list[MockRequestCall]", respx_mock.calls)
110
111assert len(calls) == 2
112
113assert calls[0].request.headers.get("Authorization") == "Bearer first"
114assert calls[1].request.headers.get("Authorization") == "Bearer second"
115
116
117@pytest.mark.asyncio
118@pytest.mark.respx()
119async def test_client_token_provider_refresh_async(respx_mock: MockRouter) -> None:
120respx_mock.post(
121"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
122).mock(
123side_effect=[
124httpx.Response(500, json={"error": "server error"}),
125httpx.Response(200, json={"foo": "bar"}),
126]
127)
128
129counter = 0
130
131def token_provider() -> str:
132nonlocal counter
133
134counter += 1
135
136if counter == 1:
137return "first"
138
139return "second"
140
141client = AsyncAzureOpenAI(
142api_version="2024-02-01",
143azure_ad_token_provider=token_provider,
144azure_endpoint="https://example-resource.azure.openai.com",
145)
146
147await client.chat.completions.create(messages=[], model="gpt-4")
148
149calls = cast("list[MockRequestCall]", respx_mock.calls)
150
151assert len(calls) == 2
152
153assert calls[0].request.headers.get("Authorization") == "Bearer first"
154assert calls[1].request.headers.get("Authorization") == "Bearer second"
1ca831cfKrista Pratico1 years ago155
156
157class TestAzureLogging:
158@pytest.fixture(autouse=True)
159def logger_with_filter(self) -> logging.Logger:
160logger = logging.getLogger("openai")
161logger.setLevel(logging.DEBUG)
162logger.addFilter(SensitiveHeadersFilter())
163return logger
164
165@pytest.mark.respx()
166def test_azure_api_key_redacted(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None:
167respx_mock.post(
168"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago169).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago170
171client = AzureOpenAI(
172api_version="2024-06-01",
173api_key="example_api_key",
174azure_endpoint="https://example-resource.azure.openai.com",
175)
176
177with caplog.at_level(logging.DEBUG):
178client.chat.completions.create(messages=[], model="gpt-4")
179
180for record in caplog.records:
181if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
182assert record.args["headers"]["api-key"] == "<redacted>"
183
184@pytest.mark.respx()
185def test_azure_bearer_token_redacted(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None:
186respx_mock.post(
187"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago188).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago189
190client = AzureOpenAI(
191api_version="2024-06-01",
192azure_ad_token="example_token",
193azure_endpoint="https://example-resource.azure.openai.com",
194)
195
196with caplog.at_level(logging.DEBUG):
197client.chat.completions.create(messages=[], model="gpt-4")
198
199for record in caplog.records:
200if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
201assert record.args["headers"]["Authorization"] == "<redacted>"
202
203@pytest.mark.asyncio
204@pytest.mark.respx()
205async def test_azure_api_key_redacted_async(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None:
206respx_mock.post(
207"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago208).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago209
210client = AsyncAzureOpenAI(
211api_version="2024-06-01",
212api_key="example_api_key",
213azure_endpoint="https://example-resource.azure.openai.com",
214)
215
216with caplog.at_level(logging.DEBUG):
217await client.chat.completions.create(messages=[], model="gpt-4")
218
219for record in caplog.records:
220if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
221assert record.args["headers"]["api-key"] == "<redacted>"
222
223@pytest.mark.asyncio
224@pytest.mark.respx()
300f58bbstainless-app[bot]1 years ago225async def test_azure_bearer_token_redacted_async(
226self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture
227) -> None:
1ca831cfKrista Pratico1 years ago228respx_mock.post(
229"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01"
300f58bbstainless-app[bot]1 years ago230).mock(return_value=httpx.Response(200, json={"model": "gpt-4"}))
1ca831cfKrista Pratico1 years ago231
232client = AsyncAzureOpenAI(
233api_version="2024-06-01",
234azure_ad_token="example_token",
235azure_endpoint="https://example-resource.azure.openai.com",
236)
237
238with caplog.at_level(logging.DEBUG):
239await client.chat.completions.create(messages=[], model="gpt-4")
240
241for record in caplog.records:
242if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]):
243assert record.args["headers"]["Authorization"] == "<redacted>"
c98d7400Krista Pratico1 years ago244
245
246@pytest.mark.parametrize(
247"client,base_url,api,json_data,expected",
248[
249# Deployment-based endpoints
250# AzureOpenAI: No deployment specified
251(
252AzureOpenAI(
253api_version="2024-02-01",
254api_key="example API key",
255azure_endpoint="https://example-resource.azure.openai.com",
256),
257"https://example-resource.azure.openai.com/openai/",
258"/chat/completions",
259{"model": "deployment-body"},
260"https://example-resource.azure.openai.com/openai/deployments/deployment-body/chat/completions?api-version=2024-02-01",
261),
262# AzureOpenAI: Deployment specified
263(
264AzureOpenAI(
265api_version="2024-02-01",
266api_key="example API key",
267azure_endpoint="https://example-resource.azure.openai.com",
268azure_deployment="deployment-client",
269),
270"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
271"/chat/completions",
272{"model": "deployment-body"},
273"https://example-resource.azure.openai.com/openai/deployments/deployment-client/chat/completions?api-version=2024-02-01",
274),
275# AzureOpenAI: "deployments" in the DNS name
276(
277AzureOpenAI(
278api_version="2024-02-01",
279api_key="example API key",
280azure_endpoint="https://deployments.example-resource.azure.openai.com",
281),
282"https://deployments.example-resource.azure.openai.com/openai/",
283"/chat/completions",
284{"model": "deployment-body"},
285"https://deployments.example-resource.azure.openai.com/openai/deployments/deployment-body/chat/completions?api-version=2024-02-01",
286),
287# AzureOpenAI: Deployment called deployments
288(
289AzureOpenAI(
290api_version="2024-02-01",
291api_key="example API key",
292azure_endpoint="https://example-resource.azure.openai.com",
293azure_deployment="deployments",
294),
295"https://example-resource.azure.openai.com/openai/deployments/deployments/",
296"/chat/completions",
297{"model": "deployment-body"},
298"https://example-resource.azure.openai.com/openai/deployments/deployments/chat/completions?api-version=2024-02-01",
299),
300# AzureOpenAI: base_url and azure_deployment specified; ignored b/c not supported
301(
302AzureOpenAI( # type: ignore
303api_version="2024-02-01",
304api_key="example API key",
305base_url="https://example.azure-api.net/PTU/",
306azure_deployment="deployment-client",
307),
308"https://example.azure-api.net/PTU/",
309"/chat/completions",
310{"model": "deployment-body"},
311"https://example.azure-api.net/PTU/deployments/deployment-body/chat/completions?api-version=2024-02-01",
312),
313# AsyncAzureOpenAI: No deployment specified
314(
315AsyncAzureOpenAI(
316api_version="2024-02-01",
317api_key="example API key",
318azure_endpoint="https://example-resource.azure.openai.com",
319),
320"https://example-resource.azure.openai.com/openai/",
321"/chat/completions",
322{"model": "deployment-body"},
323"https://example-resource.azure.openai.com/openai/deployments/deployment-body/chat/completions?api-version=2024-02-01",
324),
325# AsyncAzureOpenAI: Deployment specified
326(
327AsyncAzureOpenAI(
328api_version="2024-02-01",
329api_key="example API key",
330azure_endpoint="https://example-resource.azure.openai.com",
331azure_deployment="deployment-client",
332),
333"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
334"/chat/completions",
335{"model": "deployment-body"},
336"https://example-resource.azure.openai.com/openai/deployments/deployment-client/chat/completions?api-version=2024-02-01",
337),
338# AsyncAzureOpenAI: "deployments" in the DNS name
339(
340AsyncAzureOpenAI(
341api_version="2024-02-01",
342api_key="example API key",
343azure_endpoint="https://deployments.example-resource.azure.openai.com",
344),
345"https://deployments.example-resource.azure.openai.com/openai/",
346"/chat/completions",
347{"model": "deployment-body"},
348"https://deployments.example-resource.azure.openai.com/openai/deployments/deployment-body/chat/completions?api-version=2024-02-01",
349),
350# AsyncAzureOpenAI: Deployment called deployments
351(
352AsyncAzureOpenAI(
353api_version="2024-02-01",
354api_key="example API key",
355azure_endpoint="https://example-resource.azure.openai.com",
356azure_deployment="deployments",
357),
358"https://example-resource.azure.openai.com/openai/deployments/deployments/",
359"/chat/completions",
360{"model": "deployment-body"},
361"https://example-resource.azure.openai.com/openai/deployments/deployments/chat/completions?api-version=2024-02-01",
362),
363# AsyncAzureOpenAI: base_url and azure_deployment specified; azure_deployment ignored b/c not supported
364(
365AsyncAzureOpenAI( # type: ignore
366api_version="2024-02-01",
367api_key="example API key",
368base_url="https://example.azure-api.net/PTU/",
369azure_deployment="deployment-client",
370),
371"https://example.azure-api.net/PTU/",
372"/chat/completions",
373{"model": "deployment-body"},
374"https://example.azure-api.net/PTU/deployments/deployment-body/chat/completions?api-version=2024-02-01",
375),
376],
377)
378def test_prepare_url_deployment_endpoint(
379client: Client, base_url: str, api: str, json_data: dict[str, str], expected: str
380) -> None:
381req = client._build_request(
382FinalRequestOptions.construct(
383method="post",
384url=api,
385json_data=json_data,
386)
387)
388assert req.url == expected
389assert client.base_url == base_url
390
391
392@pytest.mark.parametrize(
393"client,base_url,api,json_data,expected",
394[
395# Non-deployment endpoints
396# AzureOpenAI: No deployment specified
397(
398AzureOpenAI(
399api_version="2024-02-01",
400api_key="example API key",
401azure_endpoint="https://example-resource.azure.openai.com",
402),
403"https://example-resource.azure.openai.com/openai/",
404"/models",
405{},
406"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
407),
408# AzureOpenAI: No deployment specified
409(
410AzureOpenAI(
411api_version="2024-02-01",
412api_key="example API key",
413azure_endpoint="https://example-resource.azure.openai.com",
414),
415"https://example-resource.azure.openai.com/openai/",
416"/assistants",
417{"model": "deployment-body"},
418"https://example-resource.azure.openai.com/openai/assistants?api-version=2024-02-01",
419),
420# AzureOpenAI: Deployment specified
421(
422AzureOpenAI(
423api_version="2024-02-01",
424api_key="example API key",
425azure_endpoint="https://example-resource.azure.openai.com",
426azure_deployment="deployment-client",
427),
428"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
429"/models",
430{},
431"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
432),
433# AzureOpenAI: Deployment specified
434(
435AzureOpenAI(
436api_version="2024-02-01",
437api_key="example API key",
438azure_endpoint="https://example-resource.azure.openai.com",
439azure_deployment="deployment-client",
440),
441"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
442"/assistants",
443{"model": "deployment-body"},
444"https://example-resource.azure.openai.com/openai/assistants?api-version=2024-02-01",
445),
446# AzureOpenAI: "deployments" in the DNS name
447(
448AzureOpenAI(
449api_version="2024-02-01",
450api_key="example API key",
451azure_endpoint="https://deployments.example-resource.azure.openai.com",
452),
453"https://deployments.example-resource.azure.openai.com/openai/",
454"/models",
455{},
456"https://deployments.example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
457),
458# AzureOpenAI: Deployment called "deployments"
459(
460AzureOpenAI(
461api_version="2024-02-01",
462api_key="example API key",
463azure_endpoint="https://example-resource.azure.openai.com",
464azure_deployment="deployments",
465),
466"https://example-resource.azure.openai.com/openai/deployments/deployments/",
467"/models",
468{},
469"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
470),
471# AzureOpenAI: base_url and azure_deployment specified; azure_deployment ignored b/c not supported
472(
473AzureOpenAI( # type: ignore
474api_version="2024-02-01",
475api_key="example API key",
476base_url="https://example.azure-api.net/PTU/",
477azure_deployment="deployment-client",
478),
479"https://example.azure-api.net/PTU/",
480"/models",
481{},
482"https://example.azure-api.net/PTU/models?api-version=2024-02-01",
483),
484# AsyncAzureOpenAI: No deployment specified
485(
486AsyncAzureOpenAI(
487api_version="2024-02-01",
488api_key="example API key",
489azure_endpoint="https://example-resource.azure.openai.com",
490),
491"https://example-resource.azure.openai.com/openai/",
492"/models",
493{},
494"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
495),
496# AsyncAzureOpenAI: No deployment specified
497(
498AsyncAzureOpenAI(
499api_version="2024-02-01",
500api_key="example API key",
501azure_endpoint="https://example-resource.azure.openai.com",
502),
503"https://example-resource.azure.openai.com/openai/",
504"/assistants",
505{"model": "deployment-body"},
506"https://example-resource.azure.openai.com/openai/assistants?api-version=2024-02-01",
507),
508# AsyncAzureOpenAI: Deployment specified
509(
510AsyncAzureOpenAI(
511api_version="2024-02-01",
512api_key="example API key",
513azure_endpoint="https://example-resource.azure.openai.com",
514azure_deployment="deployment-client",
515),
516"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
517"/models",
518{},
519"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
520),
521# AsyncAzureOpenAI: Deployment specified
522(
523AsyncAzureOpenAI(
524api_version="2024-02-01",
525api_key="example API key",
526azure_endpoint="https://example-resource.azure.openai.com",
527azure_deployment="deployment-client",
528),
529"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
530"/assistants",
531{"model": "deployment-body"},
532"https://example-resource.azure.openai.com/openai/assistants?api-version=2024-02-01",
533),
534# AsyncAzureOpenAI: "deployments" in the DNS name
535(
536AsyncAzureOpenAI(
537api_version="2024-02-01",
538api_key="example API key",
539azure_endpoint="https://deployments.example-resource.azure.openai.com",
540),
541"https://deployments.example-resource.azure.openai.com/openai/",
542"/models",
543{},
544"https://deployments.example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
545),
546# AsyncAzureOpenAI: Deployment called "deployments"
547(
548AsyncAzureOpenAI(
549api_version="2024-02-01",
550api_key="example API key",
551azure_endpoint="https://example-resource.azure.openai.com",
552azure_deployment="deployments",
553),
554"https://example-resource.azure.openai.com/openai/deployments/deployments/",
555"/models",
556{},
557"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01",
558),
559# AsyncAzureOpenAI: base_url and azure_deployment specified; azure_deployment ignored b/c not supported
560(
561AsyncAzureOpenAI( # type: ignore
562api_version="2024-02-01",
563api_key="example API key",
564base_url="https://example.azure-api.net/PTU/",
565azure_deployment="deployment-client",
566),
567"https://example.azure-api.net/PTU/",
568"/models",
569{},
570"https://example.azure-api.net/PTU/models?api-version=2024-02-01",
571),
572],
573)
574def test_prepare_url_nondeployment_endpoint(
575client: Client, base_url: str, api: str, json_data: dict[str, str], expected: str
576) -> None:
577req = client._build_request(
578FinalRequestOptions.construct(
579method="post",
580url=api,
581json_data=json_data,
582)
583)
584assert req.url == expected
585assert client.base_url == base_url
586
587
588@pytest.mark.parametrize(
589"client,base_url,json_data,expected",
590[
591# Realtime endpoint
592# AzureOpenAI: No deployment specified
593(
594AzureOpenAI(
595api_version="2024-02-01",
596api_key="example API key",
597azure_endpoint="https://example-resource.azure.openai.com",
598),
599"https://example-resource.azure.openai.com/openai/",
600{"model": "deployment-body"},
601"wss://example-resource.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployment-body",
602),
603# AzureOpenAI: Deployment specified
604(
605AzureOpenAI(
606api_version="2024-02-01",
607api_key="example API key",
608azure_endpoint="https://example-resource.azure.openai.com",
609azure_deployment="deployment-client",
610),
611"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
612{"model": "deployment-body"},
613"wss://example-resource.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployment-client",
614),
615# AzureOpenAI: "deployments" in the DNS name
616(
617AzureOpenAI(
618api_version="2024-02-01",
619api_key="example API key",
620azure_endpoint="https://deployments.azure.openai.com",
621),
622"https://deployments.azure.openai.com/openai/",
623{"model": "deployment-body"},
624"wss://deployments.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployment-body",
625),
626# AzureOpenAI: Deployment called "deployments"
627(
628AzureOpenAI(
629api_version="2024-02-01",
630api_key="example API key",
631azure_endpoint="https://example-resource.azure.openai.com",
632azure_deployment="deployments",
633),
634"https://example-resource.azure.openai.com/openai/deployments/deployments/",
635{"model": "deployment-body"},
636"wss://example-resource.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployments",
637),
638# AzureOpenAI: base_url and azure_deployment specified; azure_deployment ignored b/c not supported
639(
640AzureOpenAI( # type: ignore
641api_version="2024-02-01",
642api_key="example API key",
643base_url="https://example.azure-api.net/PTU/",
644azure_deployment="my-deployment",
645),
646"https://example.azure-api.net/PTU/",
647{"model": "deployment-body"},
648"wss://example.azure-api.net/PTU/realtime?api-version=2024-02-01&deployment=deployment-body",
649),
650# AzureOpenAI: websocket_base_url specified
651(
652AzureOpenAI(
653api_version="2024-02-01",
654api_key="example API key",
655azure_endpoint="https://example-resource.azure.openai.com",
656websocket_base_url="wss://example-resource.azure.openai.com/base",
657),
658"https://example-resource.azure.openai.com/openai/",
659{"model": "deployment-body"},
660"wss://example-resource.azure.openai.com/base/realtime?api-version=2024-02-01&deployment=deployment-body",
661),
662],
663)
664def test_prepare_url_realtime(client: AzureOpenAI, base_url: str, json_data: dict[str, str], expected: str) -> None:
665url, _ = client._configure_realtime(json_data["model"], {})
666assert str(url) == expected
667assert client.base_url == base_url
668
669
670@pytest.mark.parametrize(
671"client,base_url,json_data,expected",
672[
673# AsyncAzureOpenAI: No deployment specified
674(
675AsyncAzureOpenAI(
676api_version="2024-02-01",
677api_key="example API key",
678azure_endpoint="https://example-resource.azure.openai.com",
679),
680"https://example-resource.azure.openai.com/openai/",
681{"model": "deployment-body"},
682"wss://example-resource.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployment-body",
683),
684# AsyncAzureOpenAI: Deployment specified
685(
686AsyncAzureOpenAI(
687api_version="2024-02-01",
688api_key="example API key",
689azure_endpoint="https://example-resource.azure.openai.com",
690azure_deployment="deployment-client",
691),
692"https://example-resource.azure.openai.com/openai/deployments/deployment-client/",
693{"model": "deployment-body"},
694"wss://example-resource.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployment-client",
695),
696# AsyncAzureOpenAI: "deployments" in the DNS name
697(
698AsyncAzureOpenAI(
699api_version="2024-02-01",
700api_key="example API key",
701azure_endpoint="https://deployments.azure.openai.com",
702),
703"https://deployments.azure.openai.com/openai/",
704{"model": "deployment-body"},
705"wss://deployments.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployment-body",
706),
707# AsyncAzureOpenAI: Deployment called "deployments"
708(
709AsyncAzureOpenAI(
710api_version="2024-02-01",
711api_key="example API key",
712azure_endpoint="https://example-resource.azure.openai.com",
713azure_deployment="deployments",
714),
715"https://example-resource.azure.openai.com/openai/deployments/deployments/",
716{"model": "deployment-body"},
717"wss://example-resource.azure.openai.com/openai/realtime?api-version=2024-02-01&deployment=deployments",
718),
719# AsyncAzureOpenAI: base_url and azure_deployment specified; azure_deployment ignored b/c not supported
720(
721AsyncAzureOpenAI( # type: ignore
722api_version="2024-02-01",
723api_key="example API key",
724base_url="https://example.azure-api.net/PTU/",
725azure_deployment="deployment-client",
726),
727"https://example.azure-api.net/PTU/",
728{"model": "deployment-body"},
729"wss://example.azure-api.net/PTU/realtime?api-version=2024-02-01&deployment=deployment-body",
730),
731# AsyncAzureOpenAI: websocket_base_url specified
732(
733AsyncAzureOpenAI(
734api_version="2024-02-01",
735api_key="example API key",
736azure_endpoint="https://example-resource.azure.openai.com",
737websocket_base_url="wss://example-resource.azure.openai.com/base",
738),
739"https://example-resource.azure.openai.com/openai/",
740{"model": "deployment-body"},
741"wss://example-resource.azure.openai.com/base/realtime?api-version=2024-02-01&deployment=deployment-body",
742),
743],
744)
745async def test_prepare_url_realtime_async(
746client: AsyncAzureOpenAI, base_url: str, json_data: dict[str, str], expected: str
747) -> None:
748url, _ = await client._configure_realtime(json_data["model"], {})
749assert str(url) == expected
750assert client.base_url == base_url
751
752
753def test_client_sets_base_url(client: Client) -> None:
754client = AzureOpenAI(
755api_version="2024-02-01",
756api_key="example API key",
757azure_endpoint="https://example-resource.azure.openai.com",
758azure_deployment="my-deployment",
759)
760assert client.base_url == "https://example-resource.azure.openai.com/openai/deployments/my-deployment/"
761
762# (not recommended) user sets base_url to target different deployment
763client.base_url = "https://example-resource.azure.openai.com/openai/deployments/different-deployment/"
764req = client._build_request(
765FinalRequestOptions.construct(
766method="post",
767url="/chat/completions",
768json_data={"model": "placeholder"},
769)
770)
771assert (
772req.url
773== "https://example-resource.azure.openai.com/openai/deployments/different-deployment/chat/completions?api-version=2024-02-01"
774)
775req = client._build_request(
776FinalRequestOptions.construct(
777method="post",
778url="/models",
779json_data={},
780)
781)
782assert req.url == "https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01"
783
784# (not recommended) user sets base_url to remove deployment
785client.base_url = "https://example-resource.azure.openai.com/openai/"
786req = client._build_request(
787FinalRequestOptions.construct(
788method="post",
789url="/chat/completions",
790json_data={"model": "deployment"},
791)
792)
793assert (
794req.url
795== "https://example-resource.azure.openai.com/openai/deployments/deployment/chat/completions?api-version=2024-02-01"
796)
797req = client._build_request(
798FinalRequestOptions.construct(
799method="post",
800url="/models",
801json_data={},
802)
803)
804assert req.url == "https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01"