colbert-ko-v1 is a Korean colbert model finetuned with PyLate. This model is trained exclusively on Korean dataset. It maps sentences & paragraphs to sequences of 128-dimensional dense vectors and can be used for semantic textual similarity using the MaxSim operator.
This model can be used with Sentence Transformers as a multi-vector (ColBERT-style late interaction) retriever via the MultiVectorEncoder:
pip install "sentence-transformers>=6.0.0"
python
1from sentence_transformers import MultiVectorEncoder
23model = MultiVectorEncoder("yjoonjang/colbert-ko-v1")45query ="붉은 행성으로 알려진 행성은 무엇인가요?"6documents =[7"금성은 크기와 근접성이 비슷하여 종종 지구의 쌍둥이라고 불린다.",8"화성은 붉은 겉모습 때문에 종종 붉은 행성이라고 불린다.",9"태양계에서 가장 큰 행성인 목성에는 뚜렷한 붉은 반점이 있다.",10"고리로 유명한 토성은 때때로 붉은 행성으로 오인된다.",11]1213query_embeddings = model.encode_query(query)14document_embeddings = model.encode_document(documents)15print(query_embeddings.shape, document_embeddings[0].shape)16# (32, 128) (19, 128)1718# MaxSim late-interaction scoring (higher is more relevant)19scores = model.similarity(query_embeddings, document_embeddings)20print(scores)21# tensor([[10.9423, 22.7836, 19.7410, 22.4239]])
PyLate for 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:
Usage with PLAID
First install the PyLate library:
pip install -U pylate
Retrieval
Use this model with PyLate to index and retrieve documents. The index uses FastPLAID for efficient similarity search.
Indexing documents
Load the ColBERT model and initialize the PLAID index, then encode and index your documents:
python
1from pylate import indexes, models, retrieve
23# Step 1: Load the ColBERT model4model = models.ColBERT(5 model_name_or_path="yjoonjang/colbert-ko-v1",6)78# Step 2: Initialize the PLAID index9index = indexes.PLAID(10 index_folder="pylate-index",11 index_name="index",12 override=True,# This overwrites the existing index if any13)1415# Step 3: Encode the documents16documents_ids =["1","2","3"]17documents =["document 1 text","document 2 text","document 3 text"]1819documents_embeddings = model.encode(20 documents,21 batch_size=32,22 is_query=False,# Ensure that it is set to False to indicate that these are documents, not queries23 show_progress_bar=True,24)2526# Step 4: Add document embeddings to the index by providing embeddings and corresponding ids27index.add_documents(28 documents_ids=documents_ids,29 documents_embeddings=documents_embeddings,30)
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.PLAID(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 False 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)
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}
CachedContrastive
bibtex
1@misc{gao2021scaling,
2 title={Scaling Deep Contrastive Learning Batch Size under Memory Limited Setup},
3 author={Luyu Gao and Yunyi Zhang and Jiawei Han and Jamie Callan},
4 year={2021},
5 eprint={2101.06983},
6 archivePrefix={arXiv},
7 primaryClass={cs.LG}
8}