EdoardoMosca commited on
Commit
7953fb8
ยท
verified ยท
1 Parent(s): b77982d

readme: enrich How-to-run with the original LFM2-ColBERT-350M prose + step-comments

Browse files
Files changed (1) hide show
  1. README.md +38 -18
README.md CHANGED
@@ -90,18 +90,22 @@ We recommend LFM2.5-Embedding-350M and LFM2.5-ColBERT-350M for short-context ret
90
 
91
  ## ๐Ÿƒ How to run
92
 
93
- Install PyLate:
94
 
95
  ```bash
96
  pip install -U pylate
97
  ```
98
 
 
 
99
  ### Retrieval
100
 
101
  Use this model with PyLate to index and retrieve documents. The index uses [FastPLAID](https://github.com/lightonai/fast-plaid) for efficient similarity search.
102
 
103
  #### Indexing documents
104
 
 
 
105
  ```python
106
  from pylate import indexes, models, retrieve
107
 
@@ -116,7 +120,7 @@ model.tokenizer.pad_token = model.tokenizer.eos_token
116
  index = indexes.PLAID(
117
  index_folder="pylate-index",
118
  index_name="index",
119
- override=True, # Overwrites any existing index with the same name
120
  )
121
 
122
  # Step 3: Encode the documents
@@ -126,68 +130,86 @@ documents = ["document 1 text", "document 2 text", "document 3 text"]
126
  documents_embeddings = model.encode(
127
  documents,
128
  batch_size=32,
129
- is_query=False,
130
  show_progress_bar=True,
131
  )
132
 
133
- # Step 4: Add document embeddings to the index
134
  index.add_documents(
135
  documents_ids=documents_ids,
136
  documents_embeddings=documents_embeddings,
137
  )
138
  ```
139
 
140
- You don't have to rebuild the index every time. Reload an existing index by instantiating it with the same folder/name and `override=False`:
141
 
142
  ```python
 
143
  index = indexes.PLAID(
144
  index_folder="pylate-index",
145
  index_name="index",
146
  )
147
  ```
148
 
149
- #### Retrieving top-k documents
 
 
150
 
151
  ```python
 
152
  retriever = retrieve.ColBERT(index=index)
153
 
 
154
  queries_embeddings = model.encode(
155
  ["query for document 3", "query for document 1"],
156
  batch_size=32,
157
- is_query=True,
158
  show_progress_bar=True,
159
  )
160
 
 
161
  scores = retriever.retrieve(
162
  queries_embeddings=queries_embeddings,
163
- k=10,
164
  )
165
  ```
166
 
167
  ### Reranking
168
 
169
- For reranking on top of a first-stage pipeline, no index is needed:
170
 
171
  ```python
172
  from pylate import rank, models
173
 
174
- model = models.ColBERT(
175
- model_name_or_path="LiquidAI/LFM2.5-ColBERT-350M",
176
- trust_remote_code=True,
177
- )
178
 
179
- queries = ["query A", "query B"]
180
  documents = [
181
  ["document A", "document B"],
182
  ["document 1", "document C", "document B"],
183
  ]
 
184
  documents_ids = [
185
  [1, 2],
186
  [1, 3, 2],
187
  ]
188
 
189
- queries_embeddings = model.encode(queries, is_query=True)
190
- documents_embeddings = model.encode(documents, is_query=False)
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
  reranked_documents = rank.rerank(
193
  documents_ids=documents_ids,
@@ -196,8 +218,6 @@ reranked_documents = rank.rerank(
196
  )
197
  ```
198
 
199
- > **Note on `trust_remote_code`.** Loading requires `trust_remote_code=True` so the repo's `modeling_lfm2_bidirectional.py` can replace the causal attention mask and short-conv padding with their non-causal equivalents. Without it the model will silently use causal attention and produce poor retrieval embeddings.
200
-
201
  ## ๐Ÿ“ˆ Performance
202
 
203
  Evaluated on two open multilingual retrieval benchmarks, with queries and documents in the same language. Bold = best per column.
 
90
 
91
  ## ๐Ÿƒ How to run
92
 
93
+ First, install the PyLate and transformers libraries:
94
 
95
  ```bash
96
  pip install -U pylate
97
  ```
98
 
99
+ > **Note on `trust_remote_code`.** Loading requires `trust_remote_code=True` so the repo's `modeling_lfm2_bidirectional.py` can replace the causal attention mask and short-conv padding with their non-causal equivalents. Without it the model will silently use causal attention and produce poor retrieval embeddings.
100
+
101
  ### Retrieval
102
 
103
  Use this model with PyLate to index and retrieve documents. The index uses [FastPLAID](https://github.com/lightonai/fast-plaid) for efficient similarity search.
104
 
105
  #### Indexing documents
106
 
107
+ Load LFM2.5-ColBERT-350M and initialize the PLAID index, then encode and index your documents:
108
+
109
  ```python
110
  from pylate import indexes, models, retrieve
111
 
 
120
  index = indexes.PLAID(
121
  index_folder="pylate-index",
122
  index_name="index",
123
+ override=True, # This overwrites the existing index if any
124
  )
125
 
126
  # Step 3: Encode the documents
 
130
  documents_embeddings = model.encode(
131
  documents,
132
  batch_size=32,
133
+ is_query=False, # Ensure that it is set to False to indicate that these are documents, not queries
134
  show_progress_bar=True,
135
  )
136
 
137
+ # Step 4: Add document embeddings to the index by providing embeddings and corresponding ids
138
  index.add_documents(
139
  documents_ids=documents_ids,
140
  documents_embeddings=documents_embeddings,
141
  )
142
  ```
143
 
144
+ 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:
145
 
146
  ```python
147
+ # To load an index, simply instantiate it with the correct folder/name and without overriding it
148
  index = indexes.PLAID(
149
  index_folder="pylate-index",
150
  index_name="index",
151
  )
152
  ```
153
 
154
+ #### Retrieving top-k documents for queries
155
+
156
+ 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:
157
 
158
  ```python
159
+ # Step 1: Initialize the ColBERT retriever
160
  retriever = retrieve.ColBERT(index=index)
161
 
162
+ # Step 2: Encode the queries
163
  queries_embeddings = model.encode(
164
  ["query for document 3", "query for document 1"],
165
  batch_size=32,
166
+ is_query=True, # Ensure that it is set to True to indicate that these are queries
167
  show_progress_bar=True,
168
  )
169
 
170
+ # Step 3: Retrieve top-k documents
171
  scores = retriever.retrieve(
172
  queries_embeddings=queries_embeddings,
173
+ k=10, # Retrieve the top 10 matches for each query
174
  )
175
  ```
176
 
177
  ### Reranking
178
 
179
+ If you only want to use LFM2.5-ColBERT-350M to perform reranking on top of your first-stage retrieval pipeline without building an index, you can simply use the `rank` function and pass the queries and documents to rerank:
180
 
181
  ```python
182
  from pylate import rank, models
183
 
184
+ queries = [
185
+ "query A",
186
+ "query B",
187
+ ]
188
 
 
189
  documents = [
190
  ["document A", "document B"],
191
  ["document 1", "document C", "document B"],
192
  ]
193
+
194
  documents_ids = [
195
  [1, 2],
196
  [1, 3, 2],
197
  ]
198
 
199
+ model = models.ColBERT(
200
+ model_name_or_path="LiquidAI/LFM2.5-ColBERT-350M",
201
+ trust_remote_code=True,
202
+ )
203
+
204
+ queries_embeddings = model.encode(
205
+ queries,
206
+ is_query=True,
207
+ )
208
+
209
+ documents_embeddings = model.encode(
210
+ documents,
211
+ is_query=False,
212
+ )
213
 
214
  reranked_documents = rank.rerank(
215
  documents_ids=documents_ids,
 
218
  )
219
  ```
220
 
 
 
221
  ## ๐Ÿ“ˆ Performance
222
 
223
  Evaluated on two open multilingual retrieval benchmarks, with queries and documents in the same language. Bold = best per column.