Skip to content

Commit 4b9c4ed

Browse files
committed
Updated agentic retrieval pipeline doc per recent sample code updates
1 parent c71252a commit 4b9c4ed

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

articles/search/search-agentic-retrieval-how-to-pipeline.md

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Build an agentic retrieval solution
33
titleSuffix: Azure AI Search
4-
description: Learn how to design and build a custom agentic retrieval solution where Azure AI Search handles data retrieval for your custom agents.
4+
description: Learn how to design and build a custom agentic retrieval solution where Azure AI Search handles data retrieval for your custom agents in AI Foundry.
55
author: HeidiSteen
66
ms.author: heidist
77
manager: nitinme
8-
ms.date: 08/29/2025
8+
ms.date: 09/10/2025
99
ms.service: azure-ai-search
1010
ms.topic: how-to
1111
ms.custom:
@@ -53,9 +53,9 @@ Use one of the following chat completion models with your AI agent:
5353
Use a package version that provides preview functionality. See the [`requirements.txt`](https://github.com/Azure-Samples/azure-search-python-samples/blob/main/agentic-retrieval-pipeline-example/requirements.txt) file for more packages used in the example solution.
5454

5555
```
56-
azure-ai-projects==1.0.0b11
57-
azure-ai-agents==1.0.0
58-
azure-search-documents==11.6.0b12
56+
azure-ai-projects==1.1.0b3
57+
azure-ai-agents==1.2.0b3
58+
azure-search-documents==11.7.0b1
5959
```
6060

6161
### Configure access
@@ -100,9 +100,16 @@ Azure OpenAI hosts the models used by the agentic retrieval pipeline. Configure
100100

101101
Development tasks on the Azure AI Search side include:
102102

103-
+ Create a knowledge agent on Azure AI Search that maps to your deployed model in Azure AI Foundry Model.
104-
+ Call the retriever and provide a query, conversation, and override parameters.
105-
+ Parse the response for the parts you want to include in your chat application. For many scenarios, just the content portion of the response is sufficient.
103+
+ [Create a knowledge source](search-knowledge-source-overview.md) that maps to a [searchable index](search-agentic-retrieval-how-to-index.md).
104+
+ [Create a knowledge agent](search-agentic-retrieval-how-to-create.md) on Azure AI Search that maps to your deployed model in Azure AI Foundry Model.
105+
+ [Call the retriever](search-agentic-retrieval-how-to-retrieve.md) and provide a query, conversation, and override parameters.
106+
+ Parse the response for the parts you want to include in your chat application. For many scenarios, just the content portion of the response is sufficient. You can also try [answer synthesis](search-agentic-retrieval-how-to-synthesize.md) for a simpler workflow.
107+
108+
Developments in Azure AI Agent side include:
109+
110+
+ Set up the AI project client and an AI agent.
111+
+ Add a tool to coordinate calls from the AI agent to the retriever and knowledge agent.
112+
+ Query processing is driven by the tool, where the tool calls both the AI agent and the retriever on Azure AI Search.
106113

107114
## Components of the solution
108115

@@ -169,20 +176,20 @@ print(f"AI agent '{agent_name}' created or updated successfully")
169176

170177
### Add an agentic retrieval tool to AI Agent
171178

172-
An end-to-end pipeline needs an orchestration mechanism for coordinating calls to the retriever and knowledge agent. You can use a [tool](/azure/ai-services/agents/how-to/tools/function-calling) for this task. The tool calls the Azure AI Search knowledge retrieval client and the Azure AI agent, and it drives the conversations with the user.
179+
An end-to-end pipeline needs an orchestration mechanism for coordinating calls to the retriever and knowledge agent on Azure AI Search. You can use a [tool](/azure/ai-services/agents/how-to/tools/function-calling) for this task. The tool calls the Azure AI Search knowledge retrieval client and the Azure AI agent, and it drives the conversations with the user.
173180

174181
```python
175182
from azure.ai.agents.models import FunctionTool, ToolSet, ListSortOrder
176183

177184
from azure.search.documents.agent import KnowledgeAgentRetrievalClient
178-
from azure.search.documents.agent.models import KnowledgeAgentRetrievalRequest, KnowledgeAgentMessage, KnowledgeAgentMessageTextContent, KnowledgeAgentIndexParams
185+
from azure.search.documents.agent.models import KnowledgeAgentRetrievalRequest, KnowledgeAgentMessage, KnowledgeAgentMessageTextContent
179186

180187
agent_client = KnowledgeAgentRetrievalClient(endpoint=endpoint, agent_name=agent_name, credential=credential)
181188

182189
thread = project_client.agents.threads.create()
183190
retrieval_results = {}
184191

185-
# AGENTIC RETRIEVAL DEFINITION DEFERRED TO NEXT SECTION
192+
# AGENTIC RETRIEVAL DEFINITION "LIFTED AND SHIFTED" TO NEXT SECTION
186193

187194
functions = FunctionTool({ agentic_retrieval })
188195
toolset = ToolSet()
@@ -194,32 +201,68 @@ project_client.agents.enable_auto_function_calls(toolset)
194201

195202
The messages sent to the agent tool include instructions for chat history and using the results obtained from [knowledge retrieval](/rest/api/searchservice/knowledge-retrieval/retrieve?view=rest-searchservice-2025-08-01-preview&preserve-view=true) on Azure AI Search. The response is passed as a large single string with no serialization or structure.
196203

204+
This code snippet is the agentic retrieval definition mentioned in the previous code snippet.
205+
197206
```python
198207
def agentic_retrieval() -> str:
199208
"""
200209
Searches a NASA e-book about images of Earth at night and other science related facts.
201210
The returned string is in a JSON format that contains the reference id.
202211
Be sure to use the same format in your agent's response
212+
You must refer to references by id number
203213
"""
204214
# Take the last 5 messages in the conversation
205215
messages = project_client.agents.messages.list(thread.id, limit=5, order=ListSortOrder.DESCENDING)
206216
# Reverse the order so the most recent message is last
207-
messages.data.reverse()
208-
retrieval_result = retrieval_result = agent_client.retrieve(
217+
messages = list(messages)
218+
messages.reverse()
219+
retrieval_result = agent_client.retrieve(
209220
retrieval_request=KnowledgeAgentRetrievalRequest(
210-
messages=[KnowledgeAgentMessage(role=msg["role"], content=[KnowledgeAgentMessageTextContent(text=msg.content[0].text)]) for msg in messages.data],
211-
target_index_params=[KnowledgeAgentIndexParams(index_name=index_name, reranker_threshold=2.5)]
221+
messages=[
222+
KnowledgeAgentMessage(
223+
role=m["role"],
224+
content=[KnowledgeAgentMessageTextContent(text=m["content"])]
225+
) for m in messages if m["role"] != "system"
226+
]
212227
)
213228
)
214229

215230
# Associate the retrieval results with the last message in the conversation
216-
last_message = messages.data[-1]
231+
last_message = messages[-1]
217232
retrieval_results[last_message.id] = retrieval_result
218233

219234
# Return the grounding response to the agent
220235
return retrieval_result.response[0].content[0].text
221236
```
222237

238+
## How to start the conversation
239+
240+
To start the chat, use the standard Azure AI agent tool calling APIs. Send the message with questions, and the agent decides when to retrieve knowledge from your search index using agentic retrieval.
241+
242+
```python
243+
from azure.ai.agents.models import AgentsNamedToolChoice, AgentsNamedToolChoiceType, FunctionName
244+
245+
message = project_client.agents.messages.create(
246+
thread_id=thread.id,
247+
role="user",
248+
content="""
249+
Why do suburban belts display larger December brightening than urban cores even though absolute light levels are higher downtown?
250+
Why is the Phoenix nighttime street grid is so sharply visible from space, whereas large stretches of the interstate between midwestern cities remain comparatively dim?
251+
"""
252+
)
253+
254+
run = project_client.agents.runs.create_and_process(
255+
thread_id=thread.id,
256+
agent_id=agent.id,
257+
tool_choice=AgentsNamedToolChoice(type=AgentsNamedToolChoiceType.FUNCTION, function=FunctionName(name="agentic_retrieval")),
258+
toolset=toolset)
259+
if run.status == "failed":
260+
raise RuntimeError(f"Run failed: {run.last_error}")
261+
output = project_client.agents.messages.get_last_message_text_by_role(thread_id=thread.id, role="assistant").text.value
262+
263+
print("Agent response:", output.replace(".", "\n"))
264+
```
265+
223266
## How to improve data quality
224267

225268
Search results are consolidated into a large unified string that you can pass to a chat completion model for a grounded answer. The following indexing and relevance tuning features in Azure AI Search are available to help you generate high quality results. You can implement these features in the search index, and the improvements in search relevance are evident in the quality of the response returned during retrieval.
@@ -240,11 +283,17 @@ The LLM determines the quantity of subqueries based on these factors:
240283
+ Chat history
241284
+ Semantic ranker input constraints
242285

243-
As the developer, the best way to control the number of subqueries is by setting the `defaultMaxDocsForReranker` in either the knowledge agent definition or as an override on the retrieve action.
286+
As the developer, the best way to control the number of subqueries is by setting the [maxSubQueries](/rest/api/searchservice/knowledge-agents/create-or-update?view=rest-searchservice-2025-08-01-preview#knowledgesourcereference&preserve-view=true) property in a knowledge agent.
287+
288+
The semantic ranker processes up to 50 documents as an input, and the system creates subqueries to accommodate all of the inputs to semantic ranker. For example, if you only wanted two subqueries, you could set `maxSubQueries` to 100 to accommodate all documents in two batches.
289+
290+
The [semantic configuration](semantic-how-to-configure.md) in the index determines whether the input is 50 or not. If the value is less, the query plan specifies however many subqueries are necessary to meet the smaller input size.
291+
292+
<!-- As the developer, the best way to control the number of subqueries is by setting the `defaultMaxDocsForReranker` in either the knowledge agent definition or as an override on the retrieve action.
244293
245294
The semantic ranker processes up to 50 documents as an input, and the system creates subqueries to accommodate all of the inputs to semantic ranker. For example, if you only wanted two subqueries, you could set `defaultMaxDocsForReranker` to 100 to accommodate all documents in two batches.
246295
247-
The [semantic configuration](semantic-how-to-configure.md) in the index determines whether the input is 50 or not. If the value is less, the query plan specifies however many subqueries are necessary to meet the `defaultMaxDocsForReranker` threshold.
296+
The [semantic configuration](semantic-how-to-configure.md) in the index determines whether the input is 50 or not. If the value is less, the query plan specifies however many subqueries are necessary to meet the `defaultMaxDocsForReranker` threshold. -->
248297

249298
## Control the number of threads in chat history
250299

0 commit comments

Comments
 (0)