Skip to content

Commit 62e2ec6

Browse files
committed
2 parents 7293cd5 + f4272f4 commit 62e2ec6

File tree

85 files changed

+2425
-1593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2425
-1593
lines changed

articles/ai-foundry/agents/how-to/tools/azure-ai-search-samples.md

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
11
---
22
title: 'How to use Azure AI Search in Azure AI Foundry Agent Service'
33
titleSuffix: Azure AI Foundry
4-
description: Learn how to ground Azure AI Agents using Azure AI Search.
4+
description: Learn how to ground Azure AI Agents with content indexed in Azure AI Search.
55
services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-agent-service
88
ms.topic: how-to
9-
ms.date: 07/11/2025
10-
author: aahill
11-
ms.author: aahi
9+
ms.date: 09/11/2025
10+
author: haileytap
11+
ms.author: haileytapia
12+
ms.reviewer: aahi
1213
ms.custom: azure-ai-agents
1314
zone_pivot_groups: selection-azure-ai-search
1415
---
1516

16-
# How to use an existing Azure AI Search index with the Azure AI Search tool
17+
# How to use an existing index with the Azure AI Search tool
1718

18-
This article shows how to use an existing Azure AI Search index with the Azure AI Search tool.
19+
This article explains how to use an existing search index with the [Azure AI Search](/azure/search/search-what-is-azure-search) tool.
1920

2021
## Prerequisites
21-
Complete the [Azure AI Search tool setup](../../how-to/tools/azure-ai-search.md?pivot=overview-azure-ai-search).
22+
23+
+ Completion of the [Azure AI Search tool setup](../../how-to/tools/azure-ai-search.md?pivot=overview-azure-ai-search).
2224

2325
:::zone pivot="portal"
2426

