From 5f55021262af43cf1a56f154788ff4fa2bd54441 Mon Sep 17 00:00:00 2001 From: "Rossdan Craig rossdan@lastmileai.dev" <> Date: Thu, 25 Apr 2024 16:21:20 -0400 Subject: [PATCH] RAG Guide: Wrap individually query run into a helper function This was mainly just a personal preference choice, not a big deal if you don't like it, no worries if you don't want to approve ## Test Plan No functional changes, notebook still works --- ...h_Chat_Embed_and_Rerank_via_Pinecone.ipynb | 294 ++++++------------ 1 file changed, 92 insertions(+), 202 deletions(-) diff --git a/notebooks/guides/RAG_with_Chat_Embed_and_Rerank_via_Pinecone.ipynb b/notebooks/guides/RAG_with_Chat_Embed_and_Rerank_via_Pinecone.ipynb index 61b4aacc..5bc4ce7d 100644 --- a/notebooks/guides/RAG_with_Chat_Embed_and_Rerank_via_Pinecone.ipynb +++ b/notebooks/guides/RAG_with_Chat_Embed_and_Rerank_via_Pinecone.ipynb @@ -475,7 +475,7 @@ " 'url': 'https://docs.cohere.com/docs/transformer-models'}]" ] }, - "execution_count": 115, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -510,7 +510,89 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 9, + "id": "7542bc9c", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a helper function to implement the chatbot\n", + "\n", + "def run_single_query(conversation_id: str, vectorstore: Vectorstore) -> bool:\n", + " \"\"\"\n", + " Return true if we should exit the Chatbot.run() loop\n", + " \"\"\"\n", + " # Get the user message\n", + " message = input(\"User: \")\n", + "\n", + " # Typing \"quit\" ends the conversation\n", + " if message.lower() == \"quit\":\n", + " print(\"Ending chat.\")\n", + " return True\n", + " # else: # Uncomment for Google Colab to avoid printing the same thing twice\n", + " # print(f\"User: {message}\") # Uncomment for Google Colab to avoid printing the same thing twice\n", + "\n", + " # Generate search queries (if any)\n", + " response = co.chat(message=message,\n", + " model=\"command-r\",\n", + " search_queries_only=True)\n", + "\n", + " # If there are search queries, retrieve document chunks and respond\n", + " if response.search_queries:\n", + " print(\"Retrieving information...\", end=\"\")\n", + "\n", + " # Retrieve document chunks for each query\n", + " documents = []\n", + " for query in response.search_queries:\n", + " documents.extend(vectorstore.retrieve(query.text))\n", + "\n", + " # Use document chunks to respond\n", + " response = co.chat_stream(\n", + " message=message,\n", + " model=\"command-r\",\n", + " documents=documents,\n", + " conversation_id=conversation_id,\n", + " )\n", + "\n", + " # If there is no search query, directly respond\n", + " else:\n", + " response = co.chat_stream(\n", + " message=message,\n", + " model=\"command-r\",\n", + " conversation_id=conversation_id,\n", + " )\n", + "\n", + " # Print the chatbot response, citations, and documents\n", + " print(\"\\nChatbot:\")\n", + " citations = []\n", + " cited_documents = []\n", + "\n", + " # Display response\n", + " for event in response:\n", + " if event.event_type == \"text-generation\":\n", + " print(event.text, end=\"\")\n", + " elif event.event_type == \"citation-generation\":\n", + " citations.extend(event.citations)\n", + " elif event.event_type == \"search-results\":\n", + " cited_documents = event.documents\n", + "\n", + " # Display citations and source documents\n", + " if citations:\n", + " print(\"\\n\\nCITATIONS:\")\n", + " for citation in citations:\n", + " print(citation)\n", + "\n", + " print(\"\\nDOCUMENTS:\")\n", + " for document in cited_documents:\n", + " print(document)\n", + "\n", + " print(f\"\\n{'-'*100}\\n\")\n", + " return False\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, "id": "d2c15a1f", "metadata": { "colab": { @@ -520,44 +602,7 @@ "id": "d2c15a1f", "outputId": "8daa9159-338c-45ec-e9ed-830aedcdf0d8" }, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "\n", - " \n", - " " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "class Chatbot:\n", " def __init__(self, vectorstore: Vectorstore):\n", @@ -577,71 +622,9 @@ "\n", " \"\"\"\n", " while True:\n", - " # Get the user message\n", - " message = input(\"User: \")\n", - "\n", - " # Typing \"quit\" ends the conversation\n", - " if message.lower() == \"quit\":\n", - " print(\"Ending chat.\")\n", - " break\n", - " # else: # Uncomment for Google Colab to avoid printing the same thing twice\n", - " # print(f\"User: {message}\") # Uncomment for Google Colab to avoid printing the same thing twice\n", - "\n", - " # Generate search queries (if any)\n", - " response = co.chat(message=message,\n", - " model=\"command-r\",\n", - " search_queries_only=True)\n", - "\n", - " # If there are search queries, retrieve document chunks and respond\n", - " if response.search_queries:\n", - " print(\"Retrieving information...\", end=\"\")\n", - "\n", - " # Retrieve document chunks for each query\n", - " documents = []\n", - " for query in response.search_queries:\n", - " documents.extend(self.vectorstore.retrieve(query.text))\n", - "\n", - " # Use document chunks to respond\n", - " response = co.chat_stream(\n", - " message=message,\n", - " model=\"command-r\",\n", - " documents=documents,\n", - " conversation_id=self.conversation_id,\n", - " )\n", - "\n", - " # If there is no search query, directly respond\n", - " else:\n", - " response = co.chat_stream(\n", - " message=message,\n", - " model=\"command-r\",\n", - " conversation_id=self.conversation_id,\n", - " )\n", - "\n", - " # Print the chatbot response, citations, and documents\n", - " print(\"\\nChatbot:\")\n", - " citations = []\n", - " cited_documents = []\n", - "\n", - " # Display response\n", - " for event in response:\n", - " if event.event_type == \"text-generation\":\n", - " print(event.text, end=\"\")\n", - " elif event.event_type == \"citation-generation\":\n", - " citations.extend(event.citations)\n", - " elif event.event_type == \"search-results\":\n", - " cited_documents = event.documents\n", - "\n", - " # Display citations and source documents\n", - " if citations:\n", - " print(\"\\n\\nCITATIONS:\")\n", - " for citation in citations:\n", - " print(citation)\n", - "\n", - " print(\"\\nDOCUMENTS:\")\n", - " for document in cited_documents:\n", - " print(document)\n", - "\n", - " print(f\"\\n{'-'*100}\\n\")" + " should_exit = run_single_query(self.conversation_id, self.vectorstore)\n", + " if should_exit:\n", + " break" ] }, { @@ -672,7 +655,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 11, "id": "42d3f345", "metadata": { "colab": { @@ -683,98 +666,15 @@ "outputId": "8b935c8b-b1d4-4913-bdf8-73ba503402b8" }, "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "\n", - " \n", - " " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, { "name": "stdout", "output_type": "stream", "text": [ - "\n", - "Chatbot:\n", - "Hello! What's your question? I'm here to help you in any way I can.\n", - "----------------------------------------------------------------------------------------------------\n", - "\n", "Retrieving information...\n", "Chatbot:\n", - "Word embeddings associate words with lists of numbers, so that similar words are close to each other and dissimilar words are further away.\n", - "Sentence embeddings do the same thing, but for sentences. Each sentence is associated with a vector of numbers in a coherent way, so that similar sentences are assigned similar vectors, and different sentences are given different vectors.\n", - "\n", - "CITATIONS:\n", - "start=0 end=15 text='Word embeddings' document_ids=['doc_0']\n", - "start=16 end=53 text='associate words with lists of numbers' document_ids=['doc_0']\n", - "start=63 end=100 text='similar words are close to each other' document_ids=['doc_0']\n", - "start=105 end=139 text='dissimilar words are further away.' document_ids=['doc_0']\n", - "start=140 end=159 text='Sentence embeddings' document_ids=['doc_0', 'doc_2']\n", - "start=160 end=177 text='do the same thing' document_ids=['doc_0', 'doc_2']\n", - "start=198 end=211 text='Each sentence' document_ids=['doc_0', 'doc_2']\n", - "start=215 end=250 text='associated with a vector of numbers' document_ids=['doc_0', 'doc_2']\n", - "start=256 end=264 text='coherent' document_ids=['doc_2']\n", - "start=278 end=295 text='similar sentences' document_ids=['doc_0', 'doc_2']\n", - "start=300 end=324 text='assigned similar vectors' document_ids=['doc_0', 'doc_2']\n", - "start=330 end=349 text='different sentences' document_ids=['doc_0', 'doc_2']\n", - "start=354 end=378 text='given different vectors.' document_ids=['doc_0', 'doc_2']\n", - "\n", - "DOCUMENTS:\n", - "{'id': 'doc_0', 'text': 'In the previous chapters, you learned about word and sentence embeddings and similarity between words and sentences. In short, a word embedding is a way to associate words with lists of numbers (vectors) in such a way that similar words are associated with numbers that are close by, and dissimilar words with numbers that are far away from each other. A sentence embedding does the same thing, but associating a vector to every sentence. Similarity is a way to measure how similar two words (or', 'title': 'The Attention Mechanism', 'url': 'https://docs.cohere.com/docs/the-attention-mechanism'}\n", - "{'id': 'doc_1', 'text': 'Sentence embeddings\\n\\nSo word embeddings seem to be pretty useful, but in reality, human language is much more complicated than simply a bunch of words put together. Human language has structure, sentences, etc. How would one be able to represent, for instance, a sentence? Well, here’s an idea. How about the sums of scores of all the words? For example, say we have a word embedding that assigns the following scores to these words:\\n\\nNo: [1,0,0,0]\\n\\nI: [0,2,0,0]\\n\\nAm: [-1,0,1,0]\\n\\nGood: [0,0,1,3]', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n", - "{'id': 'doc_2', 'text': 'This is where sentence embeddings come into play. A sentence embedding is just like a word embedding, except it associates every sentence with a vector full of numbers, in a coherent way. By coherent, I mean that it satisfies similar properties as a word embedding. For instance, similar sentences are assigned to similar vectors, different sentences are assigned to different vectors, and most importantly, each of the coordinates of the vector identifies some (whether clear or obscure) property of', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n", - "\n", - "----------------------------------------------------------------------------------------------------\n", - "\n", - "Retrieving information...\n", - "Chatbot:\n", - "The similarities between words and sentences are both quantitative measures of how close the two given items are. There are two types of similarities that can be defined: dot product similarity, and cosine similarity. These methods can determine how similar two words, or sentences, are.\n", - "\n", - "CITATIONS:\n", - "start=54 end=75 text='quantitative measures' document_ids=['doc_0']\n", - "start=79 end=88 text='how close' document_ids=['doc_0']\n", - "start=124 end=133 text='two types' document_ids=['doc_0', 'doc_4']\n", - "start=171 end=193 text='dot product similarity' document_ids=['doc_0', 'doc_4']\n", - "start=199 end=217 text='cosine similarity.' document_ids=['doc_0', 'doc_4']\n", - "start=236 end=257 text='determine how similar' document_ids=['doc_0', 'doc_4']\n", - "\n", - "DOCUMENTS:\n", - "{'id': 'doc_0', 'text': 'Now that we know embeddings quite well, let’s move on to using them to find similarities. There are two types of similarities we’ll define in this post: dot product similarity and cosine similarity. Both are very similar and very useful to determine if two words (or sentences) are similar.', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n", - "{'id': 'doc_1', 'text': 'But let me add some numbers to this reasoning to make it more clear. Imagine that we calculate similarities for the words in each sentence, and we get the following:\\n\\nThis similarity makes sense in the following ways:\\n\\nThe similarity between each word and itself is 1.\\n\\nThe similarity between any irrelevant word (“the”, “of”, etc.) and any other word is 0.\\n\\nThe similarity between “bank” and “river” is 0.11.\\n\\nThe similarity between “bank” and “money” is 0.25.', 'title': 'The Attention Mechanism', 'url': 'https://docs.cohere.com/docs/the-attention-mechanism'}\n", - "{'id': 'doc_2', 'text': 'And the results are:\\n\\nThe similarity between sentences 1 and 2: 6738.2858668486715\\n\\nThe similarity between sentences 1 and 3: -122.22666955510499\\n\\nThe similarity between sentences 2 and 3: -3.494608113647928\\n\\nThese results certainly confirm our predictions. The similarity between sentences 1 and 2 is 6738, which is high. The similarities between sentences 1 and 3, and 2 and 3, are -122 and -3.5 (dot products are allowed to be negative too!), which are much lower.', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n", - "{'id': 'doc_3', 'text': 'But let me add some numbers to this reasoning to make it more clear. Imagine that we calculate similarities for the words in each sentence, and we get the following:\\n\\nThis similarity makes sense in the following ways:\\n\\nThe similarity between each word and itself is 1.\\n\\nThe similarity between any irrelevant word (“the”, “of”, etc.) and any other word is 0.\\n\\nThe similarity between “bank” and “river” is 0.11.\\n\\nThe similarity between “bank” and “money” is 0.25.', 'title': 'The Attention Mechanism', 'url': 'https://docs.cohere.com/docs/the-attention-mechanism'}\n", - "{'id': 'doc_4', 'text': 'Now that we know embeddings quite well, let’s move on to using them to find similarities. There are two types of similarities we’ll define in this post: dot product similarity and cosine similarity. Both are very similar and very useful to determine if two words (or sentences) are similar.', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n", - "{'id': 'doc_5', 'text': 'And the results are:\\n\\nThe similarity between sentences 1 and 2: 6738.2858668486715\\n\\nThe similarity between sentences 1 and 3: -122.22666955510499\\n\\nThe similarity between sentences 2 and 3: -3.494608113647928\\n\\nThese results certainly confirm our predictions. The similarity between sentences 1 and 2 is 6738, which is high. The similarities between sentences 1 and 3, and 2 and 3, are -122 and -3.5 (dot products are allowed to be negative too!), which are much lower.', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n", + "The debate of Lebron James or Michael Jordan being better at basketball has been a hot topic for many years. Both players have had an incredibly successful career and are considered legends in the sport. Lebron has the edge over Michael Jordan in terms of statistics, as he has a higher career points per game average (25.8 to 23.5), more career assists (7th all time compared to Jordan who is 89th) and a higher career rebounding average. In addition to this, Lebron's teams have won more games during the regular season than Jordan's did, and he has a higher career field goal percentage. However, Michael Jordan has a better record in the post-season, which is generally seen as a bigger achievement. He also has a higher career steals per game rate and has an overall better points per game average in the playoffs (28.8 to 26.9). \n", "\n", + "The argument of who is better will always be subjective and hard to settle as it is down to personal preference, but both players are considered to have dominated the sport during their respective eras.\n", "----------------------------------------------------------------------------------------------------\n", "\n", "Ending chat.\n" @@ -788,16 +688,6 @@ "# Run the chatbot\n", "chatbot.run()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "-JBWZVz9ObcV", - "metadata": { - "id": "-JBWZVz9ObcV" - }, - "outputs": [], - "source": [] } ], "metadata": { @@ -819,7 +709,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.6" } }, "nbformat": 4,