33# Licensed under the MIT License.
44# ------------------------------------
55
6+ import asyncio
67from azure .ai .projects .aio import AIProjectClient
8+ from azure .ai .agents .models import ListSortOrder
79from test_base import TestBase , servicePreparer
810from 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-
1516class 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