Skip to content

Commit 8294a71

Browse files
authored
Extend basic test for "project_client.agents" to do more operations (#43516)
1 parent 9411aab commit 8294a71

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

sdk/ai/azure-ai-projects/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-projects",
5-
"Tag": "python/ai/azure-ai-projects_17a05ba347"
5+
"Tag": "python/ai/azure-ai-projects_89765454c9"
66
}

sdk/ai/azure-ai-projects/tests/test_agents.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55

6+
import time
67
from azure.ai.projects import AIProjectClient
8+
from azure.ai.agents.models import ListSortOrder
79
from test_base import TestBase, servicePreparer
810
from devtools_testutils import recorded_by_proxy
911

1012
# NOTE: This is just a simple test to verify that the agent can be created and deleted using AIProjectClient.
1113
# You can find comprehensive Agent functionally tests here:
1214
# https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-agents/tests
1315

14-
1516
class TestAgents(TestBase):
1617

1718
# To run this test, use the following command in the \sdk\ai\azure-ai-projects folder:
@@ -31,15 +32,45 @@ def test_agents(self, **kwargs):
3132
credential=self.get_credential(AIProjectClient, is_async=False),
3233
) as project_client:
3334

34-
print("[test_agents] Create agent")
3535
agent = project_client.agents.create_agent(
3636
model=model_deployment_name,
3737
name=agent_name,
3838
instructions="You are helpful agent",
3939
)
40+
print(f"[test_agents] Created agent, agent ID: {agent.id}")
4041
assert agent.id
4142
assert agent.model == model_deployment_name
4243
assert agent.name == agent_name
4344

44-
print("[test_agents] Delete agent")
45+
thread = project_client.agents.threads.create()
46+
print(f"[test_agents] Created thread, thread ID: {thread.id}")
47+
48+
message = project_client.agents.messages.create(
49+
thread_id=thread.id, role="user", content="how many feet are in a mile?"
50+
)
51+
print(f"[test_agents] Created message, message ID: {message.id}")
52+
53+
run = project_client.agents.runs.create(thread_id=thread.id, agent_id=agent.id)
54+
55+
# Poll the run as long as run status is queued or in progress
56+
while run.status in ["queued", "in_progress", "requires_action"]:
57+
# Wait for a second
58+
time.sleep(1)
59+
run = project_client.agents.runs.get(thread_id=thread.id, run_id=run.id)
60+
print(f"[test_agents] Run status: {run.status}")
61+
62+
if run.status == "failed":
63+
print(f"[test_agents] Run error: {run.last_error}")
64+
assert False
65+
4566
project_client.agents.delete_agent(agent.id)
67+
print("[test_agents] Deleted agent")
68+
69+
messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
70+
last_text: str = ""
71+
for msg in messages:
72+
if msg.text_messages:
73+
last_text = msg.text_messages[-1].text.value
74+
print(f"[test_agents] {msg.role}: {last_text}")
75+
76+
assert "5280" in last_text or "5,280" in last_text

sdk/ai/azure-ai-projects/tests/test_agents_async.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55

6+
import asyncio
67
from azure.ai.projects.aio import AIProjectClient
8+
from azure.ai.agents.models import ListSortOrder
79
from test_base import TestBase, servicePreparer
810
from devtools_testutils.aio import recorded_by_proxy_async
911

1012
# NOTE: This is just a simple test to verify that the agent can be created and deleted using AIProjectClient.
1113
# You can find comprehensive Agent functionally tests here:
1214
# https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-agents/tests
1315

14-
1516
class TestAgentsAsync(TestBase):
1617

1718
# To run this test, use the following command in the \sdk\ai\azure-ai-projects folder:
18-
# cls & pytest tests\test_agents_async.py::TestAgentsAsync::test_agents -s
19+
# cls & pytest tests\test_agents_async.py::TestAgentsAsync::test_agents_async -s
1920
@servicePreparer()
2021
@recorded_by_proxy_async
2122
async def test_agents_async(self, **kwargs):
@@ -31,15 +32,45 @@ async def test_agents_async(self, **kwargs):
3132
credential=self.get_credential(AIProjectClient, is_async=True),
3233
) as project_client:
3334

34-
print("[test_agents_async] Create agent")
3535
agent = await project_client.agents.create_agent(
3636
model=model_deployment_name,
3737
name=agent_name,
3838
instructions="You are helpful agent",
3939
)
40+
print(f"[test_agents_async] Created agent, agent ID: {agent.id}")
4041
assert agent.id
4142
assert agent.model == model_deployment_name
4243
assert agent.name == agent_name
4344

44-
print("[test_agents_async] Delete agent")
45+
thread = await project_client.agents.threads.create()
46+
print(f"[test_agents_async] Created thread, thread ID: {thread.id}")
47+
48+
message = await project_client.agents.messages.create(
49+
thread_id=thread.id, role="user", content="how many feet are in a mile?"
50+
)
51+
print(f"[test_agents_async] Created message, message ID: {message.id}")
52+
53+
run = await project_client.agents.runs.create(thread_id=thread.id, agent_id=agent.id)
54+
55+
# Poll the run as long as run status is queued or in progress
56+
while run.status in ["queued", "in_progress", "requires_action"]:
57+
# Wait for a second
58+
await asyncio.sleep(1)
59+
run = await project_client.agents.runs.get(thread_id=thread.id, run_id=run.id)
60+
print(f"[test_agents_async] Run status: {run.status}")
61+
62+
if run.status == "failed":
63+
print(f"[test_agents_async] Run error: {run.last_error}")
64+
assert False
65+
4566
await project_client.agents.delete_agent(agent.id)
67+
print("[test_agents_async] Deleted agent")
68+
69+
messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
70+
last_text: str = ""
71+
async for msg in messages:
72+
if msg.text_messages:
73+
last_text = msg.text_messages[-1].text.value
74+
print(f"[test_agents_async] {msg.role}: {last_text}")
75+
76+
assert "5280" in last_text or "5,280" in last_text

0 commit comments

Comments
 (0)