Skip to content

Commit eb751a5

Browse files
add initial realtime tests for aoai (Azure#38939)
* add initial realtime tests * retrigger CI
1 parent e4ee11e commit eb751a5

File tree

5 files changed

+232
-2
lines changed

5 files changed

+232
-2
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-e ../../../tools/azure-sdk-tools
22
../../identity/azure-identity
33
aiohttp
4-
openai
4+
openai[realtime]
55
pillow
66
pydantic

sdk/openai/azure-openai/tests/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# for pytest.parametrize
2323
GA = "2024-10-21"
24-
PREVIEW = "2024-11-01-preview"
24+
PREVIEW = "2024-10-01-preview"
2525
LATEST = PREVIEW
2626

2727
AZURE = "azure"
@@ -40,6 +40,7 @@
4040
ENV_AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"
4141
ENV_AZURE_OPENAI_NORTHCENTRALUS_ENDPOINT = "AZURE_OPENAI_NORTHCENTRALUS_ENDPOINT"
4242
ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT = "AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT"
43+
ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY = "AZURE_OPENAI_SWEDENCENTRAL_KEY"
4344
ENV_AZURE_OPENAI_SEARCH_ENDPOINT = "AZURE_OPENAI_SEARCH_ENDPOINT"
4445
ENV_AZURE_OPENAI_SEARCH_INDEX = "AZURE_OPENAI_SEARCH_INDEX"
4546

@@ -178,6 +179,11 @@ def build_kwargs(args, api_type):
178179
return {"model": ENV_AZURE_OPENAI_CHAT_COMPLETIONS_GPT4_NAME}
179180
elif api_type == "gpt_4_openai":
180181
return {"model": ENV_OPENAI_CHAT_COMPLETIONS_GPT4_MODEL}
182+
if test_feature.startswith("test_realtime"):
183+
if api_type in ["gpt_4_azure"]:
184+
return {"model": "gpt-4o-realtime-preview-1001"}
185+
elif api_type == "gpt_4_openai":
186+
return {"model": "gpt-4o-realtime-preview-2024-10-01"}
181187
if test_feature.startswith(("test_module_client", "test_cli")):
182188
return {}
183189
raise ValueError(f"Test feature: {test_feature} needs to have its kwargs configured.")
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
import os
7+
import pytest
8+
import openai
9+
from devtools_testutils import AzureRecordedTestCase, get_credential
10+
from conftest import (
11+
ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT,
12+
ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY,
13+
GPT_4_AZURE,
14+
GPT_4_OPENAI,
15+
configure,
16+
PREVIEW,
17+
)
18+
19+
20+
@pytest.mark.live_test_only
21+
class TestRealtime(AzureRecordedTestCase):
22+
23+
@configure
24+
@pytest.mark.parametrize(
25+
"api_type, api_version",
26+
[(GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")],
27+
)
28+
def test_realtime_text(self, client, api_type, api_version, **kwargs):
29+
with client.beta.realtime.connect(
30+
**kwargs,
31+
) as connection:
32+
connection.session.update(session={"modalities": ["text"]})
33+
connection.conversation.item.create(
34+
item={
35+
"type": "message",
36+
"role": "user",
37+
"content": [{"type": "input_text", "text": "Say hello!"}],
38+
}
39+
)
40+
connection.response.create()
41+
for event in connection:
42+
if event.type == "response.text.delta":
43+
assert event.delta
44+
elif event.type == "response.text.done":
45+
assert event.text
46+
elif event.type == "response.done":
47+
break
48+
49+
@configure
50+
@pytest.mark.parametrize(
51+
"api_type, api_version",
52+
[(GPT_4_AZURE, PREVIEW)],
53+
)
54+
def test_realtime_text_api_key(self, client, api_type, api_version, **kwargs):
55+
client = openai.AzureOpenAI(
56+
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT],
57+
api_key=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY],
58+
api_version=api_version,
59+
)
60+
with client.beta.realtime.connect(
61+
**kwargs
62+
) as connection:
63+
connection.session.update(session={"modalities": ["text"]})
64+
connection.conversation.item.create(
65+
item={
66+
"type": "message",
67+
"role": "user",
68+
"content": [{"type": "input_text", "text": "Say hello!"}],
69+
}
70+
)
71+
connection.response.create()
72+
for event in connection:
73+
if event.type == "response.text.delta":
74+
assert event.delta
75+
elif event.type == "response.text.done":
76+
assert event.text
77+
elif event.type == "response.done":
78+
break
79+
80+
@configure
81+
@pytest.mark.parametrize(
82+
"api_type, api_version",
83+
[(GPT_4_AZURE, PREVIEW)],
84+
)
85+
def test_realtime_text_ad_token(self, client, api_type, api_version, **kwargs):
86+
client = openai.AzureOpenAI(
87+
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT],
88+
azure_ad_token=get_credential().get_token("https://cognitiveservices.azure.com/.default").token,
89+
api_version=api_version,
90+
)
91+
with client.beta.realtime.connect(
92+
**kwargs
93+
) as connection:
94+
connection.session.update(session={"modalities": ["text"]})
95+
connection.conversation.item.create(
96+
item={
97+
"type": "message",
98+
"role": "user",
99+
"content": [{"type": "input_text", "text": "Say hello!"}],
100+
}
101+
)
102+
connection.response.create()
103+
for event in connection:
104+
if event.type == "response.text.delta":
105+
assert event.delta
106+
elif event.type == "response.text.done":
107+
assert event.text
108+
elif event.type == "response.done":
109+
break
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
import os
7+
import pytest
8+
import openai
9+
from devtools_testutils import AzureRecordedTestCase, get_credential
10+
from conftest import (
11+
ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT,
12+
ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY,
13+
GPT_4_AZURE,
14+
GPT_4_OPENAI,
15+
configure_async,
16+
PREVIEW,
17+
)
18+
19+
20+
@pytest.mark.live_test_only
21+
class TestRealtimeAsync(AzureRecordedTestCase):
22+
23+
@configure_async
24+
@pytest.mark.asyncio
25+
@pytest.mark.parametrize(
26+
"api_type, api_version",
27+
[(GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")],
28+
)
29+
async def test_realtime_text(self, client_async, api_type, api_version, **kwargs):
30+
async with client_async.beta.realtime.connect(
31+
**kwargs,
32+
) as connection:
33+
await connection.session.update(session={"modalities": ["text"]})
34+
await connection.conversation.item.create(
35+
item={
36+
"type": "message",
37+
"role": "user",
38+
"content": [{"type": "input_text", "text": "Say hello!"}],
39+
}
40+
)
41+
await connection.response.create()
42+
async for event in connection:
43+
if event.type == "response.text.delta":
44+
assert event.delta
45+
elif event.type == "response.text.done":
46+
assert event.text
47+
elif event.type == "response.done":
48+
break
49+
50+
@configure_async
51+
@pytest.mark.asyncio
52+
@pytest.mark.parametrize(
53+
"api_type, api_version",
54+
[(GPT_4_AZURE, PREVIEW)],
55+
)
56+
async def test_realtime_text_api_key(self, client_async, api_type, api_version, **kwargs):
57+
client_async = openai.AsyncAzureOpenAI(
58+
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT],
59+
api_key=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY],
60+
api_version=api_version,
61+
)
62+
async with client_async.beta.realtime.connect(
63+
**kwargs
64+
) as connection:
65+
await connection.session.update(session={"modalities": ["text"]})
66+
await connection.conversation.item.create(
67+
item={
68+
"type": "message",
69+
"role": "user",
70+
"content": [{"type": "input_text", "text": "Say hello!"}],
71+
}
72+
)
73+
await connection.response.create()
74+
async for event in connection:
75+
if event.type == "response.text.delta":
76+
assert event.delta
77+
elif event.type == "response.text.done":
78+
assert event.text
79+
elif event.type == "response.done":
80+
break
81+
82+
@configure_async
83+
@pytest.mark.asyncio
84+
@pytest.mark.parametrize(
85+
"api_type, api_version",
86+
[(GPT_4_AZURE, PREVIEW)],
87+
)
88+
async def test_realtime_text_ad_token(self, client_async, api_type, api_version, **kwargs):
89+
credential = get_credential(is_async=True)
90+
access_token = await credential.get_token("https://cognitiveservices.azure.com/.default")
91+
client_async = openai.AsyncAzureOpenAI(
92+
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT],
93+
azure_ad_token=access_token.token,
94+
api_version=api_version,
95+
)
96+
async with client_async.beta.realtime.connect(
97+
**kwargs
98+
) as connection:
99+
await connection.session.update(session={"modalities": ["text"]})
100+
await connection.conversation.item.create(
101+
item={
102+
"type": "message",
103+
"role": "user",
104+
"content": [{"type": "input_text", "text": "Say hello!"}],
105+
}
106+
)
107+
await connection.response.create()
108+
async for event in connection:
109+
if event.type == "response.text.delta":
110+
assert event.delta
111+
elif event.type == "response.text.done":
112+
assert event.text
113+
elif event.type == "response.done":
114+
break

sdk/openai/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extends:
1717
AZURE_OPENAI_COMPLETIONS_DEPLOYMENT: $(AOAI-COMPLETIONS-MODEL-DEPLOYMENT)
1818
AZURE_OPENAI_NORTHCENTRALUS_ENDPOINT: $(AOAI-ENDPOINT-NORTHCENTRALUS)
1919
AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT: $(AOAI-ENDPOINT-SWEDENCENTRAL)
20+
AZURE_OPENAI_SWEDENCENTRAL_KEY: $(AOAI-API-KEY-SWEDENCENTRAL)
2021
OPENAI_KEY: $(python-nonazure-openai-key)
2122
AZURE_OPENAI_SEARCH_ENDPOINT: $(COGNITIVE-SEARCH-API-ENDPOINT)
2223
AZURE_OPENAI_SEARCH_INDEX: $(COGNITIVE-SEARCH-API-INDEX)

0 commit comments

Comments
 (0)