@@ -277,52 +277,51 @@ async def initialize_eval(project_client: AIProjectClient, agent: AgentVersionOb
277277
278278
279279async def initialize_resources ():
280+ proj_endpoint = os .environ .get ("AZURE_EXISTING_AIPROJECT_ENDPOINT" )
280281 try :
281- creds = DefaultAzureCredential ()
282-
283- proj_endpoint = os .environ .get ("AZURE_EXISTING_AIPROJECT_ENDPOINT" )
284- async with AIProjectClient (
285- credential = creds ,
286- endpoint = proj_endpoint ,
287- api_version = "2025-11-15-preview" ,
288- ) as ai_project :
289- # If the environment already has AZURE_AI_AGENT_ID or AZURE_EXISTING_AGENT_ID, try
290- # fetching that agent
291- agent_obj : Optional [AgentVersionObject ] = None
292-
293- agentID = os .environ .get ("AZURE_EXISTING_AGENT_ID" )
294- if agentID :
295- try :
296- agent_name = agentID .split (":" )[0 ]
297- agent_version = agentID .split (":" )[1 ]
298- agent_obj = await ai_project .agents .get_version (agent_name , agent_version )
299- logger .info (f"Found agent by ID: { agent_obj .id } " )
300- except Exception as e :
301- logger .warning (
302- "Could not retrieve agent by AZURE_EXISTING_AGENT_ID = "
303- f"{ agentID } , error: { e } " )
304- else :
305- logger .info ("No existing agent ID found." )
306-
307- # Check if an agent with the same name already exists
308- if not agent_obj :
309- try :
310- agent_name = os .environ ["AZURE_AI_AGENT_NAME" ]
311- logger .info (f"Retrieving agent by name: { agent_name } " )
312- agents = await ai_project .agents .get (agent_name )
313- agent_obj = agents .versions .latest
314- logger .info (f"Agent with agent id, { agent_obj .id } retrieved." )
315- except Exception as e :
316- logger .info (f"Agent name, { agent_name } not found." )
317-
318- # Create a new agent
319- if not agent_obj :
320- agent_obj = await create_agent (ai_project , creds )
321- logger .info (f"Created agent, agent ID: { agent_obj .id } " )
322-
323- os .environ ["AZURE_EXISTING_AGENT_ID" ] = agent_obj .id
324-
325- await initialize_eval (ai_project , agent_obj )
282+ async with DefaultAzureCredential () as credential :
283+ async with AIProjectClient (
284+ credential = credential ,
285+ endpoint = proj_endpoint ,
286+ api_version = "2025-11-15-preview" ,
287+ ) as ai_project :
288+ # If the environment already has AZURE_AI_AGENT_ID or AZURE_EXISTING_AGENT_ID, try
289+ # fetching that agent
290+ agent_obj : Optional [AgentVersionObject ] = None
291+
292+ agentID = os .environ .get ("AZURE_EXISTING_AGENT_ID" )
293+ if agentID :
294+ try :
295+ agent_name = agentID .split (":" )[0 ]
296+ agent_version = agentID .split (":" )[1 ]
297+ agent_obj = await ai_project .agents .get_version (agent_name , agent_version )
298+ logger .info (f"Found agent by ID: { agent_obj .id } " )
299+ except Exception as e :
300+ logger .warning (
301+ "Could not retrieve agent by AZURE_EXISTING_AGENT_ID = "
302+ f"{ agentID } , error: { e } " )
303+ else :
304+ logger .info ("No existing agent ID found." )
305+
306+ # Check if an agent with the same name already exists
307+ if not agent_obj :
308+ try :
309+ agent_name = os .environ ["AZURE_AI_AGENT_NAME" ]
310+ logger .info (f"Retrieving agent by name: { agent_name } " )
311+ agents = await ai_project .agents .get (agent_name )
312+ agent_obj = agents .versions .latest
313+ logger .info (f"Agent with agent id, { agent_obj .id } retrieved." )
314+ except Exception as e :
315+ logger .info (f"Agent name, { agent_name } not found." )
316+
317+ # Create a new agent
318+ if not agent_obj :
319+ agent_obj = await create_agent (ai_project , creds )
320+ logger .info (f"Created agent, agent ID: { agent_obj .id } " )
321+
322+ os .environ ["AZURE_EXISTING_AGENT_ID" ] = agent_obj .id
323+
324+ await initialize_eval (ai_project , agent_obj )
326325
327326 except Exception as e :
328327 logger .info ("Error creating agent: {e}" , exc_info = True )
0 commit comments