@@ -18,11 +18,35 @@ async def create_or_get_agent(cls):
1818 Returns:
1919 object: The created agent instance.
2020 """
21- project_client = AIProjectClient (
22- endpoint = app_settings .azure_ai .agent_endpoint ,
23- credential = await get_azure_credential_async (client_id = app_settings .base_settings .azure_client_id ),
24- api_version = app_settings .azure_ai .agent_api_version
25- )
21+ try :
22+ project_client = AIProjectClient (
23+ endpoint = app_settings .azure_ai .agent_endpoint ,
24+ credential = await get_azure_credential_async (client_id = app_settings .base_settings .azure_client_id ),
25+ api_version = app_settings .azure_ai .agent_api_version
26+ )
27+
28+ # Test the connection early to provide better error messages
29+ agents_list = project_client .agents .list_agents ()
30+ await agents_list .__anext__ () # Try to get first agent to test connectivity
31+
32+ except Exception as e :
33+ error_msg = f"Failed to connect to Azure AI Project endpoint '{ app_settings .azure_ai .agent_endpoint } '. "
34+
35+ if "Cannot connect to host" in str (e ):
36+ error_msg += "This is likely a private endpoint DNS resolution issue. Please ensure:\n "
37+ error_msg += "1. The private endpoint has a DNS zone group configured\n "
38+ error_msg += "2. The private DNS zone 'privatelink.cognitiveservices.azure.com' is linked to your VNet\n "
39+ error_msg += "3. Your App Service has VNet integration enabled\n "
40+
41+ error_msg += f"Original error: { str (e )} "
42+
43+ track_event_if_configured ("TemplateAgentConnectionError" , {
44+ "error" : str (e ),
45+ "endpoint" : app_settings .azure_ai .agent_endpoint ,
46+ "error_type" : "connection_failed"
47+ })
48+
49+ raise Exception (error_msg )
2650
2751 agent_name = f"DG-TemplateAgent-{ app_settings .base_settings .solution_name } "
2852 # 1. Check if the agent already exists
@@ -44,16 +68,40 @@ async def create_or_get_agent(cls):
4468 "titleField" : "sourceurl" ,
4569 }
4670
47- project_index = await project_client .indexes .create_or_update (
48- name = index_name ,
49- version = index_version ,
50- body = {
51- "connectionName" : app_settings .datasource .connection_name ,
52- "indexName" : app_settings .datasource .index ,
53- "type" : "AzureSearch" ,
54- "fieldMapping" : field_mapping
55- }
56- )
71+ try :
72+ project_index = await project_client .indexes .create_or_update (
73+ name = index_name ,
74+ version = index_version ,
75+ body = {
76+ "connectionName" : app_settings .datasource .connection_name ,
77+ "indexName" : app_settings .datasource .index ,
78+ "type" : "AzureSearch" ,
79+ "fieldMapping" : field_mapping
80+ }
81+ )
82+ except Exception as e :
83+ error_msg = f"Failed to create or update project index '{ index_name } '. "
84+
85+ if "Cannot connect to host" in str (e ) and "search.windows.net" in str (e ):
86+ error_msg += "This is likely a private endpoint DNS resolution issue for Azure AI Search. Please ensure:\n "
87+ error_msg += "1. The Azure AI Search private endpoint has a DNS zone group configured\n "
88+ error_msg += "2. The private DNS zone 'privatelink.search.windows.net' exists and is linked to your VNet\n "
89+ error_msg += "3. Your App Service has VNet integration to the same VNet as the private endpoints\n "
90+ error_msg += f"4. The search service endpoint resolves correctly from within the VNet\n "
91+
92+ error_msg += f"Connection name: { app_settings .datasource .connection_name } , "
93+ error_msg += f"Index: { app_settings .datasource .index } , "
94+ error_msg += f"Original error: { str (e )} "
95+
96+ track_event_if_configured ("TemplateAgentIndexCreationError" , {
97+ "error" : str (e ),
98+ "index_name" : index_name ,
99+ "connection_name" : app_settings .datasource .connection_name ,
100+ "index" : app_settings .datasource .index ,
101+ "error_type" : "search_connection_failed"
102+ })
103+
104+ raise Exception (error_msg )
57105
58106 ai_search = AzureAISearchTool (
59107 index_asset_id = f"{ project_index .name } /versions/{ project_index .version } " ,
0 commit comments