25-
1. Go to the Azure AI Foundry portal, and navigate to the **Agents** screen for your agent, scroll down the **Setup** pane on the right to **knowledge**. Then select **Add**.
27+
## Add the Azure AI Search tool to an agent
28+
29+
1. Sign in to the [Azure AI Foundry portal](https://ai.azure.com/?cid=learnDocs) and select your project.
30+
31+
1. From the left pane, select **Agents**.
32+
33+
1. Select your agent from the list, and then select **Knowledge** > **Add**.
2634

2735
:::image type="content" source="../../media/tools/knowledge-tools.png" alt-text="A screenshot showing the available tool categories in the Azure AI Foundry portal." lightbox="../../media/tools/knowledge-tools.png":::
2836

29-
1. Select **Azure AI Search** and follow the prompts to add the tool.
37+
1. Select **Azure AI Search**.
3038

3139
:::image type="content" source="../../media/tools/knowledge-tools-list.png" alt-text="A screenshot showing the available knowledge tools in the Azure AI Foundry portal." lightbox="../../media/tools/knowledge-tools-list.png":::
3240

41+
1. Follow the prompts to add the Azure AI Search tool.
42+
3343
:::zone-end
3444

3545
:::zone pivot="python"
3646

3747
## Create an Azure AI Client
38-
First, create an Azure AI Client using the endpoint of your project.
48+
49+
First, create an Azure AI Client using the endpoint of your Azure AI Foundry project.
3950

4051
```python
4152
import os
@@ -53,7 +64,8 @@ project_client = AIProjectClient(
5364
```
5465

5566
## Configure the Azure AI Search tool
56-
Using the connection ID of your Azure AI Search resource, configure the Azure AI Search tool to use your Azure AI Search index.
67+
68+
Using the connection ID of your Azure AI Search service, configure the Azure AI Search tool to use your search index.
5769

5870
```python
5971
from azure.ai.agents.models import AzureAISearchTool, AzureAISearchQueryType
@@ -62,7 +74,7 @@ from azure.ai.projects.models import ConnectionType
6274
# Define the Azure AI Search connection ID and index name
6375
azure_ai_conn_id = project_client.connections.get_default(ConnectionType.AZURE_AI_SEARCH).id
6476

65-
# find the index name in your AI Search Azure resource page under Search Management -> Indexes
77+
# Find the index name on the Search Management > Indexes page of your Azure AI Search service
6678
index_name = "sample_index"
6779

6880
# Initialize the Azure AI Search tool
@@ -76,7 +88,8 @@ ai_search = AzureAISearchTool(
7688
```
7789

7890
## Create an agent with the Azure AI Search tool enabled
79-
Create an agent with the Azure AI Search tool attached. Change the model to the one deployed in your project.
91+
92+
Change the model to the one deployed in your project. You can find the model name on the **Models** tab of the Azure AI Foundry portal. You can also change the agent's name and instructions to suit your needs.
8093

8194
```python
8295
# Define the model deployment name
@@ -94,7 +107,8 @@ print(f"Created agent, ID: {agent.id}")
94107
```
95108

96109
## Ask the agent questions about data in the index
97-
Now that the agent is created, ask it questions about the data in your Azure AI Search index.
110+
111+
Now that the agent is created, you can ask it questions about the data in your search index.
98112

99113
```python
100114
from azure.ai.agents.models import MessageRole, ListSortOrder
@@ -126,7 +140,8 @@ for message in messages.data:
126140
```
127141

128142
## Clean up resources
129-
After completing the operations, delete the agent to clean up resources.
143+
144+
After you complete these operations, delete the agent to clean up resources.
130145

131146
```python
132147
# Delete the agent
@@ -139,7 +154,8 @@ print("Deleted agent")
139154
:::zone pivot="csharp"
140155

141156
## Create a project client
142-
Create a client object, which will contain the project endpoint for connecting to your AI project and other resources.
157+
158+
Create a client object that contains the endpoint of your Azure AI Foundry project, which enables connections to your project and other resources.
143159

144160
```csharp
145161
using Azure;
@@ -149,7 +165,7 @@ using Microsoft.Extensions.Configuration;
149165
using System;
150166
using System.Threading;
151167

152-
// Get Connection information from app configuration
168+
// Get connection information from app configuration
153169
IConfigurationRoot configuration = new ConfigurationBuilder()
154170
.SetBasePath(AppContext.BaseDirectory)
155171
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
@@ -159,12 +175,13 @@ var projectEndpoint = configuration["ProjectEndpoint"];
159175
var modelDeploymentName = configuration["ModelDeploymentName"];
160176
var azureAiSearchConnectionId = configuration["AzureAiSearchConnectionId"];
161177

162-
// Create the Agent Client
178+
// Create the agent client
163179
PersistentAgentsClient agentClient = new(projectEndpoint, new DefaultAzureCredential());
164180
```
165181

166182
## Configure the Azure AI Search tool
167-
Using the AI Search Connection ID, configure the Azure AI Search tool to use your Azure AI Search index.
183+
184+
Using the connection ID of your Azure AI Search service, configure the Azure AI Search tool to use your search index.
168185

169186
```csharp
170187
AzureAISearchToolResource searchResource = new(
@@ -180,7 +197,8 @@ ToolResources toolResource = new() { AzureAISearch = searchResource };
180197
```
181198

182199
## Create an agent with the Azure AI Search tool enabled
183-
Change the model to the one deployed in your project. You can find the model name in the Azure AI Foundry under the **Models** tab. You can also change the name and instructions of the agent to suit your needs.
200+
201+
Change the model to the one deployed in your project. You can find the model name on the **Models** tab of the Azure AI Foundry portal. You can also change the agent's name and instructions to suit your needs.
184202

185203
```csharp
186204
// Create an agent with Tools and Tool Resources
@@ -195,7 +213,8 @@ PersistentAgent agent = agentClient.Administration.CreateAgent(
195213
```
196214

197215
## Ask the agent questions about data in the index
198-
Now that the agent is created, ask it questions about the data in your Azure AI Search index.
216+
217+
Now that the agent is created, you can ask it questions about the data in your search index.
199218

200219
```csharp
201220
// Create thread for communication
@@ -212,7 +231,7 @@ ThreadRun run = agentClient.Runs.CreateRun(thread, agent);
212231

213232
## Wait for the agent to complete and print the output
214233

215-
Wait for the agent to complete the run and print output to console.
234+
Wait for the agent to finish running and print the output to the console.
216235

217236
```csharp
218237
// Wait for the agent to finish running
@@ -244,12 +263,12 @@ foreach (PersistentThreadMessage threadMessage in messages)
244263
{
245264
if (contentItem is MessageTextContent textItem)
246265
{
247-
// We need to annotate only Agent messages.
266+
// Annotate only agent messages
248267
if (threadMessage.Role == MessageRole.Agent && textItem.Annotations.Count > 0)
249268
{
250269
string annotatedText = textItem.Text;
251270

252-
// If we have Text URL citation annotations, reformat the response to show title & URL for citations
271+
// If there are text URL citation annotations, reformat the response to show the title and URL for citations
253272
foreach (MessageTextAnnotation annotation in textItem.Annotations)
254273
{
255274
if (annotation is MessageTextUriCitationAnnotation urlAnnotation)
@@ -278,7 +297,7 @@ foreach (PersistentThreadMessage threadMessage in messages)
278297
## Optionally output the run steps used by the agent
279298

280299
```csharp
281-
// Retrieve the run steps used by the agent and print those to the console
300+
// Retrieve the run steps used by the agent and print them to the console
282301
Console.WriteLine("Run Steps used by Agent:");
283302
Pageable<RunStep> runSteps = agentClient.Runs.GetRunSteps(run);
284303

@@ -292,7 +311,7 @@ foreach (var step in runSteps)
292311
}
293312
else if (step.StepDetails is RunStepToolCallDetails toolCallDetails)
294313
{
295-
// We know this agent only has the AI Search tool, so we can cast it directly
314+
// This agent only has the Azure AI Search tool, so we can cast it directly
296315
foreach (RunStepAzureAISearchToolCall toolCall in toolCallDetails.ToolCalls)
297316
{
298317
Console.WriteLine($" Tool Call Details: {toolCall.GetType()}");
@@ -308,7 +327,7 @@ foreach (var step in runSteps)
308327
```
309328
## Clean up resources
310329

311-
Clean up the resources from this sample.
330+
Delete the resources from this sample.
312331

313332
```csharp
314333
// Clean up resources
@@ -322,7 +341,8 @@ agentClient.Administration.DeleteAgent(agent.Id);
322341
:::zone pivot="javascript"
323342

324343
## Create an Azure AI Client
325-
First, create an Azure AI Client using the endpoint your project.
344+
345+
First, create an Azure AI Client using the endpoint of your Azure AI Foundry project.
326346

327347
```javascript
328348
const projectEndpoint = process.env["PROJECT_ENDPOINT"];
@@ -335,7 +355,8 @@ const client = new AgentsClient(projectEndpoint, new DefaultAzureCredential());
335355
```
336356

337357
## Configure the Azure AI Search tool
338-
Using the connection ID of the Azure AI Search resource, configure the Azure AI Search tool to use your Azure AI Search index.
358+
359+
Using the connection ID of your Azure AI Search service, configure the Azure AI Search tool to use your search index.
339360

340361
```javascript
341362
const connectionId = process.env["AZURE_AI_CONNECTION_ID"] || "<connection-name>";
@@ -352,7 +373,7 @@ const azureAISearchTool = ToolUtility.createAzureAISearchTool(connectionId, "ai-
352373

353374
## Create an agent with the Azure AI Search tool enabled
354375

355-
Change the model to the one deployed in your project. You can find the model name in the Azure AI Foundry under the **Models** tab. You can also change the name and instructions of the agent to suit your needs.
376+
Change the model to the one deployed in your project. You can find the model name on the **Models** tab of the Azure AI Foundry portal. You can also change the agent's name and instructions to suit your needs.
356377

357378
```javascript
358379

@@ -367,7 +388,8 @@ console.log(`Created agent, agent ID : ${agent.id}`);
367388

368389

369390
## Ask the agent questions about data in the index
370-
Now that the agent is created, ask it questions about the data in your Azure AI Search index.
391+
392+
Now that the agent is created, you can ask it questions about the data in your search index.
371393

372394
```javascript
373395
// Create thread for communication
@@ -382,7 +404,7 @@ const message = await client.messages.create(
382404
);
383405
console.log(`Created message, message ID : ${message.id}`);
384406

385-
// Create and process agent run in thread with tools
407+
// Create and process the agent run in thread with tools
386408
let run = await client.runs.create(thread.id, agent.id);
387409
while (run.status === "queued" || run.status === "in_progress") {
388410
await delay(1000);
@@ -415,6 +437,7 @@ for await (const step of runSteps) {
415437
}
416438
}
417439
}
440+
418441
// Delete the assistant when done
419442
await client.deleteAgent(agent.id);
420443
console.log(`Deleted agent, agent ID: ${agent.id}`);
@@ -440,20 +463,27 @@ for await (const m of messagesIterator) {
440463

441464
:::zone pivot="rest"
442465

443-
Follow the [REST API Quickstart](../../quickstart.md?pivots=rest-api#api-call-information) to set the right values for the environment variables `AGENT_TOKEN`, `AZURE_AI_FOUNDRY_PROJECT_ENDPOINT` and `API_VERSION`.
466+
+ Completion of the [REST API quickstart](../../quickstart.md?pivots=rest-api#api-call-information) to get values for the `AGENT_TOKEN`, `AZURE_AI_FOUNDRY_PROJECT_ENDPOINT`, and `API_VERSION` environment variables.
444467

445468
## Get the connection ID for the Azure AI Search resource
446469

447-
To get the connection ID, navigate to the project in the Azure AI Foundry and click on the **Connected resources** tab and then select your Azure AI Search resource.
470+
To get the connection ID:
448471

449-
:::image type="content" source="../../media/tools/ai-search/success-connection.png" alt-text="A screenshot of an AI Search resource connection page in Azure AI Foundry." lightbox="../../media/tools/ai-search/success-connection.png":::
472+
1. Sign in to the [Azure AI Foundry portal](https://ai.azure.com/?cid=learnDocs) and select your project.
450473

451-
In the URL, you see the `wsid=/subscription/your-subscription-id...`, this is the connection ID you need to use. Copy everything that comes after `wsid=`.
474+
1. On the **Overview** page, select **Open in management center**.
452475

453-
:::image type="content" source="../../media/tools/ai-search/connection-id.png" alt-text="A screenshot of an AI Search resource connection and how to copy the connection ID." lightbox="../../media/tools/ai-search/connection-id.png":::
476+
1. From the left pane, select **Connected resources**, and then select your Azure AI Search service.
477+
478+
:::image type="content" source="../../media/tools/ai-search/success-connection.png" alt-text="A screenshot of an AI Search resource connection page in Azure AI Foundry." lightbox="../../media/tools/ai-search/success-connection.png":::
479+
480+
1. Copy everything that comes after `wsid=` in the browser URL.
481+
482+
:::image type="content" source="../../media/tools/ai-search/connection-id.png" alt-text="A screenshot of an AI Search resource connection and how to copy the connection ID." lightbox="../../media/tools/ai-search/connection-id.png":::
454483

455484
## Configure the Azure AI Search tool
456-
Using the connection ID you got in the previous step, you can now configure the Azure AI Search tool to use your Azure AI Search index.
485+
486+
Using the connection ID you got in the previous step, configure the Azure AI Search tool to use your search index.
457487

458488
```bash
459489
curl --request POST \
@@ -482,7 +512,8 @@ curl --request POST \
482512
```
483513

484514
### Ask the agent questions about data in the index
485-
Now that the agent is created, ask it questions about the data in your Azure AI Search index.
515+
516+
Now that the agent is created, you can ask it questions about the data in your search index.
486517

487518
#### Create a thread
488519

0 commit comments

Comments
 (0)