This is a PyLate model trained. It maps sentences & paragraphs to sequences of 128-dimensional dense vectors and can be used for semantic textual similarity using the MaxSim operator.
nDCG and Recall scores of this model(out-of-domain predictions) and other multilingual late interaction retrieval models on Tr-NanoBEIR.
drawing
Usage
First install required libraries (Flash Attention 2 supporting GPU is a must for consistency otherwise you need to mask query expansion tokens in the output layer manually):
Then normalize your text ---> lambda x: x.replace("İ", "i").replace("I", "ı").lower()
Retrieval
PyLate provides a streamlined interface to index and retrieve documents using ColBERT models. The index leverages the Voyager HNSW index to efficiently handle document embeddings and enable fast retrieval.
Indexing documents
First, load the ColBERT model and initialize the Voyager index, then encode and index your documents:
python
1from pylate import indexes, models, retrieve
23# Step 1: Load the ColBERT model4document_length =8192#[1,8192] for truncating documents5model = models.ColBERT(6 model_name_or_path="99eren99/TrColBERT-Long",document_length=document_length
7)8try:9 model.tokenizer.model_input_names.remove("token_type_ids")10except:11pass12model.eval()13model.to("cuda")1415# Step 2: Initialize the Voyager index16index = indexes.Voyager(17 index_folder="pylate-index",18 index_name="index",19 override=True,# This overwrites the existing index if any20)2122# Step 3: Encode the documents23documents_ids =["1","2","3"]24documents =["document 1 text","document 2 text","document 3 text"]2526documents_embeddings = model.encode(27 documents,28 batch_size=32,29 is_query=False,# Ensure that it is set to False to indicate that these are documents, not queries30 show_progress_bar=True,31)3233# Step 4: Add document embeddings to the index by providing embeddings and corresponding ids34index.add_documents(35 documents_ids=documents_ids,36 documents_embeddings=documents_embeddings,37)
Note that you do not have to recreate the index and encode the documents every time. Once you have created an index and added the documents, you can re-use the index later by loading it:
python
1# To load an index, simply instantiate it with the correct folder/name and without overriding it2index = indexes.Voyager(3 index_folder="pylate-index",4 index_name="index",5)
Retrieving top-k documents for queries
Once the documents are indexed, you can retrieve the top-k most relevant documents for a given set of queries.
To do so, initialize the ColBERT retriever with the index you want to search in, encode the queries and then retrieve the top-k documents to get the top matches ids and relevance scores:
python
1# Step 1: Initialize the ColBERT retriever2retriever = retrieve.ColBERT(index=index)34# Step 2: Encode the queries5queries_embeddings = model.encode(6["query for document 3","query for document 1"],7 batch_size=32,8 is_query=True,# Ensure that it is set to True to indicate that these are queries9 show_progress_bar=True,10)1112# Step 3: Retrieve top-k documents13scores = retriever.retrieve(14 queries_embeddings=queries_embeddings,15 k=10,# Retrieve the top 10 matches for each query16)
Reranking
If you only want to use the ColBERT model to perform reranking on top of your first-stage retrieval pipeline without building an index, you can simply use rank function and pass the queries and documents to rerank:
1@inproceedings{reimers-2019-sentence-bert,
2 title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
3 author = "Reimers, Nils and Gurevych, Iryna",
4 booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
5 month = "11",
6 year = "2019",
7 publisher = "Association for Computational Linguistics",
8 url = "https://arxiv.org/abs/1908.10084"
9}
PyLate
bibtex
1@misc{PyLate,
2title={PyLate: Flexible Training and Retrieval for Late Interaction Models},
3author={Chaffin, Antoine and Sourty, Raphaël},
4url={https://github.com/lightonai/pylate},
5year={2024}
6}