|
96 | 96 | "\n", |
97 | 97 | "Deploy the following models on Azure OpenAI:\n", |
98 | 98 | "\n", |
99 | | - "- Text-embedding-ada-02 on Azure OpenAI for embeddings\n", |
100 | | - "- GPT-35-Turbo on Azure OpenAI for chat completion\n", |
| 99 | + "- text-embedding-3-large on Azure OpenAI for embeddings\n", |
| 100 | + "- gpt-4o on Azure OpenAI for chat completion\n", |
101 | 101 | "\n", |
102 | 102 | "You must have [**Cognitive Services OpenAI Contributor**]( /azure/ai-services/openai/how-to/role-based-access-control#cognitive-services-openai-contributor) or higher to deploy models in Azure OpenAI.\n", |
103 | 103 | "\n", |
|
107 | 107 | "\n", |
108 | 108 | "1. Select **Deploy model** > **Deploy base model**.\n", |
109 | 109 | "\n", |
110 | | - "1. Select **text-embedding-ada-02** from the dropdown list and confirm the selection.\n", |
| 110 | + "1. Select **text-embedding-3-large** from the dropdown list and confirm the selection.\n", |
111 | 111 | "\n", |
112 | | - "1. Specify a deployment name. We recommend \"text-embedding-ada-002\".\n", |
| 112 | + "1. Specify a deployment name. We recommend \"text-embedding-3-large\".\n", |
113 | 113 | "\n", |
114 | 114 | "1. Accept the defaults.\n", |
115 | 115 | "\n", |
116 | 116 | "1. Select **Deploy**.\n", |
117 | 117 | "\n", |
118 | | - "1. Repeat the previous steps for **gpt-35-turbo**.\n", |
| 118 | + "1. Repeat the previous steps for **gpt-4o**.\n", |
119 | 119 | "\n", |
120 | | - "Make a note of the model names and endpoint. Embedding skills and vectorizers assemble the full endpoint internally, so you only need the resource URI. For example, given `https://MY-FAKE-ACCOUNT.openai.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2023-05-15`, the endpoint you should provide in skill and vectorizer definitions is `https://MY-FAKE-ACCOUNT.openai.azure.com`.\n", |
| 120 | + "Make a note of the model names and endpoint. Embedding skills and vectorizers assemble the full endpoint internally, so you only need the resource URI. For example, given `https://MY-FAKE-ACCOUNT.openai.azure.com/openai/deployments/text-embedding-3-large/embeddings?api-version=2024-06-01`, the endpoint you should provide in skill and vectorizer definitions is `https://MY-FAKE-ACCOUNT.openai.azure.com`.\n", |
121 | 121 | "\n", |
122 | 122 | "### Configure search engine role-based access to Azure Storage\n", |
123 | 123 | "\n", |
|
192 | 192 | }, |
193 | 193 | { |
194 | 194 | "cell_type": "code", |
195 | | - "execution_count": 11, |
| 195 | + "execution_count": 1, |
196 | 196 | "metadata": {}, |
197 | 197 | "outputs": [], |
198 | 198 | "source": [ |
|
249 | 249 | " SearchField(name=\"locations\", type=SearchFieldDataType.Collection(SearchFieldDataType.String), filterable=True),\n", |
250 | 250 | " SearchField(name=\"chunk_id\", type=SearchFieldDataType.String, key=True, sortable=True, filterable=True, facetable=True, analyzer_name=\"keyword\"), \n", |
251 | 251 | " SearchField(name=\"chunk\", type=SearchFieldDataType.String, sortable=False, filterable=False, facetable=False), \n", |
252 | | - " SearchField(name=\"text_vector\", type=SearchFieldDataType.Collection(SearchFieldDataType.Single), vector_search_dimensions=1536, vector_search_profile_name=\"myHnswProfile\")\n", |
| 252 | + " SearchField(name=\"text_vector\", type=SearchFieldDataType.Collection(SearchFieldDataType.Single), vector_search_dimensions=1024, vector_search_profile_name=\"myHnswProfile\")\n", |
253 | 253 | " ] \n", |
254 | 254 | " \n", |
255 | 255 | "# Configure the vector search configuration \n", |
|
270 | 270 | " kind=\"azureOpenAI\", \n", |
271 | 271 | " parameters=AzureOpenAIVectorizerParameters( \n", |
272 | 272 | " resource_url=AZURE_OPENAI_ACCOUNT, \n", |
273 | | - " deployment_name=\"text-embedding-ada-002\",\n", |
274 | | - " model_name=\"text-embedding-ada-002\"\n", |
| 273 | + " deployment_name=\"text-embedding-3-large\",\n", |
| 274 | + " model_name=\"text-embedding-3-large\"\n", |
275 | 275 | " ),\n", |
276 | 276 | " ), \n", |
277 | 277 | " ], \n", |
|
294 | 294 | }, |
295 | 295 | { |
296 | 296 | "cell_type": "code", |
297 | | - "execution_count": null, |
| 297 | + "execution_count": 4, |
298 | 298 | "metadata": {}, |
299 | | - "outputs": [], |
| 299 | + "outputs": [ |
| 300 | + { |
| 301 | + "name": "stdout", |
| 302 | + "output_type": "stream", |
| 303 | + "text": [ |
| 304 | + "Data source 'py-rag-tutorial-ds' created or updated\n" |
| 305 | + ] |
| 306 | + } |
| 307 | + ], |
300 | 308 | "source": [ |
301 | 309 | "from azure.search.documents.indexes import SearchIndexerClient\n", |
302 | 310 | "from azure.search.documents.indexes.models import (\n", |
|
306 | 314 | "\n", |
307 | 315 | "# Create a data source \n", |
308 | 316 | "indexer_client = SearchIndexerClient(endpoint=AZURE_SEARCH_SERVICE, credential=credential)\n", |
309 | | - "container = SearchIndexerDataContainer(name=\"nasa-ebooks-pdfs-all\")\n", |
| 317 | + "container = SearchIndexerDataContainer(name=\"nasatext\")\n", |
310 | 318 | "data_source_connection = SearchIndexerDataSourceConnection(\n", |
311 | 319 | " name=\"py-rag-tutorial-ds\",\n", |
312 | 320 | " type=\"azureblob\",\n", |
|
329 | 337 | }, |
330 | 338 | { |
331 | 339 | "cell_type": "code", |
332 | | - "execution_count": null, |
| 340 | + "execution_count": 6, |
333 | 341 | "metadata": {}, |
334 | | - "outputs": [], |
| 342 | + "outputs": [ |
| 343 | + { |
| 344 | + "name": "stdout", |
| 345 | + "output_type": "stream", |
| 346 | + "text": [ |
| 347 | + "py-rag-tutorial-ss created\n" |
| 348 | + ] |
| 349 | + } |
| 350 | + ], |
335 | 351 | "source": [ |
336 | 352 | "from azure.search.documents.indexes.models import (\n", |
337 | 353 | " SplitSkill,\n", |
|
368 | 384 | " description=\"Skill to generate embeddings via Azure OpenAI\", \n", |
369 | 385 | " context=\"/document/pages/*\", \n", |
370 | 386 | " resource_url=AZURE_OPENAI_ACCOUNT, \n", |
371 | | - " deployment_name=\"text-embedding-ada-002\", \n", |
372 | | - " model_name=\"text-embedding-ada-002\",\n", |
373 | | - " dimensions=1536,\n", |
| 387 | + " deployment_name=\"text-embedding-3-large\", \n", |
| 388 | + " model_name=\"text-embedding-3-large\",\n", |
| 389 | + " dimensions=1024,\n", |
374 | 390 | " inputs=[ \n", |
375 | 391 | " InputFieldMappingEntry(name=\"text\", source=\"/document/pages/*\"), \n", |
376 | 392 | " ], \n", |
|
439 | 455 | }, |
440 | 456 | { |
441 | 457 | "cell_type": "code", |
442 | | - "execution_count": null, |
| 458 | + "execution_count": 7, |
443 | 459 | "metadata": {}, |
444 | | - "outputs": [], |
| 460 | + "outputs": [ |
| 461 | + { |
| 462 | + "name": "stdout", |
| 463 | + "output_type": "stream", |
| 464 | + "text": [ |
| 465 | + " py-rag-tutorial-idxr is created and running. Give the indexer a few minutes before running a query.\n" |
| 466 | + ] |
| 467 | + } |
| 468 | + ], |
445 | 469 | "source": [ |
446 | 470 | "from azure.search.documents.indexes.models import (\n", |
447 | | - " SearchIndexer,\n", |
448 | | - " FieldMapping\n", |
| 471 | + " SearchIndexer\n", |
449 | 472 | ")\n", |
450 | 473 | "\n", |
451 | 474 | "# Create an indexer \n", |
|
459 | 482 | " skillset_name=skillset_name, \n", |
460 | 483 | " target_index_name=index_name, \n", |
461 | 484 | " data_source_name=data_source.name,\n", |
462 | | - " # Map the metadata_storage_name field to the title field in the index to display the PDF title in the search results \n", |
463 | | - " field_mappings=[FieldMapping(source_field_name=\"metadata_storage_name\", target_field_name=\"title\")],\n", |
464 | 485 | " parameters=indexer_parameters\n", |
465 | 486 | ") \n", |
466 | 487 | "\n", |
|
482 | 503 | }, |
483 | 504 | { |
484 | 505 | "cell_type": "code", |
485 | | - "execution_count": null, |
| 506 | + "execution_count": 34, |
486 | 507 | "metadata": {}, |
487 | | - "outputs": [], |
| 508 | + "outputs": [ |
| 509 | + { |
| 510 | + "name": "stdout", |
| 511 | + "output_type": "stream", |
| 512 | + "text": [ |
| 513 | + "Score: 0.01666666753590107\n", |
| 514 | + "Chunk: national Aeronautics and Space Administration\n", |
| 515 | + "\n", |
| 516 | + "earth Science\n", |
| 517 | + "\n", |
| 518 | + "NASA Headquarters \n", |
| 519 | + "\n", |
| 520 | + "300 E Street SW \n", |
| 521 | + "\n", |
| 522 | + "Washington, DC 20546\n", |
| 523 | + "\n", |
| 524 | + "www.nasa.gov\n", |
| 525 | + "\n", |
| 526 | + "np-2018-05-2546-hQ\n" |
| 527 | + ] |
| 528 | + } |
| 529 | + ], |
488 | 530 | "source": [ |
489 | 531 | "from azure.search.documents import SearchClient\n", |
490 | 532 | "from azure.search.documents.models import VectorizableTextQuery\n", |
491 | 533 | "\n", |
492 | 534 | "# Vector Search using text-to-vector conversion of the querystring\n", |
493 | | - "query = \"where are NASA's headquarters located?\" \n", |
| 535 | + "query = \"what's NASA's website?\" \n", |
494 | 536 | "\n", |
495 | 537 | "search_client = SearchClient(endpoint=AZURE_SEARCH_SERVICE, credential=credential, index_name=index_name)\n", |
496 | | - "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=1, fields=\"text_vector\", exhaustive=True)\n", |
| 538 | + "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"text_vector\")\n", |
497 | 539 | " \n", |
498 | 540 | "results = search_client.search( \n", |
499 | 541 | " search_text=query, \n", |
500 | 542 | " vector_queries= [vector_query],\n", |
501 | | - " select=[\"parent_id\", \"chunk_id\", \"title\", \"chunk\", \"locations\"],\n", |
| 543 | + " select=[\"chunk\"],\n", |
502 | 544 | " top=1\n", |
503 | 545 | ") \n", |
504 | 546 | " \n", |
505 | 547 | "for result in results: \n", |
506 | 548 | " print(f\"Score: {result['@search.score']}\")\n", |
507 | | - " print(f\"Title: {result['title']}\")\n", |
508 | | - " print(f\"Locations: {result['locations']}\")\n", |
509 | | - " print(f\"Content: {result['chunk']}\")" |
| 549 | + " print(f\"Chunk: {result['chunk']}\")" |
510 | 550 | ] |
511 | 551 | }, |
512 | 552 | { |
|
522 | 562 | }, |
523 | 563 | { |
524 | 564 | "cell_type": "code", |
525 | | - "execution_count": null, |
| 565 | + "execution_count": 43, |
526 | 566 | "metadata": {}, |
527 | 567 | "outputs": [], |
528 | 568 | "source": [ |
|
537 | 577 | " azure_ad_token_provider=token_provider\n", |
538 | 578 | " )\n", |
539 | 579 | "\n", |
540 | | - "deployment_name = \"gpt-35-turbo\"\n", |
| 580 | + "deployment_name = \"gpt-4o\"\n", |
541 | 581 | "\n", |
542 | 582 | "search_client = SearchClient(\n", |
543 | 583 | " endpoint=AZURE_SEARCH_SERVICE,\n", |
|
551 | 591 | "Answer the query using only the sources provided below.\n", |
552 | 592 | "Use bullets if the answer has multiple points.\n", |
553 | 593 | "If the answer is longer than 3 sentences, provide a summary.\n", |
554 | | - "Answer ONLY with the facts listed in the list of sources below.\n", |
| 594 | + "Answer ONLY with the facts listed in the list of sources below. Cite your source when you answer the question\n", |
555 | 595 | "If there isn't enough information below, say you don't know.\n", |
556 | 596 | "Do not generate answers that don't use the sources below.\n", |
557 | 597 | "Query: {query}\n", |
558 | 598 | "Sources:\\n{sources}\n", |
559 | | - "\"\"\"\n", |
560 | | - "\n", |
| 599 | + "\"\"\"" |
| 600 | + ] |
| 601 | + }, |
| 602 | + { |
| 603 | + "cell_type": "code", |
| 604 | + "execution_count": 44, |
| 605 | + "metadata": {}, |
| 606 | + "outputs": [], |
| 607 | + "source": [ |
561 | 608 | "# Provide the query. Notice it's sent to both the search engine and the LLM.\n", |
562 | 609 | "# The query sent to the search engine is hybrid. Keyword search on \"query\". Text-to-vector conversion for vector search.\n", |
563 | | - "query=\"how much of earth is covered by water\"\n", |
564 | | - "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=1, fields=\"text_vector\", exhaustive=True)\n", |
| 610 | + "query=\"What's the NASA earth book about?\"\n", |
| 611 | + "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"text_vector\")\n", |
565 | 612 | "\n", |
566 | 613 | "# Set up the search results and the chat thread.\n", |
567 | 614 | "# Retrieve the selected fields from the search index related to the question.\n", |
568 | 615 | "search_results = search_client.search(\n", |
569 | 616 | " search_text=query,\n", |
570 | 617 | " vector_queries= [vector_query],\n", |
571 | | - " select=\"title, chunk, locations\",\n", |
572 | | - " top=1,\n", |
| 618 | + " select=[\"title\", \"chunk\", \"locations\"],\n", |
| 619 | + " top=5,\n", |
573 | 620 | ")\n", |
574 | | - "sources_formatted = \"\\n\".join([f'{document[\"title\"]}:{document[\"chunk\"]}:{document[\"locations\"]}' for document in search_results])\n", |
575 | 621 | "\n", |
| 622 | + "# Newlines could be in the OCR'd content. Use a unique separator to make the sources distinct\n", |
| 623 | + "sources_formatted = \"=================\\n\".join([f'TITLE: {document[\"title\"]}, CONTENT: {document[\"chunk\"]}, LOCATIONS: {document[\"locations\"]}' for document in search_results])\n" |
| 624 | + ] |
| 625 | + }, |
| 626 | + { |
| 627 | + "cell_type": "code", |
| 628 | + "execution_count": 45, |
| 629 | + "metadata": {}, |
| 630 | + "outputs": [ |
| 631 | + { |
| 632 | + "name": "stdout", |
| 633 | + "output_type": "stream", |
| 634 | + "text": [ |
| 635 | + "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", |
| 636 | + "\n", |
| 637 | + "- It aims to inspire by presenting a 4.5-billion-year-old planet through striking images.\n", |
| 638 | + "- The book highlights how light is observed and studied, reflecting NASA’s scientific pursuits and artistic sensibilities.\n", |
| 639 | + "- It emphasizes the awe-inspiring beauty of Earth, which NASA captures from space.\n", |
| 640 | + "\n", |
| 641 | + "(Source: page-8.pdf)\n" |
| 642 | + ] |
| 643 | + } |
| 644 | + ], |
| 645 | + "source": [ |
576 | 646 | "response = openai_client.chat.completions.create(\n", |
577 | 647 | " messages=[\n", |
578 | 648 | " {\n", |
|
0 commit comments