-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtest_azure.py
150 lines (112 loc) · 4 KB
/
test_azure.py
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
from typing import Union, cast
from typing_extensions import Literal, Protocol
import httpx
import pytest
from respx import MockRouter
from openai._models import FinalRequestOptions
from openai.lib.azure import AzureOpenAI, AsyncAzureOpenAI
Client = Union[AzureOpenAI, AsyncAzureOpenAI]
sync_client = AzureOpenAI(
api_version="2023-07-01",
api_key="example API key",
azure_endpoint="https://example-resource.azure.openai.com",
)
async_client = AsyncAzureOpenAI(
api_version="2023-07-01",
api_key="example API key",
azure_endpoint="https://example-resource.azure.openai.com",
)
class MockRequestCall(Protocol):
request: httpx.Request
@pytest.mark.parametrize("client", [sync_client, async_client])
def test_implicit_deployment_path(client: Client) -> None:
req = client._build_request(
FinalRequestOptions.construct(
method="post",
url="/chat/completions",
json_data={"model": "my-deployment-model"},
)
)
assert (
req.url
== "https://example-resource.azure.openai.com/openai/deployments/my-deployment-model/chat/completions?api-version=2023-07-01"
)
@pytest.mark.parametrize(
"client,method",
[
(sync_client, "copy"),
(sync_client, "with_options"),
(async_client, "copy"),
(async_client, "with_options"),
],
)
def test_client_copying(client: Client, method: Literal["copy", "with_options"]) -> None:
if method == "copy":
copied = client.copy()
else:
copied = client.with_options()
assert copied._custom_query == {"api-version": "2023-07-01"}
@pytest.mark.parametrize(
"client",
[sync_client, async_client],
)
def test_client_copying_override_options(client: Client) -> None:
copied = client.copy(
api_version="2022-05-01",
)
assert copied._custom_query == {"api-version": "2022-05-01"}
@pytest.mark.respx()
def test_client_token_provider_refresh_sync(respx_mock: MockRouter) -> None:
respx_mock.post(
"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
).mock(
side_effect=[
httpx.Response(500, json={"error": "server error"}),
httpx.Response(200, json={"foo": "bar"}),
]
)
counter = 0
def token_provider() -> str:
nonlocal counter
counter += 1
if counter == 1:
return "first"
return "second"
client = AzureOpenAI(
api_version="2024-02-01",
azure_ad_token_provider=token_provider,
azure_endpoint="https://example-resource.azure.openai.com",
)
client.chat.completions.create(messages=[], model="gpt-4")
calls = cast("list[MockRequestCall]", respx_mock.calls)
assert len(calls) == 2
assert calls[0].request.headers.get("Authorization") == "Bearer first"
assert calls[1].request.headers.get("Authorization") == "Bearer second"
@pytest.mark.asyncio
@pytest.mark.respx()
async def test_client_token_provider_refresh_async(respx_mock: MockRouter) -> None:
respx_mock.post(
"https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
).mock(
side_effect=[
httpx.Response(500, json={"error": "server error"}),
httpx.Response(200, json={"foo": "bar"}),
]
)
counter = 0
def token_provider() -> str:
nonlocal counter
counter += 1
if counter == 1:
return "first"
return "second"
client = AsyncAzureOpenAI(
api_version="2024-02-01",
azure_ad_token_provider=token_provider,
azure_endpoint="https://example-resource.azure.openai.com",
)
await client.chat.completions.create(messages=[], model="gpt-4")
calls = cast("list[MockRequestCall]", respx_mock.calls)
assert len(calls) == 2
assert calls[0].request.headers.get("Authorization") == "Bearer first"
assert calls[1].request.headers.get("Authorization") == "Bearer second"