| 1 | ## Building Custom Rerankers |
| 2 | You can build your own custom reranker by subclassing the `Reranker` class and implementing the `rerank_hybrid()` method. Optionally, you can also implement the `rerank_vector()` and `rerank_fts()` methods if you want to support reranking for vector and FTS search separately. |
| 3 | Here's an example of a custom reranker that combines the results of semantic and full-text search using a linear combination of the scores. |
| 4 | |
| 5 | The `Reranker` base interface comes with a `merge_results()` method that can be used to combine the results of semantic and full-text search. This is a vanilla merging algorithm that simply concatenates the results and removes the duplicates without taking the scores into consideration. It only keeps the first copy of the row encountered. This works well in cases that don't require the scores of semantic and full-text search to combine the results. If you want to use the scores or want to support `return_score="all"`, you'll need to implement your own merging algorithm. |
| 6 | |
| 7 | ```python |
| 8 | |
| 9 | from lancedb.rerankers import Reranker |
| 10 | import pyarrow as pa |
| 11 | |
| 12 | class MyReranker(Reranker): |
| 13 | def __init__(self, param1, param2, ..., return_score="relevance"): |
| 14 | super().__init__(return_score) |
| 15 | self.param1 = param1 |
| 16 | self.param2 = param2 |
| 17 | |
| 18 | def rerank_hybrid(self, query: str, vector_results: pa.Table, fts_results: pa.Table): |
| 19 | # Use the built-in merging function |
| 20 | combined_result = self.merge_results(vector_results, fts_results) |
| 21 | |
| 22 | # Do something with the combined results |
| 23 | # ... |
| 24 | |
| 25 | # Return the combined results |
| 26 | return combined_result |
| 27 | |
| 28 | def rerank_vector(self, query: str, vector_results: pa.Table): |
| 29 | # Do something with the vector results |
| 30 | # ... |
| 31 | |
| 32 | # Return the vector results |
| 33 | return vector_results |
| 34 | |
| 35 | def rerank_fts(self, query: str, fts_results: pa.Table): |
| 36 | # Do something with the FTS results |
| 37 | # ... |
| 38 | |
| 39 | # Return the FTS results |
| 40 | return fts_results |
| 41 | |
| 42 | ``` |
| 43 | |
| 44 | ### Example of a Custom Reranker |
| 45 | For the sake of simplicity let's build custom reranker that just enchances the Cohere Reranker by accepting a filter query, and accept other CohereReranker params as kwags. |
| 46 | |
| 47 | ```python |
| 48 | |
| 49 | from typing import List, Union |
| 50 | import pandas as pd |
| 51 | from lancedb.rerankers import CohereReranker |
| 52 | |
| 53 | class ModifiedCohereReranker(CohereReranker): |
| 54 | def __init__(self, filters: Union[str, List[str]], **kwargs): |
| 55 | super().__init__(**kwargs) |
| 56 | filters = filters if isinstance(filters, list) else [filters] |
| 57 | self.filters = filters |
| 58 | |
| 59 | def rerank_hybrid(self, query: str, vector_results: pa.Table, fts_results: pa.Table)-> pa.Table: |
| 60 | combined_result = super().rerank_hybrid(query, vector_results, fts_results) |
| 61 | df = combined_result.to_pandas() |
| 62 | for filter in self.filters: |
| 63 | df = df.query("not text.str.contains(@filter)") |
| 64 | |
| 65 | return pa.Table.from_pandas(df) |
| 66 | |
| 67 | def rerank_vector(self, query: str, vector_results: pa.Table)-> pa.Table: |
| 68 | vector_results = super().rerank_vector(query, vector_results) |
| 69 | df = vector_results.to_pandas() |
| 70 | for filter in self.filters: |
| 71 | df = df.query("not text.str.contains(@filter)") |
| 72 | |
| 73 | return pa.Table.from_pandas(df) |
| 74 | |
| 75 | def rerank_fts(self, query: str, fts_results: pa.Table)-> pa.Table: |
| 76 | fts_results = super().rerank_fts(query, fts_results) |
| 77 | df = fts_results.to_pandas() |
| 78 | for filter in self.filters: |
| 79 | df = df.query("not text.str.contains(@filter)") |
| 80 | |
| 81 | return pa.Table.from_pandas(df) |
| 82 | |
| 83 | ``` |
| 84 | |
| 85 | !!! tip |
| 86 | The `vector_results` and `fts_results` are pyarrow tables. Lean more about pyarrow tables [here](https://arrow.apache.org/docs/python). It can be convered to other data types like pandas dataframe, pydict, pylist etc. |
| 87 | |
| 88 | For example, You can convert them to pandas dataframes using `to_pandas()` method and perform any operations you want. After you are done, you can convert the dataframe back to pyarrow table using `pa.Table.from_pandas()` method and return it. |