Skip to content

Commit 38b7a68

Browse files
committed
adding complex example; fix issues with RAG query
1 parent 45cbe59 commit 38b7a68

File tree

2 files changed

+144
-16
lines changed

2 files changed

+144
-16
lines changed

Quickstart-RAG/Quickstart-rag.ipynb

Lines changed: 141 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"\n",
3030
"- [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/how-to/create-resource)\n",
3131
"\n",
32-
" - Deploy a chat model (GPT-3.5-Turbo, GPT-4, or equivalent LLM).\n",
32+
" - Deploy a chat model (gpt-4o, gpt-4o-mini, or equivalent LLM).\n",
3333
"\n",
3434
"- [Azure AI Search](https://learn.microsoft.com/azure/search/search-create-service-portal)\n",
3535
"\n",
@@ -127,26 +127,52 @@
127127
"# Set endpoints and deployment model (provide the name of the deployment)\n",
128128
"AZURE_SEARCH_SERVICE: str = \"PUT YOUR SEARCH SERVICE ENDPOINT HERE\"\n",
129129
"AZURE_OPENAI_ACCOUNT: str = \"PUT YOUR AZURE OPENAI ENDPOINT HERE\"\n",
130-
"AZURE_DEPLOYMENT_MODEL: str = \"gpt-35-turbo\""
130+
"AZURE_DEPLOYMENT_MODEL: str = \"gpt-4o\""
131131
]
132132
},
133133
{
134-
"cell_type": "code",
135-
"execution_count": 3,
134+
"cell_type": "markdown",
136135
"metadata": {},
137-
"outputs": [],
138136
"source": [
139-
"# Set query parameters for grounding the conversation on your search index\n",
140-
"search_type=\"text\"\n",
141-
"use_semantic_reranker=True\n",
142-
"sources_to_include=5"
137+
"## Basic RAG Query\n",
138+
"\n",
139+
"The following code demonstrates how to query fields such as strings or string collections and use them to answer a question using RAG"
143140
]
144141
},
145142
{
146143
"cell_type": "code",
147-
"execution_count": null,
144+
"execution_count": 3,
148145
"metadata": {},
149-
"outputs": [],
146+
"outputs": [
147+
{
148+
"name": "stdout",
149+
"output_type": "stream",
150+
"text": [
151+
"Sure! Here are a few hotels that offer complimentary breakfast:\n",
152+
"\n",
153+
"- **Whitefish Lodge & Suites**:\n",
154+
" - Continental breakfast\n",
155+
" - Free parking\n",
156+
" - Free wifi\n",
157+
"\n",
158+
"- **Trails End Motel**:\n",
159+
" - Free hot breakfast buffet\n",
160+
" - Free wireless internet\n",
161+
"\n",
162+
"- **Peaceful Market Hotel & Spa**:\n",
163+
" - Continental breakfast\n",
164+
" - Restaurant\n",
165+
" - View\n",
166+
"\n",
167+
"- **Marquis Plaza & Suites**:\n",
168+
" - Free breakfast buffet\n",
169+
" - Free wifi\n",
170+
" - Pool\n",
171+
"\n",
172+
"Enjoy your stay!\n"
173+
]
174+
}
175+
],
150176
"source": [
151177
"# Set up the query for generating responses\n",
152178
"from azure.identity import DefaultAzureCredential\n",
@@ -180,14 +206,15 @@
180206
"\"\"\"\n",
181207
"\n",
182208
"# Query is the question being asked. It's sent to the search engine and the LLM.\n",
183-
"query=\"Can you recommend a few hotels near the ocean with beach access and good views\"\n",
209+
"query=\"Can you recommend a few hotels with complimentary breakfast?\"\n",
184210
"\n",
185211
"# Set up the search results and the chat thread.\n",
186212
"# Retrieve the selected fields from the search index related to the question.\n",
187213
"search_results = search_client.search(\n",
188214
" search_text=query,\n",
189215
" top=5,\n",
190-
" select=\"Description,HotelName,Tags\"\n",
216+
" select=\"Description,HotelName,Tags\",\n",
217+
" query_type=\"semantic\"\n",
191218
")\n",
192219
"sources_formatted = \"\\n\".join([f'{document[\"HotelName\"]}:{document[\"Description\"]}:{document[\"Tags\"]}' for document in search_results])\n",
193220
"\n",
@@ -220,6 +247,106 @@
220247
"\n",
221248
"+ For more debugging guidance, see the [troubleshooting section](https://learn.microsoft.com/azure/search/search-get-started-rag#troubleshooting-errors) in the Quickstart documentation."
222249
]
250+
},
251+
{
252+
"cell_type": "markdown",
253+
"metadata": {},
254+
"source": [
255+
"## Complex RAG Query\n",
256+
"\n",
257+
"Use complex types and collections in RAG by converting the result to JSON before sending it to the LLM"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 4,
263+
"metadata": {},
264+
"outputs": [
265+
{
266+
"name": "stdout",
267+
"output_type": "stream",
268+
"text": [
269+
"Sure! Here are a few hotels that offer complimentary breakfast:\n",
270+
"\n",
271+
"1. **Trails End Motel**\n",
272+
" - **Description**: Only 8 miles from Downtown. On-site bar/restaurant, Free hot breakfast buffet, Free wireless internet, All non-smoking hotel. Only 15 miles from the airport.\n",
273+
" - **Address**: 7014 E Camelback Rd, Scottsdale, AZ 85251, USA\n",
274+
" - **Tags**: Continental breakfast, view\n",
275+
" - **Room Recommendation**: \n",
276+
" - **Type**: Suite, 2 Queen Beds (City View)\n",
277+
" - **Rate**: $266.99 per night\n",
278+
" - **Sleeps**: 4\n",
279+
"\n",
280+
"2. **Whitefish Lodge & Suites**\n",
281+
" - **Description**: Located in the heart of the forest. Enjoy Warm Weather, Beach Club Services, Natural Hot Springs, Airport Shuttle.\n",
282+
" - **Address**: 3000 E 1st Ave, Denver, CO 80206, USA\n",
283+
" - **Tags**: Continental breakfast, free parking, free wifi\n",
284+
" - **Room Recommendation**: \n",
285+
" - **Type**: Suite, 2 Queen Beds (Mountain View)\n",
286+
" - **Rate**: $256.99 per night\n",
287+
" - **Sleeps**: 4\n",
288+
"\n",
289+
"3. **Peaceful Market Hotel & Spa**\n",
290+
" - **Description**: Book now and Save up to 30%. Central location. Steps from Empire State Building & Times Square, in Chelsea neighborhood. Brand new rooms. Impeccable service.\n",
291+
" - **Address**: 11 Times Sq, New York, NY 10036, USA\n",
292+
" - **Tags**: Continental breakfast, restaurant, view\n",
293+
" - **Room Recommendation**: \n",
294+
" - **Type**: Suite, 2 Queen Beds (Waterfront View)\n",
295+
" - **Rate**: $260.99 per night\n",
296+
" - **Sleeps**: 4\n",
297+
"\n",
298+
"4. **Nova Hotel & Spa**\n",
299+
" - **Description**: 1 Mile from the airport. Free WiFi, Outdoor Pool, Complimentary Airport Shuttle, 6 miles from the beach & 10 miles from downtown.\n",
300+
" - **Address**: 4400 Ashford Dunwoody Rd NE, Atlanta, GA 30346, USA\n",
301+
" - **Tags**: Pool, continental breakfast, free parking\n",
302+
" - **Room Recommendation**: \n",
303+
" - **Type**: Deluxe Room, 2 Queen Beds (City View)\n",
304+
" - **Rate**: $161.99 per night\n",
305+
" - **Sleeps**: 4\n",
306+
"\n",
307+
"5. **Scottish Inn**\n",
308+
" - **Description**: Newly Redesigned Rooms & airport shuttle. Minutes from the airport, enjoy lakeside amenities, a resort-style pool & stylish new guestrooms with Internet TVs.\n",
309+
" - **Address**: 3301 Veterans Memorial Blvd, Metairie, LA 70002, USA\n",
310+
" - **Tags**: 24-hour front desk service, continental breakfast, free wifi\n",
311+
" - **Room Recommendation**: \n",
312+
" - **Type**: Deluxe Room, 2 Queen Beds (City View)\n",
313+
" - **Rate**: $140.99 per night\n",
314+
" - **Sleeps**: 4\n",
315+
"\n",
316+
"I hope you find these recommendations helpful! Enjoy your stay!\n"
317+
]
318+
}
319+
],
320+
"source": [
321+
"import json\n",
322+
"\n",
323+
"# Query is the question being asked. It's sent to the search engine and the LLM.\n",
324+
"query=\"Can you recommend a few hotels that offer complimentary breakfast? Tell me their description, address, tags, and the rate for one room they have which sleep 4 people.\"\n",
325+
"\n",
326+
"# Set up the search results and the chat thread.\n",
327+
"# Retrieve the selected fields from the search index related to the question.\n",
328+
"selected_fields = [\"HotelName\",\"Description\",\"Address\",\"Rooms\",\"Tags\"]\n",
329+
"search_results = search_client.search(\n",
330+
" search_text=query,\n",
331+
" top=5,\n",
332+
" select=selected_fields,\n",
333+
" query_type=\"semantic\"\n",
334+
")\n",
335+
"sources_filtered = [{field: result[field] for field in selected_fields} for result in search_results]\n",
336+
"sources_formatted = \"\\n\".join([json.dumps(source) for source in sources_filtered])\n",
337+
"\n",
338+
"response = openai_client.chat.completions.create(\n",
339+
" messages=[\n",
340+
" {\n",
341+
" \"role\": \"user\",\n",
342+
" \"content\": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)\n",
343+
" }\n",
344+
" ],\n",
345+
" model=AZURE_DEPLOYMENT_MODEL\n",
346+
")\n",
347+
"\n",
348+
"print(response.choices[0].message.content)"
349+
]
223350
}
224351
],
225352
"metadata": {
@@ -238,7 +365,7 @@
238365
"name": "python",
239366
"nbconvert_exporter": "python",
240367
"pygments_lexer": "ipython3",
241-
"version": "3.11.3"
368+
"version": "3.11.9"
242369
}
243370
},
244371
"nbformat": 4,

Quickstart-RAG/requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
azure-search-documents==11.6.0b4
1+
azure-search-documents==11.6.0b5
22
azure-identity==1.16.1
33
openai
4-
aiohttp
4+
aiohttp
5+
ipykernel

0 commit comments

Comments
 (0)