examples

Форк
0
739 строк · 29.3 Кб
1
{
2
 "cells": [
3
  {
4
   "attachments": {},
5
   "cell_type": "markdown",
6
   "metadata": {},
7
   "source": [
8
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/chatbots/chatbot.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/chatbots/chatbot.ipynb)"
9
   ]
10
  },
11
  {
12
   "attachments": {},
13
   "cell_type": "markdown",
14
   "metadata": {},
15
   "source": [
16
    "# Chatbots\n",
17
    "\n",
18
    "The most powerful chatbots in the world all **hallucinate** and lack an up-to-date understanding of the real world. GPT-4, for example, cannot answer questions about recent world events, no matter how important they were.\n",
19
    "\n",
20
    "The world of chatbots is frozen in time. Their world exists as a static snapshot of the world as it was within their training data.\n",
21
    "\n",
22
    "A solution to this problem is *retrieval augmentation*. The idea behind this is plug-in a chatbot to an external vector database where they can then access *accurate* and up-to-date and information. Helping us limit hallucinations and answer questions about the latest events. In this example we will see how to do that.\n",
23
    "\n",
24
    "To begin, we must install the prerequisite libraries that we will be using in this notebook."
25
   ]
26
  },
27
  {
28
   "cell_type": "code",
29
   "execution_count": null,
30
   "metadata": {},
31
   "outputs": [],
32
   "source": [
33
    "!pip install -qU \\\n",
34
    "  langchain==0.0.162 \\\n",
35
    "  openai==0.27.7 \\\n",
36
    "  tiktoken==0.4.0 \\\n",
37
    "  \"pinecone-client[grpc]\"==2.2.1 \\\n",
38
    "  pinecone-datasets=='0.5.0rc11'"
39
   ]
40
  },
41
  {
42
   "attachments": {},
43
   "cell_type": "markdown",
44
   "metadata": {},
45
   "source": [
46
    "---\n",
47
    "\n",
48
    "🚨 _Note: the above `pip install` is formatted for Jupyter notebooks. If running elsewhere you may need to drop the `!`._\n",
49
    "\n",
50
    "---"
51
   ]
52
  },
53
  {
54
   "attachments": {},
55
   "cell_type": "markdown",
56
   "metadata": {},
57
   "source": [
58
    "## Data Preparation\n",
59
    "\n",
60
    "The chatbot use-case requires that we store relevant information (called **contexts**) inside the vector database. To make these retrievable we encode them to create vector embeddings. Once these embeddings are indexed we can use them to augment user queries as demonstrated below:\n",
61
    "\n",
62
    "![Chatbot with retrieval augmentation diagram](https://github.com/pinecone-io/examples/blob/master/learn/generation/chatbots/assets/chatbot-diagram.png?raw=true)"
63
   ]
64
  },
65
  {
66
   "attachments": {},
67
   "cell_type": "markdown",
68
   "metadata": {},
69
   "source": [
70
    "### Downloading the Dataset\n",
71
    "\n",
72
    "We will download a pre-embedding dataset from `pinecone-datasets`. Allowing us to skip the embedding and any other preprocessing steps. When working with your own dataset you will need to perform this embedding step but we have prebuilt the embeddings so we can jump right to the action."
73
   ]
74
  },
75
  {
76
   "cell_type": "code",
77
   "execution_count": null,
78
   "metadata": {},
79
   "outputs": [
80
    {
81
     "name": "stderr",
82
     "output_type": "stream",
83
     "text": [
84
      "/usr/local/lib/python3.10/dist-packages/pinecone_datasets/dataset.py:125: UserWarning: WARNING: No data found at: gs://pinecone-datasets-dev/wikipedia-simple-text-embedding-ada-002/queries/*.parquet. Returning empty DF\n",
85
      "  warnings.warn(\n"
86
     ]
87
    },
88
    {
89
     "data": {
90
      "text/html": [
91
       "\n",
92
       "  <div id=\"df-0063c648-f40e-4f85-aece-76870a384c34\">\n",
93
       "    <div class=\"colab-df-container\">\n",
94
       "      <div>\n",
95
       "<style scoped>\n",
96
       "    .dataframe tbody tr th:only-of-type {\n",
97
       "        vertical-align: middle;\n",
98
       "    }\n",
99
       "\n",
100
       "    .dataframe tbody tr th {\n",
101
       "        vertical-align: top;\n",
102
       "    }\n",
103
       "\n",
104
       "    .dataframe thead th {\n",
105
       "        text-align: right;\n",
106
       "    }\n",
107
       "</style>\n",
108
       "<table border=\"1\" class=\"dataframe\">\n",
109
       "  <thead>\n",
110
       "    <tr style=\"text-align: right;\">\n",
111
       "      <th></th>\n",
112
       "      <th>id</th>\n",
113
       "      <th>values</th>\n",
114
       "      <th>sparse_values</th>\n",
115
       "      <th>metadata</th>\n",
116
       "      <th>blob</th>\n",
117
       "    </tr>\n",
118
       "  </thead>\n",
119
       "  <tbody>\n",
120
       "    <tr>\n",
121
       "      <th>0</th>\n",
122
       "      <td>1-0</td>\n",
123
       "      <td>[-0.011254455894231796, -0.01698738895356655, ...</td>\n",
124
       "      <td>None</td>\n",
125
       "      <td>None</td>\n",
126
       "      <td>{'chunk': 0, 'source': 'https://simple.wikiped...</td>\n",
127
       "    </tr>\n",
128
       "    <tr>\n",
129
       "      <th>1</th>\n",
130
       "      <td>1-1</td>\n",
131
       "      <td>[-0.0015197008615359664, -0.007858820259571075...</td>\n",
132
       "      <td>None</td>\n",
133
       "      <td>None</td>\n",
134
       "      <td>{'chunk': 1, 'source': 'https://simple.wikiped...</td>\n",
135
       "    </tr>\n",
136
       "    <tr>\n",
137
       "      <th>2</th>\n",
138
       "      <td>1-2</td>\n",
139
       "      <td>[-0.009930099360644817, -0.012211072258651257,...</td>\n",
140
       "      <td>None</td>\n",
141
       "      <td>None</td>\n",
142
       "      <td>{'chunk': 2, 'source': 'https://simple.wikiped...</td>\n",
143
       "    </tr>\n",
144
       "    <tr>\n",
145
       "      <th>3</th>\n",
146
       "      <td>1-3</td>\n",
147
       "      <td>[-0.011600767262279987, -0.012608098797500134,...</td>\n",
148
       "      <td>None</td>\n",
149
       "      <td>None</td>\n",
150
       "      <td>{'chunk': 3, 'source': 'https://simple.wikiped...</td>\n",
151
       "    </tr>\n",
152
       "    <tr>\n",
153
       "      <th>4</th>\n",
154
       "      <td>1-4</td>\n",
155
       "      <td>[-0.026462381705641747, -0.016362832859158516,...</td>\n",
156
       "      <td>None</td>\n",
157
       "      <td>None</td>\n",
158
       "      <td>{'chunk': 4, 'source': 'https://simple.wikiped...</td>\n",
159
       "    </tr>\n",
160
       "  </tbody>\n",
161
       "</table>\n",
162
       "</div>\n",
163
       "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-0063c648-f40e-4f85-aece-76870a384c34')\"\n",
164
       "              title=\"Convert this dataframe to an interactive table.\"\n",
165
       "              style=\"display:none;\">\n",
166
       "        \n",
167
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
168
       "       width=\"24px\">\n",
169
       "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
170
       "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
171
       "  </svg>\n",
172
       "      </button>\n",
173
       "      \n",
174
       "  <style>\n",
175
       "    .colab-df-container {\n",
176
       "      display:flex;\n",
177
       "      flex-wrap:wrap;\n",
178
       "      gap: 12px;\n",
179
       "    }\n",
180
       "\n",
181
       "    .colab-df-convert {\n",
182
       "      background-color: #E8F0FE;\n",
183
       "      border: none;\n",
184
       "      border-radius: 50%;\n",
185
       "      cursor: pointer;\n",
186
       "      display: none;\n",
187
       "      fill: #1967D2;\n",
188
       "      height: 32px;\n",
189
       "      padding: 0 0 0 0;\n",
190
       "      width: 32px;\n",
191
       "    }\n",
192
       "\n",
193
       "    .colab-df-convert:hover {\n",
194
       "      background-color: #E2EBFA;\n",
195
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
196
       "      fill: #174EA6;\n",
197
       "    }\n",
198
       "\n",
199
       "    [theme=dark] .colab-df-convert {\n",
200
       "      background-color: #3B4455;\n",
201
       "      fill: #D2E3FC;\n",
202
       "    }\n",
203
       "\n",
204
       "    [theme=dark] .colab-df-convert:hover {\n",
205
       "      background-color: #434B5C;\n",
206
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
207
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
208
       "      fill: #FFFFFF;\n",
209
       "    }\n",
210
       "  </style>\n",
211
       "\n",
212
       "      <script>\n",
213
       "        const buttonEl =\n",
214
       "          document.querySelector('#df-0063c648-f40e-4f85-aece-76870a384c34 button.colab-df-convert');\n",
215
       "        buttonEl.style.display =\n",
216
       "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
217
       "\n",
218
       "        async function convertToInteractive(key) {\n",
219
       "          const element = document.querySelector('#df-0063c648-f40e-4f85-aece-76870a384c34');\n",
220
       "          const dataTable =\n",
221
       "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
222
       "                                                     [key], {});\n",
223
       "          if (!dataTable) return;\n",
224
       "\n",
225
       "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
226
       "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
227
       "            + ' to learn more about interactive tables.';\n",
228
       "          element.innerHTML = '';\n",
229
       "          dataTable['output_type'] = 'display_data';\n",
230
       "          await google.colab.output.renderOutput(dataTable, element);\n",
231
       "          const docLink = document.createElement('div');\n",
232
       "          docLink.innerHTML = docLinkHtml;\n",
233
       "          element.appendChild(docLink);\n",
234
       "        }\n",
235
       "      </script>\n",
236
       "    </div>\n",
237
       "  </div>\n",
238
       "  "
239
      ],
240
      "text/plain": [
241
       "    id                                             values sparse_values  \\\n",
242
       "0  1-0  [-0.011254455894231796, -0.01698738895356655, ...          None   \n",
243
       "1  1-1  [-0.0015197008615359664, -0.007858820259571075...          None   \n",
244
       "2  1-2  [-0.009930099360644817, -0.012211072258651257,...          None   \n",
245
       "3  1-3  [-0.011600767262279987, -0.012608098797500134,...          None   \n",
246
       "4  1-4  [-0.026462381705641747, -0.016362832859158516,...          None   \n",
247
       "\n",
248
       "  metadata                                               blob  \n",
249
       "0     None  {'chunk': 0, 'source': 'https://simple.wikiped...  \n",
250
       "1     None  {'chunk': 1, 'source': 'https://simple.wikiped...  \n",
251
       "2     None  {'chunk': 2, 'source': 'https://simple.wikiped...  \n",
252
       "3     None  {'chunk': 3, 'source': 'https://simple.wikiped...  \n",
253
       "4     None  {'chunk': 4, 'source': 'https://simple.wikiped...  "
254
      ]
255
     },
256
     "metadata": {},
257
     "output_type": "display_data"
258
    }
259
   ],
260
   "source": [
261
    "import pinecone_datasets\n",
262
    "\n",
263
    "dataset = pinecone_datasets.load_dataset('wikipedia-simple-text-embedding-ada-002')\n",
264
    "dataset.head()"
265
   ]
266
  },
267
  {
268
   "cell_type": "code",
269
   "execution_count": null,
270
   "metadata": {},
271
   "outputs": [
272
    {
273
     "data": {
274
      "text/plain": [
275
       "283945"
276
      ]
277
     },
278
     "metadata": {},
279
     "output_type": "display_data"
280
    }
281
   ],
282
   "source": [
283
    "len(dataset)"
284
   ]
285
  },
286
  {
287
   "attachments": {},
288
   "cell_type": "markdown",
289
   "metadata": {},
290
   "source": [
291
    "A `pinecone-dataset` always contains `id`, `values`, `sparse_values`, `metadata`, and `blob`. All we need are the IDs, vector embeddings (stored in `values`), and some metadata (which is actually stored in `blob`). Let's reformat the dataset ready for adding to Pinecone, and use a smaller subset of the full dataset."
292
   ]
293
  },
294
  {
295
   "cell_type": "code",
296
   "execution_count": null,
297
   "metadata": {},
298
   "outputs": [
299
    {
300
     "data": {
301
      "text/plain": [
302
       "30000"
303
      ]
304
     },
305
     "metadata": {},
306
     "output_type": "display_data"
307
    }
308
   ],
309
   "source": [
310
    "# we drop sparse_values as they are not needed for this example\n",
311
    "dataset.documents.drop(['sparse_values', 'metadata'], axis=1, inplace=True)\n",
312
    "dataset.documents.rename(columns={'blob': 'metadata'}, inplace=True)\n",
313
    "# we will use rows of the dataset up to index 30_000\n",
314
    "dataset.documents.drop(dataset.documents.index[30_000:], inplace=True)\n",
315
    "len(dataset)"
316
   ]
317
  },
318
  {
319
   "attachments": {},
320
   "cell_type": "markdown",
321
   "metadata": {},
322
   "source": [
323
    "Now we move on to initializing our Pinecone vector database.\n",
324
    "\n",
325
    "## Creating an Index\n",
326
    "\n",
327
    "To create our vector database we first need a [free API key from Pinecone](https://app.pinecone.io). Then we initialize like so:"
328
   ]
329
  },
330
  {
331
   "cell_type": "code",
332
   "execution_count": null,
333
   "metadata": {},
334
   "outputs": [],
335
   "source": [
336
    "index_name = 'chatbot-onboarding'"
337
   ]
338
  },
339
  {
340
   "cell_type": "code",
341
   "execution_count": null,
342
   "metadata": {},
343
   "outputs": [
344
    {
345
     "name": "stderr",
346
     "output_type": "stream",
347
     "text": [
348
      "/usr/local/lib/python3.10/dist-packages/pinecone/index.py:4: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
349
      "  from tqdm.autonotebook import tqdm\n"
350
     ]
351
    }
352
   ],
353
   "source": [
354
    "import os\n",
355
    "from pinecone import Pinecone\n",
356
    "import time\n",
357
    "\n",
358
    "# find API key in console at app.pinecone.io\n",
359
    "PINECONE_API_KEY = os.getenv('PINECONE_API_KEY') or 'PINECONE_API_KEY'\n",
360
    "# find ENV (cloud region) next to API key in console\n",
361
    "PINECONE_ENVIRONMENT = os.getenv('PINECONE_ENVIRONMENT') or 'PINECONE_ENVIRONMENT'\n",
362
    "\n",
363
    "pinecone.init(\n",
364
    "    api_key=PINECONE_API_KEY,\n",
365
    "    environment=PINECONE_ENVIRONMENT\n",
366
    ")\n",
367
    "\n",
368
    "if index_name not in pinecone.list_indexes().names():\n",
369
    "    # we create a new index\n",
370
    "    pinecone.create_index(\n",
371
    "        name=index_name,\n",
372
    "        metric='cosine',\n",
373
    "        dimension=1536,  # 1536 dim of text-embedding-ada-002\n",
374
    "        metadata_config={'indexed': ['wiki-id', 'title']}\n",
375
    "    )\n",
376
    "    # wait a moment for the index to be fully initialized\n",
377
    "    time.sleep(1)"
378
   ]
379
  },
380
  {
381
   "attachments": {},
382
   "cell_type": "markdown",
383
   "metadata": {},
384
   "source": [
385
    "Then we connect to the new index:"
386
   ]
387
  },
388
  {
389
   "cell_type": "code",
390
   "execution_count": null,
391
   "metadata": {},
392
   "outputs": [
393
    {
394
     "data": {
395
      "text/plain": [
396
       "{'dimension': 1536,\n",
397
       " 'index_fullness': 0.0,\n",
398
       " 'namespaces': {},\n",
399
       " 'total_vector_count': 0}"
400
      ]
401
     },
402
     "metadata": {},
403
     "output_type": "display_data"
404
    }
405
   ],
406
   "source": [
407
    "index = pinecone.Index(index_name)\n",
408
    "# wait a moment for the index to be fully initialized\n",
409
    "time.sleep(1)\n",
410
    "\n",
411
    "index.describe_index_stats()"
412
   ]
413
  },
414
  {
415
   "attachments": {},
416
   "cell_type": "markdown",
417
   "metadata": {},
418
   "source": [
419
    "We should see that the new Pinecone index has a `total_vector_count` of `0`, as we haven't added any vectors yet.\n",
420
    "\n",
421
    "Now we upsert the data to Pinecone:"
422
   ]
423
  },
424
  {
425
   "cell_type": "code",
426
   "execution_count": null,
427
   "metadata": {},
428
   "outputs": [
429
    {
430
     "data": {
431
      "application/vnd.jupyter.widget-view+json": {
432
       "model_id": "47188360f07a4c018bf7d2c5eb6f626c",
433
       "version_major": 2,
434
       "version_minor": 0
435
      },
436
      "text/plain": [
437
       "sending upsert requests:   0%|          | 0/30000 [00:00<?, ?it/s]"
438
      ]
439
     },
440
     "metadata": {},
441
     "output_type": "display_data"
442
    },
443
    {
444
     "data": {
445
      "application/vnd.jupyter.widget-view+json": {
446
       "model_id": "75cef31e615d4851b029c3aacacf9c2d",
447
       "version_major": 2,
448
       "version_minor": 0
449
      },
450
      "text/plain": [
451
       "collecting async responses:   0%|          | 0/300 [00:00<?, ?it/s]"
452
      ]
453
     },
454
     "metadata": {},
455
     "output_type": "display_data"
456
    },
457
    {
458
     "data": {
459
      "text/plain": [
460
       "upserted_count: 30000"
461
      ]
462
     },
463
     "metadata": {},
464
     "output_type": "display_data"
465
    }
466
   ],
467
   "source": [
468
    "index.upsert_from_dataframe(dataset.documents, batch_size=100)"
469
   ]
470
  },
471
  {
472
   "attachments": {},
473
   "cell_type": "markdown",
474
   "metadata": {},
475
   "source": [
476
    "We've now indexed everything. We can check the number of vectors in our index like so:"
477
   ]
478
  },
479
  {
480
   "cell_type": "code",
481
   "execution_count": null,
482
   "metadata": {},
483
   "outputs": [
484
    {
485
     "data": {
486
      "text/plain": [
487
       "{'dimension': 1536,\n",
488
       " 'index_fullness': 0.1,\n",
489
       " 'namespaces': {'': {'vector_count': 30000}},\n",
490
       " 'total_vector_count': 30000}"
491
      ]
492
     },
493
     "metadata": {},
494
     "output_type": "display_data"
495
    }
496
   ],
497
   "source": [
498
    "index.describe_index_stats()"
499
   ]
500
  },
501
  {
502
   "attachments": {},
503
   "cell_type": "markdown",
504
   "metadata": {},
505
   "source": [
506
    "## Building the Chatbot and Querying\n",
507
    "\n",
508
    "Now that we've build our index we can switch over to LangChain. We need to initialize a LangChain vector store using the same index we just built. For this we will also need a LangChain embedding object, which we initialize like so:"
509
   ]
510
  },
511
  {
512
   "cell_type": "code",
513
   "execution_count": null,
514
   "metadata": {},
515
   "outputs": [],
516
   "source": [
517
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
518
    "\n",
519
    "# get openai api key from platform.openai.com\n",
520
    "OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') or 'OPENAI_API_KEY'\n",
521
    "\n",
522
    "model_name = 'text-embedding-ada-002'\n",
523
    "\n",
524
    "embed = OpenAIEmbeddings(\n",
525
    "    model=model_name,\n",
526
    "    openai_api_key=OPENAI_API_KEY\n",
527
    ")"
528
   ]
529
  },
530
  {
531
   "attachments": {},
532
   "cell_type": "markdown",
533
   "metadata": {},
534
   "source": [
535
    "Now initialize the vector store:"
536
   ]
537
  },
538
  {
539
   "cell_type": "code",
540
   "execution_count": null,
541
   "metadata": {},
542
   "outputs": [],
543
   "source": [
544
    "from langchain.vectorstores import Pinecone\n",
545
    "\n",
546
    "text_field = \"text\"\n",
547
    "\n",
548
    "# switch back to normal index for langchain\n",
549
    "index = pinecone.Index(index_name)\n",
550
    "\n",
551
    "vectorstore = Pinecone(\n",
552
    "    index, embed.embed_query, text_field\n",
553
    ")"
554
   ]
555
  },
556
  {
557
   "attachments": {},
558
   "cell_type": "markdown",
559
   "metadata": {},
560
   "source": [
561
    "Now we can query the vector store directly using `vectorstore.similarity_search`:"
562
   ]
563
  },
564
  {
565
   "cell_type": "code",
566
   "execution_count": null,
567
   "metadata": {},
568
   "outputs": [
569
    {
570
     "data": {
571
      "text/plain": [
572
       "[Document(page_content='Benito Amilcare Andrea Mussolini KSMOM GCTE (29 July 1883 – 28 April 1945) was an Italian politician and journalist. He was also the Prime Minister of Italy from 1922 until 1943. He was the leader of the National Fascist Party.\\n\\nBiography\\n\\nEarly life\\nBenito Mussolini was named after Benito Juarez, a Mexican opponent of the political power of the Roman Catholic Church, by his anticlerical (a person who opposes the political interference of the Roman Catholic Church in secular affairs) father. Mussolini\\'s father was a blacksmith. Before being involved in politics, Mussolini was a newspaper editor (where he learned all his propaganda skills) and elementary school teacher.\\n\\nAt first, Mussolini was a socialist, but when he wanted Italy to join the First World War, he was thrown out of the socialist party. He \\'invented\\' a new ideology, Fascism, much out of Nationalist\\xa0and Conservative views.\\n\\nRise to power and becoming dictator\\nIn 1922, he took power by having a large group of men, \"Black Shirts,\" march on Rome and threaten to take over the government. King Vittorio Emanuele III gave in, allowed him to form a government, and made him prime minister. In the following five years, he gained power, and in 1927 created the OVRA, his personal secret police force. Using the agency to arrest, scare, or murder people against his regime, Mussolini was dictator\\xa0of Italy by the end of 1927. Only the King and his own Fascist party could challenge his power.', metadata={'chunk': 0.0, 'source': 'https://simple.wikipedia.org/wiki/Benito%20Mussolini', 'title': 'Benito Mussolini', 'wiki-id': '6754'}),\n",
573
       " Document(page_content='Fascism as practiced by Mussolini\\nMussolini\\'s form of Fascism, \"Italian Fascism\"- unlike Nazism, the racist ideology that Adolf Hitler followed- was different and less destructive than Hitler\\'s. Although a believer in the superiority of the Italian nation and national unity, Mussolini, unlike Hitler, is quoted \"Race? It is a feeling, not a reality. Nothing will ever make me believe that biologically pure races can be shown to exist today\".\\n\\nMussolini wanted Italy to become a new Roman Empire. In 1923, he attacked the island of Corfu, and in 1924, he occupied the city state of Fiume. In 1935, he attacked the African country Abyssinia (now called Ethiopia). His forces occupied it in 1936. Italy was thrown out of the League of Nations because of this aggression. In 1939, he occupied the country Albania. In 1936, Mussolini signed an alliance with Adolf Hitler, the dictator of Germany.\\n\\nFall from power and death\\nIn 1940, he sent Italy into the Second World War on the side of the Axis countries. Mussolini attacked Greece, but he failed to conquer it. In 1943, the Allies landed in Southern Italy. The Fascist party and King Vittorio Emanuel III deposed Mussolini and put him in jail, but he was set free by the Germans, who made him ruler of the Italian Social Republic puppet state which was in a small part of Central Italy. When the war was almost over, Mussolini tried to escape to Switzerland with his mistress, Clara Petacci, but they were both captured and shot by partisans. Mussolini\\'s dead body was hanged upside-down, together with his mistress and some of Mussolini\\'s helpers, on a pole at a gas station in the village of Millan, which is near the border  between Italy and Switzerland.', metadata={'chunk': 1.0, 'source': 'https://simple.wikipedia.org/wiki/Benito%20Mussolini', 'title': 'Benito Mussolini', 'wiki-id': '6754'}),\n",
574
       " Document(page_content='Veneto was made part of Italy in 1866 after a war with Austria. Italian soldiers won Latium in 1870. That was when they took away the Pope\\'s power. The Pope, who was angry, said that he was a prisoner to keep Catholic people from being active in politics. That was the year of Italian unification.\\n\\nItaly participated in World War I. It was an ally of Great Britain, France, and Russia against the Central Powers. Almost all of Italy\\'s fighting was on the Eastern border, near Austria. After the \"Caporetto defeat\", Italy thought they would lose the war. But, in 1918, the Central Powers surrendered. Italy gained the Trentino-South Tyrol, which once was owned by Austria.\\n\\nFascist Italy \\nIn 1922, a new Italian government started. It was ruled by Benito Mussolini, the leader of Fascism in Italy. He became head of government and dictator, calling himself \"Il Duce\" (which means \"leader\" in Italian). He became friends with German dictator Adolf Hitler. Germany, Japan, and Italy became the Axis Powers. In 1940, they entered World War II together against France, Great Britain, and later the Soviet Union. During the war, Italy controlled most of the Mediterranean Sea.', metadata={'chunk': 5.0, 'source': 'https://simple.wikipedia.org/wiki/Italy', 'title': 'Italy', 'wiki-id': '363'})]"
575
      ]
576
     },
577
     "metadata": {},
578
     "output_type": "display_data"
579
    }
580
   ],
581
   "source": [
582
    "query = \"who was Benito Mussolini?\"\n",
583
    "\n",
584
    "vectorstore.similarity_search(\n",
585
    "    query,  # our search query\n",
586
    "    k=3  # return 3 most relevant docs\n",
587
    ")"
588
   ]
589
  },
590
  {
591
   "attachments": {},
592
   "cell_type": "markdown",
593
   "metadata": {},
594
   "source": [
595
    "All of these are good, relevant results. But this actually covers only the first few steps of our retrieval augmented chatbot, the **retrieval augmentation** steps. We're still missing the **chatbot** part.\n",
596
    "\n",
597
    "### Creating the Chatbot\n",
598
    "\n",
599
    "Our chatbot will take the query as a question that is to be answered, but the chatbot must answer the question based on the information it is seeing being returned from the `vectorstore`.\n",
600
    "\n",
601
    "To do this we initialize a chat model and `RetrievalQA` object like so:"
602
   ]
603
  },
604
  {
605
   "cell_type": "code",
606
   "execution_count": null,
607
   "metadata": {},
608
   "outputs": [],
609
   "source": [
610
    "from langchain.chat_models import ChatOpenAI\n",
611
    "from langchain.chains import RetrievalQA\n",
612
    "\n",
613
    "# chatbot language model\n",
614
    "llm = ChatOpenAI(\n",
615
    "    openai_api_key=OPENAI_API_KEY,\n",
616
    "    model_name='gpt-3.5-turbo',\n",
617
    "    temperature=0.0\n",
618
    ")\n",
619
    "# retrieval augmented pipeline for chatbot\n",
620
    "qa = RetrievalQA.from_chain_type(\n",
621
    "    llm=llm,\n",
622
    "    chain_type=\"stuff\",\n",
623
    "    retriever=vectorstore.as_retriever()\n",
624
    ")"
625
   ]
626
  },
627
  {
628
   "cell_type": "code",
629
   "execution_count": null,
630
   "metadata": {},
631
   "outputs": [
632
    {
633
     "data": {
634
      "application/vnd.google.colaboratory.intrinsic+json": {
635
       "type": "string"
636
      },
637
      "text/plain": [
638
       "'Benito Mussolini was an Italian politician and journalist who served as the Prime Minister of Italy from 1922 until 1943. He was the leader of the National Fascist Party and was also a newspaper editor and elementary school teacher before being involved in politics. Mussolini was a dictator of Italy by the end of 1927, and his form of Fascism, \"Italian Fascism,\" was different and less destructive than Hitler\\'s Nazism. He wanted Italy to become a new Roman Empire and attacked several countries, including Abyssinia (now called Ethiopia) and Greece. Mussolini was captured and shot by partisans in 1945.'"
639
      ]
640
     },
641
     "metadata": {},
642
     "output_type": "display_data"
643
    }
644
   ],
645
   "source": [
646
    "qa.run(query)"
647
   ]
648
  },
649
  {
650
   "attachments": {},
651
   "cell_type": "markdown",
652
   "metadata": {},
653
   "source": [
654
    "We can also include the sources of information that the chatbot is using to answer our question. We can do this using a slightly different version of `RetrievalQA` called `RetrievalQAWithSourcesChain`:"
655
   ]
656
  },
657
  {
658
   "cell_type": "code",
659
   "execution_count": null,
660
   "metadata": {},
661
   "outputs": [],
662
   "source": [
663
    "from langchain.chains import RetrievalQAWithSourcesChain\n",
664
    "\n",
665
    "qa_with_sources = RetrievalQAWithSourcesChain.from_chain_type(\n",
666
    "    llm=llm,\n",
667
    "    chain_type=\"stuff\",\n",
668
    "    retriever=vectorstore.as_retriever()\n",
669
    ")"
670
   ]
671
  },
672
  {
673
   "cell_type": "code",
674
   "execution_count": null,
675
   "metadata": {},
676
   "outputs": [
677
    {
678
     "data": {
679
      "text/plain": [
680
       "{'question': 'who was Benito Mussolini?',\n",
681
       " 'answer': 'Benito Mussolini was an Italian politician and journalist who was the Prime Minister of Italy from 1922 until 1943. He was the leader of the National Fascist Party and created his own form of Fascism called \"Italian Fascism\". He wanted Italy to become a new Roman Empire and attacked several countries, including Abyssinia (now called Ethiopia) and Greece. He was dictator of Italy by the end of 1927 and was deposed in 1943. He was later captured and shot by partisans. Mario Draghi is the current head of government of Italy. Italy was not a state before 1861 and was a group of separate states ruled by other countries. In 1860, Giuseppe Garibaldi took control of Sicily, creating the Kingdom of Italy in 1861. Victor Emmanuel II was made the king. \\n',\n",
682
       " 'sources': 'https://simple.wikipedia.org/wiki/Benito%20Mussolini, https://simple.wikipedia.org/wiki/Italy'}"
683
      ]
684
     },
685
     "metadata": {},
686
     "output_type": "display_data"
687
    }
688
   ],
689
   "source": [
690
    "qa_with_sources(query)"
691
   ]
692
  },
693
  {
694
   "attachments": {},
695
   "cell_type": "markdown",
696
   "metadata": {},
697
   "source": [
698
    "Now we answer the question being asked, *and* return the source of this information being used by the LLM.\n",
699
    "\n",
700
    "Once done, we can delete the index to save resources."
701
   ]
702
  },
703
  {
704
   "cell_type": "code",
705
   "execution_count": null,
706
   "metadata": {},
707
   "outputs": [],
708
   "source": [
709
    "pinecone.delete_index(index_name)"
710
   ]
711
  },
712
  {
713
   "attachments": {},
714
   "cell_type": "markdown",
715
   "metadata": {},
716
   "source": [
717
    "## Where would we use this?\n",
718
    "\n",
719
    "Chatbots and retrieval augmented LLMs are incredibly prelevant in our world despite their lack of adoption pre-ChatGPT. Retrieval augmentation is being used to improve the performance and reliability of chatbots and the retrieval *with sources* that we demonstrated above is clearly visible in both Google's Bard and Microsoft's Bing AI. Beyond these tech giants, giving LLMs access to up-to-date information is essential for customer service chatbots that must refer to customer data and FAQ docs, for chatbots plugged into fast changing ecommerce databases, and any scenario where up-to-date information is important or helpful."
720
   ]
721
  },
722
  {
723
   "attachments": {},
724
   "cell_type": "markdown",
725
   "metadata": {},
726
   "source": [
727
    "---"
728
   ]
729
  }
730
 ],
731
 "metadata": {
732
  "language_info": {
733
   "name": "python"
734
  },
735
  "orig_nbformat": 4
736
 },
737
 "nbformat": 4,
738
 "nbformat_minor": 2
739
}
740

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.