craigmacdonald commited on
Commit
9d43a4f
·
verified ·
1 Parent(s): e4bc4f0

Upload ragwiki_indexing-e5.flex.ipynb

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