Skip to content

Commit 58bf6c6

Browse files
author
Jill Grant
authored
Merge pull request #1921 from aahill/fosteramanda-article
Azure search article
2 parents 4326d10 + 918e04b commit 58bf6c6

20 files changed

+392
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: 'How to use an existing AI Search index with the Azure AI Search tool'
3+
titleSuffix: Azure OpenAI
4+
description: Learn how to use Agents Azure AI Search tool.
5+
services: azure-ai-agent-service
6+
manager: nitinme
7+
ms.service: azure
8+
ms.topic: how-to
9+
ms.date: 11/20/2024
10+
author: fosteramanda
11+
ms.author: fosteramanda
12+
recommendations: false
13+
zone_pivot_groups: selection-azure-ai-search
14+
---
15+
16+
# Use an existing AI Search index with the Azure AI Search tool
17+
::: zone pivot="overview"
18+
19+
Use an existing Azure AI Search index with the agent's Azure AI Search tool.
20+
21+
> [!NOTE]
22+
> Azure AI Search indexes must meet the following requirements:
23+
> - The index must contain at least one searchable & retrievable text field (type Edm.String)
24+
> - The index must contain at least one searchable vector field (type Collection(Edm.Single))
25+
> - The index must use a vector profile/integrated vectorization
26+
27+
## Search types
28+
29+
**Index without semantic configuration**
30+
- By default, the Azure AI Search tool runs a hybrid search (keyword + vector) on all text fields.
31+
<br>
32+
33+
**Index with semantic configuration**
34+
- By default, the Azure AI Search tool runs hybrid + semantic search on all text fields.
35+
36+
[!INCLUDE [setup](../../includes/azure-search/setup.md)]
37+
38+
::: zone-end
39+
40+
::: zone pivot="code-examples"
41+
42+
[!INCLUDE [code-examples](../../includes/azure-search/code-examples.md)]
43+
44+
::: zone-end
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
## Quickstart – Use an existing Azure AI Search index with the Azure AI Search tool
2+
3+
This quickstart shows how to use an existing Azure AI Search index with the Azure AI Search tool.
4+
5+
### Prerequisites
6+
Complete the [Azure AI Search tool setup](?pivot=setup).
7+
8+
### Step 1: Create an Azure AI Client
9+
First, create an Azure AI Client using the connection string of your project.
10+
# [Python](#tab/python)
11+
```python
12+
import os
13+
from azure.ai.projects import AIProjectClient
14+
from azure.identity import DefaultAzureCredential
15+
from azure.ai.projects.models import AzureAISearchTool
16+
17+
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
18+
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
19+
# HostName can be found by navigating to your discovery_url and removing the leading "https://" and trailing "/discovery"
20+
# To find your discovery_url, run the CLI command: az ml workspace show -n {project_name} --resource-group {resource_group_name} --query discovery_url
21+
# Project Connection example: eastus.api.azureml.ms;my-subscription-id;my-resource-group;my-hub-name
22+
23+
connection_string = os.environ["PROJECT_CONNECTION_STRING"]
24+
25+
project_client = AIProjectClient.from_connection_string(
26+
credential=DefaultAzureCredential(),
27+
conn_str=connection_string,
28+
)
29+
```
30+
31+
# [C#](#tab/csharp)
32+
```csharp
33+
using System;
34+
using System.Threading.Tasks;
35+
using Azure.Core;
36+
using Azure.Core.TestFramework;
37+
using NUnit.Framework;
38+
using System.Collections.Generic;
39+
40+
// Create an Azure AI Client from a connection string, copied from your AI Studio project.
41+
// At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
42+
// Customer needs to login to Azure subscription via Azure CLI and set the environment variables
43+
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
44+
var clientOptions = new AIProjectClientOptions();
45+
46+
// Adding the custom headers policy
47+
clientOptions.AddPolicy(new CustomHeadersPolicy(), HttpPipelinePosition.PerCall);
48+
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential(), clientOptions);
49+
```
50+
---
51+
### Step 2: Get the connection ID for the Azure AI Search resource
52+
53+
Get the connection ID of the Azure AI Search connection in the project. You can use the code snippet to print the connection ID of all the Azure AI Search connections in the project.
54+
55+
# [Python](#tab/python)
56+
57+
```python
58+
# AI Search resource connection ID
59+
# This code prints out the connection ID of all the Azure AI Search connections in the project
60+
# If you have more than one AI search connection, make sure to select the correct one that contains the index you want to use.
61+
conn_list = project_client.connections.list()
62+
conn_id = ""
63+
for conn in conn_list:
64+
if conn.connection_type == "CognitiveSearch":
65+
print(f"Connection ID: {conn.id}")
66+
```
67+
# [C#](#tab/csharp)
68+
```csharp
69+
ListConnectionsResponse connections = await projectClient.GetConnectionsClient().GetConnectionsAsync(ConnectionType.AzureAISearch).ConfigureAwait(false);
70+
71+
if (connections?.Value == null || connections.Value.Count == 0)
72+
{
73+
throw new InvalidOperationException("No connections found for the Azure AI Search.");
74+
}
75+
76+
```
77+
---
78+
The second way to get the connection ID is to navigate to the project in the Azure AI Foundry and click on the **Connected resources** tab and then select your Azure AI Search resource.
79+
:::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":::
80+
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=.
81+
:::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":::
82+
83+
### Step 3: Configure the Azure AI Search tool
84+
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.
85+
# [Python](#tab/python)
86+
```python
87+
# TO DO: replace this value with the connection ID of the search index
88+
conn_id = "/subscriptions/<your-subscription-id>/resourceGroups/<your-resource-group>/providers/Microsoft.MachineLearningServices/workspaces/<your-project-name>/connections/<your-azure-ai-search-connection-name>"
89+
90+
# Initialize agent AI search tool and add the search index connection ID and index name
91+
# TO DO: replace <your-index-name> with the name of the index you want to use
92+
ai_search = AzureAISearchTool()
93+
ai_search.add_index(conn_id, "<your-index-name>")
94+
```
95+
# [C#](#tab/csharp)
96+
```csharp
97+
// TO DO: replace this value with the connection ID of the search index
98+
ConnectionResponse connection = connections.Value[0];
99+
100+
// Initialize agent Azure AI search tool and add the search index connection ID and index name
101+
// TO DO: replace <your-index-name> with the name of the index you want to use
102+
ToolResources searchResource = new ToolResources
103+
{
104+
AzureAISearch = new AzureAISearchResource
105+
{
106+
IndexList = { new IndexResource(connection.Id, "<your-index-name>") }
107+
}
108+
};
109+
```
110+
---
111+
112+
### Step 4: Create an agent with the Azure AI Search tool enabled
113+
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.
114+
# [Python](#tab/python)
115+
```python
116+
agent = project_client.agents.create_agent(
117+
model="gpt-4o-mini",
118+
name="my-assistant",
119+
instructions="You are a helpful assistant",
120+
tools=ai_search.definitions,
121+
tool_resources = ai_search.resources,
122+
headers={"x-ms-enable-preview": "true"},
123+
)
124+
print(f"Created agent, ID: {agent.id}")
125+
```
126+
# [C#](#tab/csharp)
127+
```csharp
128+
AgentsClient agentClient = projectClient.GetAgentsClient();
129+
130+
Response<Agent> agentResponse = await agentClient.CreateAgentAsync(
131+
model: "gpt-4o-mini",
132+
name: "my-assistant",
133+
instructions: "You are a helpful assistant.",
134+
tools: new List<ToolDefinition> { new AzureAISearchToolDefinition() },
135+
toolResources: searchResource);
136+
Agent agent = agentResponse.Value;
137+
```
138+
---
139+
140+
### Step 5: Ask the agent questions about data in the index
141+
Now that the agent is created, ask it questions about the data in your Azure AI Search index. The example assumes your Azure AI Search index contains information about health care plans.
142+
# [Python](#tab/python)
143+
```python
144+
# Create a thread
145+
thread = project_client.agents.create_thread()
146+
print(f"Created thread, thread ID: {thread.id}")
147+
148+
# Create a message
149+
message = project_client.agents.create_message(
150+
thread_id=thread.id,
151+
role="user",
152+
content="what are my health insurance plan coverage types?",
153+
)
154+
print(f"Created message, message ID: {message.id}")
155+
156+
# Run the agent
157+
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
158+
print(f"Run finished with status: {run.status}")
159+
160+
if run.status == "failed":
161+
# Check if you got "Rate limit is exceeded.", then you want to get more quota
162+
print(f"Run failed: {run.last_error}")
163+
164+
# Get messages from the thread
165+
messages = project_client.agents.list_messages(thread_id=thread.id)
166+
print(f"Messages: {messages}")
167+
168+
assistant_message = ""
169+
for message in messages.data:
170+
if message["role"] == "assistant":
171+
assistant_message = message["content"][0]["text"]["value"]
172+
173+
# Get the last message from the sender
174+
print(f"Assistant response: {assistant_message}")
175+
```
176+
# [C#](#tab/csharp)
177+
```csharp
178+
179+
// Create thread for communication
180+
Response<AgentThread> threadResponse = await agentClient.CreateThreadAsync();
181+
AgentThread thread = threadResponse.Value;
182+
183+
// Create message to thread
184+
Response<ThreadMessage> messageResponse = await agentClient.CreateMessageAsync(
185+
thread.Id,
186+
MessageRole.User,
187+
"Hello, send an email with the datetime and weather information in New York?");
188+
ThreadMessage message = messageResponse.Value;
189+
190+
// Run the agent
191+
Response<ThreadRun> runResponse = await agentClient.CreateRunAsync(thread, agent);
192+
193+
do
194+
{
195+
await Task.Delay(TimeSpan.FromMilliseconds(500));
196+
runResponse = await agentClient.GetRunAsync(thread.Id, runResponse.Value.Id);
197+
}
198+
while (runResponse.Value.Status == RunStatus.Queued
199+
|| runResponse.Value.Status == RunStatus.InProgress);
200+
201+
Response<PageableList<ThreadMessage>> afterRunMessagesResponse
202+
= await agentClient.GetMessagesAsync(thread.Id);
203+
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
204+
205+
// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
206+
foreach (ThreadMessage threadMessage in messages)
207+
{
208+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
209+
foreach (MessageContent contentItem in threadMessage.ContentItems)
210+
{
211+
if (contentItem is MessageTextContent textItem)
212+
{
213+
Console.Write(textItem.Text);
214+
}
215+
else if (contentItem is MessageImageFileContent imageFileItem)
216+
{
217+
Console.Write($"<image from ID: {imageFileItem.FileId}");
218+
}
219+
Console.WriteLine();
220+
}
221+
}
222+
```
223+
---
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
manager: nitinme
3+
author: fosteramanda
4+
ms.author: fosteramanda
5+
ms.service: azure
6+
ms.topic: include
7+
ms.date: 12/10/2024
8+
---
9+
10+
## Setup: Create an agent that can use an existing Azure AI Search index
11+
12+
#### Prerequisite: Have an existing Azure AI Search index
13+
A prerequisite of using the Azure AI Search tool is to have an existing Azure AI Search index. If you don't have an existing index, you can create one in the Azure portal or via REST API.
14+
- [Quickstart: Create a vector index using the Azure portal](../../../../search/search-get-started-portal-import-vectors.md)
15+
- [Quickstart: Create a vector index using REST API](../../../../search/search-get-started-vector.md)
16+
17+
18+
#### Complete the agent setup
19+
- **Option 1: Standard Agent Setup using an existing AI Search resource** If you want your agent to use an existing AI Search resource to create new indexes or bring existing ones you should use the [standard agent setup and add your AI Search resource ID](../../quickstart.md).
20+
- You can provide your Azure AI Search resource ID in the bicep file. Your resource ID should be in the format: `/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}`.
21+
- **Option 2: Standard Agent Setup** If you want to create a new Azure AI Search resource for your agents to use when creating new indexes follow the [standard agent setup](../../quickstart.md).
22+
23+
24+
#### Create a project connection to the Azure AI Search resource with the index you want to use
25+
If you already connected the AI Search resource that contains the index you want to use to your project, skip this step.
26+
27+
##### Get your Azure AI Search resource connection key and endpoint
28+
1. Access your Azure AI Search resource.
29+
- In the Azure portal, navigate to the AI Search resource that contains the index you want to use.
30+
2. Copy the connection endpoint.
31+
- In the Overview tab, copy the URL of your resource. The URL should be in the format `https://<your-resource-name>.search.windows.net/`.
32+
:::image type="content" source="../../media/tools/ai-search/connection-endpoint.png" alt-text="A screenshot of an AI Search resource Overview tab in the Azure portal." lightbox="../../media/tools/ai-search/connection-endpoint.png":::
33+
34+
3. Verify API Access control is set to **Both** and copy one of the keys under **Manage admin keys**.
35+
- From the left-hand navigation bar, scroll down to the Settings section and select **Keys**.
36+
- Under the **API Access Control** section, ensure the option **Both** API key and Role-based access control is selected.
37+
- If you want the connection to use API Keys for authentication, copy one of the keys under **Manage admin keys**.
38+
:::image type="content" source="../../media/tools/ai-search/azure-portal.png" alt-text="A screenshot of an AI Search resource Keys tab in the Azure portal." lightbox="../../media/tools/ai-search/azure-portal.png":::
39+
40+
##### Create an Azure AI Search project connection
41+
42+
# [Azure CLI](#tab/azurecli)
43+
**Create the following connections.yml file**
44+
45+
46+
You can use either an API key or credential-less YAML configuration file. For more information on the YAML configuration file, see the [Azure AI Search connection YAML schema](../../../../machine-learning/reference-yaml-connection-ai-search.md):
47+
- API Key example:
48+
49+
```yml
50+
name: myazaics_apk
51+
type: azure_ai_search
52+
endpoint: https://contoso.search.windows.net/
53+
api_key: XXXXXXXXXXXXXXX
54+
```
55+
56+
- Credential-less
57+
58+
```yml
59+
name: myazaics_ei
60+
type: azure_ai_search
61+
endpoint: https://contoso.search.windows.net/
62+
```
63+
**Then, run the following command:**
64+
65+
Replace ```my_resource``` and ```my_project_name``` with your resource group and project name created in the agent setup.
66+
```azurecli
67+
az ml connection create --file {connection.yml} --resource-group {my_resource_group} --workspace-name {my_project_name}
68+
```
69+
# [Python](#tab/pythonsdk)
70+
Replace the placeholders ```my_connection_name```, ```my_endpoint```, and ```my_key``` (optional) with your Azure AI Search connection details and run the following code to create a project connection to your Azure AI Search resource.
71+
```python
72+
from azure.ai.ml.entities import AzureAISearchConnection
73+
74+
# create an Azure AI Search project connection
75+
my_connection_name = "my-connection-name"
76+
my_endpoint = "my-endpoint" # this could also be called target
77+
my_api_keys = None # leave blank for Authentication type = AAD
78+
79+
my_connection = AzureAISearchConnection(name=my_connection_name,
80+
endpoint=my_endpoint,
81+
api_key= my_api_keys)
82+
83+
# Create the connection
84+
ml_client.connections.create_or_update(my_connection)
85+
```
86+
87+
# [Azure AI Foundry](#tab/azureaifoundry)
88+
1. In Azure AI Foundry, navigate to the project you created in the agent setup. Click on **Open in management center**.
89+
:::image type="content" source="../../media/tools/ai-search/project-studio.png" alt-text="A screenshot of a project in Azure AI Foundry." lightbox="../../media/tools/ai-search/project-studio.png":::
90+
91+
2. Click on the **Connections** tab and select **Add Connection**.
92+
:::image type="content" source="../../media/tools/ai-search/project-connections-page.png" alt-text="A screenshot of the project connections page." lightbox="../../media/tools/ai-search/project-connections-page.png":::
93+
94+
3. Select **Azure AI Search**.
95+
:::image type="content" source="../../media/tools/ai-search/select.png" alt-text="A screenshot of the Azure AI Search connection type the user should select." lightbox="../../media/tools/ai-search/select.png":::
96+
97+
4. Provide the required connection details for the Azure AI Search resource you want to use. Both Managed Identity and Key-based authentication are supported. Once all the fields are filled in, click **Add connection**.
98+
:::image type="content" source="../../media/tools/ai-search/connection-2.png" alt-text="A screenshot the required fields to add a new Azure AI Search connection." lightbox="../../media/tools/ai-search/connection-2.png":::
99+
100+
5. Verify that the connection was successfully created and now appears in the project's Connections tab.
101+
:::image type="content" source="../../media/tools/ai-search/success-connection.png" alt-text="A screenshot of the project connections page with a new Azure AI Search connection added." lightbox="../../media/tools/ai-search/success-connection.png":::
102+
---
103+

0 commit comments

Comments
 (0)