2626# Import necessary libraries
2727import os
2828import jsonref
29- from azure .ai .agents import AgentsClient
29+ from azure .ai .projects import AIProjectClient
3030from azure .identity import DefaultAzureCredential
3131from azure .ai .agents .models import OpenApiTool , OpenApiAnonymousAuthDetails
32- from dotenv import load_dotenv
33- load_dotenv ()
34-
35- # Initialize the Agents Client using the endpoint and default credentials
36- agents_client = AgentsClient (
37- endpoint = os .environ ["PROJECT_ENDPOINT" ],
38- credential = DefaultAzureCredential (),
39- )
40- # </initialization>
41-
42- # <weather_tool_setup>
43- # --- Weather OpenAPI Tool Setup ---
44- # Load the OpenAPI specification for the weather service from a local JSON file using jsonref to handle references
45- with open (os .path .join (os .path .dirname (__file__ ), "weather_openapi.json" ), "r" ) as f :
46- openapi_weather = jsonref .loads (f .read ())
47- # </weather_tool_setup>
48-
49- # <countries_tool_setup>
50- # --- Countries OpenAPI Tool Setup ---
51- # Load the OpenAPI specification for the countries service from a local JSON file
52- with open (os .path .join (os .path .dirname (__file__ ), "countries.json" ), "r" ) as f :
53- openapi_countries = jsonref .loads (f .read ())
54-
55- # Create Auth object for the OpenApiTool (note: using anonymous auth here; connection or managed identity requires additional setup)
56- auth = OpenApiAnonymousAuthDetails ()
57-
58- # Initialize the main OpenAPI tool definition for weather
59- openapi_tool = OpenApiTool (
60- name = "get_weather" , spec = openapi_weather , description = "Retrieve weather information for a location" , auth = auth
61- )
62- # Add the countries API definition to the same tool object
63- openapi_tool .add_definition (
64- name = "get_countries" , spec = openapi_countries , description = "Retrieve a list of countries" , auth = auth
65- )
66- # </countries_tool_setup>
67-
68- # Use the agents client context manager
69- with agents_client :
32+
33+ endpoint = os .environ ["PROJECT_ENDPOINT" ]
34+ model_deployment_name = os .environ ["MODEL_DEPLOYMENT_NAME" ]
35+ # Initialize the project client using the endpoint and default credentials
36+ with AIProjectClient (
37+ endpoint = endpoint ,
38+ credential = DefaultAzureCredential (exclude_interactive_browser_credential = False ),
39+ ) as project_client :
40+ # </initialization>
41+
42+ # <weather_tool_setup>
43+ # --- Weather OpenAPI Tool Setup ---
44+ # Load the OpenAPI specification for the weather service from a local JSON file using jsonref to handle references
45+ with open (os .path .join (os .path .dirname (__file__ ), "weather_openapi.json" ), "r" ) as f :
46+ openapi_weather = jsonref .loads (f .read ())
47+ # </weather_tool_setup>
48+
49+ # <countries_tool_setup>
50+ # --- Countries OpenAPI Tool Setup ---
51+ # Load the OpenAPI specification for the countries service from a local JSON file
52+ with open (os .path .join (os .path .dirname (__file__ ), "countries.json" ), "r" ) as f :
53+ openapi_countries = jsonref .loads (f .read ())
54+
55+ # Create Auth object for the OpenApiTool (note: using anonymous auth here; connection or managed identity requires additional setup)
56+ auth = OpenApiAnonymousAuthDetails ()
57+
58+ # Initialize the main OpenAPI tool definition for weather
59+ openapi_tool = OpenApiTool (
60+ name = "get_weather" , spec = openapi_weather , description = "Retrieve weather information for a location" , auth = auth
61+ )
62+ # Add the countries API definition to the same tool object
63+ openapi_tool .add_definition (
64+ name = "get_countries" , spec = openapi_countries , description = "Retrieve a list of countries" , auth = auth
65+ )
66+ # </countries_tool_setup>
67+
7068 # <agent_creation>
7169 # --- Agent Creation ---
7270 # Create an agent configured with the combined OpenAPI tool definitions
73- agent = agents_client .create_agent (
71+ agent = project_client . agents .create_agent (
7472 model = os .environ ["MODEL_DEPLOYMENT_NAME" ], # Specify the model deployment
7573 name = "my-agent" , # Give the agent a name
7674 instructions = "You are a helpful agent" , # Define agent's role
8280 # <thread_management>
8381 # --- Thread Management ---
8482 # Create a new conversation thread for the interaction
85- thread = agents_client .create_thread ()
83+ thread = project_client . agents .create_thread ()
8684 print (f"Created thread, ID: { thread .id } " )
8785
8886 # Create the initial user message in the thread
89- message = agents_client .create_message (
87+ message = project_client . agents .create_message (
9088 thread_id = thread .id ,
9189 role = "user" ,
9290 content = "What's the weather in Seattle and What is the name and population of the country that uses currency with abbreviation THB?" ,
9896 # --- Message Processing (Run Creation and Auto-processing) ---
9997 # Create and automatically process the run, handling tool calls internally
10098 # Note: This differs from the function_tool example where tool calls are handled manually
101- run = agents_client .create_and_process_run (thread_id = thread .id , agent_id = agent .id )
99+ run = project_client . agents .create_and_process_run (thread_id = thread .id , agent_id = agent .id )
102100 print (f"Run finished with status: { run .status } " )
103101 # </message_processing>
104102
108106 print (f"Run failed: { run .last_error } " )
109107
110108 # Retrieve the steps taken during the run for analysis
111- run_steps = agents_client .list_run_steps (thread_id = thread .id , run_id = run .id )
109+ run_steps = project_client . agents .list_run_steps (thread_id = thread .id , run_id = run .id )
112110
113111 # Loop through each step to display information
114112 for step in run_steps .data :
133131 # <cleanup>
134132 # --- Cleanup ---
135133 # Delete the agent resource to clean up
136- agents_client .delete_agent (agent .id )
134+ project_client . agents .delete_agent (agent .id )
137135 print ("Deleted agent" )
138136
139137 # Fetch and log all messages exchanged during the conversation thread
140- messages = agents_client .list_messages (thread_id = thread .id )
138+ messages = project_client . agents .list_messages (thread_id = thread .id )
141139 print (f"Messages: { messages } " )
142140 # </cleanup>
0 commit comments