derek-thomas HF staff commited on
Commit
c94e693
โ€ข
1 Parent(s): 6115cdf

Adding updated notebooks

Browse files
notebooks/02_token_analysis.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
notebooks/03_preprocessing.ipynb ADDED
@@ -0,0 +1,825 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "b1b28232-b65d-41ce-88de-fd70b93a528d",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Imports"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 1,
14
+ "id": "abb5186b-ee67-4e1e-882d-3d8d5b4575d4",
15
+ "metadata": {
16
+ "tags": []
17
+ },
18
+ "outputs": [],
19
+ "source": [
20
+ "import json\n",
21
+ "from pathlib import Path\n",
22
+ "import pickle\n",
23
+ "from tqdm.auto import tqdm\n",
24
+ "\n",
25
+ "from haystack.nodes.preprocessor import PreProcessor"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "execution_count": 2,
31
+ "id": "c4b82ea2-8b30-4c2e-99f0-9a30f2f1bfb7",
32
+ "metadata": {
33
+ "tags": []
34
+ },
35
+ "outputs": [
36
+ {
37
+ "name": "stdout",
38
+ "output_type": "stream",
39
+ "text": [
40
+ "/home/ec2-user/arabic-wiki\n"
41
+ ]
42
+ }
43
+ ],
44
+ "source": [
45
+ "proj_dir = Path.cwd().parent\n",
46
+ "print(proj_dir)"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "markdown",
51
+ "id": "76119e74-f601-436d-a253-63c5a19d1c83",
52
+ "metadata": {},
53
+ "source": [
54
+ "# Config"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": 3,
60
+ "id": "f6f74545-54a7-4f41-9f02-96964e1417f0",
61
+ "metadata": {
62
+ "tags": []
63
+ },
64
+ "outputs": [],
65
+ "source": [
66
+ "files_in = list((proj_dir / 'data/consolidated').glob('*.ndjson'))\n",
67
+ "folder_out = proj_dir / 'data/processed'\n",
68
+ "folder_out_str = str(folder_out)"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "markdown",
73
+ "id": "509f41f9-a59f-4171-b61f-ae0cf756fc92",
74
+ "metadata": {},
75
+ "source": [
76
+ "# Analysis"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": 4,
82
+ "id": "f0cbd1c9-3105-4940-85dc-c01ccaa217c7",
83
+ "metadata": {
84
+ "tags": []
85
+ },
86
+ "outputs": [],
87
+ "source": [
88
+ "with open(files_in[0], 'r') as f:\n",
89
+ " articles = [json.loads(line) for line in f]"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "execution_count": 5,
95
+ "id": "004aae7b-1a2f-4a0b-9450-5d80475258b1",
96
+ "metadata": {
97
+ "tags": []
98
+ },
99
+ "outputs": [
100
+ {
101
+ "name": "stdout",
102
+ "output_type": "stream",
103
+ "text": [
104
+ "{'content': 'ุงู„ู…ุงุก ู…ุงุฏุฉูŒ ุดูุงูุฉูŒ ุนุฏูŠู…ุฉ ุงู„ู„ูˆู† ูˆุงู„ุฑุงุฆุญุฉุŒ ูˆู‡ูˆ ุงู„ู…ูƒูˆ...',\n",
105
+ " 'meta': {'id': '7',\n",
106
+ " 'revid': '2080427',\n",
107
+ " 'title': 'ู…ุงุก',\n",
108
+ " 'url': 'https://ar.wikipedia.org/wiki?curid=7'}}\n"
109
+ ]
110
+ }
111
+ ],
112
+ "source": [
113
+ "from pprint import pprint\n",
114
+ "article = articles[0].copy()\n",
115
+ "article['content'] = article['content'][:50] + '...'\n",
116
+ "pprint(article)"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "markdown",
121
+ "id": "6a643cf2-abce-48a9-b4e0-478bcbee28c3",
122
+ "metadata": {},
123
+ "source": [
124
+ "# Preprocessing"
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "markdown",
129
+ "id": "a8f9630e-447e-423e-9f6c-e1dbc654f2dd",
130
+ "metadata": {},
131
+ "source": [
132
+ "Its important to choose good pre-processing options. \n",
133
+ "\n",
134
+ "Clean whitespace helps each stage of RAG. It adds noise to the embeddings, and wastes space when we prompt with it.\n",
135
+ "\n",
136
+ "I chose to split by word as it would be tedious to tokenize here, and that doesnt scale well. The context length for most embedding models ends up being 512 tokens. We saw this within a good z-score is ~225 token.\n",
137
+ "\n",
138
+ "I like to respect the sentence boundary, thats why I gave a ~50 word buffer."
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "code",
143
+ "execution_count": 6,
144
+ "id": "18807aea-24e4-4d74-bf10-55b24f3cb52c",
145
+ "metadata": {
146
+ "tags": []
147
+ },
148
+ "outputs": [],
149
+ "source": [
150
+ "pp = PreProcessor(clean_whitespace = True,\n",
151
+ " clean_header_footer = False,\n",
152
+ " clean_empty_lines = True,\n",
153
+ " remove_substrings = None,\n",
154
+ " split_by='word',\n",
155
+ " split_length = 225,\n",
156
+ " split_overlap = 50,\n",
157
+ " split_respect_sentence_boundary = True,\n",
158
+ " tokenizer_model_folder = None,\n",
159
+ " id_hash_keys = None,\n",
160
+ " progress_bar = False,\n",
161
+ " add_page_number = False,\n",
162
+ " max_chars_check = 10_000)"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "markdown",
167
+ "id": "3c1ab000-6574-485e-87f6-cc210f6e8a61",
168
+ "metadata": {},
169
+ "source": [
170
+ "When we break a wikipedia article up, we lose some of the context. The local context is somewhat preserved by the `split_overlap`. Im trying to preserve the global context by adding a prefix that has the article's title.\n",
171
+ "\n",
172
+ "You could enhance this with the summary as well. This is mostly to help the retrieval step of RAG. Note that the way Im doing it alters some of `haystack`'s features like the hash and the lengths, but those arent too necessary. \n",
173
+ "\n",
174
+ "A more advanced way for many business applications would be to summarize the document and add that as a prefix for sub-documents.\n",
175
+ "\n",
176
+ "One last thing to note, is that it would be prudent (in some use-cases) to preserve the original document without the summary to give to the reader (retrieve with the summary but prompt without), but since this is a demo use-case I wont be doing that."
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "execution_count": 7,
182
+ "id": "63871bdd-0369-4dd7-a65e-ccba29baed44",
183
+ "metadata": {},
184
+ "outputs": [],
185
+ "source": [
186
+ "with open(files_in[0], 'r', encoding='utf-8') as f:\n",
187
+ " articles = [json.loads(line) for line in f]"
188
+ ]
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "execution_count": 8,
193
+ "id": "5c3b48b7-3c0f-41ba-a423-b716649efcaa",
194
+ "metadata": {
195
+ "tags": []
196
+ },
197
+ "outputs": [
198
+ {
199
+ "name": "stderr",
200
+ "output_type": "stream",
201
+ "text": [
202
+ "We found one or more sentences whose word count is higher than the split length.\n",
203
+ "Document e3e2bf8b3399979cb16219b175041b4d is 11336 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
204
+ "Document 91ad1d1a24e93abacabd5a5478a96977 is 14251 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
205
+ "Document 1625c431c0fcfaf81c13e0da59071a81 is 13395 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
206
+ "Document 790d3b2d94a68cbec6d77f3c15d0e679 is 13484 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
207
+ "Document e2dcf80a1f9dfc118aed059255f9b90b is 13217 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
208
+ ]
209
+ },
210
+ {
211
+ "name": "stdout",
212
+ "output_type": "stream",
213
+ "text": [
214
+ "CPU times: user 3min 31s, sys: 95.1 ms, total: 3min 31s\n",
215
+ "Wall time: 3min 31s\n"
216
+ ]
217
+ }
218
+ ],
219
+ "source": [
220
+ "%%time\n",
221
+ "documents = pp.process(articles)"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": 9,
227
+ "id": "de6e1690-131a-41d1-a473-c908c2e40939",
228
+ "metadata": {},
229
+ "outputs": [
230
+ {
231
+ "name": "stderr",
232
+ "output_type": "stream",
233
+ "text": [
234
+ "Document 91ad1d1a24e93abacabd5a5478a96977 is 14251 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
235
+ "Document e3e2bf8b3399979cb16219b175041b4d is 11336 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
236
+ "Document 1625c431c0fcfaf81c13e0da59071a81 is 13395 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
237
+ "Document 790d3b2d94a68cbec6d77f3c15d0e679 is 13484 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
238
+ "Document e2dcf80a1f9dfc118aed059255f9b90b is 13217 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
239
+ ]
240
+ },
241
+ {
242
+ "name": "stdout",
243
+ "output_type": "stream",
244
+ "text": [
245
+ "CPU times: user 6.86 s, sys: 1.31 s, total: 8.16 s\n",
246
+ "Wall time: 1min 33s\n"
247
+ ]
248
+ }
249
+ ],
250
+ "source": [
251
+ "%%time\n",
252
+ "import os\n",
253
+ "import concurrent.futures\n",
254
+ "\n",
255
+ "def parallel_preprocessing(articles):\n",
256
+ " # Utility function to divide the articles into smaller chunks\n",
257
+ " def chunkify(lst, n):\n",
258
+ " \"\"\"Yield successive n-sized chunks from lst.\"\"\"\n",
259
+ " for i in range(0, len(lst), n):\n",
260
+ " yield lst[i:i + n]\n",
261
+ "\n",
262
+ " # Size of each chunk. Adjust based on your needs.\n",
263
+ " CHUNK_SIZE = 10_000 \n",
264
+ " article_chunks = list(chunkify(articles, CHUNK_SIZE))\n",
265
+ "\n",
266
+ " # Number of processes to run in parallel.\n",
267
+ " # Use all available CPUs, but you can reduce the number if you wish to leave some CPUs free.\n",
268
+ " NUM_PROCESSES = os.cpu_count() \n",
269
+ "\n",
270
+ " with concurrent.futures.ProcessPoolExecutor(max_workers=NUM_PROCESSES) as executor:\n",
271
+ " documents_list = list(executor.map(pp.process, article_chunks))\n",
272
+ "\n",
273
+ " # Flatten the documents_list to get a single list of documents\n",
274
+ " documents = [doc for sublist in documents_list for doc in sublist]\n",
275
+ " return documents\n",
276
+ "\n",
277
+ "documents = parallel_preprocessing(articles)\n"
278
+ ]
279
+ },
280
+ {
281
+ "cell_type": "code",
282
+ "execution_count": 10,
283
+ "id": "dab1658a-79a7-40f2-9a8c-1798e0d124bf",
284
+ "metadata": {
285
+ "tags": []
286
+ },
287
+ "outputs": [
288
+ {
289
+ "data": {
290
+ "application/vnd.jupyter.widget-view+json": {
291
+ "model_id": "a4d4ade8158144c6a06f072b550157c3",
292
+ "version_major": 2,
293
+ "version_minor": 0
294
+ },
295
+ "text/plain": [
296
+ " 0%| | 0/23 [00:00<?, ?it/s]"
297
+ ]
298
+ },
299
+ "metadata": {},
300
+ "output_type": "display_data"
301
+ },
302
+ {
303
+ "name": "stderr",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "Document 91ad1d1a24e93abacabd5a5478a96977 is 14251 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
307
+ "Document e3e2bf8b3399979cb16219b175041b4d is 11336 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
308
+ "Document 1625c431c0fcfaf81c13e0da59071a81 is 13395 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
309
+ "Document 790d3b2d94a68cbec6d77f3c15d0e679 is 13484 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
310
+ "Document e2dcf80a1f9dfc118aed059255f9b90b is 13217 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
311
+ ]
312
+ },
313
+ {
314
+ "data": {
315
+ "application/vnd.jupyter.widget-view+json": {
316
+ "model_id": "bcb8fe6999ec408f99ef25c96c85c7b3",
317
+ "version_major": 2,
318
+ "version_minor": 0
319
+ },
320
+ "text/plain": [
321
+ " 0%| | 0/243068 [00:00<?, ?it/s]"
322
+ ]
323
+ },
324
+ "metadata": {},
325
+ "output_type": "display_data"
326
+ },
327
+ {
328
+ "name": "stderr",
329
+ "output_type": "stream",
330
+ "text": [
331
+ "Document 2693d73124e824fb45633e34189a6226 is 14375 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
332
+ ]
333
+ },
334
+ {
335
+ "data": {
336
+ "application/vnd.jupyter.widget-view+json": {
337
+ "model_id": "6ac231651a214181be49584511c97fe9",
338
+ "version_major": 2,
339
+ "version_minor": 0
340
+ },
341
+ "text/plain": [
342
+ " 0%| | 0/104065 [00:00<?, ?it/s]"
343
+ ]
344
+ },
345
+ "metadata": {},
346
+ "output_type": "display_data"
347
+ },
348
+ {
349
+ "data": {
350
+ "application/vnd.jupyter.widget-view+json": {
351
+ "model_id": "c43b5ac1f3b84cac8f2715c5dcf21fb8",
352
+ "version_major": 2,
353
+ "version_minor": 0
354
+ },
355
+ "text/plain": [
356
+ " 0%| | 0/123154 [00:00<?, ?it/s]"
357
+ ]
358
+ },
359
+ "metadata": {},
360
+ "output_type": "display_data"
361
+ },
362
+ {
363
+ "data": {
364
+ "application/vnd.jupyter.widget-view+json": {
365
+ "model_id": "a1b8201fc0ba44cf9ac938556c7effc8",
366
+ "version_major": 2,
367
+ "version_minor": 0
368
+ },
369
+ "text/plain": [
370
+ " 0%| | 0/135965 [00:00<?, ?it/s]"
371
+ ]
372
+ },
373
+ "metadata": {},
374
+ "output_type": "display_data"
375
+ },
376
+ {
377
+ "data": {
378
+ "application/vnd.jupyter.widget-view+json": {
379
+ "model_id": "58ce68b1b5044b0eb1c457495159cb6d",
380
+ "version_major": 2,
381
+ "version_minor": 0
382
+ },
383
+ "text/plain": [
384
+ " 0%| | 0/99138 [00:00<?, ?it/s]"
385
+ ]
386
+ },
387
+ "metadata": {},
388
+ "output_type": "display_data"
389
+ },
390
+ {
391
+ "data": {
392
+ "application/vnd.jupyter.widget-view+json": {
393
+ "model_id": "b80a37f6986e478d85397f7295b67079",
394
+ "version_major": 2,
395
+ "version_minor": 0
396
+ },
397
+ "text/plain": [
398
+ " 0%| | 0/83678 [00:00<?, ?it/s]"
399
+ ]
400
+ },
401
+ "metadata": {},
402
+ "output_type": "display_data"
403
+ },
404
+ {
405
+ "data": {
406
+ "application/vnd.jupyter.widget-view+json": {
407
+ "model_id": "8197d71a396e47588699f22e44c843af",
408
+ "version_major": 2,
409
+ "version_minor": 0
410
+ },
411
+ "text/plain": [
412
+ " 0%| | 0/30573 [00:00<?, ?it/s]"
413
+ ]
414
+ },
415
+ "metadata": {},
416
+ "output_type": "display_data"
417
+ },
418
+ {
419
+ "data": {
420
+ "application/vnd.jupyter.widget-view+json": {
421
+ "model_id": "39024c19258b41c4bcd7b6e5c0617d54",
422
+ "version_major": 2,
423
+ "version_minor": 0
424
+ },
425
+ "text/plain": [
426
+ " 0%| | 0/78957 [00:00<?, ?it/s]"
427
+ ]
428
+ },
429
+ "metadata": {},
430
+ "output_type": "display_data"
431
+ },
432
+ {
433
+ "data": {
434
+ "application/vnd.jupyter.widget-view+json": {
435
+ "model_id": "aa26a27b522448b3bc2808f8ad650a28",
436
+ "version_major": 2,
437
+ "version_minor": 0
438
+ },
439
+ "text/plain": [
440
+ " 0%| | 0/86327 [00:00<?, ?it/s]"
441
+ ]
442
+ },
443
+ "metadata": {},
444
+ "output_type": "display_data"
445
+ },
446
+ {
447
+ "data": {
448
+ "application/vnd.jupyter.widget-view+json": {
449
+ "model_id": "3aae87d4a40d4e9e937646f02c969bb2",
450
+ "version_major": 2,
451
+ "version_minor": 0
452
+ },
453
+ "text/plain": [
454
+ " 0%| | 0/83111 [00:00<?, ?it/s]"
455
+ ]
456
+ },
457
+ "metadata": {},
458
+ "output_type": "display_data"
459
+ },
460
+ {
461
+ "data": {
462
+ "application/vnd.jupyter.widget-view+json": {
463
+ "model_id": "16038478b51a422d8ed7d597c9812999",
464
+ "version_major": 2,
465
+ "version_minor": 0
466
+ },
467
+ "text/plain": [
468
+ " 0%| | 0/92664 [00:00<?, ?it/s]"
469
+ ]
470
+ },
471
+ "metadata": {},
472
+ "output_type": "display_data"
473
+ },
474
+ {
475
+ "data": {
476
+ "application/vnd.jupyter.widget-view+json": {
477
+ "model_id": "0b9ea31fc970402f8368b4be64d91546",
478
+ "version_major": 2,
479
+ "version_minor": 0
480
+ },
481
+ "text/plain": [
482
+ " 0%| | 0/66404 [00:00<?, ?it/s]"
483
+ ]
484
+ },
485
+ "metadata": {},
486
+ "output_type": "display_data"
487
+ },
488
+ {
489
+ "data": {
490
+ "application/vnd.jupyter.widget-view+json": {
491
+ "model_id": "710bf094621c4472bc6d3973619627df",
492
+ "version_major": 2,
493
+ "version_minor": 0
494
+ },
495
+ "text/plain": [
496
+ " 0%| | 0/62844 [00:00<?, ?it/s]"
497
+ ]
498
+ },
499
+ "metadata": {},
500
+ "output_type": "display_data"
501
+ },
502
+ {
503
+ "data": {
504
+ "application/vnd.jupyter.widget-view+json": {
505
+ "model_id": "0bae815b7b7448978fd042b6381b63ba",
506
+ "version_major": 2,
507
+ "version_minor": 0
508
+ },
509
+ "text/plain": [
510
+ " 0%| | 0/59349 [00:00<?, ?it/s]"
511
+ ]
512
+ },
513
+ "metadata": {},
514
+ "output_type": "display_data"
515
+ },
516
+ {
517
+ "data": {
518
+ "application/vnd.jupyter.widget-view+json": {
519
+ "model_id": "483c99d5547a4bedb10802f9bbe414d3",
520
+ "version_major": 2,
521
+ "version_minor": 0
522
+ },
523
+ "text/plain": [
524
+ " 0%| | 0/52554 [00:00<?, ?it/s]"
525
+ ]
526
+ },
527
+ "metadata": {},
528
+ "output_type": "display_data"
529
+ },
530
+ {
531
+ "name": "stderr",
532
+ "output_type": "stream",
533
+ "text": [
534
+ "Document c55a744420239d9865b23f9ff7ab37cc is 23179 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
535
+ "Document 991fef35ead593c41f1dc73224ed7799 is 13179 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
536
+ ]
537
+ },
538
+ {
539
+ "data": {
540
+ "application/vnd.jupyter.widget-view+json": {
541
+ "model_id": "34288f7c2437440ab07134116a6f5bf2",
542
+ "version_major": 2,
543
+ "version_minor": 0
544
+ },
545
+ "text/plain": [
546
+ " 0%| | 0/34240 [00:00<?, ?it/s]"
547
+ ]
548
+ },
549
+ "metadata": {},
550
+ "output_type": "display_data"
551
+ },
552
+ {
553
+ "data": {
554
+ "application/vnd.jupyter.widget-view+json": {
555
+ "model_id": "c6b95ccc5799488980af6f73ab564c8b",
556
+ "version_major": 2,
557
+ "version_minor": 0
558
+ },
559
+ "text/plain": [
560
+ " 0%| | 0/35933 [00:00<?, ?it/s]"
561
+ ]
562
+ },
563
+ "metadata": {},
564
+ "output_type": "display_data"
565
+ },
566
+ {
567
+ "name": "stderr",
568
+ "output_type": "stream",
569
+ "text": [
570
+ "Document c4ae5d66606dcc644293946f8f5c7cab is 10612 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
571
+ ]
572
+ },
573
+ {
574
+ "data": {
575
+ "application/vnd.jupyter.widget-view+json": {
576
+ "model_id": "c6794c5150274e5cb2a09c52bfcf4c1a",
577
+ "version_major": 2,
578
+ "version_minor": 0
579
+ },
580
+ "text/plain": [
581
+ " 0%| | 0/64575 [00:00<?, ?it/s]"
582
+ ]
583
+ },
584
+ "metadata": {},
585
+ "output_type": "display_data"
586
+ },
587
+ {
588
+ "data": {
589
+ "application/vnd.jupyter.widget-view+json": {
590
+ "model_id": "5aaf9358178d4daf9169a14cd71aa20f",
591
+ "version_major": 2,
592
+ "version_minor": 0
593
+ },
594
+ "text/plain": [
595
+ " 0%| | 0/94244 [00:00<?, ?it/s]"
596
+ ]
597
+ },
598
+ "metadata": {},
599
+ "output_type": "display_data"
600
+ },
601
+ {
602
+ "name": "stderr",
603
+ "output_type": "stream",
604
+ "text": [
605
+ "Document 1d1357a32c6cd115525ceb0590e6d67d is 11314 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
606
+ "Document eca1c79de6ca898903e5db0a44a54803 is 24659 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
607
+ "Document 8a59ff0086cc7772046f54a065566ff9 is 14659 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
608
+ ]
609
+ },
610
+ {
611
+ "data": {
612
+ "application/vnd.jupyter.widget-view+json": {
613
+ "model_id": "102f3b5a0c5e46f69742b9f41cc79928",
614
+ "version_major": 2,
615
+ "version_minor": 0
616
+ },
617
+ "text/plain": [
618
+ " 0%| | 0/124472 [00:00<?, ?it/s]"
619
+ ]
620
+ },
621
+ "metadata": {},
622
+ "output_type": "display_data"
623
+ },
624
+ {
625
+ "data": {
626
+ "application/vnd.jupyter.widget-view+json": {
627
+ "model_id": "3e4803f9fc3d405eac97a64489c0d7e0",
628
+ "version_major": 2,
629
+ "version_minor": 0
630
+ },
631
+ "text/plain": [
632
+ " 0%| | 0/121849 [00:00<?, ?it/s]"
633
+ ]
634
+ },
635
+ "metadata": {},
636
+ "output_type": "display_data"
637
+ },
638
+ {
639
+ "data": {
640
+ "application/vnd.jupyter.widget-view+json": {
641
+ "model_id": "d2b4c133d207446c96b4e576aac15bf7",
642
+ "version_major": 2,
643
+ "version_minor": 0
644
+ },
645
+ "text/plain": [
646
+ " 0%| | 0/147110 [00:00<?, ?it/s]"
647
+ ]
648
+ },
649
+ "metadata": {},
650
+ "output_type": "display_data"
651
+ },
652
+ {
653
+ "data": {
654
+ "application/vnd.jupyter.widget-view+json": {
655
+ "model_id": "1cbd39af41ef4b20a8bc1bea7fe8186f",
656
+ "version_major": 2,
657
+ "version_minor": 0
658
+ },
659
+ "text/plain": [
660
+ " 0%| | 0/70322 [00:00<?, ?it/s]"
661
+ ]
662
+ },
663
+ "metadata": {},
664
+ "output_type": "display_data"
665
+ },
666
+ {
667
+ "name": "stdout",
668
+ "output_type": "stream",
669
+ "text": [
670
+ "CPU times: user 2min 21s, sys: 20.1 s, total: 2min 41s\n",
671
+ "Wall time: 13min 36s\n"
672
+ ]
673
+ }
674
+ ],
675
+ "source": [
676
+ "%%time\n",
677
+ "for file_in in tqdm(files_in):\n",
678
+ " # Load articles\n",
679
+ " with open(file_in, 'r', encoding='utf-8') as f:\n",
680
+ " articles = [json.loads(line) for line in f]\n",
681
+ " \n",
682
+ " # Preprocess articles\n",
683
+ " documents = parallel_preprocessing(articles)\n",
684
+ " \n",
685
+ " # Prefix each document's content\n",
686
+ " for document in tqdm(documents):\n",
687
+ " if document.meta['_split_id'] != 0:\n",
688
+ " document.content = f'ุนู†ูˆุงู†: {document.meta[\"title\"]}. ' + document.content\n",
689
+ " \n",
690
+ " processed_articles = [document.to_dict() for document in documents]\n",
691
+ " with open(folder_out/file_in.name, 'w', encoding='utf-8') as f:\n",
692
+ " for article in processed_articles:\n",
693
+ " json_str = json.dumps(article, ensure_ascii=False)\n",
694
+ " f.write(json_str + '\\n')\n",
695
+ " "
696
+ ]
697
+ },
698
+ {
699
+ "cell_type": "markdown",
700
+ "id": "72c1849c-1f4d-411f-b74b-6208b1e48217",
701
+ "metadata": {},
702
+ "source": [
703
+ "## Pre-processing Examples"
704
+ ]
705
+ },
706
+ {
707
+ "cell_type": "code",
708
+ "execution_count": 11,
709
+ "id": "02c1c6c8-6283-49a8-9d29-c355f1b08540",
710
+ "metadata": {
711
+ "tags": []
712
+ },
713
+ "outputs": [
714
+ {
715
+ "data": {
716
+ "text/plain": [
717
+ "<Document: {'content': 'ุนุดุงุก ูƒุงุฑูŠู† ู‡ูˆ ู…ู† ุณู„ุณู„ุฉ ู…ุทุงุนู… ุงุณุชุฑุงู„ูŠุฉ ูŠู‡ุฏู ุนู…ุฏุงู‹ ุนู† ุชุฌุฑุจุฉ ุชู†ุงูˆู„ ุทุนุงู… ุบูŠุฑ ุณุงุฑูŽุฉ ูˆูŠุชู… ุชูˆุฌูŠู‡ ุงู„ู…ูˆุธููŠู† ู„ุฅู‡ุงู†ุฉ ุงู„ุนู…ู„ุงุก ุทูˆุงู„ ูˆุฌุจุงุชู‡ู….\\nุงู‚ุชุจุณ ุงุณู… ุงู„ู…ุทุนู… ู…ู† ุงู„ู…ุตุทู„ุญ ุงู„ุนุงู…ูŠ ุนู„ู‰ ุงู„ุฅู†ุชุฑู†ุช (ูƒุงุฑูŠู†) ูˆุงู„ุฐูŠ ูŠุณุชุฎุฏู… ู„ูˆุตู ุงู…ุฑุฃุฉ ุจูŠุถุงุก ู…ุณู†ุฉ ูˆู‚ุญุฉ ุจุดูƒู„ ู†ู…ุทูŠ.\\nุชุงุฑูŠุฎ ุงู„ู…ุทุนู….\\nุชู… ุฅู†ุดุงุก ุงู„ุณู„ุณู„ุฉ ููŠ ุฃุณุชุฑุงู„ูŠุง (ุณูŠุฏู†ูŠ) ููŠ ุนุงู… 2021 ู…ู† ู‚ุจู„ ุฅูŠุฏูŠู† ู„ูŠูู† ูˆุฌูŠู…ุณ ูุงุฑูŠู„. ุงู„ู…ุทุนู… ุฐูˆ ุทุงุจุน ุฎุงุต ูŠุนุชู…ุฏ ุนู„ู‰ ุฎุฏู…ุฉ ุชุฌุฑุจุฉ ุทุนุงู… ุบูŠุฑ ุณุงุฑุฉ ุญูŠุซ ูŠุฏูุน ุงู„ุนู…ู„ุงุก ู„ู„ู…ูˆุธููŠู† ู„ุฅู‡ุงู†ุชู‡ู… ูˆูƒุงู† ู…ู† ุงู„ู…ูุชุฑุถ ุงู† ูŠูƒูˆู† ุงู„ู…ุทุนู… ู…ุทุนู…ุงู‹ ู…ู†ุจุซู‚ุงู‹ ู„ู…ุฏุฉ ุณุชุฉ ุฃุดู‡ุฑ ููŠ ูˆูˆุฑู„ุฏ ุณูƒูˆูŠุฑ.\\nุงุซุงุฑุช ููƒุฑุฉ ุงู„ู…ุทุนู… ููŠ ุงู„ุจุฏุงูŠุฉ ุฑุฏุงุช ูุนู„ ู…ุชุบุงูŠุฑุฉ ู…ู…ุง ุฃุซุงุฑ ุงู„ุฎูˆู ุจุดุฃู† ู…ุง ุฅุฐุง ูƒุงู†ุช ุงู„ุฅู‡ุงู†ุงุช ุงู„ู…ุชุจุงุฏู„ุฉ ู…ู† ุงู„ู…ู…ูƒู† ุงู† ุชุนุฑุถ ุงู„ู…ูˆุธููŠู† ู„ุณูˆุก ุงู„ู…ุนุงู…ู„ุฉ ู…ู† ู‚ุจู„ ุงู„ุนู…ู„ุงุก.\\nุงุณู… (ูƒุงุฑูŠู†) ู‡ูˆ ุฅุดุงุฑุฉ ุฅู„ู‰ ุงู„ุฅุณู… ุงู„ู…ุณุชุฎุฏู… ููŠ ุงู„ู…ูŠู…ุงุช (ุงู„ู†ูƒุช ุงู„ุชูŠ ุชู†ุดู‡ุฑ ุจุณุฑุนุฉ ููŠ ู…ูˆุงู‚ุน ุงู„ุชูˆุงุตู„) ู„ูˆุตู ุงู…ุฑุฃุฉ ุจูŠุถุงุก ููŠ ู…ู†ุชุตู ุงู„ุนู…ุฑ ูˆูˆู‚ุญุฉ ุจุดูƒู„ ู†ู…ุทูŠ.\\nูŠุทู„ุจ ู…ู† ุงู„ู…ูˆุธููŠู† ุงุฑุชุฏุงุก ุดุฎุตูŠุฉ ูˆู‚ุญุฉ ูˆุงู„ุณุฎุฑูŠุฉ ู…ู† ุงู„ุนู…ู„ุงุก ุจุดูƒู„ ู‡ุฒู„ูŠ ุงุซู†ุงุก ุชู†ุงูˆู„ ูˆุฌุจุงุชู‡ู… ูˆู…ู† ุงู„ู…ุชูˆู‚ุน ุงู† ูŠุนูŠุฏ ุงู„ุนู…ู„ุงุก ู‡ุฐุง ุงู„ุณู„ูˆูƒ ู…ู† ุฎู„ุงู„ ุงู„ุชุตุฑู ุจูˆู‚ุงุญุฉ ุชุฌุงู‡ ุงู„ู…ูˆุธููŠู† ูˆู…ุน ุฐู„ูƒ ูŠูุญุธุฑ ุนู„ู‰ ุงู„ุนู…ู„ุงุก ูˆุงู„ู…ูˆุธููŠู† ุงุณุชุฎุฏุงู… ุงู„ุฅู‡ุงู†ุงุช ุงู„ุนู†ุตุฑูŠุฉ ุฃูˆ ุงู„ุชุญูŠุฒ ุงู„ุฌู†ุณูŠ ุฃูˆ ุฑู‡ุงุจ ุงู„ู…ุซู„ูŠุฉ ุงู„ุฌู†ุณูŠุฉ.\\nุชุชุถู…ู† ุงู„ุนุฏูŠุฏ ู…ู† ู‡ุฐู‡ ุงู„ุชุจุงุฏู„ุงุช ู„ุบุฉ ู†ุงุจูŠุฉ ูˆูŠุฌุจ ุงู† ูŠูƒูˆู† ุจุฑูู‚ุฉ ุงู„ุงุดุฎุงุต ุงู„ู„ุฐูŠู† ูŠู‚ู„ูˆู† ุนู† 16 ุนุงู…ุงูŽูŽ ุจุงู„ุบูŠู†.\\nูƒู…ุง ูŠู…ูƒู† ู„ู…ุงู„ูƒูŠ ุจุทุงู‚ุฉ ู‡ูˆูŠุฉ ุชุธู‡ุฑ ุงู† ุงุณู…ู‡ู… ูƒุงุฑูŠู† ุงู„ุญุตูˆู„ ุนู„ู‰ ู…ุดุฑูˆุจ ู…ุฌุงู†ูŠ.\\n', 'content_type': 'text', 'score': None, 'meta': {'id': '8974231', 'revid': '593870', 'url': 'https://ar.wikipedia.org/wiki?curid=8974231', 'title': 'ู…ุทุนู… ุนุดุงุก ูƒุงุฑูŠู†', '_split_id': 0, '_split_overlap': [{'doc_id': '288196225044b53e6ff86f2485257a0a', 'range': (790, 1225)}]}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '1af84f3b4cc6a9f1018f2f80b4fd3ba7'}>"
718
+ ]
719
+ },
720
+ "execution_count": 11,
721
+ "metadata": {},
722
+ "output_type": "execute_result"
723
+ }
724
+ ],
725
+ "source": [
726
+ "documents[0]"
727
+ ]
728
+ },
729
+ {
730
+ "cell_type": "code",
731
+ "execution_count": 12,
732
+ "id": "b34890bf-9dba-459a-9b0d-aa4b5929cbe8",
733
+ "metadata": {
734
+ "tags": []
735
+ },
736
+ "outputs": [
737
+ {
738
+ "data": {
739
+ "text/plain": [
740
+ "<Document: {'content': 'ุนู†ูˆุงู†: ู…ุทุนู… ุนุดุงุก ูƒุงุฑูŠู†. ูŠุทู„ุจ ู…ู† ุงู„ู…ูˆุธููŠู† ุงุฑุชุฏุงุก ุดุฎุตูŠุฉ ูˆู‚ุญุฉ ูˆุงู„ุณุฎุฑูŠุฉ ู…ู† ุงู„ุนู…ู„ุงุก ุจุดูƒู„ ู‡ุฒู„ูŠ ุงุซู†ุงุก ุชู†ุงูˆู„ ูˆุฌุจุงุชู‡ู… ูˆู…ู† ุงู„ู…ุชูˆู‚ุน ุงู† ูŠุนูŠุฏ ุงู„ุนู…ู„ุงุก ู‡ุฐุง ุงู„ุณู„ูˆูƒ ู…ู† ุฎู„ุงู„ ุงู„ุชุตุฑู ุจูˆู‚ุงุญุฉ ุชุฌุงู‡ ุงู„ู…ูˆุธููŠู† ูˆู…ุน ุฐู„ูƒ ูŠูุญุธุฑ ุนู„ู‰ ุงู„ุนู…ู„ุงุก ูˆุงู„ู…ูˆุธููŠู† ุงุณุชุฎุฏุงู… ุงู„ุฅู‡ุงู†ุงุช ุงู„ุนู†ุตุฑูŠุฉ ุฃูˆ ุงู„ุชุญูŠุฒ ุงู„ุฌู†ุณูŠ ุฃูˆ ุฑู‡ุงุจ ุงู„ู…ุซู„ูŠุฉ ุงู„ุฌู†ุณูŠุฉ.\\nุชุชุถู…ู† ุงู„ุนุฏูŠุฏ ู…ู† ู‡ุฐู‡ ุงู„ุชุจุงุฏู„ุงุช ู„ุบุฉ ู†ุงุจูŠุฉ ูˆูŠุฌุจ ุงู† ูŠูƒูˆู† ุจุฑูู‚ุฉ ุงู„ุงุดุฎุงุต ุงู„ู„ุฐูŠู† ูŠู‚ู„ูˆู† ุนู† 16 ุนุงู…ุงูŽูŽ ุจุงู„ุบูŠู†.\\nูƒู…ุง ูŠู…ูƒู† ู„ู…ุงู„ูƒูŠ ุจุทุงู‚ุฉ ู‡ูˆูŠุฉ ุชุธู‡ุฑ ุงู† ุงุณู…ู‡ู… ูƒุงุฑูŠู† ุงู„ุญุตูˆู„ ุนู„ู‰ ู…ุดุฑูˆุจ ู…ุฌุงู†ูŠ.\\nูŠุฑุชูƒุฒ ุงู„ู…ุทุนู… ุนู„ูˆ ูˆุฌุจุงุช ุงู„ุนุดุงุก ุงู„ุฃู…ุฑูŠูƒูŠุฉ ููŠ ุฎู…ุณูŠู†ุงุช ุงู„ู‚ุฑู† ุงู„ู…ุงุถูŠ ูˆุชุชู…ูŠุฒ ุงู„ู‚ุงุฆู…ุฉ ุจุงู„ู‡ุงู…ุจุฑุบุฑ ูˆุฃุฌู†ุญุฉ ุงู„ุฏุฌุงุฌ.\\nุฃุตุจุญ ู…ุญุชูˆู‰ ุดุงุฆุน ู„ูˆุณุงุฆู„ ุงู„ุชูˆุงุตู„ ุงู„ุฅุฌุชู…ุงุนูŠ ุฎุตูˆุตุงู‹ ุนู„ู‰ ู…ู†ุตุฉ (ุชูŠูƒ ุชูˆูƒ) ุญูŠุซ ู†ุดุฑ ุงู„ุนู…ู„ุงุก ู…ู‚ุงุทุน ููŠุฏูŠูˆ ู„ุชูุงุนู„ุงุชู‡ู… ู…ุน ุงู„ู…ูˆุธููŠู†.\\nูุชุญุช ุงู„ุณู„ุณู„ุฉ ููŠ ู…ูˆุงู‚ุน ููŠ ุงู„ู…ู…ู„ูƒุฉ ุงู„ู…ุชุญุฏุฉ ูˆุงู„ูˆู„ุงูŠุงุช ุงู„ู…ุชุญุฏุฉ ูˆู†ูŠูˆุฒูŠู„ู†ุฏุง.\\nููŠ ุดู‡ุฑ ุฃุบุณุทุณ ุณู†ุฉ 2022 ุงุซุงุฑ ุงู„ู…ุทุนู… ุฌุฏู„ุงู‹ ุจุนุฏ ุงู† ุงู†ุชุดุฑ ู…ู‚ุทุน ูŠูุธู‡ุฑ ููŠู‡ ุฃุญุฏ ู…ูˆุธููŠ ูุฑูŠู‚ ุงู„ุนู…ู„ ููŠ ู…ู†ุทู‚ุฉ ุจุฑูŠุฒุจูŠู† ูŠุชุตุฑู ุจุดูƒู„ ุบูŠุฑ ู„ุงุฆู‚ ุนู„ู‰ ู…ู†ุตุฉ ุชูŠูƒ ุชูˆูƒ ุญูŠุซ ุงู„ู‚ู‰ ุชุนู„ูŠู‚ุงุช ุบูŠุฑ ู„ุงุฆู‚ุฉ ู…ูˆุฌู‡ุฉ ุฅู„ู‰ ุฒุจูˆู†ุฉ ู‚ุงุตุฑ ูˆูˆุงู„ุฏู‡ุง ุงู„ุฐูŠ ูƒุงู† ูŠุดุงุฑูƒู‡ุง ุงู„ุทุนุงู… ุจุฅุชู‡ุงู…ู‡ ุงู†ู‡ ูŠู…ุงุฑุณ ุงู„ุฑุฐูŠู„ุฉ ู…ุน ุงู„ุฃุทูุงู„ุŒ ูู‚ุงู… ุงู„ู…ุชุญุฏุซ ุจุงุณู… ุงู„ุณู„ุณู„ุฉ ุจุงู„ุฑุฏ ุจุฅู†ู‡ู… ุงุตูŠุจูˆ ุจุฎูŠุจุฉ ุฃู…ู„ ุจุณุจุจ ุงู„ุณู„ูˆูƒ ูˆุฃู† ุงู„ุญุงุฏุซ ูŠุชุนุงุฑุถ ู…ุน ุฅุฑุดุงุฏุงุชู‡ู….', 'content_type': 'text', 'score': None, 'meta': {'id': '8974231', 'revid': '593870', 'url': 'https://ar.wikipedia.org/wiki?curid=8974231', 'title': 'ู…ุทุนู… ุนุดุงุก ูƒุงุฑูŠู†', '_split_id': 1, '_split_overlap': [{'doc_id': '1af84f3b4cc6a9f1018f2f80b4fd3ba7', 'range': (0, 435)}]}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '288196225044b53e6ff86f2485257a0a'}>"
741
+ ]
742
+ },
743
+ "execution_count": 12,
744
+ "metadata": {},
745
+ "output_type": "execute_result"
746
+ }
747
+ ],
748
+ "source": [
749
+ "documents[1]"
750
+ ]
751
+ },
752
+ {
753
+ "cell_type": "code",
754
+ "execution_count": 13,
755
+ "id": "e6f50c27-a486-47e9-ba60-d567f5e530db",
756
+ "metadata": {
757
+ "tags": []
758
+ },
759
+ "outputs": [
760
+ {
761
+ "data": {
762
+ "text/plain": [
763
+ "<Document: {'content': 'ู†ูŠูƒูˆู„ุงูŠ ูู„ูŠูุง ูˆุงู„ู…ุนุฑูˆู ุฃูŠุถู‹ุง ุจุงุณู… ู†ูŠูƒูˆ ูู„ูŠูุง (1840 - 4 ุฃุบุณุทุณ ุนุงู… 1920) ู‡ูˆ ุณูŠุงุณูŠ ูˆุตุญููŠ ุณูŠุงุณูŠ ูˆู…ุญุงู…ูŠ ุฃูู„ุงู‚ูŠ ูˆู…ู† ุซู… ุฑูˆู…ุงู†ูŠ. ุงุดุชู‡ุฑ ุจุงู†ุฎุฑุงุทู‡ ููŠ ุงู„ู…ุฌุฑูŠุงุช ุงู„ุณูŠุงุณูŠุฉ ูˆู†ุฒุนุชู‡ ุงู„ูˆุทู†ูŠุฉ ุงู„ุตุฑูŠุญุฉ ุงู„ุชูŠ ูƒุงุฏุช ุชุตู„ ุฅู„ู‰ ุญุฏ ุงู„ุฏูŠู…ุงุบูˆุฌูŠุฉ. ุงุฎุชุจุฑ ูƒุงูุฉ ุงู„ุตูŠุบ ุงู„ุณูŠุงุณูŠุฉ ุงู„ุชูŠ ูŠุณู…ุญ ุจู‡ุง ู†ุธุงู… ุงู„ุญุฒุจูŠู† ููŠ ุฑูˆู…ุงู†ูŠุง. ุฏุงู… ุญุถูˆุฑู‡ ุนู„ู‰ ุงู„ุณุงุญุฉ ุงู„ุนุงู…ุฉ ุนู‚ูˆุฏู‹ุง ู…ู† ุงู„ุฒู…ู†ุŒ ุดุบู„ ุฎู„ุงู„ู‡ุง ู…ู‚ุนุฏู‹ุง ููŠ ุฌู…ุนูŠุฉ ุงู„ู†ูˆุงุจ ูˆุชูˆู„ู‰ ู…ู†ุตุจ ุนู…ุฏุฉ ู…ุฏูŠู†ุฉ ุจูˆุฎุงุฑุณุช ุฎู„ุงู„ ุงู„ูุชุฑุฉ ุงู„ู…ู…ุชุฏุฉ ู…ู† ุนุงู… 1884 ุญุชู‰ ุนุงู… 1886.\\nุจุงุดุฑ ูู„ูŠูุง ู…ุณูŠุฑุชู‡ ุงู„ุณูŠุงุณูŠุฉ ู…ุน ุงู„ุญุฒุจ ุงู„ู„ูŠุจุฑุงู„ูŠ ุงู„ูˆุทู†ูŠ ุงู„ุฐูŠ ุณุงุนุฏ ุนู„ู‰ ุชุฃุณูŠุณู‡ ูˆุชู…ุซูŠู„ู‡ ุฃู…ุงู… ุงู„ู‚ุถุงุกุŒ ูˆู„ูƒู†ู‡ ุงุชุฌู‡ ููŠ ู…ุง ุจุนุฏ ุฅู„ู‰ ู…ุนุงุฑุถุฉ ุงุญุชูƒุงุฑ ุงู„ุญุฒุจ ู„ู„ุณู„ุทุฉ. ุญุงูˆู„ ุฅู†ุดุงุก ุญุฒุจ ุซุงู„ุซ ูˆุฏุฎู„ ููŠ ู…ูุงูˆุถุงุช ู…ู† ุฃุฌู„ ุงุนุชู…ุงุฏ ุจุฑุงู…ุฌ ุณูŠุงุณูŠุฉ ู…ุดุชุฑูƒุฉ ุฎุงุตุฉ ุจู‚ูˆู‰ ุงู„ู…ุนุงุฑุถุฉ ุงู„ู…ุฎุชู„ูุฉ ูˆู…ู† ุจูŠู†ู‡ุง ุญุฒุจ ุงู„ู…ุญุงูุธูŠู† ูˆุฌู…ุนูŠุฉ ุฌูˆู†ูŠู…ุง ููŠ ุธู„ ุงู„ุฅุฏุงุฑุงุช ุงู„ู„ูŠุจุฑุงู„ูŠุฉ ุงู„ูˆุทู†ูŠุฉ ุงู„ู…ุชุนุงู‚ุจุฉ. ุฐุงุน ุตูŠุชู‡ ุนู†ุฏู…ุง ุชูˆุฑุท ููŠ ูุถูŠุญุชูŠู† ูƒุจูŠุฑุชูŠู† ุฎู„ุงู„ ุซู…ุงู†ูŠู†ูŠุงุช ุงู„ู‚ุฑู† ุงู„ุชุงุณุน ุนุดุฑ ุญูŠู† ุฃุฏู‰ ุงุณุชู‡ุฒุงุฆู‡ ุจุณู„ุทุฉ ุงู„ุญุฒุจ ุงู„ู„ูŠุจุฑุงู„ูŠ ุงู„ูˆุทู†ูŠ ุฅู„ู‰ ุงู†ุฏู„ุงุน ู…ุนุงุฑูƒ ููŠ ุงู„ุดูˆุงุฑุน ูˆูˆู‚ูˆุน ุญุงุฏุซุชูŠ ุฅุทู„ุงู‚ ุงู„ู†ุงุฑ ู…ู†ูุตู„ุชูŠู†. ุงุนุชูุจุฑุช ุงู„ุฌู…ุงุนุงุช ุงู„ู…ูˆุงู„ูŠุฉ ู„ูู„ูŠูุง ุงู„ุตูˆุช ุงู„ุฑุงุฆุฏ ุงู„ู…ุนุจุฑ ุนู† ุณุฎุท ุงู„ุทุจู‚ุฉ ุงู„ูˆุณุทู‰ ูˆู‚ุชุฐุงูƒุŒ ูˆุดูƒู„ุช ุฅุญุฏู‰ ุงู„ุชูŠุงุฑุงุช ุงู„ุชูŠ ุฏูุนุช ุจุงุชุฌุงู‡ ุชุจู†ูŠ ุญู‚ ุงู„ุงู‚ุชุฑุงุน ุงู„ุนุงู… ู„ู„ุฐูƒูˆุฑ.\\nุนุงุฏ ูู„ูŠูุง ุฅู„ู‰ ุงู„ู…ุนุณูƒุฑ ุงู„ู„ูŠุจุฑุงู„ูŠ ุงู„ูˆุทู†ูŠ ุจุนุฏ ู…ู†ุนู‡ ู…ู† ุชูˆู„ูŠ ุญู‚ุงุฆุจ ูˆุฒุงุฑูŠุฉ ุฑูŠุงุฏูŠุฉ ููŠ ุงู„ุญูƒูˆู…ุงุช ุงู„ู…ุญุงูุธุฉุŒ ูˆุฃุตุจุญ ูˆุฒูŠุฑู‹ุง ู„ู„ุดุคูˆู† ุงู„ุฏุงุฎู„ูŠุฉ ุฎู„ุงู„ ุงู„ูุชุฑุฉ ู…ู† ุนุงู… 1895 ุญุชู‰ ุนุงู… 1896. ', 'content_type': 'text', 'score': None, 'meta': {'id': '9044009', 'revid': '1673186', 'url': 'https://ar.wikipedia.org/wiki?curid=9044009', 'title': 'ู†ูŠูƒูˆู„ุงูŠ ูู„ูŠูุง', '_split_id': 0, '_split_overlap': [{'doc_id': '188181b1026773d720383c7e7307b241', 'range': (943, 1257)}]}, 'id_hash_keys': ['content'], 'embedding': None, 'id': 'af5cda4722fa2a961bef66de8a6b3e17'}>"
764
+ ]
765
+ },
766
+ "execution_count": 13,
767
+ "metadata": {},
768
+ "output_type": "execute_result"
769
+ }
770
+ ],
771
+ "source": [
772
+ "documents[10102]"
773
+ ]
774
+ },
775
+ {
776
+ "cell_type": "code",
777
+ "execution_count": 14,
778
+ "id": "5485cc27-3d3f-4b96-8884-accf5324da2d",
779
+ "metadata": {
780
+ "tags": []
781
+ },
782
+ "outputs": [
783
+ {
784
+ "name": "stdout",
785
+ "output_type": "stream",
786
+ "text": [
787
+ "2094596\n"
788
+ ]
789
+ }
790
+ ],
791
+ "source": [
792
+ "!cat \"$folder_out_str\"/*.ndjson | wc -l"
793
+ ]
794
+ },
795
+ {
796
+ "cell_type": "code",
797
+ "execution_count": null,
798
+ "id": "c5833dba-1bf6-48aa-be6f-0d70c71e54aa",
799
+ "metadata": {},
800
+ "outputs": [],
801
+ "source": []
802
+ }
803
+ ],
804
+ "metadata": {
805
+ "kernelspec": {
806
+ "display_name": "Python 3 (ipykernel)",
807
+ "language": "python",
808
+ "name": "python3"
809
+ },
810
+ "language_info": {
811
+ "codemirror_mode": {
812
+ "name": "ipython",
813
+ "version": 3
814
+ },
815
+ "file_extension": ".py",
816
+ "mimetype": "text/x-python",
817
+ "name": "python",
818
+ "nbconvert_exporter": "python",
819
+ "pygments_lexer": "ipython3",
820
+ "version": "3.10.13"
821
+ }
822
+ },
823
+ "nbformat": 4,
824
+ "nbformat_minor": 5
825
+ }
notebooks/04_get_embeddings.ipynb ADDED
@@ -0,0 +1,926 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "a0f21cb1-fbc8-4282-b902-f47d92974df8",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Pre-requisites"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "id": "5f625807-0707-4e2f-a0e0-8fbcdf08c865",
14
+ "metadata": {},
15
+ "source": [
16
+ "## Why TEI\n",
17
+ "There are 2 **unsung** challenges with RAG at scale:\n",
18
+ "1. Getting the embeddings efficiently\n",
19
+ "1. Efficient ingestion into the vector DB\n",
20
+ "\n",
21
+ "The issue with `1.` is that there are techniques but they are not widely *applied*. TEI solves a number of aspects:\n",
22
+ "- Token Based Dynamic Batching\n",
23
+ "- Using latest optimizations (Flash Attention, Candle and cuBLASLt)\n",
24
+ "- Fast loading with safetensors\n",
25
+ "\n",
26
+ "The issue with `2.` is that it takes a bit of planning. We wont go much into that side of things here though."
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "markdown",
31
+ "id": "3102abce-ea42-4da6-8c98-c6dd4edf7f0b",
32
+ "metadata": {},
33
+ "source": [
34
+ "## Start TEI Locally\n",
35
+ "Run [TEI](https://github.com/huggingface/text-embeddings-inference#docker), I have this running in a nvidia-docker container, but you can install as you like. Note that I ran this in a different terminal for monitoring and seperation. \n",
36
+ "\n",
37
+ "Note that as its running, its always going to pull the latest. Its at a very early stage at the time of writing. \n",
38
+ "\n",
39
+ "I chose [sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2](https://huggingface.co/sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2) based on the STS ar-ar performance on [mteb/leaderboard](https://huggingface.co/spaces/mteb/leaderboard), its the top performer and half the size of second place! TEI is fast, but this will make our life easier for storage and retrieval.\n",
40
+ "\n",
41
+ "I use the `revision=refs/pr/8` because this has the pull request with [safetensors](https://github.com/huggingface/safetensors) which is required by TEI. Check out the [pull request](https://huggingface.co/sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2/discussions/8) if you want to use a different embedding model and it doesnt have safetensors."
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": null,
47
+ "id": "7e873652-8257-4aae-92bc-94e1bac54b73",
48
+ "metadata": {
49
+ "tags": []
50
+ },
51
+ "outputs": [],
52
+ "source": [
53
+ "%%bash\n",
54
+ "\n",
55
+ "# volume=$pwd/tei\n",
56
+ "# model=sentence-transformers/paraphrase-multilingual-minilm-l12-v2\n",
57
+ "# revision=refs/pr/8\n",
58
+ "# docker run \\\n",
59
+ "# --gpus all \\\n",
60
+ "# -p 8080:80 \\\n",
61
+ "# -v $volume:/data \\\n",
62
+ "# -v /home/ec2-user/.cache/huggingface/token:/root/.cache/huggingface/token \\\n",
63
+ "# --pull always \\\n",
64
+ "# ghcr.io/huggingface/text-embeddings-inference:latest \\\n",
65
+ "# --model-id $model \\\n",
66
+ "# --revision $revision \\\n",
67
+ "# --pooling mean \\\n",
68
+ "# --max-batch-tokens 65536"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "markdown",
73
+ "id": "51959ef4-186e-4a32-826a-731813eaf593",
74
+ "metadata": {},
75
+ "source": [
76
+ "### Test Endpoint"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": null,
82
+ "id": "52edfc97-5b6f-44f9-8d89-8578cf79fae9",
83
+ "metadata": {
84
+ "tags": []
85
+ },
86
+ "outputs": [],
87
+ "source": [
88
+ "%%bash\n",
89
+ "\n",
90
+ "# response_code=$(curl -s -o /dev/null -w \"%{http_code}\" 127.0.0.1:8080/embed \\\n",
91
+ "# -X POST \\\n",
92
+ "# -d '{\"inputs\":\"What is Deep Learning?\"}' \\\n",
93
+ "# -H 'Content-Type: application/json')\n",
94
+ "\n",
95
+ "# if [ \"$response_code\" -eq 200 ]; then\n",
96
+ "# echo \"passed\"\n",
97
+ "# else\n",
98
+ "# echo \"failed\"\n",
99
+ "# fi"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "markdown",
104
+ "id": "e9d6b54a-02bd-49aa-b180-27a7ab90154e",
105
+ "metadata": {},
106
+ "source": [
107
+ "## Start TEI with Inference Endpoints\n",
108
+ "Another option is to run TEI on Inference Endpoints. Its cheap and fast. It took me less than 5 minutes to get it up and running!\n",
109
+ "\n",
110
+ "Check here for a [guide](https://huggingface.co/blog/inference-endpoints-embeddings#3-deploy-embedding-model-as-inference-endpoint). Make sure to set these options in order:\n",
111
+ "1. Model Repository = transformers/paraphrase-multilingual-minilm-l12-v2\n",
112
+ "1. Name your endpoint\n",
113
+ "1. Choose a GPU\n",
114
+ "1. Advanced Configuration\n",
115
+ " 1. Task = Sentence Embeddings\n",
116
+ " 1. Revision (based on [this pull request for safetensors](https://huggingface.co/sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2/discussions/8) = a21e6630\n",
117
+ " 1. Container Type = Text Embeddings Inference\n",
118
+ " \n",
119
+ "Set the other options as you prefer."
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "markdown",
124
+ "id": "ec78c98a-6b7b-4689-8ef8-582c3fcdf66e",
125
+ "metadata": {},
126
+ "source": [
127
+ "### Test Endpoint"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 1,
133
+ "id": "a69e2ee1-67f2-4f0a-b496-02f5415a52ca",
134
+ "metadata": {
135
+ "tags": []
136
+ },
137
+ "outputs": [
138
+ {
139
+ "name": "stdin",
140
+ "output_type": "stream",
141
+ "text": [
142
+ "What is your BEARER TOKEN? Check your endpoint. ยทยทยทยทยทยทยทยท\n",
143
+ "What is your API_URL? ยทยทยทยทยทยทยทยท\n"
144
+ ]
145
+ }
146
+ ],
147
+ "source": [
148
+ "import getpass\n",
149
+ "bearer_token = getpass.getpass(prompt='What is your BEARER TOKEN? Check your endpoint.')\n",
150
+ "API_URL = getpass.getpass(prompt='What is your API_URL?')"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 2,
156
+ "id": "949d6bf8-804f-496b-a59a-834483cc7073",
157
+ "metadata": {
158
+ "tags": []
159
+ },
160
+ "outputs": [],
161
+ "source": [
162
+ "# Constants\n",
163
+ "HEADERS = {\n",
164
+ "\t\"Authorization\": f\"Bearer {bearer_token}\",\n",
165
+ "\t\"Content-Type\": \"application/json\"\n",
166
+ "}\n",
167
+ "MAX_WORKERS = 512"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": 3,
173
+ "id": "d00b4af1-8fbc-4f7a-8a78-e1c52dd77a66",
174
+ "metadata": {
175
+ "tags": []
176
+ },
177
+ "outputs": [
178
+ {
179
+ "name": "stdout",
180
+ "output_type": "stream",
181
+ "text": [
182
+ "[0.0047912598, -0.03164673, -0.018051147, -0.057739258, -0.04498291]...\n"
183
+ ]
184
+ }
185
+ ],
186
+ "source": [
187
+ "import requests\n",
188
+ "\n",
189
+ "\n",
190
+ "def query(payload):\n",
191
+ "\tresponse = requests.post(API_URL, headers=HEADERS, json=payload)\n",
192
+ "\treturn response.json()\n",
193
+ "\t\n",
194
+ "output = query({\n",
195
+ "\t\"inputs\": \"This sound track was beautiful! It paints the senery in your mind so well I would recomend it even to people who hate vid. game music!\",\n",
196
+ "})\n",
197
+ "print(f'{output[0][:5]}...')"
198
+ ]
199
+ },
200
+ {
201
+ "cell_type": "markdown",
202
+ "id": "b1b28232-b65d-41ce-88de-fd70b93a528d",
203
+ "metadata": {},
204
+ "source": [
205
+ "# Imports"
206
+ ]
207
+ },
208
+ {
209
+ "cell_type": "code",
210
+ "execution_count": 4,
211
+ "id": "abb5186b-ee67-4e1e-882d-3d8d5b4575d4",
212
+ "metadata": {
213
+ "tags": []
214
+ },
215
+ "outputs": [],
216
+ "source": [
217
+ "import asyncio\n",
218
+ "from pathlib import Path\n",
219
+ "import json\n",
220
+ "import time\n",
221
+ "\n",
222
+ "\n",
223
+ "from aiohttp import ClientSession, ClientTimeout\n",
224
+ "from tqdm.notebook import tqdm"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": 5,
230
+ "id": "c4b82ea2-8b30-4c2e-99f0-9a30f2f1bfb7",
231
+ "metadata": {
232
+ "tags": []
233
+ },
234
+ "outputs": [
235
+ {
236
+ "name": "stdout",
237
+ "output_type": "stream",
238
+ "text": [
239
+ "/home/ec2-user/arabic-wiki\n"
240
+ ]
241
+ }
242
+ ],
243
+ "source": [
244
+ "proj_dir = Path.cwd().parent\n",
245
+ "print(proj_dir)"
246
+ ]
247
+ },
248
+ {
249
+ "cell_type": "markdown",
250
+ "id": "76119e74-f601-436d-a253-63c5a19d1c83",
251
+ "metadata": {},
252
+ "source": [
253
+ "# Config"
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "code",
258
+ "execution_count": 6,
259
+ "id": "f6f74545-54a7-4f41-9f02-96964e1417f0",
260
+ "metadata": {
261
+ "tags": []
262
+ },
263
+ "outputs": [],
264
+ "source": [
265
+ "files_in = list((proj_dir / 'data/processed/').glob('*.ndjson'))\n",
266
+ "folder_out = proj_dir / 'data/embedded/'"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "markdown",
271
+ "id": "5e73235d-6274-4958-9e57-977afeeb5f1b",
272
+ "metadata": {},
273
+ "source": [
274
+ "# Embed\n",
275
+ "## Strategy\n",
276
+ "TEI allows multiple concurrent requests, so its important that we dont waste the potential we have. I used the default `max-concurrent-requests` value of `512`, so I want to use that many `MAX_WORKERS`.\n",
277
+ "\n",
278
+ "Im using an `async` way of making requests that uses `aiohttp` as well as a nice progress bar. "
279
+ ]
280
+ },
281
+ {
282
+ "cell_type": "markdown",
283
+ "id": "cf3da8cc-1651-4704-9091-39c2a1b835be",
284
+ "metadata": {},
285
+ "source": [
286
+ "Note that Im using `'truncate':True` as even with our `350` word split earlier, there are always exceptions. Its important that as this scales we have as few issues as possible when embedding. "
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": 7,
292
+ "id": "e455dd52-aad3-4313-8738-03141ee5152a",
293
+ "metadata": {
294
+ "tags": []
295
+ },
296
+ "outputs": [],
297
+ "source": [
298
+ "async def request(document, semaphore):\n",
299
+ " # Semaphore guard\n",
300
+ " async with semaphore:\n",
301
+ " payload = {\n",
302
+ " \"inputs\": document['content'],\n",
303
+ " \"truncate\": True\n",
304
+ " }\n",
305
+ " \n",
306
+ " timeout = ClientTimeout(total=10) # Set a timeout for requests (10 seconds here)\n",
307
+ "\n",
308
+ " async with ClientSession(timeout=timeout, headers=HEADERS) as session:\n",
309
+ " async with session.post(API_URL, json=payload) as resp:\n",
310
+ " if resp.status != 200:\n",
311
+ " raise RuntimeError(await resp.text())\n",
312
+ " result = await resp.json()\n",
313
+ " \n",
314
+ " document['embedding'] = result[0] # Assuming the API's output can be directly assigned\n",
315
+ " return document\n",
316
+ "\n",
317
+ "async def main(documents):\n",
318
+ " # Semaphore to limit concurrent requests. Adjust the number as needed.\n",
319
+ " semaphore = asyncio.BoundedSemaphore(512)\n",
320
+ "\n",
321
+ " # Creating a list of tasks\n",
322
+ " tasks = [request(document, semaphore) for document in documents]\n",
323
+ " \n",
324
+ " # Using tqdm to show progress. It's been integrated into the async loop.\n",
325
+ " for f in tqdm(asyncio.as_completed(tasks), total=len(documents)):\n",
326
+ " await f\n"
327
+ ]
328
+ },
329
+ {
330
+ "cell_type": "code",
331
+ "execution_count": 11,
332
+ "id": "f0d17264-72dc-40be-aa46-17cde38c8189",
333
+ "metadata": {
334
+ "tags": []
335
+ },
336
+ "outputs": [
337
+ {
338
+ "data": {
339
+ "application/vnd.jupyter.widget-view+json": {
340
+ "model_id": "1db8949409284a7cbeec2638ed197f59",
341
+ "version_major": 2,
342
+ "version_minor": 0
343
+ },
344
+ "text/plain": [
345
+ "0it [00:00, ?it/s]"
346
+ ]
347
+ },
348
+ "metadata": {},
349
+ "output_type": "display_data"
350
+ },
351
+ {
352
+ "data": {
353
+ "application/vnd.jupyter.widget-view+json": {
354
+ "model_id": "5945500ccf8649988918e2633269cb7b",
355
+ "version_major": 2,
356
+ "version_minor": 0
357
+ },
358
+ "text/plain": [
359
+ " 0%| | 0/243068 [00:00<?, ?it/s]"
360
+ ]
361
+ },
362
+ "metadata": {},
363
+ "output_type": "display_data"
364
+ },
365
+ {
366
+ "name": "stdout",
367
+ "output_type": "stream",
368
+ "text": [
369
+ "Batch 1: Embeddings = 243068 documents = 243068\n"
370
+ ]
371
+ },
372
+ {
373
+ "data": {
374
+ "application/vnd.jupyter.widget-view+json": {
375
+ "model_id": "0cf8121a116f49fba72095fee46ef49d",
376
+ "version_major": 2,
377
+ "version_minor": 0
378
+ },
379
+ "text/plain": [
380
+ " 0%| | 0/104065 [00:00<?, ?it/s]"
381
+ ]
382
+ },
383
+ "metadata": {},
384
+ "output_type": "display_data"
385
+ },
386
+ {
387
+ "name": "stdout",
388
+ "output_type": "stream",
389
+ "text": [
390
+ "Batch 2: Embeddings = 104065 documents = 104065\n"
391
+ ]
392
+ },
393
+ {
394
+ "data": {
395
+ "application/vnd.jupyter.widget-view+json": {
396
+ "model_id": "8f94983077854b5f9ab512f7d429eb55",
397
+ "version_major": 2,
398
+ "version_minor": 0
399
+ },
400
+ "text/plain": [
401
+ " 0%| | 0/123154 [00:00<?, ?it/s]"
402
+ ]
403
+ },
404
+ "metadata": {},
405
+ "output_type": "display_data"
406
+ },
407
+ {
408
+ "name": "stdout",
409
+ "output_type": "stream",
410
+ "text": [
411
+ "Batch 3: Embeddings = 123154 documents = 123154\n"
412
+ ]
413
+ },
414
+ {
415
+ "data": {
416
+ "application/vnd.jupyter.widget-view+json": {
417
+ "model_id": "3d2932212e6b4323a377ff23758e7af7",
418
+ "version_major": 2,
419
+ "version_minor": 0
420
+ },
421
+ "text/plain": [
422
+ " 0%| | 0/135965 [00:00<?, ?it/s]"
423
+ ]
424
+ },
425
+ "metadata": {},
426
+ "output_type": "display_data"
427
+ },
428
+ {
429
+ "name": "stdout",
430
+ "output_type": "stream",
431
+ "text": [
432
+ "Batch 4: Embeddings = 135965 documents = 135965\n"
433
+ ]
434
+ },
435
+ {
436
+ "data": {
437
+ "application/vnd.jupyter.widget-view+json": {
438
+ "model_id": "3de41d88c8bb439591925de045d8afe8",
439
+ "version_major": 2,
440
+ "version_minor": 0
441
+ },
442
+ "text/plain": [
443
+ " 0%| | 0/99138 [00:00<?, ?it/s]"
444
+ ]
445
+ },
446
+ "metadata": {},
447
+ "output_type": "display_data"
448
+ },
449
+ {
450
+ "name": "stdout",
451
+ "output_type": "stream",
452
+ "text": [
453
+ "Batch 5: Embeddings = 99138 documents = 99138\n"
454
+ ]
455
+ },
456
+ {
457
+ "data": {
458
+ "application/vnd.jupyter.widget-view+json": {
459
+ "model_id": "604a4f3b1baf429687ac00aa63778cdf",
460
+ "version_major": 2,
461
+ "version_minor": 0
462
+ },
463
+ "text/plain": [
464
+ " 0%| | 0/83678 [00:00<?, ?it/s]"
465
+ ]
466
+ },
467
+ "metadata": {},
468
+ "output_type": "display_data"
469
+ },
470
+ {
471
+ "name": "stdout",
472
+ "output_type": "stream",
473
+ "text": [
474
+ "Batch 6: Embeddings = 83678 documents = 83678\n"
475
+ ]
476
+ },
477
+ {
478
+ "data": {
479
+ "application/vnd.jupyter.widget-view+json": {
480
+ "model_id": "ecc69a2b763c4296b3a1fa35b15477aa",
481
+ "version_major": 2,
482
+ "version_minor": 0
483
+ },
484
+ "text/plain": [
485
+ " 0%| | 0/30573 [00:00<?, ?it/s]"
486
+ ]
487
+ },
488
+ "metadata": {},
489
+ "output_type": "display_data"
490
+ },
491
+ {
492
+ "name": "stdout",
493
+ "output_type": "stream",
494
+ "text": [
495
+ "Batch 7: Embeddings = 30573 documents = 30573\n"
496
+ ]
497
+ },
498
+ {
499
+ "data": {
500
+ "application/vnd.jupyter.widget-view+json": {
501
+ "model_id": "bbdc03d2ca5a4099b412c1767b3d394c",
502
+ "version_major": 2,
503
+ "version_minor": 0
504
+ },
505
+ "text/plain": [
506
+ " 0%| | 0/78957 [00:00<?, ?it/s]"
507
+ ]
508
+ },
509
+ "metadata": {},
510
+ "output_type": "display_data"
511
+ },
512
+ {
513
+ "name": "stdout",
514
+ "output_type": "stream",
515
+ "text": [
516
+ "Batch 8: Embeddings = 78957 documents = 78957\n"
517
+ ]
518
+ },
519
+ {
520
+ "data": {
521
+ "application/vnd.jupyter.widget-view+json": {
522
+ "model_id": "d4c64dfc612c4b7986de5385f5d88ba7",
523
+ "version_major": 2,
524
+ "version_minor": 0
525
+ },
526
+ "text/plain": [
527
+ " 0%| | 0/86327 [00:00<?, ?it/s]"
528
+ ]
529
+ },
530
+ "metadata": {},
531
+ "output_type": "display_data"
532
+ },
533
+ {
534
+ "name": "stdout",
535
+ "output_type": "stream",
536
+ "text": [
537
+ "Batch 9: Embeddings = 86327 documents = 86327\n"
538
+ ]
539
+ },
540
+ {
541
+ "data": {
542
+ "application/vnd.jupyter.widget-view+json": {
543
+ "model_id": "d05e2884021143e0baf595a86725466a",
544
+ "version_major": 2,
545
+ "version_minor": 0
546
+ },
547
+ "text/plain": [
548
+ " 0%| | 0/83111 [00:00<?, ?it/s]"
549
+ ]
550
+ },
551
+ "metadata": {},
552
+ "output_type": "display_data"
553
+ },
554
+ {
555
+ "name": "stdout",
556
+ "output_type": "stream",
557
+ "text": [
558
+ "Batch 10: Embeddings = 83111 documents = 83111\n"
559
+ ]
560
+ },
561
+ {
562
+ "data": {
563
+ "application/vnd.jupyter.widget-view+json": {
564
+ "model_id": "47f3537175d740aba9e2dc7de4c89fec",
565
+ "version_major": 2,
566
+ "version_minor": 0
567
+ },
568
+ "text/plain": [
569
+ " 0%| | 0/92664 [00:00<?, ?it/s]"
570
+ ]
571
+ },
572
+ "metadata": {},
573
+ "output_type": "display_data"
574
+ },
575
+ {
576
+ "name": "stdout",
577
+ "output_type": "stream",
578
+ "text": [
579
+ "Batch 11: Embeddings = 92664 documents = 92664\n"
580
+ ]
581
+ },
582
+ {
583
+ "data": {
584
+ "application/vnd.jupyter.widget-view+json": {
585
+ "model_id": "02b89207ee8c407db4ad8045b3634243",
586
+ "version_major": 2,
587
+ "version_minor": 0
588
+ },
589
+ "text/plain": [
590
+ " 0%| | 0/66404 [00:00<?, ?it/s]"
591
+ ]
592
+ },
593
+ "metadata": {},
594
+ "output_type": "display_data"
595
+ },
596
+ {
597
+ "name": "stdout",
598
+ "output_type": "stream",
599
+ "text": [
600
+ "Batch 12: Embeddings = 66404 documents = 66404\n"
601
+ ]
602
+ },
603
+ {
604
+ "data": {
605
+ "application/vnd.jupyter.widget-view+json": {
606
+ "model_id": "e1a96dfd66644007a39a5fef38e008ab",
607
+ "version_major": 2,
608
+ "version_minor": 0
609
+ },
610
+ "text/plain": [
611
+ " 0%| | 0/62844 [00:00<?, ?it/s]"
612
+ ]
613
+ },
614
+ "metadata": {},
615
+ "output_type": "display_data"
616
+ },
617
+ {
618
+ "name": "stdout",
619
+ "output_type": "stream",
620
+ "text": [
621
+ "Batch 13: Embeddings = 62844 documents = 62844\n"
622
+ ]
623
+ },
624
+ {
625
+ "data": {
626
+ "application/vnd.jupyter.widget-view+json": {
627
+ "model_id": "3bef95b8dff044fa922e52e1e88b9813",
628
+ "version_major": 2,
629
+ "version_minor": 0
630
+ },
631
+ "text/plain": [
632
+ " 0%| | 0/59349 [00:00<?, ?it/s]"
633
+ ]
634
+ },
635
+ "metadata": {},
636
+ "output_type": "display_data"
637
+ },
638
+ {
639
+ "name": "stdout",
640
+ "output_type": "stream",
641
+ "text": [
642
+ "Batch 14: Embeddings = 59349 documents = 59349\n"
643
+ ]
644
+ },
645
+ {
646
+ "data": {
647
+ "application/vnd.jupyter.widget-view+json": {
648
+ "model_id": "eb74103e549c4b0386d19f5d475af812",
649
+ "version_major": 2,
650
+ "version_minor": 0
651
+ },
652
+ "text/plain": [
653
+ " 0%| | 0/52554 [00:00<?, ?it/s]"
654
+ ]
655
+ },
656
+ "metadata": {},
657
+ "output_type": "display_data"
658
+ },
659
+ {
660
+ "name": "stdout",
661
+ "output_type": "stream",
662
+ "text": [
663
+ "Batch 15: Embeddings = 52554 documents = 52554\n"
664
+ ]
665
+ },
666
+ {
667
+ "data": {
668
+ "application/vnd.jupyter.widget-view+json": {
669
+ "model_id": "41228a5bf8294c1e95f34b9376714543",
670
+ "version_major": 2,
671
+ "version_minor": 0
672
+ },
673
+ "text/plain": [
674
+ " 0%| | 0/34240 [00:00<?, ?it/s]"
675
+ ]
676
+ },
677
+ "metadata": {},
678
+ "output_type": "display_data"
679
+ },
680
+ {
681
+ "name": "stdout",
682
+ "output_type": "stream",
683
+ "text": [
684
+ "Batch 16: Embeddings = 34240 documents = 34240\n"
685
+ ]
686
+ },
687
+ {
688
+ "data": {
689
+ "application/vnd.jupyter.widget-view+json": {
690
+ "model_id": "ba6eb3a975514d33ae3acac8859278d1",
691
+ "version_major": 2,
692
+ "version_minor": 0
693
+ },
694
+ "text/plain": [
695
+ " 0%| | 0/35933 [00:00<?, ?it/s]"
696
+ ]
697
+ },
698
+ "metadata": {},
699
+ "output_type": "display_data"
700
+ },
701
+ {
702
+ "name": "stdout",
703
+ "output_type": "stream",
704
+ "text": [
705
+ "Batch 17: Embeddings = 35933 documents = 35933\n"
706
+ ]
707
+ },
708
+ {
709
+ "data": {
710
+ "application/vnd.jupyter.widget-view+json": {
711
+ "model_id": "67ecca34d2a1414c9e9817d835fe2083",
712
+ "version_major": 2,
713
+ "version_minor": 0
714
+ },
715
+ "text/plain": [
716
+ " 0%| | 0/64575 [00:00<?, ?it/s]"
717
+ ]
718
+ },
719
+ "metadata": {},
720
+ "output_type": "display_data"
721
+ },
722
+ {
723
+ "name": "stdout",
724
+ "output_type": "stream",
725
+ "text": [
726
+ "Batch 18: Embeddings = 64575 documents = 64575\n"
727
+ ]
728
+ },
729
+ {
730
+ "data": {
731
+ "application/vnd.jupyter.widget-view+json": {
732
+ "model_id": "7fb632776adb4933b92c48f852b0ae6b",
733
+ "version_major": 2,
734
+ "version_minor": 0
735
+ },
736
+ "text/plain": [
737
+ " 0%| | 0/94244 [00:00<?, ?it/s]"
738
+ ]
739
+ },
740
+ "metadata": {},
741
+ "output_type": "display_data"
742
+ },
743
+ {
744
+ "name": "stdout",
745
+ "output_type": "stream",
746
+ "text": [
747
+ "Batch 19: Embeddings = 94244 documents = 94244\n"
748
+ ]
749
+ },
750
+ {
751
+ "data": {
752
+ "application/vnd.jupyter.widget-view+json": {
753
+ "model_id": "17d73c88d4334357854f852c9783bfdb",
754
+ "version_major": 2,
755
+ "version_minor": 0
756
+ },
757
+ "text/plain": [
758
+ " 0%| | 0/124472 [00:00<?, ?it/s]"
759
+ ]
760
+ },
761
+ "metadata": {},
762
+ "output_type": "display_data"
763
+ },
764
+ {
765
+ "name": "stdout",
766
+ "output_type": "stream",
767
+ "text": [
768
+ "Batch 20: Embeddings = 124472 documents = 124472\n"
769
+ ]
770
+ },
771
+ {
772
+ "data": {
773
+ "application/vnd.jupyter.widget-view+json": {
774
+ "model_id": "4dd7c3477a244d43b1d85417d4549eaa",
775
+ "version_major": 2,
776
+ "version_minor": 0
777
+ },
778
+ "text/plain": [
779
+ " 0%| | 0/121849 [00:00<?, ?it/s]"
780
+ ]
781
+ },
782
+ "metadata": {},
783
+ "output_type": "display_data"
784
+ },
785
+ {
786
+ "name": "stdout",
787
+ "output_type": "stream",
788
+ "text": [
789
+ "Batch 21: Embeddings = 121849 documents = 121849\n"
790
+ ]
791
+ },
792
+ {
793
+ "data": {
794
+ "application/vnd.jupyter.widget-view+json": {
795
+ "model_id": "e8b657d57f584128ae5a7ee2ecf23c7f",
796
+ "version_major": 2,
797
+ "version_minor": 0
798
+ },
799
+ "text/plain": [
800
+ " 0%| | 0/147110 [00:00<?, ?it/s]"
801
+ ]
802
+ },
803
+ "metadata": {},
804
+ "output_type": "display_data"
805
+ },
806
+ {
807
+ "name": "stdout",
808
+ "output_type": "stream",
809
+ "text": [
810
+ "Batch 22: Embeddings = 147110 documents = 147110\n"
811
+ ]
812
+ },
813
+ {
814
+ "data": {
815
+ "application/vnd.jupyter.widget-view+json": {
816
+ "model_id": "f3465378528a425e8dc9d040a003588a",
817
+ "version_major": 2,
818
+ "version_minor": 0
819
+ },
820
+ "text/plain": [
821
+ " 0%| | 0/70322 [00:00<?, ?it/s]"
822
+ ]
823
+ },
824
+ "metadata": {},
825
+ "output_type": "display_data"
826
+ },
827
+ {
828
+ "name": "stdout",
829
+ "output_type": "stream",
830
+ "text": [
831
+ "Batch 23: Embeddings = 70322 documents = 70322\n",
832
+ "6250.827601939993\n"
833
+ ]
834
+ }
835
+ ],
836
+ "source": [
837
+ "start = time.perf_counter()\n",
838
+ "for i, file_in in tqdm(enumerate(files_in)):\n",
839
+ "\n",
840
+ " with open(file_in, 'r') as f:\n",
841
+ " documents = [json.loads(line) for line in f]\n",
842
+ " \n",
843
+ " # Get embeddings\n",
844
+ " await main(documents)\n",
845
+ " \n",
846
+ " # Make sure we got it all\n",
847
+ " count = 0\n",
848
+ " for document in documents:\n",
849
+ " if document['embedding'] and len(document['embedding']) == 384:\n",
850
+ " count += 1\n",
851
+ " print(f'Batch {i+1}: Embeddings = {count} documents = {len(documents)}')\n",
852
+ "\n",
853
+ " # Write to file\n",
854
+ " with open(folder_out/file_in.name, 'w', encoding='utf-8') as f:\n",
855
+ " for document in documents:\n",
856
+ " json_str = json.dumps(document, ensure_ascii=False)\n",
857
+ " f.write(json_str + '\\n')\n",
858
+ "print(time.perf_counter() - start)"
859
+ ]
860
+ },
861
+ {
862
+ "cell_type": "code",
863
+ "execution_count": null,
864
+ "id": "cdee2b1c-0493-4b3e-8ecb-9d79109c756e",
865
+ "metadata": {
866
+ "tags": []
867
+ },
868
+ "outputs": [],
869
+ "source": [
870
+ "documents[0]"
871
+ ]
872
+ },
873
+ {
874
+ "cell_type": "markdown",
875
+ "id": "f90a0ed7-b5e9-4ae4-9e87-4c04875ebcc9",
876
+ "metadata": {},
877
+ "source": [
878
+ "Lets double check that we got all the embeddings we expected!"
879
+ ]
880
+ },
881
+ {
882
+ "cell_type": "markdown",
883
+ "id": "5b78bfa4-d365-4906-a71c-f444eabf6bf8",
884
+ "metadata": {
885
+ "tags": []
886
+ },
887
+ "source": [
888
+ "Great, we can see that they match.\n",
889
+ "\n",
890
+ "Let's write our embeddings to file"
891
+ ]
892
+ },
893
+ {
894
+ "cell_type": "markdown",
895
+ "id": "fc1e7cc5-b878-42bb-9fb4-e810f3f5006a",
896
+ "metadata": {
897
+ "tags": []
898
+ },
899
+ "source": [
900
+ "# Next Steps\n",
901
+ "We need to import this into a vector db. "
902
+ ]
903
+ }
904
+ ],
905
+ "metadata": {
906
+ "kernelspec": {
907
+ "display_name": "Python 3 (ipykernel)",
908
+ "language": "python",
909
+ "name": "python3"
910
+ },
911
+ "language_info": {
912
+ "codemirror_mode": {
913
+ "name": "ipython",
914
+ "version": 3
915
+ },
916
+ "file_extension": ".py",
917
+ "mimetype": "text/x-python",
918
+ "name": "python",
919
+ "nbconvert_exporter": "python",
920
+ "pygments_lexer": "ipython3",
921
+ "version": "3.10.13"
922
+ }
923
+ },
924
+ "nbformat": 4,
925
+ "nbformat_minor": 5
926
+ }
notebooks/05_vector_db.ipynb ADDED
@@ -0,0 +1,904 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "6a151ade-7d86-4a2e-bfe7-462089f4e04c",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Approach\n",
9
+ "There are a number of aspects of choosing a vector db that might be unique to your situation. You should think through your HW, utilization, latency requirements, scale, etc before choosing. \n",
10
+ "\n",
11
+ "Im targeting a demo (low utilization, latency can be relaxed) that will live on a huggingface space. I have a small scale that could even fit in memory. I like [Qdrant](https://qdrant.tech) for this. "
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "id": "b1b28232-b65d-41ce-88de-fd70b93a528d",
17
+ "metadata": {},
18
+ "source": [
19
+ "# Imports"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 1,
25
+ "id": "88408486-566a-4791-8ef2-5ee3e6941156",
26
+ "metadata": {
27
+ "tags": []
28
+ },
29
+ "outputs": [],
30
+ "source": [
31
+ "from IPython.core.interactiveshell import InteractiveShell\n",
32
+ "InteractiveShell.ast_node_interactivity = 'all'"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 2,
38
+ "id": "abb5186b-ee67-4e1e-882d-3d8d5b4575d4",
39
+ "metadata": {
40
+ "tags": []
41
+ },
42
+ "outputs": [],
43
+ "source": [
44
+ "from pathlib import Path\n",
45
+ "import json\n",
46
+ "\n",
47
+ "from tqdm.notebook import tqdm\n",
48
+ "import lancedb"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "code",
53
+ "execution_count": 3,
54
+ "id": "c4b82ea2-8b30-4c2e-99f0-9a30f2f1bfb7",
55
+ "metadata": {
56
+ "tags": []
57
+ },
58
+ "outputs": [
59
+ {
60
+ "name": "stdout",
61
+ "output_type": "stream",
62
+ "text": [
63
+ "/home/ec2-user/arabic-wiki\n"
64
+ ]
65
+ }
66
+ ],
67
+ "source": [
68
+ "proj_dir = Path.cwd().parent\n",
69
+ "print(proj_dir)"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "markdown",
74
+ "id": "76119e74-f601-436d-a253-63c5a19d1c83",
75
+ "metadata": {},
76
+ "source": [
77
+ "# Config"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": 4,
83
+ "id": "f6f74545-54a7-4f41-9f02-96964e1417f0",
84
+ "metadata": {
85
+ "tags": []
86
+ },
87
+ "outputs": [],
88
+ "source": [
89
+ "files_in = list((proj_dir / 'data/embedded/').glob('*.ndjson'))"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "markdown",
94
+ "id": "d2dd0df0-4274-45b3-9ee5-0205494e4d75",
95
+ "metadata": {
96
+ "tags": []
97
+ },
98
+ "source": [
99
+ "# Setup\n",
100
+ "Read in our list of dictionaries. This is the upper end for the machine Im using. This takes ~10GB of RAM. We could easily do this in batches of ~100k and be fine in most machines. "
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": 5,
106
+ "id": "3c08e039-3686-4eca-9f87-7c469e3f19bc",
107
+ "metadata": {
108
+ "tags": []
109
+ },
110
+ "outputs": [],
111
+ "source": [
112
+ "with open(files_in[0], 'r') as f:\n",
113
+ " first_line = f.readline().strip() # read only the first line\n",
114
+ " document = json.loads(first_line)\n",
115
+ " document['vector'] = document.pop('embedding')"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type": "markdown",
120
+ "id": "98aec715-8d97-439e-99c0-0eff63df386b",
121
+ "metadata": {},
122
+ "source": [
123
+ "Convert the dictionaries to `Documents`"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": 6,
129
+ "id": "4821e3c1-697d-4b69-bae3-300168755df9",
130
+ "metadata": {
131
+ "tags": []
132
+ },
133
+ "outputs": [
134
+ {
135
+ "data": {
136
+ "text/plain": [
137
+ "{'content': 'ุงู„ู…ุงุก ู…ุงุฏุฉูŒ ุดูุงูุฉูŒ ุนุฏูŠู…ุฉ ุงู„ู„ูˆู† ูˆุงู„ุฑุงุฆุญุฉุŒ ูˆู‡ูˆ ุงู„ู…ูƒูˆู‘ู† ุงู„ุฃุณุงุณูŠ ู„ู„ุฌุฏุงูˆู„ ูˆุงู„ุจุญูŠุฑุงุช ูˆุงู„ุจุญุงุฑ ูˆุงู„ู…ุญูŠุทุงุช ูˆูƒุฐู„ูƒ ู„ู„ุณูˆุงุฆู„ ููŠ ุฌู…ูŠุน ุงู„ูƒุงุฆู†ุงุช ุงู„ุญูŠู‘ุฉุŒ ูˆู‡ูˆ ุฃูƒุซุฑ ุงู„ู…ุฑูƒู‘ุจุงุช ุงู„ูƒูŠู…ูŠุงุฆูŠู‘ุฉ ุงู†ุชุดุงุฑุงู‹ ุนู„ู‰ ุณุทุญ ุงู„ุฃุฑุถ. ูŠุชุฃู„ู‘ู ุฌุฒูŠุก ุงู„ู…ุงุก ู…ู† ุฐุฑู‘ุฉ ุฃูƒุณุฌูŠู† ู…ุฑูƒุฒูŠุฉ ุชุฑุชุจุท ุจู‡ุง ุฐุฑู‘ุชุง ู‡ูŠุฏุฑูˆุฌูŠู† ุนู„ู‰ ุทุฑููŠู‡ุง ุจุฑุงุจุทุฉ ุชุณุงู‡ู…ูŠู‘ุฉ ุจุญูŠุซ ุชูƒูˆู† ุตูŠุบุชู‡ ุงู„ูƒูŠู…ูŠุงุฆูŠุฉ H2O. ุนู†ุฏ ุงู„ุธุฑูˆู ุงู„ู‚ูŠุงุณูŠุฉ ู…ู† ุงู„ุถุบุท ูˆุฏุฑุฌุฉ ุงู„ุญุฑุงุฑุฉ ูŠูƒูˆู† ุงู„ู…ุงุก ุณุงุฆู„ุงู‹ุ› ุฃู…ู‘ุง ุงู„ุญุงู„ุฉ ุงู„ุตู„ุจุฉ ูุชุชุดูƒู‘ู„ ุนู†ุฏ ู†ู‚ุทุฉ ุงู„ุชุฌู…ู‘ุฏุŒ ูˆุชุฏุนู‰ ุจุงู„ุฌู„ูŠุฏุ› ุฃู…ู‘ุง ุงู„ุญุงู„ุฉ ุงู„ุบุงุฒูŠุฉ ูุชุชุดูƒู‘ู„ ุนู†ุฏ ู†ู‚ุทุฉ ุงู„ุบู„ูŠุงู†ุŒ ูˆุชุณู…ู‘ู‰ ุจุฎุงุฑ ุงู„ู…ุงุก.\\nุฅู†ู‘ ุงู„ู…ุงุก ู‡ูˆ ุฃุณุงุณ ูˆุฌูˆุฏ ุงู„ุญูŠุงุฉ ุนู„ู‰ ูƒูˆูƒุจ ุงู„ุฃุฑุถุŒ ูˆู‡ูˆ ูŠุบุทู‘ูŠ 71% ู…ู† ุณุทุญู‡ุงุŒ ูˆุชู…ุซู‘ู„ ู…ูŠุงู‡ ุงู„ุจุญุงุฑ ูˆุงู„ู…ุญูŠุทุงุช ุฃูƒุจุฑ ู†ุณุจุฉ ู„ู„ู…ุงุก ุนู„ู‰ ุงู„ุฃุฑุถุŒ ุญูŠุซ ุชุจู„ุบ ุญูˆุงู„ูŠ 96.5%. ูˆุชุชูˆุฒู‘ุน ุงู„ู†ุณุจ ุงู„ุจุงู‚ูŠุฉ ุจูŠู† ุงู„ู…ูŠุงู‡ ุงู„ุฌูˆููŠู‘ุฉ ูˆุจูŠู† ุฌู„ูŠุฏ ุงู„ู…ู†ุงุทู‚ ุงู„ู‚ุทุจูŠู‘ุฉ (1.7% ู„ูƒู„ูŠู‡ู…ุง)ุŒ ู…ุน ูˆุฌูˆุฏ ู†ุณุจุฉ ุตุบูŠุฑุฉ ุนู„ู‰ ุดูƒู„ ุจุฎุงุฑ ู…ุงุก ู…ุนู„ู‘ู‚ ููŠ ุงู„ู‡ูˆุงุก ุนู„ู‰ ู‡ูŠุฆุฉ ุณุญุงุจ (ุบูŠูˆู…)ุŒ ูˆุฃุญูŠุงู†ุงู‹ ุฃุฎุฑู‰ ุนู„ู‰ ู‡ูŠุฆุฉ ุถุจุงุจ ุฃูˆ ู†ุฏู‰ุŒ ุจุงู„ุฅุถุงูุฉ ุฅู„ู‰ ุงู„ุฒุฎุงุช ุงู„ู…ุทุฑูŠู‘ุฉ ุฃูˆ ุงู„ุซู„ุฌูŠู‘ุฉ. ุชุจู„ุบ ู†ุณุจุฉ ุงู„ู…ุงุก ุงู„ุนุฐุจ ุญูˆุงู„ูŠ 2.5% ูู‚ุท ู…ู† ุงู„ู…ุงุก ุงู„ู…ูˆุฌูˆุฏ ุนู„ู‰ ุงู„ุฃุฑุถุŒ ๏ฟฝ๏ฟฝุฃุบู„ุจ ู‡ุฐู‡ ุงู„ูƒู…ู‘ูŠู‘ุฉ (ุญูˆุงู„ูŠ 99%) ู…ูˆุฌูˆุฏุฉ ููŠ ุงู„ูƒุชู„ ุงู„ุฌู„ูŠุฏูŠู‘ุฉ ููŠ ุงู„ู…ู†ุงุทู‚ ุงู„ู‚ุทุจูŠู‘ุฉุŒ ููŠ ุญูŠู† ุชุชูˆุงุฌุฏ 0.3% ู…ู† ุงู„ู…ุงุก ุงู„ุนุฐุจ ููŠ ุงู„ุฃู†ู‡ุงุฑ ูˆุงู„ุจุญูŠุฑุงุช ูˆููŠ ุงู„ุบู„ุงู ุงู„ุฌูˆู‘ูŠ.\\nุฃู…ุง ููŠ ุงู„ุทุจูŠุนุฉุŒ ูุชุชุบูŠู‘ุฑ ุญุงู„ุฉ ุงู„ู…ุงุก ุจูŠู† ุงู„ุญุงู„ุงุช ุงู„ุซู„ุงุซุฉ ู„ู„ู…ุงุฏุฉ ุนู„ู‰ ุณุทุญ ุงู„ุฃุฑุถ ุจุงุณุชู…ุฑุงุฑ ู…ู† ุฎู„ุงู„ ู…ุง ูŠุนุฑู ุจุงุณู… ุงู„ุฏูˆุฑุฉ ุงู„ู…ุงุฆูŠู‘ุฉ (ุฃูˆ ุฏูˆุฑุฉ ุงู„ู…ุงุก)ุŒ ูˆุงู„ุชูŠ ุชุชุถู…ู‘ู† ุญุฏูˆุซ ุชุจุฎู‘ุฑ ูˆู†ุชุญ (ู†ุชุญ ุชุจุฎู‘ุฑูŠ) ุซู… ุชูƒุซูŠู ูู‡ุทูˆู„ ุซู… ุฌุฑูŠุงู† ู„ุชุตู„ ุฅู„ู‰ ุงู„ู…ุตุจู‘ ููŠ ุงู„ู…ุณุทู‘ุญุงุช ุงู„ู…ุงุฆูŠู‘ุฉ.\\n',\n",
138
+ " 'content_type': 'text',\n",
139
+ " 'score': None,\n",
140
+ " 'meta': {'id': '7',\n",
141
+ " 'revid': '2080427',\n",
142
+ " 'url': 'https://ar.wikipedia.org/wiki?curid=7',\n",
143
+ " 'title': 'ู…ุงุก',\n",
144
+ " '_split_id': 0,\n",
145
+ " '_split_overlap': [{'doc_id': '725ec671057ef790ad582509a8653584',\n",
146
+ " 'range': [887, 1347]}]},\n",
147
+ " 'id_hash_keys': ['content'],\n",
148
+ " 'id': '109a29bb227b1aaa5b784e972d8e1e3e',\n",
149
+ " 'vector': [-0.07318115,\n",
150
+ " 0.087646484,\n",
151
+ " 0.03274536,\n",
152
+ " 0.034942627,\n",
153
+ " 0.097961426,\n",
154
+ " '...']}"
155
+ ]
156
+ },
157
+ "execution_count": 6,
158
+ "metadata": {},
159
+ "output_type": "execute_result"
160
+ }
161
+ ],
162
+ "source": [
163
+ "doc = document.copy()\n",
164
+ "doc['vector'] = doc['vector'][:5] + ['...']\n",
165
+ "doc"
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "markdown",
170
+ "id": "676f644c-fb09-4d17-89ba-30c92aad8777",
171
+ "metadata": {},
172
+ "source": [
173
+ "Instantiate our `DocumentStore`. Note that Im saving this to disk, this is for portability which is good considering I want to move from this ec2 instance into a Hugging Face Space. \n",
174
+ "\n",
175
+ "Note that if you are doing this at scale, you should use a proper instance and not saving to file. You should also take a [measured ingestion](https://qdrant.tech/documentation/tutorials/bulk-upload/) approach instead of using a convenient loader. "
176
+ ]
177
+ },
178
+ {
179
+ "cell_type": "code",
180
+ "execution_count": 7,
181
+ "id": "78033b87-8f68-4a44-899e-36fa8167cacf",
182
+ "metadata": {
183
+ "tags": []
184
+ },
185
+ "outputs": [],
186
+ "source": [
187
+ "from lancedb.embeddings.registry import EmbeddingFunctionRegistry\n",
188
+ "from lancedb.embeddings.sentence_transformers import SentenceTransformerEmbeddings\n",
189
+ "\n",
190
+ "\n",
191
+ "db = lancedb.connect(proj_dir/\".lancedb\")\n",
192
+ "tbl = db.create_table('arabic-wiki', [document])"
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "code",
197
+ "execution_count": 8,
198
+ "id": "21d5fa58-519e-4a23-9fc6-eed31e4723b5",
199
+ "metadata": {
200
+ "tags": []
201
+ },
202
+ "outputs": [
203
+ {
204
+ "data": {
205
+ "application/vnd.jupyter.widget-view+json": {
206
+ "model_id": "789efc342218412aa31d5a5a74b34c52",
207
+ "version_major": 2,
208
+ "version_minor": 0
209
+ },
210
+ "text/plain": [
211
+ "Wiki Files: 0%| | 0/23 [00:00<?, ?it/s]"
212
+ ]
213
+ },
214
+ "metadata": {},
215
+ "output_type": "display_data"
216
+ },
217
+ {
218
+ "name": "stdout",
219
+ "output_type": "stream",
220
+ "text": [
221
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_1.ndjson\n",
222
+ "Read documents\n"
223
+ ]
224
+ },
225
+ {
226
+ "data": {
227
+ "application/vnd.jupyter.widget-view+json": {
228
+ "model_id": "5bcf4a53ecaf471589d35588047969ea",
229
+ "version_major": 2,
230
+ "version_minor": 0
231
+ },
232
+ "text/plain": [
233
+ " 0%| | 0/243068 [00:00<?, ?it/s]"
234
+ ]
235
+ },
236
+ "metadata": {},
237
+ "output_type": "display_data"
238
+ },
239
+ {
240
+ "name": "stdout",
241
+ "output_type": "stream",
242
+ "text": [
243
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_1.ndjson\n",
244
+ "Added documents\n",
245
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_2.ndjson\n",
246
+ "Read documents\n"
247
+ ]
248
+ },
249
+ {
250
+ "data": {
251
+ "application/vnd.jupyter.widget-view+json": {
252
+ "model_id": "763e45a7657e40288807556b1eb6c598",
253
+ "version_major": 2,
254
+ "version_minor": 0
255
+ },
256
+ "text/plain": [
257
+ " 0%| | 0/104065 [00:00<?, ?it/s]"
258
+ ]
259
+ },
260
+ "metadata": {},
261
+ "output_type": "display_data"
262
+ },
263
+ {
264
+ "name": "stdout",
265
+ "output_type": "stream",
266
+ "text": [
267
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_2.ndjson\n",
268
+ "Added documents\n",
269
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_3.ndjson\n",
270
+ "Read documents\n"
271
+ ]
272
+ },
273
+ {
274
+ "data": {
275
+ "application/vnd.jupyter.widget-view+json": {
276
+ "model_id": "6df7e0f7acfd409dbbaa04f9a21f6e18",
277
+ "version_major": 2,
278
+ "version_minor": 0
279
+ },
280
+ "text/plain": [
281
+ " 0%| | 0/123154 [00:00<?, ?it/s]"
282
+ ]
283
+ },
284
+ "metadata": {},
285
+ "output_type": "display_data"
286
+ },
287
+ {
288
+ "name": "stdout",
289
+ "output_type": "stream",
290
+ "text": [
291
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_3.ndjson\n",
292
+ "Added documents\n",
293
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_4.ndjson\n",
294
+ "Read documents\n"
295
+ ]
296
+ },
297
+ {
298
+ "data": {
299
+ "application/vnd.jupyter.widget-view+json": {
300
+ "model_id": "87a7b93d85964254b7ae106795e55cbe",
301
+ "version_major": 2,
302
+ "version_minor": 0
303
+ },
304
+ "text/plain": [
305
+ " 0%| | 0/135965 [00:00<?, ?it/s]"
306
+ ]
307
+ },
308
+ "metadata": {},
309
+ "output_type": "display_data"
310
+ },
311
+ {
312
+ "name": "stdout",
313
+ "output_type": "stream",
314
+ "text": [
315
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_4.ndjson\n",
316
+ "Added documents\n",
317
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_5.ndjson\n",
318
+ "Read documents\n"
319
+ ]
320
+ },
321
+ {
322
+ "data": {
323
+ "application/vnd.jupyter.widget-view+json": {
324
+ "model_id": "b4c9eebb1be24e42885df1d4444c4856",
325
+ "version_major": 2,
326
+ "version_minor": 0
327
+ },
328
+ "text/plain": [
329
+ " 0%| | 0/99138 [00:00<?, ?it/s]"
330
+ ]
331
+ },
332
+ "metadata": {},
333
+ "output_type": "display_data"
334
+ },
335
+ {
336
+ "name": "stdout",
337
+ "output_type": "stream",
338
+ "text": [
339
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_5.ndjson\n",
340
+ "Added documents\n",
341
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_6.ndjson\n",
342
+ "Read documents\n"
343
+ ]
344
+ },
345
+ {
346
+ "data": {
347
+ "application/vnd.jupyter.widget-view+json": {
348
+ "model_id": "feb88bc980534ca2bb7545784f9080df",
349
+ "version_major": 2,
350
+ "version_minor": 0
351
+ },
352
+ "text/plain": [
353
+ " 0%| | 0/83678 [00:00<?, ?it/s]"
354
+ ]
355
+ },
356
+ "metadata": {},
357
+ "output_type": "display_data"
358
+ },
359
+ {
360
+ "name": "stdout",
361
+ "output_type": "stream",
362
+ "text": [
363
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_6.ndjson\n",
364
+ "Added documents\n",
365
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_7.ndjson\n",
366
+ "Read documents\n"
367
+ ]
368
+ },
369
+ {
370
+ "data": {
371
+ "application/vnd.jupyter.widget-view+json": {
372
+ "model_id": "7ae8940509a94c17b209a783319af0b5",
373
+ "version_major": 2,
374
+ "version_minor": 0
375
+ },
376
+ "text/plain": [
377
+ " 0%| | 0/30573 [00:00<?, ?it/s]"
378
+ ]
379
+ },
380
+ "metadata": {},
381
+ "output_type": "display_data"
382
+ },
383
+ {
384
+ "name": "stdout",
385
+ "output_type": "stream",
386
+ "text": [
387
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_7.ndjson\n",
388
+ "Added documents\n",
389
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_8.ndjson\n",
390
+ "Read documents\n"
391
+ ]
392
+ },
393
+ {
394
+ "data": {
395
+ "application/vnd.jupyter.widget-view+json": {
396
+ "model_id": "edbbeb596b034859856b525755331021",
397
+ "version_major": 2,
398
+ "version_minor": 0
399
+ },
400
+ "text/plain": [
401
+ " 0%| | 0/78957 [00:00<?, ?it/s]"
402
+ ]
403
+ },
404
+ "metadata": {},
405
+ "output_type": "display_data"
406
+ },
407
+ {
408
+ "name": "stdout",
409
+ "output_type": "stream",
410
+ "text": [
411
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_8.ndjson\n",
412
+ "Added documents\n",
413
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_9.ndjson\n",
414
+ "Read documents\n"
415
+ ]
416
+ },
417
+ {
418
+ "data": {
419
+ "application/vnd.jupyter.widget-view+json": {
420
+ "model_id": "1c0c7b32f0c14e77b3567dae6dea7c0f",
421
+ "version_major": 2,
422
+ "version_minor": 0
423
+ },
424
+ "text/plain": [
425
+ " 0%| | 0/86327 [00:00<?, ?it/s]"
426
+ ]
427
+ },
428
+ "metadata": {},
429
+ "output_type": "display_data"
430
+ },
431
+ {
432
+ "name": "stdout",
433
+ "output_type": "stream",
434
+ "text": [
435
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_9.ndjson\n",
436
+ "Added documents\n",
437
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_10.ndjson\n",
438
+ "Read documents\n"
439
+ ]
440
+ },
441
+ {
442
+ "data": {
443
+ "application/vnd.jupyter.widget-view+json": {
444
+ "model_id": "193cc9f4f4ce4e22baf682bb31d77f13",
445
+ "version_major": 2,
446
+ "version_minor": 0
447
+ },
448
+ "text/plain": [
449
+ " 0%| | 0/83111 [00:00<?, ?it/s]"
450
+ ]
451
+ },
452
+ "metadata": {},
453
+ "output_type": "display_data"
454
+ },
455
+ {
456
+ "name": "stdout",
457
+ "output_type": "stream",
458
+ "text": [
459
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_10.ndjson\n",
460
+ "Added documents\n",
461
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_11.ndjson\n",
462
+ "Read documents\n"
463
+ ]
464
+ },
465
+ {
466
+ "data": {
467
+ "application/vnd.jupyter.widget-view+json": {
468
+ "model_id": "e5c2e514cae1462896f856e625d61c2b",
469
+ "version_major": 2,
470
+ "version_minor": 0
471
+ },
472
+ "text/plain": [
473
+ " 0%| | 0/92664 [00:00<?, ?it/s]"
474
+ ]
475
+ },
476
+ "metadata": {},
477
+ "output_type": "display_data"
478
+ },
479
+ {
480
+ "name": "stdout",
481
+ "output_type": "stream",
482
+ "text": [
483
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_11.ndjson\n",
484
+ "Added documents\n",
485
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_12.ndjson\n",
486
+ "Read documents\n"
487
+ ]
488
+ },
489
+ {
490
+ "data": {
491
+ "application/vnd.jupyter.widget-view+json": {
492
+ "model_id": "818e7ebd889b4bbfa745904b5f132e48",
493
+ "version_major": 2,
494
+ "version_minor": 0
495
+ },
496
+ "text/plain": [
497
+ " 0%| | 0/66404 [00:00<?, ?it/s]"
498
+ ]
499
+ },
500
+ "metadata": {},
501
+ "output_type": "display_data"
502
+ },
503
+ {
504
+ "name": "stdout",
505
+ "output_type": "stream",
506
+ "text": [
507
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_12.ndjson\n",
508
+ "Added documents\n",
509
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_13.ndjson\n",
510
+ "Read documents\n"
511
+ ]
512
+ },
513
+ {
514
+ "data": {
515
+ "application/vnd.jupyter.widget-view+json": {
516
+ "model_id": "fe6268b0d85045da839a15f0dce577c9",
517
+ "version_major": 2,
518
+ "version_minor": 0
519
+ },
520
+ "text/plain": [
521
+ " 0%| | 0/62844 [00:00<?, ?it/s]"
522
+ ]
523
+ },
524
+ "metadata": {},
525
+ "output_type": "display_data"
526
+ },
527
+ {
528
+ "name": "stdout",
529
+ "output_type": "stream",
530
+ "text": [
531
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_13.ndjson\n",
532
+ "Added documents\n",
533
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_14.ndjson\n",
534
+ "Read documents\n"
535
+ ]
536
+ },
537
+ {
538
+ "data": {
539
+ "application/vnd.jupyter.widget-view+json": {
540
+ "model_id": "f67b03d9829545d8bfcbcdc55daac3a7",
541
+ "version_major": 2,
542
+ "version_minor": 0
543
+ },
544
+ "text/plain": [
545
+ " 0%| | 0/59349 [00:00<?, ?it/s]"
546
+ ]
547
+ },
548
+ "metadata": {},
549
+ "output_type": "display_data"
550
+ },
551
+ {
552
+ "name": "stdout",
553
+ "output_type": "stream",
554
+ "text": [
555
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_14.ndjson\n",
556
+ "Added documents\n",
557
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_15.ndjson\n",
558
+ "Read documents\n"
559
+ ]
560
+ },
561
+ {
562
+ "data": {
563
+ "application/vnd.jupyter.widget-view+json": {
564
+ "model_id": "95bd941e1516437ca7e7cb59a55971ca",
565
+ "version_major": 2,
566
+ "version_minor": 0
567
+ },
568
+ "text/plain": [
569
+ " 0%| | 0/52554 [00:00<?, ?it/s]"
570
+ ]
571
+ },
572
+ "metadata": {},
573
+ "output_type": "display_data"
574
+ },
575
+ {
576
+ "name": "stdout",
577
+ "output_type": "stream",
578
+ "text": [
579
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_15.ndjson\n",
580
+ "Added documents\n",
581
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_16.ndjson\n",
582
+ "Read documents\n"
583
+ ]
584
+ },
585
+ {
586
+ "data": {
587
+ "application/vnd.jupyter.widget-view+json": {
588
+ "model_id": "684b1247cf614817be09f34a2b349632",
589
+ "version_major": 2,
590
+ "version_minor": 0
591
+ },
592
+ "text/plain": [
593
+ " 0%| | 0/34240 [00:00<?, ?it/s]"
594
+ ]
595
+ },
596
+ "metadata": {},
597
+ "output_type": "display_data"
598
+ },
599
+ {
600
+ "name": "stdout",
601
+ "output_type": "stream",
602
+ "text": [
603
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_16.ndjson\n",
604
+ "Added documents\n",
605
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_17.ndjson\n",
606
+ "Read documents\n"
607
+ ]
608
+ },
609
+ {
610
+ "data": {
611
+ "application/vnd.jupyter.widget-view+json": {
612
+ "model_id": "d8c7f992f5064e5e83ec292a8e7c33bf",
613
+ "version_major": 2,
614
+ "version_minor": 0
615
+ },
616
+ "text/plain": [
617
+ " 0%| | 0/35933 [00:00<?, ?it/s]"
618
+ ]
619
+ },
620
+ "metadata": {},
621
+ "output_type": "display_data"
622
+ },
623
+ {
624
+ "name": "stdout",
625
+ "output_type": "stream",
626
+ "text": [
627
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_17.ndjson\n",
628
+ "Added documents\n",
629
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_18.ndjson\n",
630
+ "Read documents\n"
631
+ ]
632
+ },
633
+ {
634
+ "data": {
635
+ "application/vnd.jupyter.widget-view+json": {
636
+ "model_id": "5f7582025ad9462da77c0a57ba4a24f7",
637
+ "version_major": 2,
638
+ "version_minor": 0
639
+ },
640
+ "text/plain": [
641
+ " 0%| | 0/64575 [00:00<?, ?it/s]"
642
+ ]
643
+ },
644
+ "metadata": {},
645
+ "output_type": "display_data"
646
+ },
647
+ {
648
+ "name": "stdout",
649
+ "output_type": "stream",
650
+ "text": [
651
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_18.ndjson\n",
652
+ "Added documents\n",
653
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_19.ndjson\n",
654
+ "Read documents\n"
655
+ ]
656
+ },
657
+ {
658
+ "data": {
659
+ "application/vnd.jupyter.widget-view+json": {
660
+ "model_id": "e574e5ee70564cf78684eae0ca1ea6c4",
661
+ "version_major": 2,
662
+ "version_minor": 0
663
+ },
664
+ "text/plain": [
665
+ " 0%| | 0/94244 [00:00<?, ?it/s]"
666
+ ]
667
+ },
668
+ "metadata": {},
669
+ "output_type": "display_data"
670
+ },
671
+ {
672
+ "name": "stdout",
673
+ "output_type": "stream",
674
+ "text": [
675
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_19.ndjson\n",
676
+ "Added documents\n",
677
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_20.ndjson\n",
678
+ "Read documents\n"
679
+ ]
680
+ },
681
+ {
682
+ "data": {
683
+ "application/vnd.jupyter.widget-view+json": {
684
+ "model_id": "21e50920d25047038bbad906e428ab5b",
685
+ "version_major": 2,
686
+ "version_minor": 0
687
+ },
688
+ "text/plain": [
689
+ " 0%| | 0/124472 [00:00<?, ?it/s]"
690
+ ]
691
+ },
692
+ "metadata": {},
693
+ "output_type": "display_data"
694
+ },
695
+ {
696
+ "name": "stdout",
697
+ "output_type": "stream",
698
+ "text": [
699
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_20.ndjson\n",
700
+ "Added documents\n",
701
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_21.ndjson\n",
702
+ "Read documents\n"
703
+ ]
704
+ },
705
+ {
706
+ "data": {
707
+ "application/vnd.jupyter.widget-view+json": {
708
+ "model_id": "da30c5cd7cc14c47a739a1a2e410b012",
709
+ "version_major": 2,
710
+ "version_minor": 0
711
+ },
712
+ "text/plain": [
713
+ " 0%| | 0/121849 [00:00<?, ?it/s]"
714
+ ]
715
+ },
716
+ "metadata": {},
717
+ "output_type": "display_data"
718
+ },
719
+ {
720
+ "name": "stdout",
721
+ "output_type": "stream",
722
+ "text": [
723
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_21.ndjson\n",
724
+ "Added documents\n",
725
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_22.ndjson\n",
726
+ "Read documents\n"
727
+ ]
728
+ },
729
+ {
730
+ "data": {
731
+ "application/vnd.jupyter.widget-view+json": {
732
+ "model_id": "8efe4d8bf6f5437e88415819b0b88114",
733
+ "version_major": 2,
734
+ "version_minor": 0
735
+ },
736
+ "text/plain": [
737
+ " 0%| | 0/147110 [00:00<?, ?it/s]"
738
+ ]
739
+ },
740
+ "metadata": {},
741
+ "output_type": "display_data"
742
+ },
743
+ {
744
+ "name": "stdout",
745
+ "output_type": "stream",
746
+ "text": [
747
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_22.ndjson\n",
748
+ "Added documents\n",
749
+ "Reading documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_23.ndjson\n",
750
+ "Read documents\n"
751
+ ]
752
+ },
753
+ {
754
+ "data": {
755
+ "application/vnd.jupyter.widget-view+json": {
756
+ "model_id": "7bda66b6033346559907a3bb56a95957",
757
+ "version_major": 2,
758
+ "version_minor": 0
759
+ },
760
+ "text/plain": [
761
+ " 0%| | 0/70322 [00:00<?, ?it/s]"
762
+ ]
763
+ },
764
+ "metadata": {},
765
+ "output_type": "display_data"
766
+ },
767
+ {
768
+ "name": "stdout",
769
+ "output_type": "stream",
770
+ "text": [
771
+ "Adding documents /home/ec2-user/arabic-wiki/data/embedded/ar_wiki_23.ndjson\n",
772
+ "Added documents\n"
773
+ ]
774
+ },
775
+ {
776
+ "name": "stderr",
777
+ "output_type": "stream",
778
+ "text": [
779
+ " 0%| | 0/50 [00:00<?, ?it/s]/opt/conda/envs/arwiki/lib/python3.10/site-packages/lance/torch/kmeans.py:232: UserWarning: index_reduce() is in beta and the API may change at any time. (Triggered internally at ../aten/src/ATen/native/cuda/Indexing.cu:1047.)\n",
780
+ " new_centroids.index_reduce_(\n",
781
+ " 34%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ | 17/50 [01:26<02:47, 5.06s/it]\n",
782
+ "[2023-10-31T18:47:43Z WARN lance_linalg::kmeans] KMeans: cluster 108 is empty\n",
783
+ "[2023-10-31T18:52:24Z WARN lance_linalg::kmeans] KMeans: cluster 227 is empty\n",
784
+ "[2023-10-31T18:57:24Z WARN lance_linalg::kmeans] KMeans: cluster 167 is empty\n",
785
+ "[2023-10-31T19:14:19Z WARN lance_linalg::kmeans] KMeans: cluster 160 is empty\n"
786
+ ]
787
+ },
788
+ {
789
+ "name": "stdout",
790
+ "output_type": "stream",
791
+ "text": [
792
+ "CPU times: user 2h 2min 51s, sys: 1min 38s, total: 2h 4min 30s\n",
793
+ "Wall time: 42min 56s\n"
794
+ ]
795
+ }
796
+ ],
797
+ "source": [
798
+ "%%time\n",
799
+ "for file_in in tqdm(files_in, desc='Wiki Files: '):\n",
800
+ "\n",
801
+ " tqdm.write(f\"Reading documents {str(file_in)}\")\n",
802
+ " with open(file_in, 'r') as f:\n",
803
+ " documents = [json.loads(line) for line in f]\n",
804
+ " tqdm.write(f\"Read documents\")\n",
805
+ "\n",
806
+ " for doc in tqdm(documents):\n",
807
+ " if 'embedding' in doc:\n",
808
+ " doc['vector'] = doc.pop('embedding')\n",
809
+ " \n",
810
+ " tqdm.write(f\"Adding documents {str(file_in)}\")\n",
811
+ " tbl.add(documents)\n",
812
+ " tqdm.write(f\"Added documents\")\n",
813
+ "tbl.create_index(\n",
814
+ " num_partitions=1024,\n",
815
+ " num_sub_vectors=384,\n",
816
+ " accelerator=\"cuda\"\n",
817
+ ")\n",
818
+ " "
819
+ ]
820
+ },
821
+ {
822
+ "cell_type": "code",
823
+ "execution_count": 9,
824
+ "id": "8ad72ca5-6ca3-43e3-bf2c-7461906576b9",
825
+ "metadata": {
826
+ "tags": []
827
+ },
828
+ "outputs": [],
829
+ "source": [
830
+ "from sentence_transformers import SentenceTransformer\n",
831
+ "\n",
832
+ "name=\"sentence-transformers/paraphrase-multilingual-minilm-l12-v2\"\n",
833
+ "model = SentenceTransformer(name)\n",
834
+ "\n",
835
+ "# used for both training and querying\n",
836
+ "def embed_func(batch):\n",
837
+ " return [model.encode(sentence) for sentence in batch]"
838
+ ]
839
+ },
840
+ {
841
+ "cell_type": "code",
842
+ "execution_count": 11,
843
+ "id": "41ab5a84-8984-4726-acd8-57ca0fce9e76",
844
+ "metadata": {
845
+ "tags": []
846
+ },
847
+ "outputs": [
848
+ {
849
+ "data": {
850
+ "text/plain": [
851
+ "['ุจูƒูŠู†',\n",
852
+ " 'ูƒูˆู†ู…ูŠู†ุบ',\n",
853
+ " 'ู†ูŠู†ุบุดูŠุง',\n",
854
+ " 'ุชุงูŠ ูŠูˆุงู†',\n",
855
+ " 'ุชุดู†ุบุชุดูˆ',\n",
856
+ " 'ุดุงู†ุบู‡ุงูŠ',\n",
857
+ " 'ุณู†ุบุงููˆุฑุฉ',\n",
858
+ " 'ุฏู„ุชุง ู†ู‡ุฑ ูŠุงู†ุบุชุณูŠ',\n",
859
+ " 'ุชุดุงู†ุบุชุดูˆู†',\n",
860
+ " 'ุจูƒูŠู†']"
861
+ ]
862
+ },
863
+ "execution_count": 11,
864
+ "metadata": {},
865
+ "output_type": "execute_result"
866
+ }
867
+ ],
868
+ "source": [
869
+ "query = \"What is the capital of China? I think it's Singapore.\"\n",
870
+ "query_vector = embed_func([query])[0]\n",
871
+ "[doc['meta']['title'] for doc in tbl.search(query_vector).limit(10).to_list()]"
872
+ ]
873
+ },
874
+ {
875
+ "cell_type": "code",
876
+ "execution_count": null,
877
+ "id": "c0abad86-652a-4d7d-b118-21dc23a7a5c5",
878
+ "metadata": {},
879
+ "outputs": [],
880
+ "source": []
881
+ }
882
+ ],
883
+ "metadata": {
884
+ "kernelspec": {
885
+ "display_name": "Python 3 (ipykernel)",
886
+ "language": "python",
887
+ "name": "python3"
888
+ },
889
+ "language_info": {
890
+ "codemirror_mode": {
891
+ "name": "ipython",
892
+ "version": 3
893
+ },
894
+ "file_extension": ".py",
895
+ "mimetype": "text/x-python",
896
+ "name": "python",
897
+ "nbconvert_exporter": "python",
898
+ "pygments_lexer": "ipython3",
899
+ "version": "3.10.13"
900
+ }
901
+ },
902
+ "nbformat": 4,
903
+ "nbformat_minor": 5
904
+ }