Skip to content

Commit 3841b53

Browse files
committed
Refactor Azure AI Search config and connection handling
Removed unused API key references and improved connection resolution logic for Azure AI Search. SearchConfig now requires index_name as an argument and no longer uses api_key. Updated agent factory and foundry agent to align with these changes and simplified query type handling.
1 parent 797aa9a commit 3841b53

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

src/backend/.env.sample

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ SUPPORTED_MODELS='["o3","o4-mini","gpt-4.1","gpt-4.1-mini"]'
3131
AZURE_AI_SEARCH_CONNECTION_NAME=
3232
AZURE_AI_SEARCH_INDEX_NAME=
3333
AZURE_AI_SEARCH_ENDPOINT=
34-
AZURE_AI_SEARCH_API_KEY=
3534
BING_CONNECTION_NAME=

src/backend/v4/magentic_agents/foundry_agent.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def _create_azure_search_enabled_client(self):
105105
a) API key + endpoint, OR
106106
b) Managed Identity (RBAC enabled on the Search service with Search Service Contributor + Search Index Data Reader).
107107
- search_config.index_name must exist in the Search service.
108-
- search_config.connection_name OR search_config.connection_id must reference the desired connection.
108+
109109
110110
Returns:
111111
AzureAIAgentClient | None
@@ -114,10 +114,10 @@ async def _create_azure_search_enabled_client(self):
114114
self.logger.error("Search configuration missing.")
115115
return None
116116

117-
desired_connection_id = getattr(self.search, "connection_id", None)
117+
118118
desired_connection_name = getattr(self.search, "connection_name", None)
119119
index_name = getattr(self.search, "index_name", "")
120-
query_type = getattr(self.search, "search_query_type", "vector")
120+
query_type = getattr(self.search, "search_query_type", "simple")
121121

122122
if not index_name:
123123
self.logger.error("index_name not provided in search_config; aborting Azure Search path.")
@@ -128,33 +128,27 @@ async def _create_azure_search_enabled_client(self):
128128
try:
129129
async for connection in self.project_client.connections.list():
130130
if connection.type == ConnectionType.AZURE_AI_SEARCH:
131-
# Allow direct id override
132-
if desired_connection_id and connection.id == desired_connection_id:
133-
resolved_connection_id = connection.id
134-
break
135-
# Else match by name
131+
136132
if desired_connection_name and connection.name == desired_connection_name:
137133
resolved_connection_id = connection.id
138134
break
139135
# Fallback: if no specific connection requested and none resolved yet, take the first
140-
if not desired_connection_id and not desired_connection_name and not resolved_connection_id:
136+
if not desired_connection_name and not resolved_connection_id:
141137
resolved_connection_id = connection.id
142138
# Do not break yet; we log but allow chance to find a name match later. If not, this stays.
143139

144140
if not resolved_connection_id:
145141
self.logger.error(
146142
"No Azure AI Search connection resolved. "
147-
"Provided connection_id=%s, connection_name=%s",
148-
desired_connection_id,
143+
"connection_name=%s",
149144
desired_connection_name,
150145
)
151146
# return None
152147

153148
self.logger.info(
154-
"Using Azure AI Search connection (id=%s, requested_name=%s, requested_id=%s).",
149+
"Using Azure AI Search connection (id=%s, requested_name=%s).",
155150
resolved_connection_id,
156151
desired_connection_name,
157-
desired_connection_id,
158152
)
159153
except Exception as ex:
160154
self.logger.error("Failed to enumerate connections: %s", ex)
@@ -176,7 +170,7 @@ async def _create_azure_search_enabled_client(self):
176170
{
177171
"index_connection_id": resolved_connection_id,
178172
"index_name": index_name,
179-
"query_type": "simple",
173+
"query_type": query_type,
180174
}
181175
]
182176
}

src/backend/v4/magentic_agents/magentic_agent_factory.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ async def create_agent_from_config(self, user_id: str, agent_obj: SimpleNamespac
8585
)
8686

8787
# Only create configs for explicitly requested capabilities
88+
index_name = getattr(agent_obj, "index_name", None)
8889
search_config = (
89-
SearchConfig.from_env() if getattr(agent_obj, "use_rag", False) else None
90+
SearchConfig.from_env(index_name) if getattr(agent_obj, "use_rag", False) else None
9091
)
9192
mcp_config = (
9293
MCPConfig.from_env() if getattr(agent_obj, "use_mcp", False) else None
9394
)
9495
# bing_config = BingConfig.from_env() if getattr(agent_obj, 'use_bing', False) else None
9596

9697
self.logger.info(
97-
f"Creating agent '{agent_obj.name}' with model '{deployment_name}' "
98+
f"Creating agent '{agent_obj.name}' with model '{deployment_name}' {index_name} "
9899
f"(Template: {'Reasoning' if use_reasoning else 'Foundry'})"
99100
)
100101

src/backend/v4/magentic_agents/models/agent_models.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ class SearchConfig:
6161
connection_name: str | None = None
6262
endpoint: str | None = None
6363
index_name: str | None = None
64-
api_key: str | None = None # API key for Azure AI Search
64+
6565

6666
@classmethod
67-
def from_env(cls) -> "SearchConfig":
67+
def from_env(cls, index_name: str) -> "SearchConfig":
6868
connection_name = config.AZURE_AI_SEARCH_CONNECTION_NAME
69-
index_name = config.AZURE_AI_SEARCH_INDEX_NAME
69+
index_name = index_name or config.AZURE_AI_SEARCH_INDEX_NAME
7070
endpoint = config.AZURE_AI_SEARCH_ENDPOINT
71-
api_key = config.AZURE_AI_SEARCH_API_KEY
71+
7272

7373
# Raise exception if any required environment variable is missing
7474
if not all([connection_name, index_name, endpoint]):
@@ -79,6 +79,5 @@ def from_env(cls) -> "SearchConfig":
7979
return cls(
8080
connection_name=connection_name,
8181
index_name=index_name,
82-
endpoint=endpoint,
83-
api_key=api_key,
82+
endpoint=endpoint
8483
)

0 commit comments

Comments
 (0)