AlaFalaki commited on
Commit
a0fa984
β€’
1 Parent(s): 7d3f9c1

Created using Colaboratory

Browse files
Files changed (1) hide show
  1. notebooks/14-Adding_Chat.ipynb +70 -29
notebooks/14-Adding_Chat.ipynb CHANGED
@@ -4,7 +4,7 @@
4
  "metadata": {
5
  "colab": {
6
  "provenance": [],
7
- "authorship_tag": "ABX9TyOIU+C03mTlevo1fu+yiDTM",
8
  "include_colab_link": true
9
  },
10
  "kernelspec": {
@@ -725,7 +725,7 @@
725
  },
726
  {
727
  "cell_type": "code",
728
- "execution_count": 1,
729
  "metadata": {
730
  "id": "QPJzr-I9XQ7l",
731
  "colab": {
@@ -792,7 +792,7 @@
792
  "metadata": {
793
  "id": "riuXwpSPcvWC"
794
  },
795
- "execution_count": 2,
796
  "outputs": []
797
  },
798
  {
@@ -805,7 +805,7 @@
805
  "metadata": {
806
  "id": "jIEeZzqLbz0J"
807
  },
808
- "execution_count": 3,
809
  "outputs": []
810
  },
811
  {
@@ -827,7 +827,7 @@
827
  "metadata": {
828
  "id": "9oGT6crooSSj"
829
  },
830
- "execution_count": 4,
831
  "outputs": []
832
  },
833
  {
@@ -852,7 +852,7 @@
852
  "metadata": {
853
  "id": "SQP87lHczHKc"
854
  },
855
- "execution_count": 5,
856
  "outputs": []
857
  },
858
  {
@@ -908,7 +908,7 @@
908
  "id": "wl_pbPvMlv1h",
909
  "outputId": "5418de57-b95b-4b90-b7d0-a801ea3c73f7"
910
  },
911
- "execution_count": 5,
912
  "outputs": [
913
  {
914
  "output_type": "stream",
@@ -963,7 +963,7 @@
963
  },
964
  "outputId": "801f2ba8-b498-4923-c1cc-c17d3208850c"
965
  },
966
- "execution_count": 6,
967
  "outputs": [
968
  {
969
  "output_type": "execute_result",
@@ -997,7 +997,7 @@
997
  "metadata": {
998
  "id": "YizvmXPejkJE"
999
  },
1000
- "execution_count": 7,
1001
  "outputs": []
1002
  },
1003
  {
@@ -1014,6 +1014,8 @@
1014
  "source": [
1015
  "from llama_index.text_splitter import TokenTextSplitter\n",
1016
  "\n",
 
 
1017
  "text_splitter = TokenTextSplitter(\n",
1018
  " separator=\" \", chunk_size=512, chunk_overlap=128\n",
1019
  ")"
@@ -1035,6 +1037,8 @@
1035
  "from llama_index.embeddings import OpenAIEmbedding\n",
1036
  "from llama_index.ingestion import IngestionPipeline\n",
1037
  "\n",
 
 
1038
  "pipeline = IngestionPipeline(\n",
1039
  " transformations=[\n",
1040
  " text_splitter,\n",
@@ -1161,6 +1165,7 @@
1161
  {
1162
  "cell_type": "code",
1163
  "source": [
 
1164
  "!zip -r vectorstore.zip mini-llama-articles"
1165
  ],
1166
  "metadata": {
@@ -1178,10 +1183,19 @@
1178
  "id": "OWaT6rL7ksp8"
1179
  }
1180
  },
 
 
 
 
 
 
 
 
 
1181
  {
1182
  "cell_type": "code",
1183
  "source": [
1184
- "!unzip vectorstore.zip"
1185
  ],
1186
  "metadata": {
1187
  "colab": {
@@ -1190,7 +1204,7 @@
1190
  "id": "SodY2Xpf_kxg",
1191
  "outputId": "a6f7ae4a-447c-4222-e400-0fe55e7e26d9"
1192
  },
1193
- "execution_count": 8,
1194
  "outputs": [
1195
  {
1196
  "output_type": "stream",
@@ -1214,7 +1228,7 @@
1214
  "import chromadb\n",
1215
  "from llama_index.vector_stores import ChromaVectorStore\n",
1216
  "\n",
1217
- "# Create your index\n",
1218
  "db = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
1219
  "chroma_collection = db.get_or_create_collection(\"mini-llama-articles\")\n",
1220
  "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
@@ -1222,21 +1236,21 @@
1222
  "metadata": {
1223
  "id": "mXi56KTXk2sp"
1224
  },
1225
- "execution_count": 9,
1226
  "outputs": []
1227
  },
1228
  {
1229
  "cell_type": "code",
1230
  "source": [
1231
- "# Create your index\n",
1232
  "from llama_index import VectorStoreIndex\n",
1233
  "\n",
 
1234
  "vector_index = VectorStoreIndex.from_vector_store(vector_store)"
1235
  ],
1236
  "metadata": {
1237
  "id": "jKXURvLtkuTS"
1238
  },
1239
- "execution_count": 10,
1240
  "outputs": []
1241
  },
1242
  {
@@ -1251,6 +1265,7 @@
1251
  {
1252
  "cell_type": "code",
1253
  "source": [
 
1254
  "def display_res(response):\n",
1255
  " print(\"Response:\\n\\t\", response.response.replace(\"\\n\", \"\") )\n",
1256
  "\n",
@@ -1267,7 +1282,7 @@
1267
  "metadata": {
1268
  "id": "4JpaHEmF5dSS"
1269
  },
1270
- "execution_count": 34,
1271
  "outputs": []
1272
  },
1273
  {
@@ -1282,17 +1297,19 @@
1282
  {
1283
  "cell_type": "code",
1284
  "source": [
 
1285
  "chat_engine = vector_index.as_chat_engine() #chat_mode=\"best\""
1286
  ],
1287
  "metadata": {
1288
  "id": "kwWlDpoR1cRI"
1289
  },
1290
- "execution_count": 47,
1291
  "outputs": []
1292
  },
1293
  {
1294
  "cell_type": "code",
1295
  "source": [
 
1296
  "response = chat_engine.chat(\"Use the tool to answer, How many parameters LLaMA2 model has?\")\n",
1297
  "display_res(response)"
1298
  ],
@@ -1303,7 +1320,7 @@
1303
  "id": "ER3Lb-oN46lJ",
1304
  "outputId": "8b34da39-622f-43f2-cb45-01a1ff37efd7"
1305
  },
1306
- "execution_count": 48,
1307
  "outputs": [
1308
  {
1309
  "output_type": "stream",
@@ -1327,6 +1344,7 @@
1327
  {
1328
  "cell_type": "code",
1329
  "source": [
 
1330
  "response = chat_engine.chat(\"Tell me a joke?\")\n",
1331
  "display_res(response)"
1332
  ],
@@ -1337,7 +1355,7 @@
1337
  "id": "3RRmiJEQ5R1Q",
1338
  "outputId": "15efcc9b-583f-4efe-8e36-fa8b5160da16"
1339
  },
1340
- "execution_count": 49,
1341
  "outputs": [
1342
  {
1343
  "output_type": "stream",
@@ -1361,6 +1379,7 @@
1361
  {
1362
  "cell_type": "code",
1363
  "source": [
 
1364
  "response = chat_engine.chat(\"What was the first question I asked?\")\n",
1365
  "display_res(response)"
1366
  ],
@@ -1371,7 +1390,7 @@
1371
  "id": "8eOzp5Xc5Vbj",
1372
  "outputId": "13bc6714-dd89-45b3-a86b-759806245241"
1373
  },
1374
- "execution_count": 50,
1375
  "outputs": [
1376
  {
1377
  "output_type": "stream",
@@ -1388,17 +1407,19 @@
1388
  {
1389
  "cell_type": "code",
1390
  "source": [
 
1391
  "chat_engine.reset()"
1392
  ],
1393
  "metadata": {
1394
  "id": "7jfiLpru5VZT"
1395
  },
1396
- "execution_count": 51,
1397
  "outputs": []
1398
  },
1399
  {
1400
  "cell_type": "code",
1401
  "source": [
 
1402
  "response = chat_engine.chat(\"What was the first question I asked?\")\n",
1403
  "display_res(response)"
1404
  ],
@@ -1409,7 +1430,7 @@
1409
  "id": "Jt0q8RW25VXN",
1410
  "outputId": "0e2d0d4e-c0ff-48bf-8df3-478fcdc66abd"
1411
  },
1412
- "execution_count": 52,
1413
  "outputs": [
1414
  {
1415
  "output_type": "stream",
@@ -1435,6 +1456,7 @@
1435
  {
1436
  "cell_type": "code",
1437
  "source": [
 
1438
  "streaming_response = chat_engine.stream_chat(\"Write a paragraph about the LLaMA2 model's capabilities.\")\n",
1439
  "for token in streaming_response.response_gen:\n",
1440
  " print(token, end=\"\")"
@@ -1446,7 +1468,7 @@
1446
  "id": "zanJeMbaPJcq",
1447
  "outputId": "de7f0905-c1b1-49ac-fb66-d1578da35cad"
1448
  },
1449
- "execution_count": 68,
1450
  "outputs": [
1451
  {
1452
  "output_type": "stream",
@@ -1467,15 +1489,25 @@
1467
  "id": "DuRgOJ2AHMJh"
1468
  }
1469
  },
 
 
 
 
 
 
 
 
 
1470
  {
1471
  "cell_type": "code",
1472
  "source": [
 
1473
  "gpt4 = OpenAI(temperature=0.9, model=\"gpt-4\")"
1474
  ],
1475
  "metadata": {
1476
  "id": "v0gmM5LGIaRl"
1477
  },
1478
- "execution_count": 57,
1479
  "outputs": []
1480
  },
1481
  {
@@ -1486,7 +1518,7 @@
1486
  "metadata": {
1487
  "id": "EDWsaBTBIhK7"
1488
  },
1489
- "execution_count": 66,
1490
  "outputs": []
1491
  },
1492
  {
@@ -1502,7 +1534,7 @@
1502
  "id": "h4c--hJ75VU2",
1503
  "outputId": "e80fd9bf-e6d5-4532-8771-8cbf781e782e"
1504
  },
1505
- "execution_count": 69,
1506
  "outputs": [
1507
  {
1508
  "output_type": "stream",
@@ -1533,6 +1565,15 @@
1533
  "id": "ysL9ONePOsGB"
1534
  }
1535
  },
 
 
 
 
 
 
 
 
 
1536
  {
1537
  "cell_type": "code",
1538
  "source": [
@@ -1541,7 +1582,7 @@
1541
  "metadata": {
1542
  "id": "-M1jWoKXOs2t"
1543
  },
1544
- "execution_count": 70,
1545
  "outputs": []
1546
  },
1547
  {
@@ -1556,7 +1597,7 @@
1556
  "id": "UZkEW1SSOs0H",
1557
  "outputId": "4869c5fc-e0e1-44c6-e7f0-87db92bb2eb6"
1558
  },
1559
- "execution_count": 71,
1560
  "outputs": [
1561
  {
1562
  "output_type": "stream",
@@ -1589,7 +1630,7 @@
1589
  "id": "eW5P1lD4Osxf",
1590
  "outputId": "b128bc94-081b-49aa-c549-7d7d7be90b63"
1591
  },
1592
- "execution_count": 72,
1593
  "outputs": [
1594
  {
1595
  "output_type": "stream",
 
4
  "metadata": {
5
  "colab": {
6
  "provenance": [],
7
+ "authorship_tag": "ABX9TyNlfE1aMk+m6avCgDavT2ZF",
8
  "include_colab_link": true
9
  },
10
  "kernelspec": {
 
725
  },
726
  {
727
  "cell_type": "code",
728
+ "execution_count": null,
729
  "metadata": {
730
  "id": "QPJzr-I9XQ7l",
731
  "colab": {
 
792
  "metadata": {
793
  "id": "riuXwpSPcvWC"
794
  },
795
+ "execution_count": null,
796
  "outputs": []
797
  },
798
  {
 
805
  "metadata": {
806
  "id": "jIEeZzqLbz0J"
807
  },
808
+ "execution_count": null,
809
  "outputs": []
810
  },
811
  {
 
827
  "metadata": {
828
  "id": "9oGT6crooSSj"
829
  },
830
+ "execution_count": null,
831
  "outputs": []
832
  },
833
  {
 
852
  "metadata": {
853
  "id": "SQP87lHczHKc"
854
  },
855
+ "execution_count": null,
856
  "outputs": []
857
  },
858
  {
 
908
  "id": "wl_pbPvMlv1h",
909
  "outputId": "5418de57-b95b-4b90-b7d0-a801ea3c73f7"
910
  },
911
+ "execution_count": null,
912
  "outputs": [
913
  {
914
  "output_type": "stream",
 
963
  },
964
  "outputId": "801f2ba8-b498-4923-c1cc-c17d3208850c"
965
  },
966
+ "execution_count": null,
967
  "outputs": [
968
  {
969
  "output_type": "execute_result",
 
997
  "metadata": {
998
  "id": "YizvmXPejkJE"
999
  },
1000
+ "execution_count": null,
1001
  "outputs": []
1002
  },
1003
  {
 
1014
  "source": [
1015
  "from llama_index.text_splitter import TokenTextSplitter\n",
1016
  "\n",
1017
+ "# Define the splitter object that split the text into segments with 512 tokens,\n",
1018
+ "# with a 128 overlap between the segments.\n",
1019
  "text_splitter = TokenTextSplitter(\n",
1020
  " separator=\" \", chunk_size=512, chunk_overlap=128\n",
1021
  ")"
 
1037
  "from llama_index.embeddings import OpenAIEmbedding\n",
1038
  "from llama_index.ingestion import IngestionPipeline\n",
1039
  "\n",
1040
+ "# Create the pipeline to apply the transformation on each chunk,\n",
1041
+ "# and store the transformed text in the chroma vector store.\n",
1042
  "pipeline = IngestionPipeline(\n",
1043
  " transformations=[\n",
1044
  " text_splitter,\n",
 
1165
  {
1166
  "cell_type": "code",
1167
  "source": [
1168
+ "# Compress the vector store directory to a zip file to be able to download and use later.\n",
1169
  "!zip -r vectorstore.zip mini-llama-articles"
1170
  ],
1171
  "metadata": {
 
1183
  "id": "OWaT6rL7ksp8"
1184
  }
1185
  },
1186
+ {
1187
+ "cell_type": "markdown",
1188
+ "source": [
1189
+ "If you have already uploaded the zip file for the vector store checkpoint, please uncomment the code in the following cell block to extract its contents. After doing so, you will be able to load the dataset from local storage."
1190
+ ],
1191
+ "metadata": {
1192
+ "id": "BLkmv3Yxp9mu"
1193
+ }
1194
+ },
1195
  {
1196
  "cell_type": "code",
1197
  "source": [
1198
+ "# !unzip vectorstore.zip"
1199
  ],
1200
  "metadata": {
1201
  "colab": {
 
1204
  "id": "SodY2Xpf_kxg",
1205
  "outputId": "a6f7ae4a-447c-4222-e400-0fe55e7e26d9"
1206
  },
1207
+ "execution_count": null,
1208
  "outputs": [
1209
  {
1210
  "output_type": "stream",
 
1228
  "import chromadb\n",
1229
  "from llama_index.vector_stores import ChromaVectorStore\n",
1230
  "\n",
1231
+ "# Load the vector store from the local storage.\n",
1232
  "db = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
1233
  "chroma_collection = db.get_or_create_collection(\"mini-llama-articles\")\n",
1234
  "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
 
1236
  "metadata": {
1237
  "id": "mXi56KTXk2sp"
1238
  },
1239
+ "execution_count": null,
1240
  "outputs": []
1241
  },
1242
  {
1243
  "cell_type": "code",
1244
  "source": [
 
1245
  "from llama_index import VectorStoreIndex\n",
1246
  "\n",
1247
+ "# Create the index based on the vector store.\n",
1248
  "vector_index = VectorStoreIndex.from_vector_store(vector_store)"
1249
  ],
1250
  "metadata": {
1251
  "id": "jKXURvLtkuTS"
1252
  },
1253
+ "execution_count": null,
1254
  "outputs": []
1255
  },
1256
  {
 
1265
  {
1266
  "cell_type": "code",
1267
  "source": [
1268
+ "# A simple function to show the response and the sources.\n",
1269
  "def display_res(response):\n",
1270
  " print(\"Response:\\n\\t\", response.response.replace(\"\\n\", \"\") )\n",
1271
  "\n",
 
1282
  "metadata": {
1283
  "id": "4JpaHEmF5dSS"
1284
  },
1285
+ "execution_count": null,
1286
  "outputs": []
1287
  },
1288
  {
 
1297
  {
1298
  "cell_type": "code",
1299
  "source": [
1300
+ "# define the chat_engine by using the index\n",
1301
  "chat_engine = vector_index.as_chat_engine() #chat_mode=\"best\""
1302
  ],
1303
  "metadata": {
1304
  "id": "kwWlDpoR1cRI"
1305
  },
1306
+ "execution_count": null,
1307
  "outputs": []
1308
  },
1309
  {
1310
  "cell_type": "code",
1311
  "source": [
1312
+ "# First Question:\n",
1313
  "response = chat_engine.chat(\"Use the tool to answer, How many parameters LLaMA2 model has?\")\n",
1314
  "display_res(response)"
1315
  ],
 
1320
  "id": "ER3Lb-oN46lJ",
1321
  "outputId": "8b34da39-622f-43f2-cb45-01a1ff37efd7"
1322
  },
1323
+ "execution_count": null,
1324
  "outputs": [
1325
  {
1326
  "output_type": "stream",
 
1344
  {
1345
  "cell_type": "code",
1346
  "source": [
1347
+ "# Second Question:\n",
1348
  "response = chat_engine.chat(\"Tell me a joke?\")\n",
1349
  "display_res(response)"
1350
  ],
 
1355
  "id": "3RRmiJEQ5R1Q",
1356
  "outputId": "15efcc9b-583f-4efe-8e36-fa8b5160da16"
1357
  },
1358
+ "execution_count": null,
1359
  "outputs": [
1360
  {
1361
  "output_type": "stream",
 
1379
  {
1380
  "cell_type": "code",
1381
  "source": [
1382
+ "# Third Question: (check if it can recall previous interactions)\n",
1383
  "response = chat_engine.chat(\"What was the first question I asked?\")\n",
1384
  "display_res(response)"
1385
  ],
 
1390
  "id": "8eOzp5Xc5Vbj",
1391
  "outputId": "13bc6714-dd89-45b3-a86b-759806245241"
1392
  },
1393
+ "execution_count": null,
1394
  "outputs": [
1395
  {
1396
  "output_type": "stream",
 
1407
  {
1408
  "cell_type": "code",
1409
  "source": [
1410
+ "# Reset the session to clear the memory\n",
1411
  "chat_engine.reset()"
1412
  ],
1413
  "metadata": {
1414
  "id": "7jfiLpru5VZT"
1415
  },
1416
+ "execution_count": null,
1417
  "outputs": []
1418
  },
1419
  {
1420
  "cell_type": "code",
1421
  "source": [
1422
+ "# Fourth Question: (don't recall the previous interactions.)\n",
1423
  "response = chat_engine.chat(\"What was the first question I asked?\")\n",
1424
  "display_res(response)"
1425
  ],
 
1430
  "id": "Jt0q8RW25VXN",
1431
  "outputId": "0e2d0d4e-c0ff-48bf-8df3-478fcdc66abd"
1432
  },
1433
+ "execution_count": null,
1434
  "outputs": [
1435
  {
1436
  "output_type": "stream",
 
1456
  {
1457
  "cell_type": "code",
1458
  "source": [
1459
+ "# Stream the words as soon as they are available instead of waiting for the model to finish generation.\n",
1460
  "streaming_response = chat_engine.stream_chat(\"Write a paragraph about the LLaMA2 model's capabilities.\")\n",
1461
  "for token in streaming_response.response_gen:\n",
1462
  " print(token, end=\"\")"
 
1468
  "id": "zanJeMbaPJcq",
1469
  "outputId": "de7f0905-c1b1-49ac-fb66-d1578da35cad"
1470
  },
1471
+ "execution_count": null,
1472
  "outputs": [
1473
  {
1474
  "output_type": "stream",
 
1489
  "id": "DuRgOJ2AHMJh"
1490
  }
1491
  },
1492
+ {
1493
+ "cell_type": "markdown",
1494
+ "source": [
1495
+ "Enhance the input prompt by looking at the previous chat history along with the present question. The refined prompt can then be used to fetch the nodes."
1496
+ ],
1497
+ "metadata": {
1498
+ "id": "Yb2Lt41jq145"
1499
+ }
1500
+ },
1501
  {
1502
  "cell_type": "code",
1503
  "source": [
1504
+ "# Define GPT-4 model that will be used by the chat_engine to improve the query.\n",
1505
  "gpt4 = OpenAI(temperature=0.9, model=\"gpt-4\")"
1506
  ],
1507
  "metadata": {
1508
  "id": "v0gmM5LGIaRl"
1509
  },
1510
+ "execution_count": null,
1511
  "outputs": []
1512
  },
1513
  {
 
1518
  "metadata": {
1519
  "id": "EDWsaBTBIhK7"
1520
  },
1521
+ "execution_count": null,
1522
  "outputs": []
1523
  },
1524
  {
 
1534
  "id": "h4c--hJ75VU2",
1535
  "outputId": "e80fd9bf-e6d5-4532-8771-8cbf781e782e"
1536
  },
1537
+ "execution_count": null,
1538
  "outputs": [
1539
  {
1540
  "output_type": "stream",
 
1565
  "id": "ysL9ONePOsGB"
1566
  }
1567
  },
1568
+ {
1569
+ "cell_type": "markdown",
1570
+ "source": [
1571
+ "ReAct is an agent-based chat mode that uses a loop to decide on querying a data engine during interactions, offering flexibility but relying on the Large Language Model's quality for effective responses, requiring careful management to avoid inaccurate answers."
1572
+ ],
1573
+ "metadata": {
1574
+ "id": "KiEFmxAtrmF-"
1575
+ }
1576
+ },
1577
  {
1578
  "cell_type": "code",
1579
  "source": [
 
1582
  "metadata": {
1583
  "id": "-M1jWoKXOs2t"
1584
  },
1585
+ "execution_count": null,
1586
  "outputs": []
1587
  },
1588
  {
 
1597
  "id": "UZkEW1SSOs0H",
1598
  "outputId": "4869c5fc-e0e1-44c6-e7f0-87db92bb2eb6"
1599
  },
1600
+ "execution_count": null,
1601
  "outputs": [
1602
  {
1603
  "output_type": "stream",
 
1630
  "id": "eW5P1lD4Osxf",
1631
  "outputId": "b128bc94-081b-49aa-c549-7d7d7be90b63"
1632
  },
1633
+ "execution_count": null,
1634
  "outputs": [
1635
  {
1636
  "output_type": "stream",