|
29 | 29 | "\n", |
30 | 30 | "- [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/how-to/create-resource)\n", |
31 | 31 | "\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", |
33 | 33 | "\n", |
34 | 34 | "- [Azure AI Search](https://learn.microsoft.com/azure/search/search-create-service-portal)\n", |
35 | 35 | "\n", |
|
127 | 127 | "# Set endpoints and deployment model (provide the name of the deployment)\n", |
128 | 128 | "AZURE_SEARCH_SERVICE: str = \"PUT YOUR SEARCH SERVICE ENDPOINT HERE\"\n", |
129 | 129 | "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\"" |
131 | 131 | ] |
132 | 132 | }, |
133 | 133 | { |
134 | | - "cell_type": "code", |
135 | | - "execution_count": 3, |
| 134 | + "cell_type": "markdown", |
136 | 135 | "metadata": {}, |
137 | | - "outputs": [], |
138 | 136 | "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" |
143 | 140 | ] |
144 | 141 | }, |
145 | 142 | { |
146 | 143 | "cell_type": "code", |
147 | | - "execution_count": null, |
| 144 | + "execution_count": 3, |
148 | 145 | "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 | + ], |
150 | 176 | "source": [ |
151 | 177 | "# Set up the query for generating responses\n", |
152 | 178 | "from azure.identity import DefaultAzureCredential\n", |
|
180 | 206 | "\"\"\"\n", |
181 | 207 | "\n", |
182 | 208 | "# 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", |
184 | 210 | "\n", |
185 | 211 | "# Set up the search results and the chat thread.\n", |
186 | 212 | "# Retrieve the selected fields from the search index related to the question.\n", |
187 | 213 | "search_results = search_client.search(\n", |
188 | 214 | " search_text=query,\n", |
189 | 215 | " top=5,\n", |
190 | | - " select=\"Description,HotelName,Tags\"\n", |
| 216 | + " select=\"Description,HotelName,Tags\",\n", |
| 217 | + " query_type=\"semantic\"\n", |
191 | 218 | ")\n", |
192 | 219 | "sources_formatted = \"\\n\".join([f'{document[\"HotelName\"]}:{document[\"Description\"]}:{document[\"Tags\"]}' for document in search_results])\n", |
193 | 220 | "\n", |
|
220 | 247 | "\n", |
221 | 248 | "+ For more debugging guidance, see the [troubleshooting section](https://learn.microsoft.com/azure/search/search-get-started-rag#troubleshooting-errors) in the Quickstart documentation." |
222 | 249 | ] |
| 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 | + ] |
223 | 350 | } |
224 | 351 | ], |
225 | 352 | "metadata": { |
|
238 | 365 | "name": "python", |
239 | 366 | "nbconvert_exporter": "python", |
240 | 367 | "pygments_lexer": "ipython3", |
241 | | - "version": "3.11.3" |
| 368 | + "version": "3.11.9" |
242 | 369 | } |
243 | 370 | }, |
244 | 371 | "nbformat": 4, |
|
0 commit comments