|
220 | 220 | }, |
221 | 221 | { |
222 | 222 | "cell_type": "code", |
223 | | - "execution_count": null, |
| 223 | + "execution_count": 2, |
224 | 224 | "metadata": {}, |
225 | | - "outputs": [], |
| 225 | + "outputs": [ |
| 226 | + { |
| 227 | + "name": "stdout", |
| 228 | + "output_type": "stream", |
| 229 | + "text": [ |
| 230 | + "py-rag-tutorial-idx created\n" |
| 231 | + ] |
| 232 | + } |
| 233 | + ], |
226 | 234 | "source": [ |
227 | 235 | "from azure.identity import DefaultAzureCredential\n", |
228 | 236 | "from azure.identity import get_bearer_token_provider\n", |
|
294 | 302 | }, |
295 | 303 | { |
296 | 304 | "cell_type": "code", |
297 | | - "execution_count": 4, |
| 305 | + "execution_count": 3, |
298 | 306 | "metadata": {}, |
299 | 307 | "outputs": [ |
300 | 308 | { |
|
337 | 345 | }, |
338 | 346 | { |
339 | 347 | "cell_type": "code", |
340 | | - "execution_count": 6, |
| 348 | + "execution_count": 4, |
341 | 349 | "metadata": {}, |
342 | 350 | "outputs": [ |
343 | 351 | { |
|
455 | 463 | }, |
456 | 464 | { |
457 | 465 | "cell_type": "code", |
458 | | - "execution_count": 7, |
| 466 | + "execution_count": 5, |
459 | 467 | "metadata": {}, |
460 | 468 | "outputs": [ |
461 | 469 | { |
|
503 | 511 | }, |
504 | 512 | { |
505 | 513 | "cell_type": "code", |
506 | | - "execution_count": 34, |
| 514 | + "execution_count": 6, |
507 | 515 | "metadata": {}, |
508 | 516 | "outputs": [ |
509 | 517 | { |
|
531 | 539 | "from azure.search.documents import SearchClient\n", |
532 | 540 | "from azure.search.documents.models import VectorizableTextQuery\n", |
533 | 541 | "\n", |
534 | | - "# Vector Search using text-to-vector conversion of the querystring\n", |
| 542 | + "# Vector Search using text-to-vector conversion of the query string\n", |
535 | 543 | "query = \"what's NASA's website?\" \n", |
536 | 544 | "\n", |
537 | 545 | "search_client = SearchClient(endpoint=AZURE_SEARCH_SERVICE, credential=credential, index_name=index_name)\n", |
|
555 | 563 | "source": [ |
556 | 564 | "## Search using a chat model\n", |
557 | 565 | "\n", |
558 | | - "This script sends a query, the query response, and a prompt to an LLM for chat completion. This time, the response is created using generative AI.\n", |
| 566 | + "This script sends a query, the query response, and a prompt to an LLM for chat completion. This time, the response is created using generative AI. We broke this task out into three separate tasks: set up the clients, set up the search query, call the LLM and get the response. We also give it a more interesting query. \n", |
559 | 567 | "\n", |
560 | | - "We broke this task out into three separate tasks: set up the clients, set up the search query, call the LLM and get the response. For more information about this step, its behaviors, and changing the settings, revisit [Search and generate answers](https://learn.microsoft.com/azure/search/tutorial-rag-build-solution-query) in the tutorial." |
| 568 | + "To learn more about this step, revisit [Search and generate answers](https://learn.microsoft.com/azure/search/tutorial-rag-build-solution-query) in the tutorial." |
561 | 569 | ] |
562 | 570 | }, |
563 | 571 | { |
564 | 572 | "cell_type": "code", |
565 | | - "execution_count": 43, |
| 573 | + "execution_count": 7, |
566 | 574 | "metadata": {}, |
567 | 575 | "outputs": [], |
568 | 576 | "source": [ |
|
603 | 611 | }, |
604 | 612 | { |
605 | 613 | "cell_type": "code", |
606 | | - "execution_count": 44, |
| 614 | + "execution_count": 8, |
607 | 615 | "metadata": {}, |
608 | 616 | "outputs": [], |
609 | 617 | "source": [ |
|
631 | 639 | }, |
632 | 640 | { |
633 | 641 | "cell_type": "code", |
634 | | - "execution_count": 45, |
| 642 | + "execution_count": 9, |
635 | 643 | "metadata": {}, |
636 | 644 | "outputs": [ |
637 | 645 | { |
638 | 646 | "name": "stdout", |
639 | 647 | "output_type": "stream", |
640 | 648 | "text": [ |
641 | | - "The NASA Earth book stands at the intersection of science and art, using NASA's unique vantage point and tools to study Earth’s physical processes from beneath the crust to the edge of the atmosphere. It presents Earth as a dynamic system, examining cycles and processes such as the water cycle, carbon cycle, ocean circulation, and the movement of heat. The book uses images to tell the story of Earth's land, wind, water, ice, and air as seen from above, showcasing the planet’s diverse colors, textures, and shapes.\n", |
642 | | - "\n", |
643 | | - "- It aims to inspire by presenting a 4.5-billion-year-old planet through striking images.\n", |
644 | | - "- The book highlights how light is observed and studied, reflecting NASA’s scientific pursuits and artistic sensibilities.\n", |
645 | | - "- It emphasizes the awe-inspiring beauty of Earth, which NASA captures from space.\n", |
| 649 | + "The NASA Earth book is about the intricate and captivating science of our planet, studied through NASA's unique perspective and tools. It presents Earth as a dynamic and complex system, observed through various cycles and processes such as the water cycle and ocean circulation. The book combines stunning satellite images with detailed scientific insights, portraying Earth’s beauty and the continuous interaction of land, wind, water, ice, and air seen from above. It aims to inspire and demonstrate that the truth of our planet is as compelling as any fiction.\n", |
646 | 650 | "\n", |
647 | | - "(Source: page-8.pdf)\n" |
| 651 | + "Source: page-8.pdf\n" |
648 | 652 | ] |
649 | 653 | } |
650 | 654 | ], |
|
666 | 670 | "cell_type": "markdown", |
667 | 671 | "metadata": {}, |
668 | 672 | "source": [ |
| 673 | + "## Try another query\n", |
| 674 | + "\n", |
| 675 | + "The first query is very broad. Let's ask another question that requires the search engine and the LLM to find more granular information." |
| 676 | + ] |
| 677 | + }, |
| 678 | + { |
| 679 | + "cell_type": "code", |
| 680 | + "execution_count": 10, |
| 681 | + "metadata": {}, |
| 682 | + "outputs": [ |
| 683 | + { |
| 684 | + "name": "stdout", |
| 685 | + "output_type": "stream", |
| 686 | + "text": [ |
| 687 | + "Yes, there are cloud formations specific to oceans and large bodies of water. These include:\n", |
| 688 | + "\n", |
| 689 | + "- **Cloud Streets**: Formed when wind blows from a cold surface like sea ice over the warmer, moister air near the open ocean. The winds create cylinders of spinning air, with clouds forming along the upward cycle of these cylinders. This phenomenon was observed over the Bering Strait in January 2010 (Source: page-21.pdf).\n", |
| 690 | + "- **Dense Marine Clouds**: Commonly form over the ocean due to cooler, moist marine air. For example, along the coast of China, onshore winds carry these clouds toward the land, but they tend to evaporate as they move onshore due to the warmer, drier landmass (Source: page-33.pdf).\n", |
| 691 | + "\n", |
| 692 | + "Summary: Specific cloud formations, such as cloud streets and dense marine clouds, occur over oceans and large bodies of water.\n" |
| 693 | + ] |
| 694 | + } |
| 695 | + ], |
| 696 | + "source": [ |
| 697 | + "# Focused query on cloud formations and bodies of water\n", |
| 698 | + "query=\"Are there any cloud formations specific to oceans and large bodies of water?\"\n", |
| 699 | + "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"text_vector\")\n", |
| 700 | + "\n", |
| 701 | + "search_results = search_client.search(\n", |
| 702 | + " search_text=query,\n", |
| 703 | + " vector_queries= [vector_query],\n", |
| 704 | + " select=[\"title\", \"chunk\", \"locations\"],\n", |
| 705 | + " top=5,\n", |
| 706 | + ")\n", |
| 707 | + "\n", |
| 708 | + "sources_formatted = \"=================\\n\".join([f'TITLE: {document[\"title\"]}, CONTENT: {document[\"chunk\"]}, LOCATIONS: {document[\"locations\"]}' for document in search_results])\n", |
669 | 709 | "\n", |
| 710 | + "response = openai_client.chat.completions.create(\n", |
| 711 | + " messages=[\n", |
| 712 | + " {\n", |
| 713 | + " \"role\": \"user\",\n", |
| 714 | + " \"content\": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)\n", |
| 715 | + " }\n", |
| 716 | + " ],\n", |
| 717 | + " model=deployment_name\n", |
| 718 | + ")\n", |
| 719 | + "\n", |
| 720 | + "print(response.choices[0].message.content)" |
| 721 | + ] |
| 722 | + }, |
| 723 | + { |
| 724 | + "cell_type": "markdown", |
| 725 | + "metadata": {}, |
| 726 | + "source": [ |
670 | 727 | "## Update the schema for semantic ranking and scoring profile\n", |
671 | 728 | "\n", |
672 | | - "Semantic ranking and scoring profile configurations exist in the index schema. You can update an existing index to use both without incurring a [rebuild requirement](/azure/search/search-howto-reindex).\n", |
| 729 | + "Azure AI Search has multiple features and capabilities that improve relevance. In this step, we add two of them: semantic ranking and scoring profiles. \n", |
673 | 730 | "\n", |
674 | | - "An update request should include all of the existing schema definitions that you want to keep, plus the new or changed elements. It's a best practice to issue a GET INDEX request to retrieve the current index before adding new elements.\n", |
| 731 | + "Semantic ranking and scoring profile configurations exist in the index schema. You can update an existing index to use both without incurring a [rebuild requirement](/azure/search/search-howto-reindex). An update request should include all of the existing schema definitions that you want to keep, plus the new or changed elements. It's a best practice to issue a GET INDEX request to retrieve the current index before adding new elements.\n", |
675 | 732 | "\n", |
676 | | - "For more information about this step, its behaviors, see [Maximimze relevance](https://learn.microsoft.com/azure/search/tutorial-rag-build-maximize-relevance) in the RAG tutorial series." |
| 733 | + "To learn more about this step, see [Maximimze relevance](https://learn.microsoft.com/azure/search/tutorial-rag-build-maximize-relevance) in the RAG tutorial series." |
677 | 734 | ] |
678 | 735 | }, |
679 | 736 | { |
680 | 737 | "cell_type": "code", |
681 | | - "execution_count": null, |
| 738 | + "execution_count": 11, |
682 | 739 | "metadata": {}, |
683 | | - "outputs": [], |
| 740 | + "outputs": [ |
| 741 | + { |
| 742 | + "name": "stdout", |
| 743 | + "output_type": "stream", |
| 744 | + "text": [ |
| 745 | + "py-rag-tutorial-idx updated\n" |
| 746 | + ] |
| 747 | + } |
| 748 | + ], |
684 | 749 | "source": [ |
685 | 750 | "# Update the classes to include the new fields\n", |
686 | 751 | "from azure.identity import DefaultAzureCredential\n", |
|
783 | 848 | "cell_type": "markdown", |
784 | 849 | "metadata": {}, |
785 | 850 | "source": [ |
786 | | - "## Update query request using semantic configuration and scoring profile\n", |
| 851 | + "## Rerun the query using semantic configuration and scoring profile\n", |
| 852 | + "\n", |
| 853 | + "This example updates the query request. It's using the same query as before, but with the addition of semantic ranking and a scoring profile that boosts any matching search documents that mention water-related terms. \n", |
787 | 854 | "\n", |
788 | | - "This example updates the query request. An exlanation for this script can be found in [Maximimze relevance](https://learn.microsoft.com/azure/search/tutorial-rag-build-maximize-relevance) in the RAG tutorial series." |
| 855 | + "Compared to the \"before\" query that gave us a reasonable response to the question about cloud formations and water, this query should provide a better answer based on the extra relevance tuning capabilities.\n", |
| 856 | + "\n", |
| 857 | + "An explanation for this script can be found in [Maximimze relevance](https://learn.microsoft.com/azure/search/tutorial-rag-build-maximize-relevance) in the RAG tutorial series." |
789 | 858 | ] |
790 | 859 | }, |
791 | 860 | { |
792 | 861 | "cell_type": "code", |
793 | | - "execution_count": null, |
| 862 | + "execution_count": 13, |
794 | 863 | "metadata": {}, |
795 | | - "outputs": [], |
| 864 | + "outputs": [ |
| 865 | + { |
| 866 | + "name": "stdout", |
| 867 | + "output_type": "stream", |
| 868 | + "text": [ |
| 869 | + "Yes, there are specific cloud formations associated with oceans and large bodies of water:\n", |
| 870 | + "\n", |
| 871 | + "- **Low Stratus Clouds**: Observed framing a hole over iceberg A-56 in the South Atlantic Ocean. These clouds can be influenced by thermal instabilities created by large obstacles like icebergs (page-39.pdf).\n", |
| 872 | + "- **Undular Bore/Solitary Wave**: Created by the interaction between cool, dry air from Africa and warm, moist air over the Atlantic Ocean off the coast of Mauritania. This results in a wave structure in the atmosphere that influences cloud formation (page-23.pdf).\n", |
| 873 | + "- **Ship Tracks**: Narrow clouds formed by water vapor condensing around pollution particles from ship exhaust, observed over the Pacific Ocean. These clouds can stretch for many hundreds of kilometers (page-31.pdf).\n", |
| 874 | + "- **Volcanic Eruption Plumes**: Ash and volcanic particles from eruptions in the South Sandwich Islands act as seeds for cloud formation. These plumes were observed in the South Atlantic Ocean (page-13.pdf).\n", |
| 875 | + "\n", |
| 876 | + "Summary: Specific cloud formations over oceans include low stratus clouds, undular bores, ship tracks, and volcanic eruption plumes, influenced by various factors such as thermal instabilities, air interactions, pollution, and volcanic activity.\n" |
| 877 | + ] |
| 878 | + } |
| 879 | + ], |
796 | 880 | "source": [ |
797 | 881 | "# Import libraries\n", |
798 | 882 | "from azure.search.documents import SearchClient\n", |
|
819 | 903 | "Answer the query using only the sources provided below.\n", |
820 | 904 | "Use bullets if the answer has multiple points.\n", |
821 | 905 | "If the answer is longer than 3 sentences, provide a summary.\n", |
822 | | - "Answer ONLY with the facts listed in the list of sources below.\n", |
| 906 | + "Answer ONLY with the facts listed in the list of sources below. Cite your source when you answer the question\n", |
823 | 907 | "If there isn't enough information below, say you don't know.\n", |
824 | 908 | "Do not generate answers that don't use the sources below.\n", |
825 | 909 | "Query: {query}\n", |
826 | 910 | "Sources:\\n{sources}\n", |
827 | 911 | "\"\"\"\n", |
828 | 912 | "\n", |
829 | 913 | "# Queries are unchanged in this update\n", |
830 | | - "query=\"What's the NASA earth book about?\"\n", |
831 | | - "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=1, fields=\"text_vector\")\n", |
| 914 | + "query=\"Are there any cloud formations specific to oceans and large bodies of water?\"\n", |
| 915 | + "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"text_vector\")\n", |
832 | 916 | "\n", |
833 | 917 | "# Add query_type semantic and semantic_configuration_name\n", |
834 | 918 | "# Add scoring_profile and scoring_parameters\n", |
|
842 | 926 | " select=\"title, chunk, locations\",\n", |
843 | 927 | " top=5,\n", |
844 | 928 | ")\n", |
845 | | - "sources_formatted = \"\\n\".join([f'{document[\"title\"]}:{document[\"chunk\"]}:{document[\"locations\"]}' for document in search_results])\n", |
| 929 | + "sources_formatted = \"=================\\n\".join([f'TITLE: {document[\"title\"]}, CONTENT: {document[\"chunk\"]}, LOCATIONS: {document[\"locations\"]}' for document in search_results])\n", |
846 | 930 | "\n", |
847 | 931 | "response = openai_client.chat.completions.create(\n", |
848 | 932 | " messages=[\n", |
|
0 commit comments