Spaces:
Sleeping
Sleeping
Commit ·
8acf68c
1
Parent(s): ad9fa8b
Update the vectorstore creation logic -- create a lighter version of chromadb for production code
Browse files
prototypes/build_rag_datastore.ipynb
CHANGED
|
@@ -10,19 +10,10 @@
|
|
| 10 |
},
|
| 11 |
{
|
| 12 |
"cell_type": "code",
|
| 13 |
-
"execution_count":
|
| 14 |
"id": "b4e0a36f-f8c2-42d1-8920-54396e5737c5",
|
| 15 |
"metadata": {},
|
| 16 |
-
"outputs": [
|
| 17 |
-
{
|
| 18 |
-
"name": "stderr",
|
| 19 |
-
"output_type": "stream",
|
| 20 |
-
"text": [
|
| 21 |
-
"/Volumes/VTG/Dev/C_5/Projects/week8/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 22 |
-
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 23 |
-
]
|
| 24 |
-
}
|
| 25 |
-
],
|
| 26 |
"source": [
|
| 27 |
"import os\n",
|
| 28 |
"from tqdm import tqdm\n",
|
|
@@ -37,7 +28,7 @@
|
|
| 37 |
},
|
| 38 |
{
|
| 39 |
"cell_type": "code",
|
| 40 |
-
"execution_count":
|
| 41 |
"id": "66a1a814-76ef-44d0-b22f-b3568b7195fc",
|
| 42 |
"metadata": {},
|
| 43 |
"outputs": [],
|
|
@@ -53,7 +44,7 @@
|
|
| 53 |
},
|
| 54 |
{
|
| 55 |
"cell_type": "code",
|
| 56 |
-
"execution_count":
|
| 57 |
"id": "fd156583-053e-402d-88a8-aac74e703894",
|
| 58 |
"metadata": {},
|
| 59 |
"outputs": [
|
|
@@ -82,18 +73,30 @@
|
|
| 82 |
},
|
| 83 |
{
|
| 84 |
"cell_type": "code",
|
| 85 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
"id": "35c3e944-1c63-4c65-b32b-99d8b807d671",
|
| 87 |
"metadata": {},
|
| 88 |
"outputs": [],
|
| 89 |
"source": [
|
| 90 |
"with open(\"train.pkl\", \"rb\") as f:\n",
|
| 91 |
-
" train = pickle.load(f)"
|
| 92 |
]
|
| 93 |
},
|
| 94 |
{
|
| 95 |
"cell_type": "code",
|
| 96 |
-
"execution_count":
|
| 97 |
"id": "e89e5566-0974-4524-a2cc-ec45a64bcbe6",
|
| 98 |
"metadata": {},
|
| 99 |
"outputs": [
|
|
@@ -116,6 +119,27 @@
|
|
| 116 |
"print(train[0].prompt)"
|
| 117 |
]
|
| 118 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
{
|
| 120 |
"cell_type": "markdown",
|
| 121 |
"id": "c75e1399-a9b1-466e-9a80-2f61d947defc",
|
|
@@ -128,14 +152,14 @@
|
|
| 128 |
},
|
| 129 |
{
|
| 130 |
"cell_type": "code",
|
| 131 |
-
"execution_count":
|
| 132 |
"id": "5820500d-f2e3-43ad-b0b2-85d73d49112f",
|
| 133 |
"metadata": {},
|
| 134 |
"outputs": [],
|
| 135 |
"source": [
|
| 136 |
-
"
|
| 137 |
"DB_PATH = DB if DEV_MODE else PRODUCTION_DB\n",
|
| 138 |
-
"collection_name = \"products\"
|
| 139 |
"\n",
|
| 140 |
"client = chromadb.PersistentClient(path=DB_PATH)\n",
|
| 141 |
"collection = client.get_or_create_collection(collection_name) "
|
|
@@ -143,30 +167,24 @@
|
|
| 143 |
},
|
| 144 |
{
|
| 145 |
"cell_type": "code",
|
| 146 |
-
"execution_count":
|
| 147 |
"id": "be6d4079-a9e1-442b-9825-2e8f6ffc09c8",
|
| 148 |
"metadata": {},
|
| 149 |
-
"outputs": [
|
| 150 |
-
{
|
| 151 |
-
"name": "stdout",
|
| 152 |
-
"output_type": "stream",
|
| 153 |
-
"text": [
|
| 154 |
-
"Creating new collection.\n"
|
| 155 |
-
]
|
| 156 |
-
}
|
| 157 |
-
],
|
| 158 |
"source": [
|
| 159 |
"### Use the existing collection, otherwise create it if none of it.\n",
|
| 160 |
-
"
|
|
|
|
|
|
|
| 161 |
"\n",
|
| 162 |
-
"existing_collection_names = client.list_collections()\n",
|
| 163 |
"\n",
|
| 164 |
-
"if any(c.name == collection_name for c in existing_collection_names): \n",
|
| 165 |
-
"
|
| 166 |
-
"
|
| 167 |
-
"else: \n",
|
| 168 |
-
"
|
| 169 |
-
"
|
| 170 |
]
|
| 171 |
},
|
| 172 |
{
|
|
@@ -179,7 +197,7 @@
|
|
| 179 |
},
|
| 180 |
{
|
| 181 |
"cell_type": "code",
|
| 182 |
-
"execution_count":
|
| 183 |
"id": "0328e557-fbbb-4412-9c18-dcab38975839",
|
| 184 |
"metadata": {},
|
| 185 |
"outputs": [],
|
|
@@ -203,7 +221,7 @@
|
|
| 203 |
},
|
| 204 |
{
|
| 205 |
"cell_type": "code",
|
| 206 |
-
"execution_count":
|
| 207 |
"id": "d5366cea-3468-4443-a2c6-dce30f774c17",
|
| 208 |
"metadata": {},
|
| 209 |
"outputs": [],
|
|
@@ -222,23 +240,10 @@
|
|
| 222 |
},
|
| 223 |
{
|
| 224 |
"cell_type": "code",
|
| 225 |
-
"execution_count":
|
| 226 |
"id": "5c5c230b-f58a-4617-b0b3-3f928c7b96b1",
|
| 227 |
"metadata": {},
|
| 228 |
-
"outputs": [
|
| 229 |
-
{
|
| 230 |
-
"ename": "NameError",
|
| 231 |
-
"evalue": "name 'model' is not defined",
|
| 232 |
-
"output_type": "error",
|
| 233 |
-
"traceback": [
|
| 234 |
-
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
| 235 |
-
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
|
| 236 |
-
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[12]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mhow_similar\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mJava\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mC++\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 2\u001b[39m how_similar(\u001b[33m\"\u001b[39m\u001b[33mJava\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mmug\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 3\u001b[39m how_similar(\u001b[33m\"\u001b[39m\u001b[33mCup of Java\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mmug\u001b[39m\u001b[33m\"\u001b[39m)\n",
|
| 237 |
-
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 8\u001b[39m, in \u001b[36mhow_similar\u001b[39m\u001b[34m(text1, text2)\u001b[39m\n\u001b[32m 7\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mhow_similar\u001b[39m(text1, text2):\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m vector1, vector2 = \u001b[43mmodel\u001b[49m.encode([text1, text2])\n\u001b[32m 9\u001b[39m similarity = cosine_similarity(vector1, vector2)\n\u001b[32m 10\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mSimilarity between \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtext1\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m and \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtext2\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m is \u001b[39m\u001b[38;5;132;01m{\u001b[39;00msimilarity*\u001b[32m100\u001b[39m\u001b[38;5;132;01m:\u001b[39;00m\u001b[33m.1f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m%\u001b[39m\u001b[33m\"\u001b[39m)\n",
|
| 238 |
-
"\u001b[31mNameError\u001b[39m: name 'model' is not defined"
|
| 239 |
-
]
|
| 240 |
-
}
|
| 241 |
-
],
|
| 242 |
"source": [
|
| 243 |
"how_similar(\"Java\", \"C++\")\n",
|
| 244 |
"how_similar(\"Java\", \"mug\")\n",
|
|
@@ -247,7 +252,7 @@
|
|
| 247 |
},
|
| 248 |
{
|
| 249 |
"cell_type": "code",
|
| 250 |
-
"execution_count":
|
| 251 |
"id": "b0b743ff-2606-47fc-be86-69c62ec788b1",
|
| 252 |
"metadata": {},
|
| 253 |
"outputs": [],
|
|
@@ -268,18 +273,10 @@
|
|
| 268 |
},
|
| 269 |
{
|
| 270 |
"cell_type": "code",
|
| 271 |
-
"execution_count":
|
| 272 |
"id": "373ba2ae-db0c-4ff0-a27f-9a442a6590a0",
|
| 273 |
"metadata": {},
|
| 274 |
-
"outputs": [
|
| 275 |
-
{
|
| 276 |
-
"name": "stderr",
|
| 277 |
-
"output_type": "stream",
|
| 278 |
-
"text": [
|
| 279 |
-
"100%|████████████████████████████████████████████████████████████████| 150/150 [17:43<00:00, 7.09s/it]\n"
|
| 280 |
-
]
|
| 281 |
-
}
|
| 282 |
-
],
|
| 283 |
"source": [
|
| 284 |
"NUMBER_OF_DOCS = len(train)\n",
|
| 285 |
"\n",
|
|
|
|
| 10 |
},
|
| 11 |
{
|
| 12 |
"cell_type": "code",
|
| 13 |
+
"execution_count": 7,
|
| 14 |
"id": "b4e0a36f-f8c2-42d1-8920-54396e5737c5",
|
| 15 |
"metadata": {},
|
| 16 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"source": [
|
| 18 |
"import os\n",
|
| 19 |
"from tqdm import tqdm\n",
|
|
|
|
| 28 |
},
|
| 29 |
{
|
| 30 |
"cell_type": "code",
|
| 31 |
+
"execution_count": 8,
|
| 32 |
"id": "66a1a814-76ef-44d0-b22f-b3568b7195fc",
|
| 33 |
"metadata": {},
|
| 34 |
"outputs": [],
|
|
|
|
| 44 |
},
|
| 45 |
{
|
| 46 |
"cell_type": "code",
|
| 47 |
+
"execution_count": 9,
|
| 48 |
"id": "fd156583-053e-402d-88a8-aac74e703894",
|
| 49 |
"metadata": {},
|
| 50 |
"outputs": [
|
|
|
|
| 73 |
},
|
| 74 |
{
|
| 75 |
"cell_type": "code",
|
| 76 |
+
"execution_count": 10,
|
| 77 |
+
"id": "bb0bd7c2-2194-482a-8afb-c1cb4643875b",
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": [
|
| 81 |
+
"### Want to create cvector db for local development or production?\n",
|
| 82 |
+
"### If DEV_MODE = false(for production code), lighter chromadb will be created due to the hosting file size limit\n",
|
| 83 |
+
"DEV_MODE = False"
|
| 84 |
+
]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"cell_type": "code",
|
| 88 |
+
"execution_count": 11,
|
| 89 |
"id": "35c3e944-1c63-4c65-b32b-99d8b807d671",
|
| 90 |
"metadata": {},
|
| 91 |
"outputs": [],
|
| 92 |
"source": [
|
| 93 |
"with open(\"train.pkl\", \"rb\") as f:\n",
|
| 94 |
+
" train = pickle.load(f) if DEV_MODE else pickle.load(f)[:100000]"
|
| 95 |
]
|
| 96 |
},
|
| 97 |
{
|
| 98 |
"cell_type": "code",
|
| 99 |
+
"execution_count": 12,
|
| 100 |
"id": "e89e5566-0974-4524-a2cc-ec45a64bcbe6",
|
| 101 |
"metadata": {},
|
| 102 |
"outputs": [
|
|
|
|
| 119 |
"print(train[0].prompt)"
|
| 120 |
]
|
| 121 |
},
|
| 122 |
+
{
|
| 123 |
+
"cell_type": "code",
|
| 124 |
+
"execution_count": 13,
|
| 125 |
+
"id": "038b88c2-ced5-43e0-9f21-735e4460e7db",
|
| 126 |
+
"metadata": {},
|
| 127 |
+
"outputs": [
|
| 128 |
+
{
|
| 129 |
+
"data": {
|
| 130 |
+
"text/plain": [
|
| 131 |
+
"100000"
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
"execution_count": 13,
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"output_type": "execute_result"
|
| 137 |
+
}
|
| 138 |
+
],
|
| 139 |
+
"source": [
|
| 140 |
+
"len(train)"
|
| 141 |
+
]
|
| 142 |
+
},
|
| 143 |
{
|
| 144 |
"cell_type": "markdown",
|
| 145 |
"id": "c75e1399-a9b1-466e-9a80-2f61d947defc",
|
|
|
|
| 152 |
},
|
| 153 |
{
|
| 154 |
"cell_type": "code",
|
| 155 |
+
"execution_count": null,
|
| 156 |
"id": "5820500d-f2e3-43ad-b0b2-85d73d49112f",
|
| 157 |
"metadata": {},
|
| 158 |
"outputs": [],
|
| 159 |
"source": [
|
| 160 |
+
"\n",
|
| 161 |
"DB_PATH = DB if DEV_MODE else PRODUCTION_DB\n",
|
| 162 |
+
"collection_name = \"products\"\n",
|
| 163 |
"\n",
|
| 164 |
"client = chromadb.PersistentClient(path=DB_PATH)\n",
|
| 165 |
"collection = client.get_or_create_collection(collection_name) "
|
|
|
|
| 167 |
},
|
| 168 |
{
|
| 169 |
"cell_type": "code",
|
| 170 |
+
"execution_count": null,
|
| 171 |
"id": "be6d4079-a9e1-442b-9825-2e8f6ffc09c8",
|
| 172 |
"metadata": {},
|
| 173 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
"source": [
|
| 175 |
"### Use the existing collection, otherwise create it if none of it.\n",
|
| 176 |
+
"###### NOTE: simply get_or_create_collection does the job perfectly without this messy lines of code\n",
|
| 177 |
+
"\n",
|
| 178 |
+
"# collection_name = \"products\"\n",
|
| 179 |
"\n",
|
| 180 |
+
"# existing_collection_names = client.list_collections()\n",
|
| 181 |
"\n",
|
| 182 |
+
"# if any(c.name == collection_name for c in existing_collection_names): \n",
|
| 183 |
+
"# print(\"Collection already exists. Using it.\")\n",
|
| 184 |
+
"# collection = client.get_collection(collection_name)\n",
|
| 185 |
+
"# else: \n",
|
| 186 |
+
"# print(\"Creating new collection.\")\n",
|
| 187 |
+
"# collection = client.create_collection(collection_name)"
|
| 188 |
]
|
| 189 |
},
|
| 190 |
{
|
|
|
|
| 197 |
},
|
| 198 |
{
|
| 199 |
"cell_type": "code",
|
| 200 |
+
"execution_count": null,
|
| 201 |
"id": "0328e557-fbbb-4412-9c18-dcab38975839",
|
| 202 |
"metadata": {},
|
| 203 |
"outputs": [],
|
|
|
|
| 221 |
},
|
| 222 |
{
|
| 223 |
"cell_type": "code",
|
| 224 |
+
"execution_count": null,
|
| 225 |
"id": "d5366cea-3468-4443-a2c6-dce30f774c17",
|
| 226 |
"metadata": {},
|
| 227 |
"outputs": [],
|
|
|
|
| 240 |
},
|
| 241 |
{
|
| 242 |
"cell_type": "code",
|
| 243 |
+
"execution_count": null,
|
| 244 |
"id": "5c5c230b-f58a-4617-b0b3-3f928c7b96b1",
|
| 245 |
"metadata": {},
|
| 246 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
"source": [
|
| 248 |
"how_similar(\"Java\", \"C++\")\n",
|
| 249 |
"how_similar(\"Java\", \"mug\")\n",
|
|
|
|
| 252 |
},
|
| 253 |
{
|
| 254 |
"cell_type": "code",
|
| 255 |
+
"execution_count": null,
|
| 256 |
"id": "b0b743ff-2606-47fc-be86-69c62ec788b1",
|
| 257 |
"metadata": {},
|
| 258 |
"outputs": [],
|
|
|
|
| 273 |
},
|
| 274 |
{
|
| 275 |
"cell_type": "code",
|
| 276 |
+
"execution_count": null,
|
| 277 |
"id": "373ba2ae-db0c-4ff0-a27f-9a442a6590a0",
|
| 278 |
"metadata": {},
|
| 279 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
"source": [
|
| 281 |
"NUMBER_OF_DOCS = len(train)\n",
|
| 282 |
"\n",
|