File size: 5,845 Bytes
5aa458f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sparse Index for RAG Wikipedia Corpus\n",
    "\n",
    "This creates a sparse Terrier index using PyTerrier for the Wikipedia corpus used by Natural Questions and TextbookQuestionAnswering.\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": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyterrier as pt\n",
    "import pyterrier_rag"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook requires PyTerrier 0.13 or higher."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.13.0'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pt.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets prepare the index. We're going to store the title and text of the documents in the Terrier index, so we can use them for reranking. A study of title and text length distributions found that very few were cutoff with for max lengths of 1750 and 125, respectively.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13:45:49.361 [ForkJoinPool-2-worker-3] WARN org.terrier.structures.BaseCompressingMetaIndex -- Structure meta reading lookup file directly from disk (SLOW) - try index.meta.index-source=fileinmem in the index properties file. 137.3 MiB of memory would be required.\n",
      "13:45:49.366 [ForkJoinPool-2-worker-3] WARN org.terrier.structures.BaseCompressingMetaIndex -- Structure meta reading data file directly from disk (SLOW) - try index.meta.data-source=fileinmem in the index properties file. 7 GiB of memory would be required.\n",
      "13:56:25.302 [ForkJoinPool-2-worker-3] WARN org.terrier.structures.BaseCompressingMetaIndex -- Structure meta reading data file directly from disk (SLOW) - try index.meta.data-source=fileinmem in the index properties file. 1.2 GiB of memory would be required.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<org.terrier.querying.IndexRef at 0x7fa3d024d5b0 jclass=org/terrier/querying/IndexRef jself=<LocalRef obj=0xc526808 at 0x7fa274037470>>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_dir = \"./nq_index_new\"\n",
    "ref = pt.IterDictIndexer(\n",
    "        index_dir, \n",
    "        text_attrs=['title', 'text'], \n",
    "        meta={'docno' : 20, 'text' : 1750, 'title' : 125}\n",
    "    ).index(pt.get_dataset('rag:nq_wiki').get_corpus_iter())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then upload the index to Huggingface..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adding data.direct.bf [1.9 GB]\n",
      "adding data.document.fsarrayfile [340.7 MB]\n",
      "adding data.inverted.bf [1.5 GB]\n",
      "adding data.lexicon.fsomapfile [330.0 MB]\n",
      "adding data.lexicon.fsomaphash [1017 B]\n",
      "adding data.lexicon.fsomapid [15.3 MB]\n",
      "adding data.meta-0.fsomapfile [1.3 GB]\n",
      "adding data.meta.idx [160.3 MB]\n",
      "adding data.meta.zdata [8.2 GB]\n",
      "adding data.properties [4.1 KB]\n",
      "adding pt_meta.json [79 B]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d807844944c94c4cb5b76e1472d062f8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4.json:   0%|          | 0.00/913 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8477f74a10114db0ab4c62be17d21385",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "artifact.tar.lz4:   0%|          | 0.00/12.9G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b7082bc99c9a439dbb6ed8ab9fc484a1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Upload 2 LFS files:   0%|          | 0/2 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Artifact uploaded to https://huggingface.co/datasets/pyterrier/ragwiki-terrier/tree/main/\n",
      "Consider editing the README.md to help explain this artifact to others.\n"
     ]
    }
   ],
   "source": [
    "index = pt.terrier.TerrierIndex(ref)\n",
    "index.to_hf('pyterrier/ragwiki-terrier')"
   ]
  }
 ],
 "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
}