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