Describe CrossEncoder integration with Sentence Transformers

#8
by tomaarsen HF staff - opened
Files changed (1) hide show
  1. README.md +60 -0
README.md CHANGED
@@ -147,6 +147,66 @@ Inside the `result` object, you will find the reranked documents along with thei
147
  The `rerank()` function will automatically chunk the input documents into smaller pieces if they exceed the model's maximum input length. This allows you to rerank long documents without running into memory issues.
148
  Specifically, the `rerank()` function will split the documents into chunks of size `max_length` and rerank each chunk separately. The scores from all the chunks are then combined to produce the final reranking results. You can control the query length and document length in each chunk by setting the `max_query_length` and `max_length` parameters. The `rerank()` function also supports the `overlap` parameter (default is `80`) which determines how much overlap there is between adjacent chunks. This can be useful when reranking long documents to ensure that the model has enough context to make accurate predictions.
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  # Evaluation
152
 
 
147
  The `rerank()` function will automatically chunk the input documents into smaller pieces if they exceed the model's maximum input length. This allows you to rerank long documents without running into memory issues.
148
  Specifically, the `rerank()` function will split the documents into chunks of size `max_length` and rerank each chunk separately. The scores from all the chunks are then combined to produce the final reranking results. You can control the query length and document length in each chunk by setting the `max_query_length` and `max_length` parameters. The `rerank()` function also supports the `overlap` parameter (default is `80`) which determines how much overlap there is between adjacent chunks. This can be useful when reranking long documents to ensure that the model has enough context to make accurate predictions.
149
 
150
+ 3. Alternatively, `jina-reranker-v2-base-multilingual` has been integrated with `CrossEncoder` from the `sentence-transformers` library.
151
+
152
+ Before you start, install the `sentence-transformers` libraries:
153
+
154
+ ```bash
155
+ pip install sentence-transformers
156
+ ```
157
+
158
+ The [`CrossEncoder`](https://sbert.net/docs/package_reference/cross_encoder/cross_encoder.html) class supports a [`predict`](https://sbert.net/docs/package_reference/cross_encoder/cross_encoder.html#sentence_transformers.cross_encoder.CrossEncoder.predict) method to get query-document relevance scores, and a [`rank`](https://sbert.net/docs/package_reference/cross_encoder/cross_encoder.html#sentence_transformers.cross_encoder.CrossEncoder.rank) method to rank all documents given your query.
159
+
160
+ ```python
161
+ from sentence_transformers import CrossEncoder
162
+
163
+ model = CrossEncoder(
164
+ "jinaai/jina-reranker-v2-base-multilingual",
165
+ automodel_args={"torch_dtype": "auto"},
166
+ trust_remote_code=True,
167
+ )
168
+
169
+ # Example query and documents
170
+ query = "Organic skincare products for sensitive skin"
171
+ documents = [
172
+ "Organic skincare for sensitive skin with aloe vera and chamomile.",
173
+ "New makeup trends focus on bold colors and innovative techniques",
174
+ "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille",
175
+ "Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken",
176
+ "Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla",
177
+ "Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras",
178
+ "针对敏感肌专门设计的天然有机护肤产品",
179
+ "新的化妆趋势注重鲜艳的颜色和创新的技巧",
180
+ "敏感肌のために特別に設計された天然有機スキンケア製品",
181
+ "新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています",
182
+ ]
183
+
184
+ # construct sentence pairs
185
+ sentence_pairs = [[query, doc] for doc in documents]
186
+
187
+ scores = model.predict(sentence_pairs, convert_to_tensor=True).tolist()
188
+ """
189
+ [0.828125, 0.0927734375, 0.6328125, 0.08251953125, 0.76171875, 0.099609375, 0.92578125, 0.058349609375, 0.84375, 0.111328125]
190
+ """
191
+
192
+ rankings = model.rank(query, documents, return_documents=True, convert_to_tensor=True)
193
+ print(f"Query: {query}")
194
+ for ranking in rankings:
195
+ print(f"ID: {ranking['corpus_id']}, Score: {ranking['score']:.4f}, Text: {ranking['text']}")
196
+ """
197
+ Query: Organic skincare products for sensitive skin
198
+ ID: 6, Score: 0.9258, Text: 针对敏感肌专门设计的天然有机护肤产品
199
+ ID: 8, Score: 0.8438, Text: 敏感肌のために特別に設計された天然有機スキンケア製品
200
+ ID: 0, Score: 0.8281, Text: Organic skincare for sensitive skin with aloe vera and chamomile.
201
+ ID: 4, Score: 0.7617, Text: Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla
202
+ ID: 2, Score: 0.6328, Text: Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille
203
+ ID: 9, Score: 0.1113, Text: 新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています
204
+ ID: 5, Score: 0.0996, Text: Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras
205
+ ID: 1, Score: 0.0928, Text: New makeup trends focus on bold colors and innovative techniques
206
+ ID: 3, Score: 0.0825, Text: Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken
207
+ ID: 7, Score: 0.0583, Text: 新的化妆趋势注重鲜艳的颜色和创新的技巧
208
+ """
209
+ ```
210
 
211
  # Evaluation
212