Skip to content

Commit c500e9c

Browse files
Merge pull request #809 from HeidiSteen/heidist-rag-qs
RAG quickstart update per MattG's recent code updates
2 parents 9367926 + 56373c6 commit c500e9c

File tree

4 files changed

+243
-101
lines changed

4 files changed

+243
-101
lines changed

articles/search/search-get-started-rag.md

Lines changed: 162 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ author: HeidiSteen
66
ms.author: heidist
77
ms.service: azure-ai-search
88
ms.topic: quickstart
9-
ms.date: 09/16/2024
9+
ms.date: 10/14/2024
1010
---
1111

1212
# Quickstart: Generative search (RAG) with grounding data from Azure AI Search
1313

14-
This quickstart shows you how to send queries to a Large Language Model (LLM) for a conversational search experience over your indexed content on Azure AI Search. You use the Azure portal to set up the resources, and then run Python code to call the APIs.
14+
This quickstart shows you how to send basic and complex queries to a Large Language Model (LLM) for a conversational search experience over your indexed content on Azure AI Search. You use the Azure portal to set up the resources, and then run Python code to call the APIs.
1515

1616
## Prerequisites
1717

1818
- An Azure subscription. [Create one for free](https://azure.microsoft.com/free/).
1919

2020
- [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.
2121

22-
- [Azure OpenAI](https://aka.ms/oai/access) resource with a deployment of `gpt-35-turbo`, `gpt-4`, or equivalent model, in the same region as Azure AI Search.
22+
- [Azure OpenAI](https://aka.ms/oai/access) resource with a deployment of `gpt-4o`, `gpt-4o-mini`, or equivalent LLM, in the same region as Azure AI Search.
2323

2424
- [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).
2525

@@ -122,28 +122,68 @@ We recommend the hotels-sample-index, which can be created in minutes and runs o
122122

123123
1. **Save** your changes.
124124

125-
1. Run the following query in [Search Explorer](search-explorer.md) to test your index: `hotels near the ocean with beach access and good views`.
125+
1. Run the following query in [Search Explorer](search-explorer.md) to test your index: `complimentary breakfast`.
126126

127-
Output should look similar to the following example. Results that are returned directly from the search engine consist of fields and their verbatim values, along with metadata like a search score and a semantic ranking score and caption if you use semantic ranker.
127+
Output should look similar to the following example. Results that are returned directly from the search engine consist of fields and their verbatim values, along with metadata like a search score and a semantic ranking score and caption if you use semantic ranker. We used a [select statement](search-query-odata-select.md) to return just the HotelName, Description, and Tags fields.
128128

129129
```
130-
"@search.score": 5.600783,
131-
"@search.rerankerScore": 2.4191176891326904,
132-
"@search.captions": [
133-
{
134-
"text": "Contoso Ocean Motel. Budget. pool\r\nair conditioning\r\nbar. Oceanfront hotel overlooking the beach features rooms with a private balcony and 2 indoor and outdoor pools. Various shops and art entertainment are on the boardwalk, just steps away..",
135-
"highlights": "Contoso Ocean Motel. Budget.<em> pool\r\nair conditioning\r\nbar. O</em>ceanfront hotel overlooking the beach features rooms with a private balcony and 2 indoor and outdoor pools. Various shops and art entertainment are on the boardwalk, just steps away."
136-
}
137-
],
138-
"HotelId": "41",
139-
"HotelName": "Contoso Ocean Motel",
140-
"Description": "Oceanfront hotel overlooking the beach features rooms with a private balcony and 2 indoor and outdoor pools. Various shops and art entertainment are on the boardwalk, just steps away.",
141-
"Category": "Budget",
142-
"Tags": [
143-
"pool",
144-
"air conditioning",
145-
"bar"
146-
],
130+
{
131+
"@odata.count": 18,
132+
"@search.answers": [],
133+
"value": [
134+
{
135+
"@search.score": 2.2896252,
136+
"@search.rerankerScore": 2.506816864013672,
137+
"@search.captions": [
138+
{
139+
"text": "Head Wind Resort. Suite. coffee in lobby\r\nfree wifi\r\nview. The best of old town hospitality combined with views of the river and cool breezes off the prairie. Our penthouse suites offer views for miles and the rooftop plaza is open to all guests from sunset to 10 p.m. Enjoy a **complimentary continental breakfast** in the lobby, and free Wi-Fi throughout the hotel..",
140+
"highlights": ""
141+
}
142+
],
143+
"HotelName": "Head Wind Resort",
144+
"Description": "The best of old town hospitality combined with views of the river and cool breezes off the prairie. Our penthouse suites offer views for miles and the rooftop plaza is open to all guests from sunset to 10 p.m. Enjoy a complimentary continental breakfast in the lobby, and free Wi-Fi throughout the hotel.",
145+
"Tags": [
146+
"coffee in lobby",
147+
"free wifi",
148+
"view"
149+
]
150+
},
151+
{
152+
"@search.score": 2.2158256,
153+
"@search.rerankerScore": 2.288334846496582,
154+
"@search.captions": [
155+
{
156+
"text": "Swan Bird Lake Inn. Budget. continental breakfast\r\nfree wifi\r\n24-hour front desk service. We serve a continental-style breakfast each morning, featuring a variety of food and drinks. Our locally made, oh-so-soft, caramel cinnamon rolls are a favorite with our guests. Other breakfast items include coffee, orange juice, milk, cereal, instant oatmeal, bagels, and muffins..",
157+
"highlights": ""
158+
}
159+
],
160+
"HotelName": "Swan Bird Lake Inn",
161+
"Description": "We serve a continental-style breakfast each morning, featuring a variety of food and drinks. Our locally made, oh-so-soft, caramel cinnamon rolls are a favorite with our guests. Other breakfast items include coffee, orange juice, milk, cereal, instant oatmeal, bagels, and muffins.",
162+
"Tags": [
163+
"continental breakfast",
164+
"free wifi",
165+
"24-hour front desk service"
166+
]
167+
},
168+
{
169+
"@search.score": 0.92481667,
170+
"@search.rerankerScore": 2.221315860748291,
171+
"@search.captions": [
172+
{
173+
"text": "White Mountain Lodge & Suites. Resort and Spa. continental breakfast\r\npool\r\nrestaurant. Live amongst the trees in the heart of the forest. Hike along our extensive trail system. Visit the Natural Hot Springs, or enjoy our signature hot stone massage in the Cathedral of Firs. Relax in the meditation gardens, or join new friends around the communal firepit. Weekend evening entertainment on the patio features special guest musicians or poetry readings..",
174+
"highlights": ""
175+
}
176+
],
177+
"HotelName": "White Mountain Lodge & Suites",
178+
"Description": "Live amongst the trees in the heart of the forest. Hike along our extensive trail system. Visit the Natural Hot Springs, or enjoy our signature hot stone massage in the Cathedral of Firs. Relax in the meditation gardens, or join new friends around the communal firepit. Weekend evening entertainment on the patio features special guest musicians or poetry readings.",
179+
"Tags": [
180+
"continental breakfast",
181+
"pool",
182+
"restaurant"
183+
]
184+
},
185+
. . .
186+
]}
147187
```
148188
149189
## Get service endpoints
@@ -169,29 +209,21 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
169209
1. Install the following Python packages.
170210
171211
```python
172-
! pip install azure-search-documents==11.6.0b4 --quiet
173-
! pip install azure-identity==1.16.0 --quiet
212+
! pip install azure-search-documents==11.6.0b5 --quiet
213+
! pip install azure-identity==1.16.1 --quiet
174214
! pip install openai --quiet
175215
! pip intall aiohttp --quiet
216+
! pip intall ipykernel --quiet
176217
```
177218

178219
1. Set the following variables, substituting placeholders with the endpoints you collected in the previous step.
179220

180221
```python
181222
AZURE_SEARCH_SERVICE: str = "PUT YOUR SEARCH SERVICE ENDPOINT HERE"
182223
AZURE_OPENAI_ACCOUNT: str = "PUT YOUR AZURE OPENAI ENDPOINT HERE"
183-
AZURE_DEPLOYMENT_MODEL: str = "gpt-35-turbo"
224+
AZURE_DEPLOYMENT_MODEL: str = "gpt-4o"
184225
```
185226

186-
1. Run the following code to set query parameters. The query is a keyword search using semantic ranking. In a keyword search, the search engine returns up to 50 matches, but only the top 5 are provided to the model. If you can't [enable semantic rankersemantic-how-to-enable-disable.md) on your search service, set the value to false.
187-
188-
```python
189-
# Set query parameters for grounding the conversation on your search index
190-
search_type="text"
191-
use_semantic_reranker=True
192-
sources_to_include=5
193-
```
194-
195227
1. Set up clients, the prompt, query, and response.
196228

197229
```python
@@ -227,7 +259,7 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
227259
"""
228260

229261
# Query is the question being asked. It's sent to the search engine and the LLM.
230-
query="Can you recommend a few hotels near the ocean with beach access and good views"
262+
query="Can you recommend a few hotels with complimentary breakfast?"
231263

232264
# Set up the search results and the chat thread.
233265
# Retrieve the selected fields from the search index related to the question.
@@ -254,12 +286,22 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
254286
Output is from Azure OpenAI, and it consists of recommendations for several hotels. Here's an example of what the output might look like:
255287

256288
```
257-
Based on your criteria, we recommend the following hotels:
258-
259-
- Contoso Ocean Motel: located right on the beach and has private balconies with ocean views. They also have indoor and outdoor pools. It's located on the boardwalk near shops and art entertainment.
260-
- Northwind Plaza & Suites: offers ocean views, free Wi-Fi, full kitchen, and a free breakfast buffet. Although not directly on the beach, this hotel has great views and is near the aquarium. They also have a pool.
261-
262-
Several other hotels have views and water features, but do not offer beach access or views of the ocean.
289+
Sure! Here are a few hotels that offer complimentary breakfast:
290+
291+
- **Head Wind Resort**
292+
- Complimentary continental breakfast in the lobby
293+
- Free Wi-Fi throughout the hotel
294+
295+
- **Double Sanctuary Resort**
296+
- Continental breakfast included
297+
298+
- **White Mountain Lodge & Suites**
299+
- Continental breakfast available
300+
301+
- **Swan Bird Lake Inn**
302+
- Continental-style breakfast each morning with a variety of food and drinks
303+
such as caramel cinnamon rolls, coffee, orange juice, milk, cereal,
304+
instant oatmeal, bagels, and muffins
263305
```
264306

265307
If you get a **Forbidden** error message, check Azure AI Search configuration to make sure role-based access is enabled.
@@ -272,6 +314,85 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
272314

273315
You might also try the query without semantic ranking by setting `use_semantic_reranker=False` in the query parameters step. Semantic ranking can noticably improve the relevance of query results and the ability of the LLM to return useful information. Experimentation can help you decide whether it makes a difference for your content.
274316

317+
## Send a complex RAG query
318+
319+
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 complex type, consisting of `Address.StreetAddress`, `Address.City`, `Address.StateProvince`, `Address.PostalCode`, and `Address.Country`. The index also has complex collection of `Rooms` for each hotel.
320+
321+
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.
322+
323+
```python
324+
import json
325+
326+
# Query is the question being asked. It's sent to the search engine and the LLM.
327+
query="Can you recommend a few hotels that offer complimentary breakfast?
328+
Tell me their description, address, tags, and the rate for one room that sleeps 4 people."
329+
330+
# Set up the search results and the chat thread.
331+
# Retrieve the selected fields from the search index related to the question.
332+
selected_fields = ["HotelName","Description","Address","Rooms","Tags"]
333+
search_results = search_client.search(
334+
search_text=query,
335+
top=5,
336+
select=selected_fields,
337+
query_type="semantic"
338+
)
339+
sources_filtered = [{field: result[field] for field in selected_fields} for result in search_results]
340+
sources_formatted = "\n".join([json.dumps(source) for source in sources_filtered])
341+
342+
response = openai_client.chat.completions.create(
343+
messages=[
344+
{
345+
"role": "user",
346+
"content": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)
347+
}
348+
],
349+
model=AZURE_DEPLOYMENT_MODEL
350+
)
351+
352+
print(response.choices[0].message.content)
353+
```
354+
355+
Output is from Azure OpenAI, and it adds content from complex types.
356+
357+
```
358+
Here are a few hotels that offer complimentary breakfast and have rooms that sleep 4 people:
359+
360+
1. **Head Wind Resort**
361+
- **Description:** The best of old town hospitality combined with views of the river and
362+
cool breezes off the prairie. Enjoy a complimentary continental breakfast in the lobby,
363+
and free Wi-Fi throughout the hotel.
364+
- **Address:** 7633 E 63rd Pl, Tulsa, OK 74133, USA
365+
- **Tags:** Coffee in lobby, free Wi-Fi, view
366+
- **Room for 4:** Suite, 2 Queen Beds (Amenities) - $254.99
367+
368+
2. **Double Sanctuary Resort**
369+
- **Description:** 5-star Luxury Hotel - Biggest Rooms in the city. #1 Hotel in the area
370+
listed by Traveler magazine. Free WiFi, Flexible check in/out, Fitness Center & espresso
371+
in room. Offers continental breakfast.
372+
- **Address:** 2211 Elliott Ave, Seattle, WA 98121, USA
373+
- **Tags:** View, pool, restaurant, bar, continental breakfast
374+
- **Room for 4:** Suite, 2 Queen Beds (Amenities) - $254.99
375+
376+
3. **Swan Bird Lake Inn**
377+
- **Description:** Continental-style breakfast featuring a variety of food and drinks.
378+
Locally made caramel cinnamon rolls are a favorite.
379+
- **Address:** 1 Memorial Dr, Cambridge, MA 02142, USA
380+
- **Tags:** Continental breakfast, free Wi-Fi, 24-hour front desk service
381+
- **Room for 4:** Budget Room, 2 Queen Beds (City View) - $85.99
382+
383+
4. **Gastronomic Landscape Hotel**
384+
- **Description:** Known for its culinary excellence under the management of William Dough,
385+
offers continental breakfast.
386+
- **Address:** 3393 Peachtree Rd, Atlanta, GA 30326, USA
387+
- **Tags:** Restaurant, bar, continental breakfast
388+
- **Room for 4:** Budget Room, 2 Queen Beds (Amenities) - $66.99
389+
...
390+
- **Tags:** Pool, continental breakfast, free parking
391+
- **Room for 4:** Budget Room, 2 Queen Beds (Amenities) - $60.99
392+
393+
Enjoy your stay! Let me know if you need any more information.
394+
```
395+
275396
## Troubleshooting errors
276397

277398
To debug authentication errors, insert the following code before the step that calls the search engine and the LLM.

0 commit comments

Comments
 (0)