forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial realtime tests for aoai (Azure#38939)
* add initial realtime tests * retrigger CI
- Loading branch information
1 parent
e4ee11e
commit eb751a5
Showing
5 changed files
with
232 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
-e ../../../tools/azure-sdk-tools | ||
../../identity/azure-identity | ||
aiohttp | ||
openai | ||
openai[realtime] | ||
pillow | ||
pydantic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
|
||
import os | ||
import pytest | ||
import openai | ||
from devtools_testutils import AzureRecordedTestCase, get_credential | ||
from conftest import ( | ||
ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT, | ||
ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY, | ||
GPT_4_AZURE, | ||
GPT_4_OPENAI, | ||
configure, | ||
PREVIEW, | ||
) | ||
|
||
|
||
@pytest.mark.live_test_only | ||
class TestRealtime(AzureRecordedTestCase): | ||
|
||
@configure | ||
@pytest.mark.parametrize( | ||
"api_type, api_version", | ||
[(GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")], | ||
) | ||
def test_realtime_text(self, client, api_type, api_version, **kwargs): | ||
with client.beta.realtime.connect( | ||
**kwargs, | ||
) as connection: | ||
connection.session.update(session={"modalities": ["text"]}) | ||
connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": "Say hello!"}], | ||
} | ||
) | ||
connection.response.create() | ||
for event in connection: | ||
if event.type == "response.text.delta": | ||
assert event.delta | ||
elif event.type == "response.text.done": | ||
assert event.text | ||
elif event.type == "response.done": | ||
break | ||
|
||
@configure | ||
@pytest.mark.parametrize( | ||
"api_type, api_version", | ||
[(GPT_4_AZURE, PREVIEW)], | ||
) | ||
def test_realtime_text_api_key(self, client, api_type, api_version, **kwargs): | ||
client = openai.AzureOpenAI( | ||
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT], | ||
api_key=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY], | ||
api_version=api_version, | ||
) | ||
with client.beta.realtime.connect( | ||
**kwargs | ||
) as connection: | ||
connection.session.update(session={"modalities": ["text"]}) | ||
connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": "Say hello!"}], | ||
} | ||
) | ||
connection.response.create() | ||
for event in connection: | ||
if event.type == "response.text.delta": | ||
assert event.delta | ||
elif event.type == "response.text.done": | ||
assert event.text | ||
elif event.type == "response.done": | ||
break | ||
|
||
@configure | ||
@pytest.mark.parametrize( | ||
"api_type, api_version", | ||
[(GPT_4_AZURE, PREVIEW)], | ||
) | ||
def test_realtime_text_ad_token(self, client, api_type, api_version, **kwargs): | ||
client = openai.AzureOpenAI( | ||
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT], | ||
azure_ad_token=get_credential().get_token("https://cognitiveservices.azure.com/.default").token, | ||
api_version=api_version, | ||
) | ||
with client.beta.realtime.connect( | ||
**kwargs | ||
) as connection: | ||
connection.session.update(session={"modalities": ["text"]}) | ||
connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": "Say hello!"}], | ||
} | ||
) | ||
connection.response.create() | ||
for event in connection: | ||
if event.type == "response.text.delta": | ||
assert event.delta | ||
elif event.type == "response.text.done": | ||
assert event.text | ||
elif event.type == "response.done": | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
|
||
import os | ||
import pytest | ||
import openai | ||
from devtools_testutils import AzureRecordedTestCase, get_credential | ||
from conftest import ( | ||
ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT, | ||
ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY, | ||
GPT_4_AZURE, | ||
GPT_4_OPENAI, | ||
configure_async, | ||
PREVIEW, | ||
) | ||
|
||
|
||
@pytest.mark.live_test_only | ||
class TestRealtimeAsync(AzureRecordedTestCase): | ||
|
||
@configure_async | ||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"api_type, api_version", | ||
[(GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")], | ||
) | ||
async def test_realtime_text(self, client_async, api_type, api_version, **kwargs): | ||
async with client_async.beta.realtime.connect( | ||
**kwargs, | ||
) as connection: | ||
await connection.session.update(session={"modalities": ["text"]}) | ||
await connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": "Say hello!"}], | ||
} | ||
) | ||
await connection.response.create() | ||
async for event in connection: | ||
if event.type == "response.text.delta": | ||
assert event.delta | ||
elif event.type == "response.text.done": | ||
assert event.text | ||
elif event.type == "response.done": | ||
break | ||
|
||
@configure_async | ||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"api_type, api_version", | ||
[(GPT_4_AZURE, PREVIEW)], | ||
) | ||
async def test_realtime_text_api_key(self, client_async, api_type, api_version, **kwargs): | ||
client_async = openai.AsyncAzureOpenAI( | ||
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT], | ||
api_key=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_KEY], | ||
api_version=api_version, | ||
) | ||
async with client_async.beta.realtime.connect( | ||
**kwargs | ||
) as connection: | ||
await connection.session.update(session={"modalities": ["text"]}) | ||
await connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": "Say hello!"}], | ||
} | ||
) | ||
await connection.response.create() | ||
async for event in connection: | ||
if event.type == "response.text.delta": | ||
assert event.delta | ||
elif event.type == "response.text.done": | ||
assert event.text | ||
elif event.type == "response.done": | ||
break | ||
|
||
@configure_async | ||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"api_type, api_version", | ||
[(GPT_4_AZURE, PREVIEW)], | ||
) | ||
async def test_realtime_text_ad_token(self, client_async, api_type, api_version, **kwargs): | ||
credential = get_credential(is_async=True) | ||
access_token = await credential.get_token("https://cognitiveservices.azure.com/.default") | ||
client_async = openai.AsyncAzureOpenAI( | ||
azure_endpoint=os.environ[ENV_AZURE_OPENAI_SWEDENCENTRAL_ENDPOINT], | ||
azure_ad_token=access_token.token, | ||
api_version=api_version, | ||
) | ||
async with client_async.beta.realtime.connect( | ||
**kwargs | ||
) as connection: | ||
await connection.session.update(session={"modalities": ["text"]}) | ||
await connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": "Say hello!"}], | ||
} | ||
) | ||
await connection.response.create() | ||
async for event in connection: | ||
if event.type == "response.text.delta": | ||
assert event.delta | ||
elif event.type == "response.text.done": | ||
assert event.text | ||
elif event.type == "response.done": | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters