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

Commit 0b40861

Browse files
authored
Merge pull request #113 from cohere-ai/chat-rag-upd
Update chat rag notebooks
2 parents 07a6135 + 09c9854 commit 0b40861

File tree

3 files changed

+161
-121
lines changed

3 files changed

+161
-121
lines changed

examples/chat_rag_connector/RAG_Chatbot_with_Connectors.ipynb

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 2,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -11,8 +11,7 @@
1111
"import uuid\n",
1212
"from typing import List, Dict\n",
1313
"\n",
14-
"COHERE_API_KEY = os.getenv(\"COHERE_API_KEY\")\n",
15-
"co = cohere.Client(COHERE_API_KEY)"
14+
"co = cohere.Client(\"COHERE_API_KEY\")"
1615
]
1716
},
1817
{
@@ -79,7 +78,7 @@
7978
},
8079
{
8180
"cell_type": "code",
82-
"execution_count": 10,
81+
"execution_count": 3,
8382
"metadata": {},
8483
"outputs": [],
8584
"source": [
@@ -98,7 +97,8 @@
9897
" )\n",
9998
"\n",
10099
" for event in response:\n",
101-
" yield event"
100+
" yield event\n",
101+
" yield response"
102102
]
103103
},
104104
{
@@ -110,7 +110,7 @@
110110
},
111111
{
112112
"cell_type": "code",
113-
"execution_count": 11,
113+
"execution_count": 4,
114114
"metadata": {},
115115
"outputs": [],
116116
"source": [
@@ -136,31 +136,33 @@
136136
" # Print the chatbot response\n",
137137
" print(\"Chatbot:\")\n",
138138
" \n",
139-
" documents = []\n",
140-
" documents_flag = False\n",
141139
" citations_flag = False\n",
142140
" \n",
143141
" for event in response:\n",
144-
" # Documents\n",
145-
" if event.event_type == \"search-results\":\n",
146-
" documents_flag = True\n",
147-
" documents = event.documents\n",
148-
" \n",
142+
" stream_type = type(event).__name__\n",
143+
" \n",
149144
" # Text\n",
150-
" if event.event_type == \"text-generation\":\n",
151-
" print(event.text, end=\"\") \n",
145+
" if stream_type == \"StreamTextGeneration\":\n",
146+
" print(event.text, end=\"\")\n",
152147
"\n",
153148
" # Citations\n",
154-
" if event.event_type == \"citation-generation\":\n",
149+
" if stream_type == \"StreamCitationGeneration\":\n",
155150
" if not citations_flag:\n",
156151
" print(\"\\n\\nCITATIONS:\")\n",
157152
" citations_flag = True\n",
158-
" print(event.citations)\n",
159-
" \n",
160-
" if documents_flag:\n",
161-
" print(\"\\n\\nDOCUMENTS:\")\n",
162-
" for d in documents:\n",
163-
" print(f'{d[\"title\"]} ({d[\"id\"]}). URL: {d[\"url\"]}')\n",
153+
" print(event.citations[0])\n",
154+
" \n",
155+
" # Documents\n",
156+
" if citations_flag:\n",
157+
" if stream_type == \"StreamingChat\":\n",
158+
" print(\"\\n\\nDOCUMENTS:\")\n",
159+
" documents = [{'id': doc['id'],\n",
160+
" 'text': doc['text'][:50] + '...',\n",
161+
" 'title': doc['title'],\n",
162+
" 'url': doc['url']} \n",
163+
" for doc in event.documents]\n",
164+
" for doc in documents:\n",
165+
" print(doc)\n",
164166
"\n",
165167
" print(f\"\\n{'-'*100}\\n\")"
166168
]
@@ -174,30 +176,47 @@
174176
},
175177
{
176178
"cell_type": "code",
177-
"execution_count": 13,
179+
"execution_count": 10,
178180
"metadata": {},
179181
"outputs": [
180182
{
181183
"name": "stdout",
182184
"output_type": "stream",
183185
"text": [
184-
"User: What is attention\n",
186+
"User: What are sentence embeddings\n",
187+
"Chatbot:\n",
188+
"Sentence embeddings are the building blocks of language models. They associate each sentence with a vector (list of numbers) in a way that similar sentences are assigned similar vectors. These vectors are composed of numbers and carry important properties of the sentence. The embeddings act as a form of translation between languages as well, as they provide a relatable vector for similar sentences in different languages.\n",
189+
"\n",
190+
"CITATIONS:\n",
191+
"{'start': 69, 'end': 124, 'text': 'associate each sentence with a vector (list of numbers)', 'document_ids': ['demo-conn-e5y5ps_0', 'demo-conn-e5y5ps_1', 'demo-conn-e5y5ps_2']}\n",
192+
"{'start': 139, 'end': 186, 'text': 'similar sentences are assigned similar vectors.', 'document_ids': ['demo-conn-e5y5ps_0', 'demo-conn-e5y5ps_1']}\n",
193+
"{'start': 235, 'end': 272, 'text': 'important properties of the sentence.', 'document_ids': ['demo-conn-e5y5ps_1', 'demo-conn-e5y5ps_2']}\n",
194+
"\n",
195+
"\n",
196+
"DOCUMENTS:\n",
197+
"{'id': 'demo-conn-e5y5ps_0', 'text': 'In the previous chapter, we learned that sentence ...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
198+
"{'id': 'demo-conn-e5y5ps_1', 'text': 'This is where sentence embeddings come into play. ...', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n",
199+
"{'id': 'demo-conn-e5y5ps_2', 'text': 'Sentence embeddings are even more powerful, as the...', 'title': 'Similarity Between Words and Sentences', 'url': 'https://docs.cohere.com/docs/similarity-between-words-and-sentences'}\n",
200+
"\n",
201+
"----------------------------------------------------------------------------------------------------\n",
202+
"\n",
203+
"User: How is it different from word embeddings\n",
185204
"Chatbot:\n",
186-
"Attention is a technique used in language models to provide context to each word in a sentence or text, based on the other words. Attention plays a crucial role in transformer models, which can help improve large language models.\n",
205+
"The primary distinction between word embeddings and sentence embeddings is that the latter assigns a vector to every sentence whereas the former does the same thing but for individual words. \n",
206+
"\n",
207+
"Both embeddings are similar in the sense that they associate vectors in a way that similar items (words or sentences) are mapped to similar vectors. Word embeddings are a subset of sentence embeddings.\n",
187208
"\n",
188209
"CITATIONS:\n",
189-
"[{'start': 60, 'end': 67, 'text': 'context', 'document_ids': ['demo-conn-tm17qr_0', 'demo-conn-tm17qr_1', 'demo-conn-tm17qr_2']}]\n",
190-
"[{'start': 68, 'end': 102, 'text': 'to each word in a sentence or text', 'document_ids': ['demo-conn-tm17qr_1', 'demo-conn-tm17qr_2']}]\n",
191-
"[{'start': 117, 'end': 129, 'text': 'other words.', 'document_ids': ['demo-conn-tm17qr_1']}]\n",
192-
"[{'start': 148, 'end': 160, 'text': 'crucial role', 'document_ids': ['demo-conn-tm17qr_2']}]\n",
193-
"[{'start': 164, 'end': 182, 'text': 'transformer models', 'document_ids': ['demo-conn-tm17qr_2']}]\n",
194-
"[{'start': 199, 'end': 229, 'text': 'improve large language models.', 'document_ids': ['demo-conn-tm17qr_2']}]\n",
210+
"{'start': 91, 'end': 125, 'text': 'assigns a vector to every sentence', 'document_ids': ['demo-conn-e5y5ps_0', 'demo-conn-e5y5ps_1']}\n",
211+
"{'start': 165, 'end': 190, 'text': 'but for individual words.', 'document_ids': ['demo-conn-e5y5ps_0']}\n",
212+
"{'start': 244, 'end': 261, 'text': 'associate vectors', 'document_ids': ['demo-conn-e5y5ps_0', 'demo-conn-e5y5ps_1']}\n",
213+
"{'start': 315, 'end': 341, 'text': 'mapped to similar vectors.', 'document_ids': ['demo-conn-e5y5ps_0', 'demo-conn-e5y5ps_1']}\n",
214+
"{'start': 342, 'end': 394, 'text': 'Word embeddings are a subset of sentence embeddings.', 'document_ids': ['demo-conn-e5y5ps_1']}\n",
195215
"\n",
196216
"\n",
197217
"DOCUMENTS:\n",
198-
"Transformer Models (demo-conn-tm17qr_0). URL: https://docs.cohere.com/docs/transformer-models\n",
199-
"Transformer Models (demo-conn-tm17qr_1). URL: https://docs.cohere.com/docs/transformer-models\n",
200-
"Transformer Models (demo-conn-tm17qr_2). URL: https://docs.cohere.com/docs/transformer-models\n",
218+
"{'id': 'demo-conn-e5y5ps_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",
219+
"{'id': 'demo-conn-e5y5ps_1', 'text': 'This is where sentence embeddings come into play. ...', 'title': 'Text Embeddings', 'url': 'https://docs.cohere.com/docs/text-embeddings'}\n",
201220
"\n",
202221
"----------------------------------------------------------------------------------------------------\n",
203222
"\n",
@@ -207,7 +226,7 @@
207226
],
208227
"source": [
209228
"# Define connectors\n",
210-
"connectors = [\"demo-conn-tm17qr\"]\n",
229+
"connectors = [\"demo-conn-e5y5ps\"]\n",
211230
"\n",
212231
"# Create an instance of the Chatbot class by supplying the connectors\n",
213232
"chatbot = Chatbot(connectors)\n",

0 commit comments

Comments
 (0)