{ "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": [ ">" ] }, "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