Skip to content

Commit

Permalink
Merge pull request #134 from Azure-Samples/matt/hotels-rag-complex
Browse files Browse the repository at this point in the history
Update RAG Quickstart to include hotels complex types
  • Loading branch information
HeidiSteen authored Oct 11, 2024
2 parents 45cbe59 + 38b7a68 commit 9a460f3
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 16 deletions.
155 changes: 141 additions & 14 deletions Quickstart-RAG/Quickstart-rag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"\n",
"- [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/how-to/create-resource)\n",
"\n",
" - Deploy a chat model (GPT-3.5-Turbo, GPT-4, or equivalent LLM).\n",
" - Deploy a chat model (gpt-4o, gpt-4o-mini, or equivalent LLM).\n",
"\n",
"- [Azure AI Search](https://learn.microsoft.com/azure/search/search-create-service-portal)\n",
"\n",
Expand Down Expand Up @@ -127,26 +127,52 @@
"# Set endpoints and deployment model (provide the name of the deployment)\n",
"AZURE_SEARCH_SERVICE: str = \"PUT YOUR SEARCH SERVICE ENDPOINT HERE\"\n",
"AZURE_OPENAI_ACCOUNT: str = \"PUT YOUR AZURE OPENAI ENDPOINT HERE\"\n",
"AZURE_DEPLOYMENT_MODEL: str = \"gpt-35-turbo\""
"AZURE_DEPLOYMENT_MODEL: str = \"gpt-4o\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Set query parameters for grounding the conversation on your search index\n",
"search_type=\"text\"\n",
"use_semantic_reranker=True\n",
"sources_to_include=5"
"## Basic RAG Query\n",
"\n",
"The following code demonstrates how to query fields such as strings or string collections and use them to answer a question using RAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure! Here are a few hotels that offer complimentary breakfast:\n",
"\n",
"- **Whitefish Lodge & Suites**:\n",
" - Continental breakfast\n",
" - Free parking\n",
" - Free wifi\n",
"\n",
"- **Trails End Motel**:\n",
" - Free hot breakfast buffet\n",
" - Free wireless internet\n",
"\n",
"- **Peaceful Market Hotel & Spa**:\n",
" - Continental breakfast\n",
" - Restaurant\n",
" - View\n",
"\n",
"- **Marquis Plaza & Suites**:\n",
" - Free breakfast buffet\n",
" - Free wifi\n",
" - Pool\n",
"\n",
"Enjoy your stay!\n"
]
}
],
"source": [
"# Set up the query for generating responses\n",
"from azure.identity import DefaultAzureCredential\n",
Expand Down Expand Up @@ -180,14 +206,15 @@
"\"\"\"\n",
"\n",
"# Query is the question being asked. It's sent to the search engine and the LLM.\n",
"query=\"Can you recommend a few hotels near the ocean with beach access and good views\"\n",
"query=\"Can you recommend a few hotels with complimentary breakfast?\"\n",
"\n",
"# Set up the search results and the chat thread.\n",
"# Retrieve the selected fields from the search index related to the question.\n",
"search_results = search_client.search(\n",
" search_text=query,\n",
" top=5,\n",
" select=\"Description,HotelName,Tags\"\n",
" select=\"Description,HotelName,Tags\",\n",
" query_type=\"semantic\"\n",
")\n",
"sources_formatted = \"\\n\".join([f'{document[\"HotelName\"]}:{document[\"Description\"]}:{document[\"Tags\"]}' for document in search_results])\n",
"\n",
Expand Down Expand Up @@ -220,6 +247,106 @@
"\n",
"+ For more debugging guidance, see the [troubleshooting section](https://learn.microsoft.com/azure/search/search-get-started-rag#troubleshooting-errors) in the Quickstart documentation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complex RAG Query\n",
"\n",
"Use complex types and collections in RAG by converting the result to JSON before sending it to the LLM"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure! Here are a few hotels that offer complimentary breakfast:\n",
"\n",
"1. **Trails End Motel**\n",
" - **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",
" - **Address**: 7014 E Camelback Rd, Scottsdale, AZ 85251, USA\n",
" - **Tags**: Continental breakfast, view\n",
" - **Room Recommendation**: \n",
" - **Type**: Suite, 2 Queen Beds (City View)\n",
" - **Rate**: $266.99 per night\n",
" - **Sleeps**: 4\n",
"\n",
"2. **Whitefish Lodge & Suites**\n",
" - **Description**: Located in the heart of the forest. Enjoy Warm Weather, Beach Club Services, Natural Hot Springs, Airport Shuttle.\n",
" - **Address**: 3000 E 1st Ave, Denver, CO 80206, USA\n",
" - **Tags**: Continental breakfast, free parking, free wifi\n",
" - **Room Recommendation**: \n",
" - **Type**: Suite, 2 Queen Beds (Mountain View)\n",
" - **Rate**: $256.99 per night\n",
" - **Sleeps**: 4\n",
"\n",
"3. **Peaceful Market Hotel & Spa**\n",
" - **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",
" - **Address**: 11 Times Sq, New York, NY 10036, USA\n",
" - **Tags**: Continental breakfast, restaurant, view\n",
" - **Room Recommendation**: \n",
" - **Type**: Suite, 2 Queen Beds (Waterfront View)\n",
" - **Rate**: $260.99 per night\n",
" - **Sleeps**: 4\n",
"\n",
"4. **Nova Hotel & Spa**\n",
" - **Description**: 1 Mile from the airport. Free WiFi, Outdoor Pool, Complimentary Airport Shuttle, 6 miles from the beach & 10 miles from downtown.\n",
" - **Address**: 4400 Ashford Dunwoody Rd NE, Atlanta, GA 30346, USA\n",
" - **Tags**: Pool, continental breakfast, free parking\n",
" - **Room Recommendation**: \n",
" - **Type**: Deluxe Room, 2 Queen Beds (City View)\n",
" - **Rate**: $161.99 per night\n",
" - **Sleeps**: 4\n",
"\n",
"5. **Scottish Inn**\n",
" - **Description**: Newly Redesigned Rooms & airport shuttle. Minutes from the airport, enjoy lakeside amenities, a resort-style pool & stylish new guestrooms with Internet TVs.\n",
" - **Address**: 3301 Veterans Memorial Blvd, Metairie, LA 70002, USA\n",
" - **Tags**: 24-hour front desk service, continental breakfast, free wifi\n",
" - **Room Recommendation**: \n",
" - **Type**: Deluxe Room, 2 Queen Beds (City View)\n",
" - **Rate**: $140.99 per night\n",
" - **Sleeps**: 4\n",
"\n",
"I hope you find these recommendations helpful! Enjoy your stay!\n"
]
}
],
"source": [
"import json\n",
"\n",
"# Query is the question being asked. It's sent to the search engine and the LLM.\n",
"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",
"\n",
"# Set up the search results and the chat thread.\n",
"# Retrieve the selected fields from the search index related to the question.\n",
"selected_fields = [\"HotelName\",\"Description\",\"Address\",\"Rooms\",\"Tags\"]\n",
"search_results = search_client.search(\n",
" search_text=query,\n",
" top=5,\n",
" select=selected_fields,\n",
" query_type=\"semantic\"\n",
")\n",
"sources_filtered = [{field: result[field] for field in selected_fields} for result in search_results]\n",
"sources_formatted = \"\\n\".join([json.dumps(source) for source in sources_filtered])\n",
"\n",
"response = openai_client.chat.completions.create(\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)\n",
" }\n",
" ],\n",
" model=AZURE_DEPLOYMENT_MODEL\n",
")\n",
"\n",
"print(response.choices[0].message.content)"
]
}
],
"metadata": {
Expand All @@ -238,7 +365,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
5 changes: 3 additions & 2 deletions Quickstart-RAG/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
azure-search-documents==11.6.0b4
azure-search-documents==11.6.0b5
azure-identity==1.16.1
openai
aiohttp
aiohttp
ipykernel

0 comments on commit 9a460f3

Please sign in to comment.