You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/search/search-get-started-rag.md
+59-21Lines changed: 59 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ ms.service: azure-ai-search
8
8
ms.custom:
9
9
- ignite-2024
10
10
ms.topic: quickstart
11
-
ms.date: 01/07/2025
11
+
ms.date: 01/09/2025
12
12
---
13
13
14
14
# Quickstart: Generative search (RAG) with grounding data from Azure AI Search
@@ -17,13 +17,26 @@ This quickstart shows you how to send queries to a chat completion model for a c
17
17
18
18
## Prerequisites
19
19
20
-
-An Azure subscription. [Create one for free](https://azure.microsoft.com/free/).
20
+
-[Visual Studio Code](https://code.visualstudio.com/download) with the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) and the [Jupyter package](https://pypi.org/project/jupyter/). For more information, see [Python in Visual Studio Code](https://code.visualstudio.com/docs/languages/python).
21
21
22
-
-[Azure AI Search](search-create-service-portal.md), Basic tier or higher so that you can [enable semantic ranker](semantic-how-to-enable-disable.md). Region must be the same one used for Azure OpenAI.
22
+
-An Azure subscription with permissions to assign roles. [Create one for free](https://azure.microsoft.com/free/).
23
23
24
-
-[Azure OpenAI](https://aka.ms/oai/access) resource with a deployment of `gpt-4o`, `gpt-4o-mini`, or equivalent chat completion model, in the same region as Azure AI Search.
-[Visual Studio Code](https://code.visualstudio.com/download) with the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) and the [Jupyter package](https://pypi.org/project/jupyter/). For more information, see [Python in Visual Studio Code](https://code.visualstudio.com/docs/languages/python).
26
+
-[Choose a region](https://learn.microsoft.com/azure/ai-services/openai/concepts/models?tabs=global-standard%2Cstandard-chat-completions#global-standard-model-availability) that supports the chat completion model you want to use (gpt-4o, gpt-4o-mini, or equivalent model).
27
+
-[Deploy the chat completion model](https://learn.microsoft.com/azure/ai-studio/how-to/deploy-models-openai) in Azure AI Foundry or [use another approach](https://learn.microsoft.com/azure/ai-services/openai/how-to/working-with-models?tabs=powershell).
28
+
29
+
-[Azure AI Search](https://learn.microsoft.com/azure/search/search-create-service-portal)
To meet the same-region requirement, start by reviewing the [regions for the chat model](https://learn.microsoft.com/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability) you want to use. Once you identify a region, confirm that Azure AI Search is available in the [same region](https://learn.microsoft.com/azure/search/search-region-support#azure-public-regions).
38
+
39
+
Make sure you know the name of the deployed model, and have the endpoints for both Azure resources at hand. You'll provide this information in the steps that follow.
27
40
28
41
## Download file
29
42
@@ -37,20 +50,12 @@ Requests to the search endpoint must be authenticated and authorized. You can us
37
50
38
51
You're setting up two clients, so you need permissions on both resources.
39
52
40
-
Azure AI Search is receiving the query request from your local system. Assign yourself the **Search Index Data Reader** role assignment for that task. If you're also creating and loading the hotel sample index, add **Search Service Contributor** and **Search Index Data Contributor** roles as well.
53
+
Azure AI Search is receiving the query request from your local system. Assign yourself the **Search Index Data Reader** role assignment if the hotels sample index already exists. If it doesn't exist, assign yourself **Search Service Contributor** and **Search Index Data Contributor** roles so that you can create and query the index.
41
54
42
-
Azure OpenAI is receiving the (query) "Can you recommend a few hotels" from your local system, plus its receiving the search results (source) from the search service. Assign yourself and the search service the **Cognitive Services OpenAI User** role.
55
+
Azure OpenAI is receiving the query and the search results from your local system. Assign yourself the **Cognitive Services OpenAI User** role on Azure OpenAI.
43
56
44
57
1. Sign in to the [Azure portal](https://portal.azure.com).
45
58
46
-
1. Configure Azure AI Search to use a system-assigned managed identity so that you can you give it role assignments:
47
-
48
-
1. In the Azure portal, [find your search service](https://portal.azure.com/#blade/HubsExtension/BrowseResourceBlade/resourceType/Microsoft.Search%2FsearchServices).
49
-
50
-
1. On the left menu, select **Settings** > **Identity**.
51
-
52
-
1. On the System assigned tab, set status to **On**.
53
-
54
59
1. Configure Azure AI Search for role-based access:
55
60
56
61
1. In the Azure portal, find your Azure AI Search service.
@@ -61,12 +66,12 @@ Azure OpenAI is receiving the (query) "Can you recommend a few hotels" from your
61
66
62
67
1. On the left menu, select **Access control (IAM)**.
63
68
64
-
1. On Azure AI Search, make sure you have permissions to create, load, and query a search index:
69
+
1. On Azure AI Search, select these roles to create, load, and query a search index, and then assign them to your Microsoft Entra ID user identity:
65
70
66
71
-**Search Index Data Contributor**
67
72
-**Search Service Contributor**
68
73
69
-
1. On Azure OpenAI, select **Access control (IAM)** to assign yourself and the search service identity permissions on Azure OpenAI. The code for this quickstart runs locally. Requests to Azure OpenAI originate from your system. Also, search results from the search engine are passed to Azure OpenAI. For these reasons, both you and the search service need permissions on Azure OpenAI.
74
+
1. On Azure OpenAI, select **Access control (IAM)** to assign this role to yourself on Azure OpenAI:
70
75
71
76
-**Cognitive Services OpenAI User**
72
77
@@ -211,6 +216,34 @@ In the remaining sections, you set up API calls to Azure OpenAI and Azure AI Sea
211
216
212
217
1. On the **Overview** home page, select the link to view the endpoints. Copy the URL. An example endpoint might look like `https://example.openai.azure.com/`.
213
218
219
+
## Create a virtual environment
220
+
221
+
In this step, switch back to your local system and Visual Studio Code. We recommend that you create a virtual environment so that you can install the dependencies in isolation.
222
+
223
+
1. In Visual Studio Code, open the folder containing Quickstart-RAG.ipynb.
224
+
225
+
1. Press Ctrl-shift-P to open the command palette, search for "Python: Create Environment", and then select `Venv` to create a virtual environment in the current workspace.
226
+
227
+
1. Select Quickstart-RAG\requirements.txt for the dependencies.
228
+
229
+
It takes several minutes to create the environment. When the environment is ready, continue to the next step.
230
+
231
+
## Sign in to Azure
232
+
233
+
You're using Microsoft Entra ID and role assignments for the connection. Make sure you're logged in to the same tenant and subscription as Azure AI Search and Azure OpenAI. You can use the Azure CLI on the command line to show current properties, change properties, and to sign in. For more information, see [Connect without keys](https://learn.microsoft.com/azure/search/search-get-started-rbac).
234
+
235
+
Run each of the following commands in sequence.
236
+
237
+
```azure-cli
238
+
az account show
239
+
240
+
az account set --subscription <PUT YOUR SUBSCRIPTION ID HERE>
241
+
242
+
az login --tenant <PUT YOUR TENANT ID HERE>
243
+
```
244
+
245
+
You should now be logged in to Azure from your local device.
246
+
214
247
## Set up the query and chat thread
215
248
216
249
This section uses Visual Studio Code and Python to call the chat completion APIs on Azure OpenAI.
@@ -271,18 +304,20 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
271
304
Sources:\n{sources}
272
305
"""
273
306
274
-
# Query is the question being asked. It's sent to the search engine and the LLM.
307
+
# Query is the question being asked. It's sent to the search engine and the chat model
275
308
query="Can you recommend a few hotels with complimentary breakfast?"
276
309
277
-
# Set up the search results and the chat thread.
278
-
# Retrieve the selected fields from the search index related to the question.
310
+
# Search results are created by the search client
311
+
# Search results are composed of the top 5 results and the fields selected from the search index
312
+
# Search results include the top 5 matches to your query
279
313
search_results = search_client.search(
280
314
search_text=query,
281
315
top=5,
282
316
select="Description,HotelName,Tags"
283
317
)
284
318
sources_formatted ="\n".join([f'{document["HotelName"]}:{document["Description"]}:{document["Tags"]}'for document in search_results])
285
319
320
+
# Send the search results and the query to the LLM to generate a response based on the prompt.
286
321
response = openai_client.chat.completions.create(
287
322
messages=[
288
323
{
@@ -293,6 +328,7 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
293
328
model=AZURE_DEPLOYMENT_MODEL
294
329
)
295
330
331
+
# Here is the response from the chat model.
296
332
print(response.choices[0].message.content)
297
333
```
298
334
@@ -321,6 +357,8 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
321
357
322
358
If you get an **Authorization failed** error message, wait a few minutes andtry again. It can take several minutes for role assignments to become operational.
323
359
360
+
If you get a **Resource not found** error message, check the resource URIs and make sure the API version on the chat model is valid.
361
+
324
362
Otherwise, to experiment further, change the query and rerun the last step to better understand how the model works with the grounding data.
325
363
326
364
You can also modify the prompt to change the tone or structure of the output.
@@ -331,7 +369,7 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
331
369
332
370
Azure AI Search supports [complex types](search-howto-complex-data-types.md) for nested JSON structures. In the hotels-sample-index, `Address`is an example of a complextype, consisting of `Address.StreetAddress`, `Address.City`, `Address.StateProvince`, `Address.PostalCode`, and`Address.Country`. The index also has complex collection of `Rooms`for each hotel.
333
371
334
-
If your index has complex types, your query can provide those fields if you first convert the search results output to JSON, and then pass the JSON to the LLM. The following example adds complex types to the request. The formatting instructions include a JSON specification.
372
+
If your index has complex types, your query can provide those fields if you first convert the search results output to JSON, and then pass the JSON to the chat model. The following example adds complex types to the request. The formatting instructions include a JSON specification.
0 commit comments