Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit e471d91

Browse files
authored
Merge pull request #111 from cohere-ai/misc-updates
Update Chat & RAG Part 3
2 parents 66a3772 + 98f0be1 commit e471d91

File tree

1 file changed

+72
-61
lines changed

1 file changed

+72
-61
lines changed

notebooks/RAG_Chatbot_with_Chat_Embed_Rerank.ipynb

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,16 @@
7474
"outputs": [],
7575
"source": [
7676
"import cohere\n",
77-
"import os\n",
7877
"import hnswlib\n",
7978
"import json\n",
8079
"import uuid\n",
8180
"from typing import List, Dict\n",
8281
"from unstructured.partition.html import partition_html\n",
8382
"from unstructured.chunking.title import chunk_by_title\n",
8483
"\n",
85-
"co = cohere.Client(os.environ[\"COHERE_API_KEY\"])"
84+
"# co = cohere.Client(\"COHERE_API_KEY\")\n",
85+
"import os\n",
86+
"co = cohere.Client(os.getenv(\"COHERE_API_KEY\"))"
8687
]
8788
},
8889
{
@@ -274,7 +275,7 @@
274275
},
275276
{
276277
"cell_type": "code",
277-
"execution_count": 4,
278+
"execution_count": 19,
278279
"metadata": {},
279280
"outputs": [
280281
{
@@ -336,24 +337,29 @@
336337
" response = co.chat(message=message, search_queries_only=True)\n",
337338
"\n",
338339
" # If there are search queries, retrieve documents and respond\n",
340+
" # preamble_override = \"You only answer questions using on the documents you have provided with\"\n",
341+
" \n",
339342
" if response.search_queries:\n",
340343
" print(\"Retrieving information...\")\n",
341344
"\n",
342345
" documents = self.retrieve_docs(response)\n",
343346
"\n",
344347
" response = co.chat(\n",
345348
" message=message,\n",
349+
" # preamble_override = preamble_override,\n",
346350
" documents=documents,\n",
347351
" conversation_id=self.conversation_id,\n",
348352
" stream=True,\n",
349353
" )\n",
350354
" for event in response:\n",
351355
" yield event\n",
356+
" yield response\n",
352357
"\n",
353358
" # If there is no search query, directly respond\n",
354359
" else:\n",
355360
" response = co.chat(\n",
356-
" message=message, \n",
361+
" message=message,\n",
362+
" # preamble_override = preamble_override,\n",
357363
" conversation_id=self.conversation_id, \n",
358364
" stream=True\n",
359365
" )\n",
@@ -381,12 +387,6 @@
381387
" for query in queries:\n",
382388
" retrieved_docs.extend(self.docs.retrieve(query))\n",
383389
"\n",
384-
" # # Uncomment this code block to display the chatbot's retrieved documents\n",
385-
" # print(\"DOCUMENTS RETRIEVED:\")\n",
386-
" # for idx, doc in enumerate(retrieved_docs):\n",
387-
" # print(f\"doc_{idx}: {doc}\")\n",
388-
" # print(\"\\n\")\n",
389-
"\n",
390390
" return retrieved_docs"
391391
]
392392
},
@@ -400,7 +400,7 @@
400400
},
401401
{
402402
"cell_type": "code",
403-
"execution_count": 5,
403+
"execution_count": 13,
404404
"metadata": {},
405405
"outputs": [
406406
{
@@ -455,18 +455,29 @@
455455
"\n",
456456
" # Print the chatbot response\n",
457457
" print(\"Chatbot:\")\n",
458-
" flag = False\n",
458+
" \n",
459+
" citations_flag = False\n",
460+
" \n",
459461
" for event in response:\n",
460-
" # Text\n",
461-
" if event.event_type == \"text-generation\":\n",
462+
" stream_type = type(event).__name__\n",
463+
" # Text\n",
464+
" if stream_type == \"StreamTextGeneration\":\n",
462465
" print(event.text, end=\"\")\n",
463466
"\n",
464467
" # Citations\n",
465-
" if event.event_type == \"citation-generation\":\n",
466-
" if not flag:\n",
468+
" if stream_type == \"StreamCitationGeneration\":\n",
469+
" if not citations_flag:\n",
467470
" print(\"\\n\\nCITATIONS:\")\n",
468-
" flag = True\n",
469-
" print(event.citations)\n",
471+
" citations_flag = True\n",
472+
" print(event.citations[0])\n",
473+
" \n",
474+
" if citations_flag:\n",
475+
" if stream_type == \"StreamingChat\":\n",
476+
" print(\"\\n\\nDOCUMENTS:\")\n",
477+
" documents = [{'id': doc['id'], 'text': doc['text'][:50] + '...', 'title': doc['title'], 'url': doc['url']} \n",
478+
" for doc in event.documents]\n",
479+
" for doc in documents:\n",
480+
" print(doc)\n",
470481
"\n",
471482
" print(f\"\\n{'-'*100}\\n\")"
472483
]
@@ -534,7 +545,7 @@
534545
},
535546
{
536547
"cell_type": "code",
537-
"execution_count": 9,
548+
"execution_count": 8,
538549
"metadata": {},
539550
"outputs": [
540551
{
@@ -581,7 +592,7 @@
581592
},
582593
{
583594
"cell_type": "code",
584-
"execution_count": 15,
595+
"execution_count": 27,
585596
"metadata": {},
586597
"outputs": [
587598
{
@@ -606,69 +617,69 @@
606617
"name": "stdout",
607618
"output_type": "stream",
608619
"text": [
609-
"User: Hi there\n",
620+
"User: Hello, I have a question\n",
610621
"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",
622+
"Hello there! I'm happy to help with any questions or discussions you have in mind today. Go ahead and ask away, and I'll do my best to provide helpful, informative responses.\n",
612623
"----------------------------------------------------------------------------------------------------\n",
613624
"\n",
614-
"User: What are text embeddings\n",
625+
"User: What’s the difference between word and sentence embeddings\n",
615626
"Chatbot:\n",
616627
"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",
628+
"Word embeddings and sentence embeddings are both responsible for translating human language (words) into computer language (numbers).\n",
618629
"\n",
619-
"Would you like me to go into more detail about any of the information mentioned above?\n",
630+
"Word embeddings associate words with lists of numbers (vectors) in a way that similar words are grouped close together. Sentence embeddings do the same thing but for sentences, where similar sentences are grouped closer together.\n",
620631
"\n",
621632
"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",
633+
"{'start': 0, 'end': 15, 'text': 'Word embeddings', 'document_ids': ['doc_0', 'doc_1', 'doc_2']}\n",
634+
"{'start': 20, 'end': 39, 'text': 'sentence embeddings', 'document_ids': ['doc_0', 'doc_1', 'doc_2']}\n",
635+
"{'start': 65, 'end': 132, 'text': 'translating human language (words) into computer language (numbers)', 'document_ids': ['doc_2']}\n",
636+
"{'start': 135, 'end': 254, 'text': 'Word embeddings associate words with lists of numbers (vectors) in a way that similar words are grouped close together.', 'document_ids': ['doc_0']}\n",
637+
"{'start': 255, 'end': 310, 'text': 'Sentence embeddings do the same thing but for sentences', 'document_ids': ['doc_0', 'doc_1']}\n",
638+
"{'start': 318, 'end': 364, 'text': 'similar sentences are grouped closer together.', 'document_ids': ['doc_1']}\n",
639+
"\n",
640+
"\n",
641+
"DOCUMENTS:\n",
642+
"{'id': 'doc_0', 'text': 'In the previous chapters, you learned about word a...', 'title': 'The Attention Mechanism', 'url': 'https://docs.cohere.com/docs/the-attention-mechanism'}\n",
643+
"{'id': 'doc_1', 'text': 'This is where sentence embeddings come into play. ...', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n",
644+
"{'id': 'doc_2', 'text': 'Conclusion\\n\\nWord and sentence embeddings are the b...', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n",
628645
"\n",
629646
"----------------------------------------------------------------------------------------------------\n",
630647
"\n",
631-
"User: What are they useful for\n",
648+
"User: And what are their similarities\n",
632649
"Chatbot:\n",
633650
"Retrieving information...\n",
634-
"Text embeddings are useful for a variety of tasks, including:\n",
651+
"The similarities between word and sentence embeddings are that they both rely on vector representations of numerical values, and they both allow for calculations of similarity. Word embeddings focus on the similarity between individual words, whereas sentence embeddings compare entire sentences. \n",
635652
"\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",
653+
"The similarities between sentences can be calculated using cosine similarity, which is also the case for word embeddings. The similarity between each sentence and itself is always the highest (around 8000 for cosine similarity), while the similarity between different sentences is much lower.\n",
641654
"\n",
642655
"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",
656+
"{'start': 73, 'end': 123, 'text': 'rely on vector representations of numerical values', 'document_ids': ['doc_2']}\n",
657+
"{'start': 134, 'end': 176, 'text': 'both allow for calculations of similarity.', 'document_ids': ['doc_0', 'doc_1', 'doc_3', 'doc_4', 'doc_5']}\n",
658+
"{'start': 177, 'end': 241, 'text': 'Word embeddings focus on the similarity between individual words', 'document_ids': ['doc_0', 'doc_1', 'doc_3', 'doc_4', 'doc_5']}\n",
659+
"{'start': 251, 'end': 296, 'text': 'sentence embeddings compare entire sentences.', 'document_ids': ['doc_0', 'doc_1', 'doc_3', 'doc_4', 'doc_5']}\n",
660+
"{'start': 303, 'end': 375, 'text': 'similarities between sentences can be calculated using cosine similarity', 'document_ids': ['doc_0', 'doc_1', 'doc_3', 'doc_4', 'doc_5']}\n",
661+
"{'start': 404, 'end': 420, 'text': 'word embeddings.', 'document_ids': ['doc_0', 'doc_1', 'doc_3', 'doc_4', 'doc_5']}\n",
662+
"{'start': 425, 'end': 490, 'text': 'similarity between each sentence and itself is always the highest', 'document_ids': ['doc_5']}\n",
663+
"{'start': 491, 'end': 526, 'text': '(around 8000 for cosine similarity)', 'document_ids': ['doc_1', 'doc_3', 'doc_5']}\n",
664+
"{'start': 538, 'end': 591, 'text': 'similarity between different sentences is much lower.', 'document_ids': ['doc_0', 'doc_1', 'doc_3', 'doc_4']}\n",
665+
"\n",
666+
"\n",
667+
"DOCUMENTS:\n",
668+
"{'id': 'doc_2', 'text': 'One would expect the two first sentences to have a...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
669+
"{'id': 'doc_0', 'text': 'And the results are:\\n\\nThe similarity between sente...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
670+
"{'id': 'doc_1', 'text': 'This checks out as well! The similarity between se...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
671+
"{'id': 'doc_3', 'text': 'This checks out as well! The similarity between se...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
672+
"{'id': 'doc_4', 'text': 'And the results are:\\n\\nThe similarity between sente...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
673+
"{'id': 'doc_5', 'text': 'Just for consistency, let’s calculate the similari...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
654674
"\n",
655675
"----------------------------------------------------------------------------------------------------\n",
656676
"\n",
657-
"User: How do you generate them\n",
677+
"User: What do you know about graph neural networks\n",
658678
"Chatbot:\n",
659679
"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",
680+
"Graph neural networks are a newer concept in the world of neural networks. Using graph neural networks, programmers can develop models that analyze datasets comprised of pairs of entities and edges between those entities. These edges convey relationships between the entities, such as a connection between two entities or a similarity between entities. \n",
671681
"\n",
682+
"To construct graph neural networks, programmers use node embedding, a process that assigns a vector to each node in the graph. The vectors are designed so that their underlying information is preserved when compared to the original graph.\n",
672683
"----------------------------------------------------------------------------------------------------\n",
673684
"\n",
674685
"Ending chat.\n"

0 commit comments

Comments
 (0)