|
69 | 69 | },
|
70 | 70 | {
|
71 | 71 | "cell_type": "code",
|
72 |
| - "execution_count": 1, |
| 72 | + "execution_count": 3, |
73 | 73 | "metadata": {},
|
74 |
| - "outputs": [], |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/html": [ |
| 78 | + "\n", |
| 79 | + " <style>\n", |
| 80 | + " pre {\n", |
| 81 | + " white-space: pre-wrap;\n", |
| 82 | + " }\n", |
| 83 | + " </style>\n", |
| 84 | + " " |
| 85 | + ], |
| 86 | + "text/plain": [ |
| 87 | + "<IPython.core.display.HTML object>" |
| 88 | + ] |
| 89 | + }, |
| 90 | + "metadata": {}, |
| 91 | + "output_type": "display_data" |
| 92 | + } |
| 93 | + ], |
75 | 94 | "source": [
|
76 | 95 | "import cohere\n",
|
77 |
| - "import os\n", |
78 | 96 | "import hnswlib\n",
|
79 | 97 | "import json\n",
|
80 | 98 | "import uuid\n",
|
81 | 99 | "from typing import List, Dict\n",
|
82 | 100 | "from unstructured.partition.html import partition_html\n",
|
83 | 101 | "from unstructured.chunking.title import chunk_by_title\n",
|
84 | 102 | "\n",
|
| 103 | + "import os\n", |
85 | 104 | "co = cohere.Client(os.environ[\"COHERE_API_KEY\"])"
|
86 | 105 | ]
|
87 | 106 | },
|
88 | 107 | {
|
89 | 108 | "cell_type": "code",
|
90 |
| - "execution_count": 2, |
| 109 | + "execution_count": 4, |
91 | 110 | "metadata": {},
|
92 |
| - "outputs": [], |
| 111 | + "outputs": [ |
| 112 | + { |
| 113 | + "data": { |
| 114 | + "text/html": [ |
| 115 | + "\n", |
| 116 | + " <style>\n", |
| 117 | + " pre {\n", |
| 118 | + " white-space: pre-wrap;\n", |
| 119 | + " }\n", |
| 120 | + " </style>\n", |
| 121 | + " " |
| 122 | + ], |
| 123 | + "text/plain": [ |
| 124 | + "<IPython.core.display.HTML object>" |
| 125 | + ] |
| 126 | + }, |
| 127 | + "metadata": {}, |
| 128 | + "output_type": "display_data" |
| 129 | + } |
| 130 | + ], |
93 | 131 | "source": [
|
94 | 132 | "#@title Enable text wrapping in Google colab\n",
|
95 | 133 | "\n",
|
|
116 | 154 | },
|
117 | 155 | {
|
118 | 156 | "cell_type": "code",
|
119 |
| - "execution_count": 3, |
| 157 | + "execution_count": 5, |
120 | 158 | "metadata": {},
|
121 | 159 | "outputs": [
|
| 160 | + { |
| 161 | + "data": { |
| 162 | + "text/html": [ |
| 163 | + "\n", |
| 164 | + " <style>\n", |
| 165 | + " pre {\n", |
| 166 | + " white-space: pre-wrap;\n", |
| 167 | + " }\n", |
| 168 | + " </style>\n", |
| 169 | + " " |
| 170 | + ], |
| 171 | + "text/plain": [ |
| 172 | + "<IPython.core.display.HTML object>" |
| 173 | + ] |
| 174 | + }, |
| 175 | + "metadata": {}, |
| 176 | + "output_type": "display_data" |
| 177 | + }, |
122 | 178 | {
|
123 | 179 | "data": {
|
124 | 180 | "text/html": [
|
|
274 | 330 | },
|
275 | 331 | {
|
276 | 332 | "cell_type": "code",
|
277 |
| - "execution_count": 4, |
| 333 | + "execution_count": 28, |
278 | 334 | "metadata": {},
|
279 | 335 | "outputs": [
|
| 336 | + { |
| 337 | + "data": { |
| 338 | + "text/html": [ |
| 339 | + "\n", |
| 340 | + " <style>\n", |
| 341 | + " pre {\n", |
| 342 | + " white-space: pre-wrap;\n", |
| 343 | + " }\n", |
| 344 | + " </style>\n", |
| 345 | + " " |
| 346 | + ], |
| 347 | + "text/plain": [ |
| 348 | + "<IPython.core.display.HTML object>" |
| 349 | + ] |
| 350 | + }, |
| 351 | + "metadata": {}, |
| 352 | + "output_type": "display_data" |
| 353 | + }, |
280 | 354 | {
|
281 | 355 | "data": {
|
282 | 356 | "text/html": [
|
|
349 | 423 | " )\n",
|
350 | 424 | " for event in response:\n",
|
351 | 425 | " yield event\n",
|
| 426 | + " yield response\n", |
352 | 427 | "\n",
|
353 | 428 | " # If there is no search query, directly respond\n",
|
354 | 429 | " else:\n",
|
|
400 | 475 | },
|
401 | 476 | {
|
402 | 477 | "cell_type": "code",
|
403 |
| - "execution_count": 5, |
| 478 | + "execution_count": 37, |
404 | 479 | "metadata": {},
|
405 | 480 | "outputs": [
|
| 481 | + { |
| 482 | + "data": { |
| 483 | + "text/html": [ |
| 484 | + "\n", |
| 485 | + " <style>\n", |
| 486 | + " pre {\n", |
| 487 | + " white-space: pre-wrap;\n", |
| 488 | + " }\n", |
| 489 | + " </style>\n", |
| 490 | + " " |
| 491 | + ], |
| 492 | + "text/plain": [ |
| 493 | + "<IPython.core.display.HTML object>" |
| 494 | + ] |
| 495 | + }, |
| 496 | + "metadata": {}, |
| 497 | + "output_type": "display_data" |
| 498 | + }, |
406 | 499 | {
|
407 | 500 | "data": {
|
408 | 501 | "text/html": [
|
|
455 | 548 | "\n",
|
456 | 549 | " # Print the chatbot response\n",
|
457 | 550 | " print(\"Chatbot:\")\n",
|
458 |
| - " flag = False\n", |
| 551 | + " \n", |
| 552 | + " citations_flag = False\n", |
| 553 | + " \n", |
459 | 554 | " for event in response:\n",
|
460 |
| - " # Text\n", |
461 |
| - " if event.event_type == \"text-generation\":\n", |
| 555 | + " stream_type = type(event).__name__\n", |
| 556 | + " # Text\n", |
| 557 | + " if stream_type == \"StreamTextGeneration\":\n", |
462 | 558 | " print(event.text, end=\"\")\n",
|
463 | 559 | "\n",
|
464 | 560 | " # Citations\n",
|
465 |
| - " if event.event_type == \"citation-generation\":\n", |
466 |
| - " if not flag:\n", |
| 561 | + " if stream_type == \"StreamCitationGeneration\":\n", |
| 562 | + " if not citations_flag:\n", |
467 | 563 | " print(\"\\n\\nCITATIONS:\")\n",
|
468 |
| - " flag = True\n", |
469 |
| - " print(event.citations)\n", |
| 564 | + " citations_flag = True\n", |
| 565 | + " print(event.citations[0])\n", |
| 566 | + " \n", |
| 567 | + " if citations_flag:\n", |
| 568 | + " if stream_type == \"StreamingChat\":\n", |
| 569 | + " print(\"\\n\\nDOCUMENTS:\")\n", |
| 570 | + " for document in event.documents:\n", |
| 571 | + " print(document)\n", |
470 | 572 | "\n",
|
471 | 573 | " print(f\"\\n{'-'*100}\\n\")"
|
472 | 574 | ]
|
|
481 | 583 | },
|
482 | 584 | {
|
483 | 585 | "cell_type": "code",
|
484 |
| - "execution_count": 6, |
| 586 | + "execution_count": 8, |
485 | 587 | "metadata": {},
|
486 | 588 | "outputs": [
|
| 589 | + { |
| 590 | + "data": { |
| 591 | + "text/html": [ |
| 592 | + "\n", |
| 593 | + " <style>\n", |
| 594 | + " pre {\n", |
| 595 | + " white-space: pre-wrap;\n", |
| 596 | + " }\n", |
| 597 | + " </style>\n", |
| 598 | + " " |
| 599 | + ], |
| 600 | + "text/plain": [ |
| 601 | + "<IPython.core.display.HTML object>" |
| 602 | + ] |
| 603 | + }, |
| 604 | + "metadata": {}, |
| 605 | + "output_type": "display_data" |
| 606 | + }, |
487 | 607 | {
|
488 | 608 | "data": {
|
489 | 609 | "text/html": [
|
|
534 | 654 | },
|
535 | 655 | {
|
536 | 656 | "cell_type": "code",
|
537 |
| - "execution_count": 9, |
| 657 | + "execution_count": 10, |
538 | 658 | "metadata": {},
|
539 | 659 | "outputs": [
|
| 660 | + { |
| 661 | + "data": { |
| 662 | + "text/html": [ |
| 663 | + "\n", |
| 664 | + " <style>\n", |
| 665 | + " pre {\n", |
| 666 | + " white-space: pre-wrap;\n", |
| 667 | + " }\n", |
| 668 | + " </style>\n", |
| 669 | + " " |
| 670 | + ], |
| 671 | + "text/plain": [ |
| 672 | + "<IPython.core.display.HTML object>" |
| 673 | + ] |
| 674 | + }, |
| 675 | + "metadata": {}, |
| 676 | + "output_type": "display_data" |
| 677 | + }, |
540 | 678 | {
|
541 | 679 | "data": {
|
542 | 680 | "text/html": [
|
|
581 | 719 | },
|
582 | 720 | {
|
583 | 721 | "cell_type": "code",
|
584 |
| - "execution_count": 15, |
| 722 | + "execution_count": 38, |
585 | 723 | "metadata": {},
|
586 | 724 | "outputs": [
|
| 725 | + { |
| 726 | + "data": { |
| 727 | + "text/html": [ |
| 728 | + "\n", |
| 729 | + " <style>\n", |
| 730 | + " pre {\n", |
| 731 | + " white-space: pre-wrap;\n", |
| 732 | + " }\n", |
| 733 | + " </style>\n", |
| 734 | + " " |
| 735 | + ], |
| 736 | + "text/plain": [ |
| 737 | + "<IPython.core.display.HTML object>" |
| 738 | + ] |
| 739 | + }, |
| 740 | + "metadata": {}, |
| 741 | + "output_type": "display_data" |
| 742 | + }, |
587 | 743 | {
|
588 | 744 | "data": {
|
589 | 745 | "text/html": [
|
|
606 | 762 | "name": "stdout",
|
607 | 763 | "output_type": "stream",
|
608 | 764 | "text": [
|
609 |
| - "User: Hi there\n", |
610 |
| - "Chatbot:\n", |
611 |
| - "Hi there! I'm Coral, an AI-assistant chatbot trained to assist human users by providing thorough responses. Is there anything I can help you with today?\n", |
612 |
| - "----------------------------------------------------------------------------------------------------\n", |
613 |
| - "\n", |
614 |
| - "User: What are text embeddings\n", |
615 |
| - "Chatbot:\n", |
616 |
| - "Retrieving information...\n", |
617 |
| - "Text embeddings are used to convert words into numbers, creating a vector of numerical data for every piece of text. If the vectors for two pieces of text are similar, this means that the corresponding pieces of text are similar too, and vice versa. Text embeddings are particularly useful for tasks like machine translation and searching for text in different languages. \n", |
618 |
| - "\n", |
619 |
| - "Would you like me to go into more detail about any of the information mentioned above?\n", |
620 |
| - "\n", |
621 |
| - "CITATIONS:\n", |
622 |
| - "[{'start': 28, 'end': 54, 'text': 'convert words into numbers', 'document_ids': ['doc_1']}]\n", |
623 |
| - "[{'start': 67, 'end': 116, 'text': 'vector of numerical data for every piece of text.', 'document_ids': ['doc_1']}]\n", |
624 |
| - "[{'start': 136, 'end': 166, 'text': 'two pieces of text are similar', 'document_ids': ['doc_1']}]\n", |
625 |
| - "[{'start': 188, 'end': 249, 'text': 'corresponding pieces of text are similar too, and vice versa.', 'document_ids': ['doc_1']}]\n", |
626 |
| - "[{'start': 305, 'end': 324, 'text': 'machine translation', 'document_ids': ['doc_2']}]\n", |
627 |
| - "[{'start': 329, 'end': 371, 'text': 'searching for text in different languages.', 'document_ids': ['doc_2']}]\n", |
628 |
| - "\n", |
629 |
| - "----------------------------------------------------------------------------------------------------\n", |
630 |
| - "\n", |
631 |
| - "User: What are they useful for\n", |
| 765 | + "User: embedding\n", |
632 | 766 | "Chatbot:\n",
|
633 | 767 | "Retrieving information...\n",
|
634 |
| - "Text embeddings are useful for a variety of tasks, including:\n", |
635 |
| - "\n", |
636 |
| - "- Machine learning - for example, transformer models can be used to write stories, essays and poems, as well as answer questions and chat with humans.\n", |
637 |
| - "- Machine translation - text embeddings can translate text from one language to another.\n", |
638 |
| - "- Searching for text - text embeddings can be used to find text in any language. \n", |
639 |
| - "\n", |
640 |
| - "Would you like me to go into more detail about any of the tasks text embeddings are useful for?\n", |
| 768 | + "Embedding is a way to locate each sentence in space, in a way that similar sentences are located close by. It associates each sentence with a particular list of numbers (a vector). Word and sentence embeddings are the bread and butter of language models. In Chapter 9 of the Cohere documentation, there is an example of trying to fit a French sentence (\"Bonjour, comment ça va?\") into an embedding and the struggle it will have to understand that it should be close to the English sentence (\"Hello, how are you?\"). Cohere has trained a large multilingual model to unify many languages into one and be able to understand text in all those languages.\n", |
641 | 769 | "\n",
|
642 | 770 | "CITATIONS:\n",
|
643 |
| - "[{'start': 65, 'end': 81, 'text': 'Machine learning', 'document_ids': ['doc_1']}]\n", |
644 |
| - "[{'start': 97, 'end': 115, 'text': 'transformer models', 'document_ids': ['doc_0', 'doc_1']}]\n", |
645 |
| - "[{'start': 131, 'end': 144, 'text': 'write stories', 'document_ids': ['doc_0', 'doc_1']}]\n", |
646 |
| - "[{'start': 146, 'end': 152, 'text': 'essays', 'document_ids': ['doc_1']}]\n", |
647 |
| - "[{'start': 157, 'end': 162, 'text': 'poems', 'document_ids': ['doc_0', 'doc_1']}]\n", |
648 |
| - "[{'start': 175, 'end': 191, 'text': 'answer questions', 'document_ids': ['doc_1']}]\n", |
649 |
| - "[{'start': 196, 'end': 213, 'text': 'chat with humans.', 'document_ids': ['doc_1']}]\n", |
650 |
| - "[{'start': 216, 'end': 235, 'text': 'Machine translation', 'document_ids': ['doc_1']}]\n", |
651 |
| - "[{'start': 258, 'end': 302, 'text': 'translate text from one language to another.', 'document_ids': ['doc_1']}]\n", |
652 |
| - "[{'start': 305, 'end': 323, 'text': 'Searching for text', 'document_ids': ['doc_1']}]\n", |
653 |
| - "[{'start': 370, 'end': 383, 'text': 'any language.', 'document_ids': ['doc_1']}]\n", |
| 771 | + "{'start': 22, 'end': 51, 'text': 'locate each sentence in space', 'document_ids': ['doc_1']}\n", |
| 772 | + "{'start': 67, 'end': 106, 'text': 'similar sentences are located close by.', 'document_ids': ['doc_1']}\n", |
| 773 | + "{'start': 110, 'end': 168, 'text': 'associates each sentence with a particular list of numbers', 'document_ids': ['doc_1']}\n", |
| 774 | + "{'start': 169, 'end': 179, 'text': '(a vector)', 'document_ids': ['doc_1']}\n", |
| 775 | + "{'start': 181, 'end': 254, 'text': 'Word and sentence embeddings are the bread and butter of language models.', 'document_ids': ['doc_2']}\n", |
| 776 | + "{'start': 258, 'end': 295, 'text': 'Chapter 9 of the Cohere documentation', 'document_ids': ['doc_0']}\n", |
| 777 | + "{'start': 320, 'end': 351, 'text': 'trying to fit a French sentence', 'document_ids': ['doc_0']}\n", |
| 778 | + "{'start': 402, 'end': 489, 'text': 'the struggle it will have to understand that it should be close to the English sentence', 'document_ids': ['doc_0']}\n", |
| 779 | + "{'start': 515, 'end': 593, 'text': 'Cohere has trained a large multilingual model to unify many languages into one', 'document_ids': ['doc_0']}\n", |
| 780 | + "{'start': 598, 'end': 648, 'text': 'be able to understand text in all those languages.', 'document_ids': ['doc_0']}\n", |
654 | 781 | "\n",
|
655 |
| - "----------------------------------------------------------------------------------------------------\n", |
656 | 782 | "\n",
|
657 |
| - "User: How do you generate them\n", |
658 |
| - "Chatbot:\n", |
659 |
| - "Retrieving information...\n", |
660 |
| - "Text embeddings are generated by training transformer models on large datasets, such as the entire internet or large datasets of conversations. Post-training helps improve the model's performance on specific tasks by focusing on datasets corresponding to questions and answers or conversations. \n", |
661 |
| - "\n", |
662 |
| - "Would you like me to go into more detail about transformer models?\n", |
663 |
| - "\n", |
664 |
| - "CITATIONS:\n", |
665 |
| - "[{'start': 42, 'end': 60, 'text': 'transformer models', 'document_ids': ['doc_0', 'doc_1', 'doc_2']}]\n", |
666 |
| - "[{'start': 92, 'end': 107, 'text': 'entire internet', 'document_ids': ['doc_0']}]\n", |
667 |
| - "[{'start': 129, 'end': 143, 'text': 'conversations.', 'document_ids': ['doc_1']}]\n", |
668 |
| - "[{'start': 144, 'end': 195, 'text': \"Post-training helps improve the model's performance\", 'document_ids': ['doc_0', 'doc_1']}]\n", |
669 |
| - "[{'start': 255, 'end': 276, 'text': 'questions and answers', 'document_ids': ['doc_0']}]\n", |
670 |
| - "[{'start': 280, 'end': 294, 'text': 'conversations.', 'document_ids': ['doc_1']}]\n", |
| 783 | + "DOCUMENTS:\n", |
| 784 | + "{'id': 'doc_1', 'text': 'In the previous chapter, we learned that sentence embeddings are the bread and butter of language models, as they associate each sentence with a particular list of numbers (a vector), in a way that similar sentences give similar vectors. We can think of embeddings as a way to locate each sentence in space (a high dimensional space, but a space nonetheless), in a way that similar sentences are located close by. Once we have each sentence somewhere in space, it’s natural to think of distances betw', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n", |
| 785 | + "{'id': 'doc_2', 'text': 'Text Embeddings\\n\\nWord and sentence embeddings are the bread and butter of language models. This chapter shows a very simple introduction to what they are.', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n", |
| 786 | + "{'id': 'doc_0', 'text': 'Most word and sentence embeddings are dependent on the language that the model is trained on. If you were to try to fit the French sentence “Bonjour, comment ça va?” (meaning: hello, how are you?) in the embedding from the previous section, it will struggle to understand that it should be close to the sentence “Hello, how are you?” in English. For the purpose of unifying many languages into one, and being able to understand text in all these languages, Cohere has trained a large multilingual mod', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n", |
671 | 787 | "\n",
|
672 | 788 | "----------------------------------------------------------------------------------------------------\n",
|
673 | 789 | "\n",
|
|
683 | 799 | "app = App(chatbot)\n",
|
684 | 800 | "\n",
|
685 | 801 | "# Run the chatbot\n",
|
686 |
| - "app.run()" |
| 802 | + "app.run()\n" |
687 | 803 | ]
|
688 | 804 | },
|
689 | 805 | {
|
|
0 commit comments