Add Sentence Transformers usage
#18
by tomaarsen HF Staff - opened
README.md
CHANGED
|
@@ -4,6 +4,8 @@ language:
|
|
| 4 |
- en
|
| 5 |
tags:
|
| 6 |
- ColBERT
|
|
|
|
|
|
|
| 7 |
- RAGatouille
|
| 8 |
- passage-retrieval
|
| 9 |
---
|
|
@@ -18,24 +20,48 @@ For more information about this model or how it was trained, head over to the [a
|
|
| 18 |
|
| 19 |
## Usage
|
| 20 |
|
| 21 |
-
### Installation
|
| 22 |
-
|
| 23 |
This model was designed with the upcoming RAGatouille overhaul in mind. However, it's compatible with all recent ColBERT implementations!
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
``
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
```
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
```
|
| 36 |
|
| 37 |
### Rerankers
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
```python
|
| 40 |
from rerankers import Reranker
|
| 41 |
|
|
@@ -47,6 +73,10 @@ ranker.rank(query=query, docs=docs)
|
|
| 47 |
|
| 48 |
### RAGatouille
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
```python
|
| 51 |
from ragatouille import RAGPretrainedModel
|
| 52 |
|
|
@@ -62,6 +92,10 @@ results = RAG.search(query)
|
|
| 62 |
|
| 63 |
### Stanford ColBERT
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
#### Indexing
|
| 66 |
|
| 67 |
```python
|
|
|
|
| 4 |
- en
|
| 5 |
tags:
|
| 6 |
- ColBERT
|
| 7 |
+
- multi-vector
|
| 8 |
+
- sentence-transformers
|
| 9 |
- RAGatouille
|
| 10 |
- passage-retrieval
|
| 11 |
---
|
|
|
|
| 20 |
|
| 21 |
## Usage
|
| 22 |
|
|
|
|
|
|
|
| 23 |
This model was designed with the upcoming RAGatouille overhaul in mind. However, it's compatible with all recent ColBERT implementations!
|
| 24 |
|
| 25 |
+
### Sentence Transformers
|
| 26 |
|
| 27 |
+
This model can be used with [Sentence Transformers](https://www.sbert.net/) as a multi-vector (ColBERT-style late interaction) retriever via the `MultiVectorEncoder`:
|
| 28 |
+
|
| 29 |
+
```bash
|
| 30 |
+
pip install "sentence-transformers>=6.0.0"
|
| 31 |
```
|
| 32 |
|
| 33 |
+
```python
|
| 34 |
+
from sentence_transformers import MultiVectorEncoder
|
| 35 |
+
|
| 36 |
+
model = MultiVectorEncoder("answerdotai/answerai-colbert-small-v1")
|
| 37 |
+
|
| 38 |
+
query = "Which planet is known as the Red Planet?"
|
| 39 |
+
documents = [
|
| 40 |
+
"Venus is often called Earth's twin because of its similar size and proximity.",
|
| 41 |
+
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
|
| 42 |
+
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
|
| 43 |
+
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet.",
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
query_embeddings = model.encode_query(query)
|
| 47 |
+
document_embeddings = model.encode_document(documents)
|
| 48 |
+
print(query_embeddings.shape, document_embeddings[0].shape)
|
| 49 |
+
# (32, 96) (17, 96)
|
| 50 |
+
|
| 51 |
+
# MaxSim late-interaction scoring (higher is more relevant)
|
| 52 |
+
scores = model.similarity(query_embeddings, document_embeddings)
|
| 53 |
+
print(scores)
|
| 54 |
+
# tensor([[30.5692, 31.4895, 31.3029, 31.3072]])
|
| 55 |
```
|
| 56 |
|
| 57 |
### Rerankers
|
| 58 |
|
| 59 |
+
If you're interested in using this model as a re-ranker (it vastly outperforms cross-encoders its size!), you can do so via the [rerankers](https://github.com/AnswerDotAI/rerankers) library:
|
| 60 |
+
|
| 61 |
+
```bash
|
| 62 |
+
pip install --upgrade rerankers[transformers]
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
```python
|
| 66 |
from rerankers import Reranker
|
| 67 |
|
|
|
|
| 73 |
|
| 74 |
### RAGatouille
|
| 75 |
|
| 76 |
+
```bash
|
| 77 |
+
pip install --upgrade ragatouille
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
```python
|
| 81 |
from ragatouille import RAGPretrainedModel
|
| 82 |
|
|
|
|
| 92 |
|
| 93 |
### Stanford ColBERT
|
| 94 |
|
| 95 |
+
```bash
|
| 96 |
+
pip install --upgrade colbert-ai
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
#### Indexing
|
| 100 |
|
| 101 |
```python
|