22import os
33import sys
44from dotenv import load_dotenv
5- from azure .identity import AzureCliCredential
6- from azure .ai .agents import AgentsClient
5+ from azure .identity import DefaultAzureCredential
6+ from azure .ai .projects import AIProjectClient
77
88load_dotenv ()
99
1010def main ():
1111 parser = argparse .ArgumentParser (description = "Call an Azure Foundry Agent." )
1212 parser .add_argument ("prompt" , help = "The prompt to send to the agent." )
1313 parser .add_argument (
14- "--agent-id " ,
15- default = os .environ .get ("AZURE_AI_AGENT_ID " ),
16- help = "The ID of the agent to call. Defaults to AZURE_AI_AGENT_ID environment variable." ,
14+ "--agent-name " ,
15+ default = os .environ .get ("AZURE_AI_AGENT_NAME " ),
16+ help = "The name of the agent to call. Defaults to AZURE_AI_AGENT_NAME environment variable." ,
1717 )
1818 parser .add_argument (
1919 "--endpoint" ,
@@ -27,32 +27,31 @@ def main():
2727 print ("Error: Azure AI Project Endpoint is required. Set AZURE_AI_PROJECT_ENDPOINT env var or use --endpoint." , file = sys .stderr )
2828 sys .exit (1 )
2929
30- if not args .agent_id :
31- print ("Error: Agent ID is required. Set AZURE_AI_AGENT_ID env var or use --agent-id ." , file = sys .stderr )
30+ if not args .agent_name :
31+ print ("Error: Agent Name is required. Set AZURE_AI_AGENT_NAME env var or use --agent-name ." , file = sys .stderr )
3232 sys .exit (1 )
3333
3434 try :
35- credential = AzureCliCredential ()
36- with AgentsClient (endpoint = args .endpoint , credential = credential ) as client :
37- # Create a thread and run the agent, then wait for completion
38- result = client .create_thread_and_process_run (
39- agent_id = args .agent_id ,
40- thread = {"messages" : [{"role" : "user" , "content" : args .prompt }]},
41- )
42-
43- if result .status == "failed" :
44- print (f"Run failed: { result .last_error } " , file = sys .stderr )
45- sys .exit (1 )
46-
47- # Get the agent's response
48- messages = client .messages .list (thread_id = result .thread_id )
49- for message in messages :
50- if message .role == "assistant" :
51- for content in message .content :
52- if hasattr (content , "text" ):
53- print (content .text .value )
35+ project_client = AIProjectClient (
36+ endpoint = args .endpoint ,
37+ credential = DefaultAzureCredential (),
38+ )
39+
40+ # Get an existing agent
41+ agent = project_client .agents .get (agent_name = args .agent_name )
42+
43+ openai_client = project_client .get_openai_client ()
44+
45+ # Reference the agent to get a response
46+ response = openai_client .responses .create (
47+ input = [{"role" : "user" , "content" : args .prompt }],
48+ extra_body = {"agent" : {"name" : agent .name , "type" : "agent_reference" }},
49+ )
50+
51+ print (response .output_text )
52+
5453 except Exception as e :
55- print (f"An error occurred : { e } " , file = sys .stderr )
54+ print (f"Error : { e } " , file = sys .stderr )
5655 sys .exit (1 )
5756
5857
0 commit comments