Elffuss BraidHub

kikocis/lancedb public

85830ec79fd2 · 2.9 KiBRenderedRawPermalink
1# Cross Encoder Reranker
2
3This re-ranker uses Cross Encoder models from sentence-transformers to rerank the search results. You can use this re-ranker by passing `CrossEncoderReranker()` to the `rerank()` method.
4!!! note
5 Supported Query Types: Hybrid, Vector, FTS
6
7
8```python
9import numpy
10import lancedb
11from lancedb.embeddings import get_registry
12from lancedb.pydantic import LanceModel, Vector
13from lancedb.rerankers import CrossEncoderReranker
14
15embedder = get_registry().get("sentence-transformers").create()
16db = lancedb.connect("~/.lancedb")
17
18class Schema(LanceModel):
19 text: str = embedder.SourceField()
20 vector: Vector(embedder.ndims()) = embedder.VectorField()
21
22data = [
23 {"text": "hello world"},
24 {"text": "goodbye world"}
25 ]
26tbl = db.create_table("test", schema=Schema, mode="overwrite")
27tbl.add(data)
28reranker = CrossEncoderReranker()
29
30# Run vector search with a reranker
31result = tbl.search("hello").rerank(reranker=reranker).to_list()
32
33# Run FTS search with a reranker
34result = tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
35
36# Run hybrid search with a reranker
37tbl.create_fts_index("text", replace=True)
38result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
39
40```
41
42Accepted Arguments
43----------------
44| Argument | Type | Default | Description |
45| --- | --- | --- | --- |
46| `model_name` | `str` | `""cross-encoder/ms-marco-TinyBERT-L-6"` | The name of the reranker model to use.|
47| `column` | `str` | `"text"` | The name of the column to use as input to the cross encoder model. |
48| `device` | `str` | `None` | The device to use for the cross encoder model. If None, will use "cuda" if available, otherwise "cpu". |
49| `return_score` | str | `"relevance"` | Options are "relevance" or "all". The type of score to return. If "relevance", will return only the `_relevance_score. If "all" is supported, will return relevance score along with the vector and/or fts scores depending on query type |
50
51## Supported Scores for each query type
52You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
53
54### Hybrid Search
55|`return_score`| Status | Description |
56| --- | --- | --- |
57| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
58| `all` | ❌ Not Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
59
60### Vector Search
61|`return_score`| Status | Description |
62| --- | --- | --- |
63| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
64| `all` | ✅ Supported | Returns have vector(`_distance`) along with Hybrid Search score(`_relevance_score`) |
65
66### FTS Search
67|`return_score`| Status | Description |
68| --- | --- | --- |
69| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
70| `all` | ✅ Supported | Returns have FTS(`score`) along with Hybrid Search score(`_relevance_score`) |