|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": 2, |
| 5 | + "execution_count": 1, |
6 | 6 | "metadata": {},
|
7 | 7 | "outputs": [],
|
8 | 8 | "source": [
|
|
11 | 11 | "import uuid\n",
|
12 | 12 | "from typing import List, Dict\n",
|
13 | 13 | "\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\")" |
16 | 15 | ]
|
17 | 16 | },
|
18 | 17 | {
|
|
79 | 78 | },
|
80 | 79 | {
|
81 | 80 | "cell_type": "code",
|
82 |
| - "execution_count": 10, |
| 81 | + "execution_count": 3, |
83 | 82 | "metadata": {},
|
84 | 83 | "outputs": [],
|
85 | 84 | "source": [
|
|
98 | 97 | " )\n",
|
99 | 98 | "\n",
|
100 | 99 | " for event in response:\n",
|
101 |
| - " yield event" |
| 100 | + " yield event\n", |
| 101 | + " yield response" |
102 | 102 | ]
|
103 | 103 | },
|
104 | 104 | {
|
|
110 | 110 | },
|
111 | 111 | {
|
112 | 112 | "cell_type": "code",
|
113 |
| - "execution_count": 11, |
| 113 | + "execution_count": 4, |
114 | 114 | "metadata": {},
|
115 | 115 | "outputs": [],
|
116 | 116 | "source": [
|
|
136 | 136 | " # Print the chatbot response\n",
|
137 | 137 | " print(\"Chatbot:\")\n",
|
138 | 138 | " \n",
|
139 |
| - " documents = []\n", |
140 |
| - " documents_flag = False\n", |
141 | 139 | " citations_flag = False\n",
|
142 | 140 | " \n",
|
143 | 141 | " 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", |
149 | 144 | " # 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", |
152 | 147 | "\n",
|
153 | 148 | " # Citations\n",
|
154 |
| - " if event.event_type == \"citation-generation\":\n", |
| 149 | + " if stream_type == \"StreamCitationGeneration\":\n", |
155 | 150 | " if not citations_flag:\n",
|
156 | 151 | " print(\"\\n\\nCITATIONS:\")\n",
|
157 | 152 | " 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", |
164 | 166 | "\n",
|
165 | 167 | " print(f\"\\n{'-'*100}\\n\")"
|
166 | 168 | ]
|
|
174 | 176 | },
|
175 | 177 | {
|
176 | 178 | "cell_type": "code",
|
177 |
| - "execution_count": 13, |
| 179 | + "execution_count": 10, |
178 | 180 | "metadata": {},
|
179 | 181 | "outputs": [
|
180 | 182 | {
|
181 | 183 | "name": "stdout",
|
182 | 184 | "output_type": "stream",
|
183 | 185 | "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", |
185 | 204 | "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", |
187 | 208 | "\n",
|
188 | 209 | "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", |
195 | 215 | "\n",
|
196 | 216 | "\n",
|
197 | 217 | "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", |
201 | 220 | "\n",
|
202 | 221 | "----------------------------------------------------------------------------------------------------\n",
|
203 | 222 | "\n",
|
|
207 | 226 | ],
|
208 | 227 | "source": [
|
209 | 228 | "# Define connectors\n",
|
210 |
| - "connectors = [\"demo-conn-tm17qr\"]\n", |
| 229 | + "connectors = [\"demo-conn-e5y5ps\"]\n", |
211 | 230 | "\n",
|
212 | 231 | "# Create an instance of the Chatbot class by supplying the connectors\n",
|
213 | 232 | "chatbot = Chatbot(connectors)\n",
|
|
0 commit comments