|
1 | 1 | """ Agent template for building foundry agents with Azure AI Search, Bing, and MCP plugins. """ |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import logging |
4 | 5 | from typing import List, Optional |
5 | 6 |
|
@@ -39,7 +40,7 @@ def __init__(self, agent_name: str, |
39 | 40 | self._bing_connection = None |
40 | 41 | self.logger = logging.getLogger(__name__) |
41 | 42 | # input validation |
42 | | - if self.model_deployment_name is any(["o3", "o4-mini"]): |
| 43 | + if self.model_deployment_name in any(["o3", "o4-mini"]): |
43 | 44 | raise ValueError("The current version of Foundry agents do not support reasoning models.") |
44 | 45 |
|
45 | 46 | # async def _make_bing_tool(self) -> Optional[BingGroundingTool]: |
@@ -76,9 +77,11 @@ async def _make_azure_search_tool(self) -> Optional[AzureAISearchTool]: |
76 | 77 | return search_tool |
77 | 78 |
|
78 | 79 | except Exception as ex: |
79 | | - self.logger.error(f"Azure AI Search tool creation failed: {ex} Connection name: " / |
80 | | - f"{self.search.connection_name} Index name: {self.search.index_name}" / |
81 | | - " Make sure the connection exists in Azure AI Foundry portal") |
| 80 | + self.logger.error( |
| 81 | + "Azure AI Search tool creation failed: %s | Connection name: %s | Index name: %s | " |
| 82 | + "Make sure the connection exists in Azure AI Foundry portal", |
| 83 | + ex, self.search.connection_name, self.search.index_name |
| 84 | + ) |
82 | 85 | return None |
83 | 86 |
|
84 | 87 | async def _collect_tools_and_resources(self) -> tuple[List, dict]: |
@@ -143,8 +146,39 @@ async def _after_open(self) -> None: |
143 | 146 | self.logger.error("Failed to create AzureAIAgent: %s", ex) |
144 | 147 | raise |
145 | 148 |
|
| 149 | + # After self._agent creation in _after_open: |
| 150 | + # Diagnostics |
| 151 | + try: |
| 152 | + tool_names = [t.get("function", {}).get("name") for t in (definition.tools or []) if isinstance(t, dict)] |
| 153 | + self.logger.info( |
| 154 | + "Foundry agent '%s' initialized. Azure tools: %s | MCP plugin: %s", |
| 155 | + self.agent_name, |
| 156 | + tool_names, |
| 157 | + getattr(self.mcp_plugin, 'name', None) |
| 158 | + ) |
| 159 | + if not tool_names and not plugins: |
| 160 | + self.logger.warning( |
| 161 | + "Foundry agent '%s' has no Azure tool definitions and no MCP plugin. " |
| 162 | + "Subsequent tool calls may fail.", self.agent_name |
| 163 | + ) |
| 164 | + except Exception as diag_ex: |
| 165 | + self.logger.warning("Diagnostics collection failed: %s", diag_ex) |
| 166 | + |
146 | 167 | self.logger.info("%s initialized with %d tools and %d plugins", self.agent_name, len(tools), len(plugins)) |
147 | 168 |
|
| 169 | + async def fetch_run_details(self, thread_id: str, run_id: str): |
| 170 | + """Fetch and log run details after a failure.""" |
| 171 | + try: |
| 172 | + run = await self.client.agents.runs.get(thread=thread_id, run=run_id) |
| 173 | + self.logger.error( |
| 174 | + "Run failure details | status=%s | id=%s | last_error=%s | usage=%s", |
| 175 | + getattr(run, 'status', None), |
| 176 | + run_id, |
| 177 | + getattr(run, 'last_error', None), |
| 178 | + getattr(run, 'usage', None), |
| 179 | + ) |
| 180 | + except Exception as ex: |
| 181 | + self.logger.error("Could not fetch run details: %s", ex) |
148 | 182 |
|
149 | 183 | async def create_foundry_agent(agent_name:str, |
150 | 184 | agent_description:str, |
|
0 commit comments