Add Sentence Transformers usage
#5
by tomaarsen HF Staff - opened
README.md
CHANGED
|
@@ -7,6 +7,8 @@ metrics:
|
|
| 7 |
- recall
|
| 8 |
tags:
|
| 9 |
- colbert
|
|
|
|
|
|
|
| 10 |
- passage-retrieval
|
| 11 |
library_name: colbert-ai
|
| 12 |
base_model: facebook/xmod-base
|
|
@@ -458,6 +460,40 @@ which allows it to learn from monolingual fine-tuning in a high-resource languag
|
|
| 458 |
|
| 459 |
## Usage
|
| 460 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
Start by installing the [colbert-ai](https://github.com/stanford-futuredata/ColBERT) and some extra requirements:
|
| 462 |
|
| 463 |
```bash
|
|
|
|
| 7 |
- recall
|
| 8 |
tags:
|
| 9 |
- colbert
|
| 10 |
+
- multi-vector
|
| 11 |
+
- sentence-transformers
|
| 12 |
- passage-retrieval
|
| 13 |
library_name: colbert-ai
|
| 14 |
base_model: facebook/xmod-base
|
|
|
|
| 460 |
|
| 461 |
## Usage
|
| 462 |
|
| 463 |
+
### Sentence Transformers
|
| 464 |
+
|
| 465 |
+
This model can be used with [Sentence Transformers](https://www.sbert.net/) as a multi-vector (ColBERT-style late interaction) retriever via the `MultiVectorEncoder`:
|
| 466 |
+
|
| 467 |
+
```bash
|
| 468 |
+
pip install "sentence-transformers>=6.0.0"
|
| 469 |
+
```
|
| 470 |
+
|
| 471 |
+
```python
|
| 472 |
+
from sentence_transformers import MultiVectorEncoder
|
| 473 |
+
|
| 474 |
+
model = MultiVectorEncoder("antoinelouis/colbert-xm")
|
| 475 |
+
|
| 476 |
+
query = "Which planet is known as the Red Planet?"
|
| 477 |
+
documents = [
|
| 478 |
+
"Venus wird oft als Zwilling der Erde bezeichnet, wegen ihrer ähnlichen Größe.",
|
| 479 |
+
"Mars, connue pour son apparence rougeâtre, est souvent appelée la planète rouge.",
|
| 480 |
+
"Júpiter es el planeta más grande del sistema solar.",
|
| 481 |
+
"Saturno è famoso per i suoi bellissimi anelli.",
|
| 482 |
+
]
|
| 483 |
+
|
| 484 |
+
query_embeddings = model.encode_query(query)
|
| 485 |
+
document_embeddings = model.encode_document(documents)
|
| 486 |
+
print(query_embeddings.shape, document_embeddings[0].shape)
|
| 487 |
+
# (32, 128) (21, 128)
|
| 488 |
+
|
| 489 |
+
# MaxSim late-interaction scoring (higher is more relevant)
|
| 490 |
+
scores = model.similarity(query_embeddings, document_embeddings)
|
| 491 |
+
print(scores)
|
| 492 |
+
# tensor([[11.1658, 18.7137, 12.6985, 8.1900]])
|
| 493 |
+
```
|
| 494 |
+
|
| 495 |
+
### Using Stanford ColBERT
|
| 496 |
+
|
| 497 |
Start by installing the [colbert-ai](https://github.com/stanford-futuredata/ColBERT) and some extra requirements:
|
| 498 |
|
| 499 |
```bash
|