Created using Colaboratory
Browse files
notebooks/06-RAG_Improve_Chunking.ipynb
ADDED
@@ -0,0 +1,380 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": [],
|
7 |
+
"authorship_tag": "ABX9TyOXQSTuXN8LHQooW46XZ9Xr",
|
8 |
+
"include_colab_link": true
|
9 |
+
},
|
10 |
+
"kernelspec": {
|
11 |
+
"name": "python3",
|
12 |
+
"display_name": "Python 3"
|
13 |
+
},
|
14 |
+
"language_info": {
|
15 |
+
"name": "python"
|
16 |
+
}
|
17 |
+
},
|
18 |
+
"cells": [
|
19 |
+
{
|
20 |
+
"cell_type": "markdown",
|
21 |
+
"metadata": {
|
22 |
+
"id": "view-in-github",
|
23 |
+
"colab_type": "text"
|
24 |
+
},
|
25 |
+
"source": [
|
26 |
+
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/06-RAG_Improve_Chunking.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "code",
|
31 |
+
"execution_count": 1,
|
32 |
+
"metadata": {
|
33 |
+
"id": "QPJzr-I9XQ7l",
|
34 |
+
"colab": {
|
35 |
+
"base_uri": "https://localhost:8080/"
|
36 |
+
},
|
37 |
+
"outputId": "971cf4cb-ee33-477b-cc7d-d652b55b81f3"
|
38 |
+
},
|
39 |
+
"outputs": [
|
40 |
+
{
|
41 |
+
"output_type": "stream",
|
42 |
+
"name": "stdout",
|
43 |
+
"text": [
|
44 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m15.7/15.7 MB\u001b[0m \u001b[31m41.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
45 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m225.4/225.4 kB\u001b[0m \u001b[31m16.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
46 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m51.7/51.7 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
47 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m45.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
48 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m508.6/508.6 kB\u001b[0m \u001b[31m32.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
49 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m79.9/79.9 MB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
50 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.7/45.7 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
51 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.0/143.0 kB\u001b[0m \u001b[31m9.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
52 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.9/75.9 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
53 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m53.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
54 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.4/2.4 MB\u001b[0m \u001b[31m53.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
55 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.1/92.1 kB\u001b[0m \u001b[31m7.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
56 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.3/60.3 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
57 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.4/5.4 MB\u001b[0m \u001b[31m57.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
58 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.4/6.4 MB\u001b[0m \u001b[31m66.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
59 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.9/57.9 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
60 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m105.6/105.6 kB\u001b[0m \u001b[31m11.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
61 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.3/67.3 kB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
62 |
+
"\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
|
63 |
+
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
|
64 |
+
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
|
65 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m698.9/698.9 kB\u001b[0m \u001b[31m54.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
66 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m82.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
67 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m72.6/72.6 kB\u001b[0m \u001b[31m1.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
68 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m68.9/68.9 kB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
69 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.9/76.9 kB\u001b[0m \u001b[31m9.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
70 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
71 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.8/143.8 kB\u001b[0m \u001b[31m17.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
72 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
73 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.8/50.8 kB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
74 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m37.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
75 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m87.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
76 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m76.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
77 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.2/130.2 kB\u001b[0m \u001b[31m16.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
78 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
79 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m11.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
80 |
+
"\u001b[?25h Building wheel for pypika (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
|
81 |
+
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
|
82 |
+
"tensorflow-probability 0.22.0 requires typing-extensions<4.6.0, but you have typing-extensions 4.9.0 which is incompatible.\u001b[0m\u001b[31m\n",
|
83 |
+
"\u001b[0m"
|
84 |
+
]
|
85 |
+
}
|
86 |
+
],
|
87 |
+
"source": [
|
88 |
+
"!pip install -q llama-index==0.9.21 openai==1.6.0 cohere==4.39 tiktoken==0.5.2 chromadb==0.4.21 kaleido==0.2.1 python-multipart==0.0.6"
|
89 |
+
]
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"cell_type": "code",
|
93 |
+
"source": [
|
94 |
+
"import os\n",
|
95 |
+
"\n",
|
96 |
+
"os.environ[\"OPENAI_API_KEY\"] = \"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\""
|
97 |
+
],
|
98 |
+
"metadata": {
|
99 |
+
"id": "riuXwpSPcvWC"
|
100 |
+
},
|
101 |
+
"execution_count": 2,
|
102 |
+
"outputs": []
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"cell_type": "markdown",
|
106 |
+
"source": [
|
107 |
+
"# Load the Dataset (CSV)"
|
108 |
+
],
|
109 |
+
"metadata": {
|
110 |
+
"id": "I9JbAzFcjkpn"
|
111 |
+
}
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"cell_type": "code",
|
115 |
+
"source": [
|
116 |
+
"!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-dataset.csv"
|
117 |
+
],
|
118 |
+
"metadata": {
|
119 |
+
"colab": {
|
120 |
+
"base_uri": "https://localhost:8080/"
|
121 |
+
},
|
122 |
+
"id": "wl_pbPvMlv1h",
|
123 |
+
"outputId": "70f7f4be-7b80-431b-8570-f388eb21878f"
|
124 |
+
},
|
125 |
+
"execution_count": 3,
|
126 |
+
"outputs": [
|
127 |
+
{
|
128 |
+
"output_type": "stream",
|
129 |
+
"name": "stdout",
|
130 |
+
"text": [
|
131 |
+
"--2023-12-26 19:25:41-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-dataset.csv\n",
|
132 |
+
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.108.133, ...\n",
|
133 |
+
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.\n",
|
134 |
+
"HTTP request sent, awaiting response... 200 OK\n",
|
135 |
+
"Length: 23689 (23K) [text/plain]\n",
|
136 |
+
"Saving to: ‘mini-dataset.csv’\n",
|
137 |
+
"\n",
|
138 |
+
"mini-dataset.csv 100%[===================>] 23.13K --.-KB/s in 0.007s \n",
|
139 |
+
"\n",
|
140 |
+
"2023-12-26 19:25:41 (3.10 MB/s) - ‘mini-dataset.csv’ saved [23689/23689]\n",
|
141 |
+
"\n"
|
142 |
+
]
|
143 |
+
}
|
144 |
+
]
|
145 |
+
},
|
146 |
+
{
|
147 |
+
"cell_type": "code",
|
148 |
+
"source": [
|
149 |
+
"from llama_index import download_loader\n",
|
150 |
+
"\n",
|
151 |
+
"SimpleCSVReader = download_loader(\"SimpleCSVReader\")\n",
|
152 |
+
"\n",
|
153 |
+
"loader = SimpleCSVReader(encoding=\"ISO-8859-1\")\n",
|
154 |
+
"documents = loader.load_data(file='./mini-dataset.csv')"
|
155 |
+
],
|
156 |
+
"metadata": {
|
157 |
+
"id": "0Q9sxuW0g3Gd"
|
158 |
+
},
|
159 |
+
"execution_count": 4,
|
160 |
+
"outputs": []
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"cell_type": "markdown",
|
164 |
+
"source": [
|
165 |
+
"# Chunking"
|
166 |
+
],
|
167 |
+
"metadata": {
|
168 |
+
"id": "S17g2RYOjmf2"
|
169 |
+
}
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"cell_type": "code",
|
173 |
+
"source": [
|
174 |
+
"from llama_index import ServiceContext\n",
|
175 |
+
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
|
176 |
+
"\n",
|
177 |
+
"# We use OpenAI's embedding model \"text-embedding-ada-002\"\n",
|
178 |
+
"embed_model = OpenAIEmbedding()\n",
|
179 |
+
"\n",
|
180 |
+
"# initialize service context (set chunk size)\n",
|
181 |
+
"service_context = ServiceContext.from_defaults(chunk_size=512, chunk_overlap=64, embed_model=embed_model)"
|
182 |
+
],
|
183 |
+
"metadata": {
|
184 |
+
"id": "YizvmXPejkJE"
|
185 |
+
},
|
186 |
+
"execution_count": 5,
|
187 |
+
"outputs": []
|
188 |
+
},
|
189 |
+
{
|
190 |
+
"cell_type": "markdown",
|
191 |
+
"source": [
|
192 |
+
"### Test chunking"
|
193 |
+
],
|
194 |
+
"metadata": {
|
195 |
+
"id": "ROMhNRvolTmI"
|
196 |
+
}
|
197 |
+
},
|
198 |
+
{
|
199 |
+
"cell_type": "code",
|
200 |
+
"source": [
|
201 |
+
"node_parser = service_context.node_parser\n",
|
202 |
+
"\n",
|
203 |
+
"nodes = node_parser.get_nodes_from_documents(documents)\n",
|
204 |
+
"len( nodes )"
|
205 |
+
],
|
206 |
+
"metadata": {
|
207 |
+
"colab": {
|
208 |
+
"base_uri": "https://localhost:8080/"
|
209 |
+
},
|
210 |
+
"id": "Oe_ePZh7lVmQ",
|
211 |
+
"outputId": "8f9a2250-2c8f-4f92-f6e6-037f3a18cdbb"
|
212 |
+
},
|
213 |
+
"execution_count": 6,
|
214 |
+
"outputs": [
|
215 |
+
{
|
216 |
+
"output_type": "execute_result",
|
217 |
+
"data": {
|
218 |
+
"text/plain": [
|
219 |
+
"13"
|
220 |
+
]
|
221 |
+
},
|
222 |
+
"metadata": {},
|
223 |
+
"execution_count": 6
|
224 |
+
}
|
225 |
+
]
|
226 |
+
},
|
227 |
+
{
|
228 |
+
"cell_type": "markdown",
|
229 |
+
"source": [
|
230 |
+
"# Save on Chroma"
|
231 |
+
],
|
232 |
+
"metadata": {
|
233 |
+
"id": "OWaT6rL7ksp8"
|
234 |
+
}
|
235 |
+
},
|
236 |
+
{
|
237 |
+
"cell_type": "code",
|
238 |
+
"source": [
|
239 |
+
"import chromadb\n",
|
240 |
+
"\n",
|
241 |
+
"# create client and a new collection\n",
|
242 |
+
"# chromadb.EphemeralClient to save in-memory.\n",
|
243 |
+
"chroma_client = chromadb.PersistentClient(path=\"./mini-dataset\")\n",
|
244 |
+
"chroma_collection = chroma_client.create_collection(\"mini-dataset\")"
|
245 |
+
],
|
246 |
+
"metadata": {
|
247 |
+
"id": "mXi56KTXk2sp"
|
248 |
+
},
|
249 |
+
"execution_count": 7,
|
250 |
+
"outputs": []
|
251 |
+
},
|
252 |
+
{
|
253 |
+
"cell_type": "code",
|
254 |
+
"source": [
|
255 |
+
"from llama_index.vector_stores import ChromaVectorStore\n",
|
256 |
+
"from llama_index.storage.storage_context import StorageContext\n",
|
257 |
+
"\n",
|
258 |
+
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
|
259 |
+
"storage_context = StorageContext.from_defaults(vector_store=vector_store)"
|
260 |
+
],
|
261 |
+
"metadata": {
|
262 |
+
"id": "jKXURvLtkuTS"
|
263 |
+
},
|
264 |
+
"execution_count": 8,
|
265 |
+
"outputs": []
|
266 |
+
},
|
267 |
+
{
|
268 |
+
"cell_type": "code",
|
269 |
+
"source": [
|
270 |
+
"from llama_index import VectorStoreIndex\n",
|
271 |
+
"\n",
|
272 |
+
"index = VectorStoreIndex.from_documents(\n",
|
273 |
+
" documents, storage_context=storage_context, service_context=service_context\n",
|
274 |
+
")"
|
275 |
+
],
|
276 |
+
"metadata": {
|
277 |
+
"id": "WsD52wtrlESi"
|
278 |
+
},
|
279 |
+
"execution_count": 9,
|
280 |
+
"outputs": []
|
281 |
+
},
|
282 |
+
{
|
283 |
+
"cell_type": "markdown",
|
284 |
+
"source": [
|
285 |
+
"# Query Dataset"
|
286 |
+
],
|
287 |
+
"metadata": {
|
288 |
+
"id": "8JPD8yAinVSq"
|
289 |
+
}
|
290 |
+
},
|
291 |
+
{
|
292 |
+
"cell_type": "code",
|
293 |
+
"source": [
|
294 |
+
"query_engine = index.as_query_engine()"
|
295 |
+
],
|
296 |
+
"metadata": {
|
297 |
+
"id": "mzS13x1ZlZ5X"
|
298 |
+
},
|
299 |
+
"execution_count": 10,
|
300 |
+
"outputs": []
|
301 |
+
},
|
302 |
+
{
|
303 |
+
"cell_type": "code",
|
304 |
+
"source": [
|
305 |
+
"response = query_engine.query(\n",
|
306 |
+
" \"How many parameters LLaMA2 model has?\"\n",
|
307 |
+
")\n"
|
308 |
+
],
|
309 |
+
"metadata": {
|
310 |
+
"id": "sb8f_wwPnZcG"
|
311 |
+
},
|
312 |
+
"execution_count": 11,
|
313 |
+
"outputs": []
|
314 |
+
},
|
315 |
+
{
|
316 |
+
"cell_type": "code",
|
317 |
+
"source": [
|
318 |
+
"print(f\"Answer: \\n\\t{response}\\n\\n\\nSources:\")\n",
|
319 |
+
"\n",
|
320 |
+
"for idx, source in enumerate( response.source_nodes ):\n",
|
321 |
+
" print(\">\", idx+1)\n",
|
322 |
+
" print(source.node)\n",
|
323 |
+
" print(source.score)\n",
|
324 |
+
" print(\"_-\"*40)"
|
325 |
+
],
|
326 |
+
"metadata": {
|
327 |
+
"colab": {
|
328 |
+
"base_uri": "https://localhost:8080/"
|
329 |
+
},
|
330 |
+
"id": "N3Ri8E5Dl4Ar",
|
331 |
+
"outputId": "6de37908-c3b0-41c9-e74e-8ad03b53ae6c"
|
332 |
+
},
|
333 |
+
"execution_count": 30,
|
334 |
+
"outputs": [
|
335 |
+
{
|
336 |
+
"output_type": "stream",
|
337 |
+
"name": "stdout",
|
338 |
+
"text": [
|
339 |
+
"Answer: \n",
|
340 |
+
"\tThe Llama 2 model is available in four different sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters.\n",
|
341 |
+
"\n",
|
342 |
+
"\n",
|
343 |
+
"Sources:\n",
|
344 |
+
"> 1\n",
|
345 |
+
"Node ID: c8db296d-ad40-4f56-b67a-15d5d5807b36\n",
|
346 |
+
"Text: Meta has once again pushed the boundaries of AI with the release\n",
|
347 |
+
"of Llama 2, the highly anticipated successor to its groundbreaking\n",
|
348 |
+
"Llama 1 language model. Boasting a range of cutting-edge features,\n",
|
349 |
+
"Llama 2 has already disrupted the AI landscape and poses a real\n",
|
350 |
+
"challenge to ChatGPTÕs dominance. In this article, we will dive into\n",
|
351 |
+
"the exciting wo...\n",
|
352 |
+
"0.7188979822197016\n",
|
353 |
+
"_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n",
|
354 |
+
"> 2\n",
|
355 |
+
"Node ID: 2c1194e4-df31-474f-85a4-b19d16b4ece7\n",
|
356 |
+
"Text: Source: Meta Llama 2 paper Finding the right balance between\n",
|
357 |
+
"helpfulness and safety when optimizing a model poses significant\n",
|
358 |
+
"challenges. While a highly helpful model may be capable of answering\n",
|
359 |
+
"any question, including sensitive ones like ÒHow do I build a bomb?Ó,\n",
|
360 |
+
"it also raises concerns about potential misuse. Thus, striking the\n",
|
361 |
+
"perfect equilib...\n",
|
362 |
+
"0.7130334174007259\n",
|
363 |
+
"_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n"
|
364 |
+
]
|
365 |
+
}
|
366 |
+
]
|
367 |
+
},
|
368 |
+
{
|
369 |
+
"cell_type": "code",
|
370 |
+
"source": [
|
371 |
+
"print(response)"
|
372 |
+
],
|
373 |
+
"metadata": {
|
374 |
+
"id": "hjYiWAocnalt"
|
375 |
+
},
|
376 |
+
"execution_count": null,
|
377 |
+
"outputs": []
|
378 |
+
}
|
379 |
+
]
|
380 |
+
}
|