File size: 10,401 Bytes
9d43a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# E5 PyTerrier_DR Index for RAG Wikipedia Corpus\n",
    "\n",
    "This creates a dense index using [PyTerrier](https://github.com/terrier-org/pyterrier) and [PyTerrier_dr](https://github.com/terrierteam/pyterrier_dr) for the Wikipedia corpus used by Natural Questions and TextbookQuestionAnswering datasets.\n",
    "\n",
    "The corpus is downloaded from https://huggingface.co/datasets/RUC-NLPIR/FlashRAG_datasets/resolve/main/retrieval-corpus/wiki18_100w.zip by `\n",
    "pt.get_dataset('rag:nq_wiki').get_corpus_iter()`.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyterrier as pt\n",
    "import pyterrier_rag\n",
    "\n",
    "# print pretty progress bars\n",
    "pt.utils.set_tqdm('notebook')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ensure pyterrier_dr is installed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001b[0m\u001b[33m\n",
      "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install -q pyterrier_dr\n",
    "from pyterrier_dr import FlexIndex, E5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll need an E5 model - this will transform the document text into document embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "e5 = E5()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's setup an indexing pipeline. Documents are encoded using `e5` before being stored in the FlexIndex emebdding store. The FlexIndex can be used for retrieval later."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "53fb275a60da417daa05d5e1240ae039",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "indexing: 0dvec [00:00, ?dvec/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "index = \"./nq_tctindex.flex\"\n",
    "index = FlexIndex(index)\n",
    "(e5 >> index).index(pt.get_dataset('rag:nq_wiki').get_corpus_iter())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we upload the index to Huggingface."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adding docnos.npids [207 B]\n",
      "adding pt_meta.json [81 B]\n",
      "adding vecs.f4 [60.1 GB]\n",
      "starting segment 1\n",
      "starting segment 2\n",
      "starting segment 3\n",
      "starting segment 4\n",
      "starting segment 5\n",
      "starting segment 6\n",
      "starting segment 7\n",
      "starting segment 8\n",
      "starting segment 9\n",
      "starting segment 10\n",
      "starting segment 11\n",
      "starting segment 12\n",
      "starting segment 13\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ea2c0152abb34d28aff00335ebcdf6e7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.12:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5184bd70ae9e42a0bc3221278d807fe9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.1:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b84c5bc6a823413981e493077d86f283",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.10:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f19b5a687afa4e329937a4e3cb46f66e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.11:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b0d8c9b33e6b4a68b470738fe0f82923",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.0:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "deeb302680ce4e089b264ab9ff5f9e0a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Upload 15 LFS files:   0%|          | 0/15 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "df192fdb3fea483999f38e474e1f5b86",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.13:   0%|          | 0.00/802M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7b3bfeaec1814b74ba9bdb905f0f2713",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.2:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4705436114824e4cb962d2906c62959a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.3:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b233353405404a7099a752d4ad1576db",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.4:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "51aa06c9c85746ef981bc3cfc86ea5e0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.5:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d950b616a88240c8ac3b28d72dde8d59",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.6:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a1939208cc3e4c248926a75068ebf7d2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.7:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "301870e8556f4da59f2779a3d2ba4d26",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.8:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "201baa6e39054d5285ba702b116a63ae",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.9:   0%|          | 0.00/4.90G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a030063cfe764414ba7033e8d352c899",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.json:   0%|          | 0.00/788 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Artifact uploaded to https://huggingface.co/datasets/pyterrier/ragwiki-e5.flex/tree/main/\n",
      "Consider editing the README.md to help explain this artifact to others.\n"
     ]
    }
   ],
   "source": [
    "e5_emb_index.to_hf('pyterrier/ragwiki-e5.flex')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:rag]",
   "language": "python",
   "name": "conda-env-rag-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}