Elffuss BraidHub

kikocis/lancedb public

main · 3.8 KiBRenderedRawPermalink
1Continuing from the previous section, we can now rerank the results using more complex rerankers.
2
3Try it yourself - <a href="https://colab.research.google.com/github/lancedb/lancedb/blob/main/docs/src/notebooks/lancedb_reranking.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a><br/>
4
5## Reranking search results
6You can rerank any search results using a reranker. The syntax for reranking is as follows:
7
8```python
9from lancedb.rerankers import LinearCombinationReranker
10
11reranker = LinearCombinationReranker()
12table.search(quries[0], query_type="hybrid").rerank(reranker=reranker).limit(5).to_pandas()
13```
14Based on the `query_type`, the `rerank()` function can accept other arguments as well. For example, hybrid search accepts a `normalize` param to determine the score normalization method.
15
16!!! note "Note"
17 LanceDB provides a `Reranker` base class that can be extended to implement custom rerankers. Each reranker must implement the `rerank_hybrid` method. `rerank_vector` and `rerank_fts` methods are optional. For example, the `LinearCombinationReranker` only implements the `rerank_hybrid` method and so it can only be used for reranking hybrid search results.
18
19## Choosing a Reranker
20There are many rerankers available in LanceDB like `CrossEncoderReranker`, `CohereReranker`, and `ColBERT`. The choice of reranker depends on the dataset and the application. You can even implement you own custom reranker by extending the `Reranker` class. For more details about each available reranker and performance comparison, refer to the [rerankers](https://lancedb.github.io/lancedb/reranking/) documentation.
21
22In this example, we'll use the `CohereReranker` to rerank the search results. It requires `cohere` to be installed and `COHERE_API_KEY` to be set in the environment. To get your API key, sign up on [Cohere](https://cohere.ai/).
23
24```python
25from lancedb.rerankers import CohereReranker
26
27# use Cohere reranker v3
28reranker = CohereReranker(model_name="rerank-english-v3.0") # default model is "rerank-english-v2.0"
29```
30
31### Reranking search results
32Now we can rerank all query type results using the `CohereReranker`:
33
34```python
35
36# rerank hybrid search results
37table.search(quries[0], query_type="hybrid").rerank(reranker=reranker).limit(5).to_pandas()
38
39# rerank vector search results
40table.search(quries[0], query_type="vector").rerank(reranker=reranker).limit(5).to_pandas()
41
42# rerank fts search results
43table.search(quries[0], query_type="fts").rerank(reranker=reranker).limit(5).to_pandas()
44```
45
46Each reranker can accept additional arguments. For example, `CohereReranker` accepts `top_k` and `batch_size` params to control the number of documents to rerank and the batch size for reranking respectively. Similarly, a custom reranker can accept any number of arguments based on the implementation. For example, a reranker can accept a `filter` that implements some custom logic to filter out documents before reranking.
47
48## Results
49
50Let us take a look at the same datasets from the previous sections, using the same embedding table but with Cohere reranker applied to all query types.
51
52!!! note "Note"
53 When reranking fts or vector search results, the search results are over-fetched by a factor of 2 and then reranked. From the reranked set, `top_k` (5 in this case) results are taken. This is done because reranking will have no effect on the hit-rate if we only fetch the `top_k` results.
54
55### Synthetic LLama2 paper dataset
56
57| Query Type | Hit-rate@5 |
58| --- | --- |
59| Vector | 0.640 |
60| FTS | 0.595 |
61| Reranked vector | 0.677 |
62| Reranked fts | 0.672 |
63| Hybrid | 0.759 |
64
65### SQuAD Dataset
66
67
68### Uber10K sec filing Dataset
69
70| Query Type | Hit-rate@5 |
71| --- | --- |
72| Vector | 0.608 |
73| FTS | 0.824 |
74| Reranked vector | 0.671 |
75| Reranked fts | 0.843 |
76| Hybrid | 0.849 |
77
78
79
80