{ "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 }