Changesets · Branches · Tags · Compare
| Changeset | 4742b7cc5764778cb4adc9bdff5eb97ea69224cf8857494a181bd72cbb50d8cd |
|---|---|
| Author | Ayush Chaurasia <ayush.chaurarsia@gmail.com> · 2024-09-23 12:12 +05:30 |
| Committer | GitHub <noreply@github.com> · 2024-09-23 12:12 +05:30 |
| Parents | 4a63e77b7e97 |
7 files changed
modified docs/src/reranking/linear_combination.md +4 −1
| @@ -1,6 +1,9 @@ | ||
| 1 | 1 | # Linear Combination Reranker |
| 2 | 2 | |
| 3 | -This is the default re-ranker used by LanceDB hybrid search. It combines the results of semantic and full-text search using a linear combination of the scores. The weights for the linear combination can be specified. It defaults to 0.7, i.e, 70% weight for semantic search and 30% weight for full-text search. | |
| 3 | +!!! note | |
| 4 | + This is depricated. It is recommended to use the `RRFReranker` instead, if you want to use a score based reranker. | |
| 5 | + | |
| 6 | +It combines the results of semantic and full-text search using a linear combination of the scores. The weights for the linear combination can be specified. It defaults to 0.7, i.e, 70% weight for semantic search and 30% weight for full-text search. | |
| 4 | 7 | |
| 5 | 8 | !!! note |
| 6 | 9 | Supported Query Types: Hybrid |
modified docs/src/reranking/rrf.md +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reciprocal Rank Fusion Reranker |
| 2 | 2 | |
| 3 | -Reciprocal Rank Fusion (RRF) is an algorithm that evaluates the search scores by leveraging the positions/rank of the documents. The implementation follows this [paper](https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf). | |
| 3 | +This is the default re-ranker used by LanceDB hybrid search. Reciprocal Rank Fusion (RRF) is an algorithm that evaluates the search scores by leveraging the positions/rank of the documents. The implementation follows this [paper](https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf). | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | !!! note |
modified python/python/lancedb/query.py +9 −9
| @@ -36,6 +36,7 @@ | ||
| 36 | 36 | from .arrow import AsyncRecordBatchReader |
| 37 | 37 | from .rerankers.base import Reranker |
| 38 | 38 | from .rerankers.rrf import RRFReranker |
| 39 | +from .rerankers.util import check_reranker_result | |
| 39 | 40 | from .util import safe_import_pandas |
| 40 | 41 | |
| 41 | 42 | if TYPE_CHECKING: |
| @@ -679,6 +680,7 @@ | ||
| 679 | 680 | if self._reranker is not None: |
| 680 | 681 | rs_table = result_set.read_all() |
| 681 | 682 | result_set = self._reranker.rerank_vector(self._str_query, rs_table) |
| 683 | + check_reranker_result(result_set) | |
| 682 | 684 | # convert result_set back to RecordBatchReader |
| 683 | 685 | result_set = pa.RecordBatchReader.from_batches( |
| 684 | 686 | result_set.schema, result_set.to_batches() |
| @@ -811,6 +813,7 @@ | ||
| 811 | 813 | results = results.read_all() |
| 812 | 814 | if self._reranker is not None: |
| 813 | 815 | results = self._reranker.rerank_fts(self._query, results) |
| 816 | + check_reranker_result(results) | |
| 814 | 817 | return results |
| 815 | 818 | |
| 816 | 819 | def tantivy_to_arrow(self) -> pa.Table: |
| @@ -953,8 +956,8 @@ | ||
| 953 | 956 | def __init__( |
| 954 | 957 | self, |
| 955 | 958 | table: "Table", |
| 956 | - query: str = None, | |
| 957 | - vector_column: str = None, | |
| 959 | + query: Optional[str] = None, | |
| 960 | + vector_column: Optional[str] = None, | |
| 958 | 961 | fts_columns: Union[str, List[str]] = [], |
| 959 | 962 | ): |
| 960 | 963 | super().__init__(table) |
| @@ -1060,10 +1063,7 @@ | ||
| 1060 | 1063 | self._fts_query._query, vector_results, fts_results |
| 1061 | 1064 | ) |
| 1062 | 1065 | |
| 1063 | - if not isinstance(results, pa.Table): # Enforce type | |
| 1064 | - raise TypeError( | |
| 1065 | - f"rerank_hybrid must return a pyarrow.Table, got {type(results)}" | |
| 1066 | - ) | |
| 1066 | + check_reranker_result(results) | |
| 1067 | 1067 | |
| 1068 | 1068 | # apply limit after reranking |
| 1069 | 1069 | results = results.slice(length=self._limit) |
| @@ -1112,8 +1112,8 @@ | ||
| 1112 | 1112 | |
| 1113 | 1113 | def rerank( |
| 1114 | 1114 | self, |
| 1115 | - normalize="score", | |
| 1116 | 1115 | reranker: Reranker = RRFReranker(), |
| 1116 | + normalize: str = "score", | |
| 1117 | 1117 | ) -> LanceHybridQueryBuilder: |
| 1118 | 1118 | """ |
| 1119 | 1119 | Rerank the hybrid search results using the specified reranker. The reranker |
| @@ -1121,12 +1121,12 @@ | ||
| 1121 | 1121 | |
| 1122 | 1122 | Parameters |
| 1123 | 1123 | ---------- |
| 1124 | + reranker: Reranker, default RRFReranker() | |
| 1125 | + The reranker to use. Must be an instance of Reranker class. | |
| 1124 | 1126 | normalize: str, default "score" |
| 1125 | 1127 | The method to normalize the scores. Can be "rank" or "score". If "rank", |
| 1126 | 1128 | the scores are converted to ranks and then normalized. If "score", the |
| 1127 | 1129 | scores are normalized directly. |
| 1128 | - reranker: Reranker, default RRFReranker() | |
| 1129 | - The reranker to use. Must be an instance of Reranker class. | |
| 1130 | 1130 | Returns |
| 1131 | 1131 | ------- |
| 1132 | 1132 | LanceHybridQueryBuilder |
modified python/python/lancedb/rerankers/base.py +1 −1
| @@ -105,7 +105,7 @@ | ||
| 105 | 105 | query: str, |
| 106 | 106 | vector_results: pa.Table, |
| 107 | 107 | fts_results: pa.Table, |
| 108 | - ): | |
| 108 | + ) -> pa.Table: | |
| 109 | 109 | """ |
| 110 | 110 | Rerank function receives the individual results from the vector and FTS search |
| 111 | 111 | results. You can choose to use any of the results to generate the final results, |
modified python/python/lancedb/rerankers/linear_combination.py +35 −6
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | # See the License for the specific language governing permissions and |
| 12 | 12 | # limitations under the License. |
| 13 | 13 | |
| 14 | +from numpy import NaN | |
| 14 | 15 | import pyarrow as pa |
| 15 | 16 | |
| 16 | 17 | from .base import Reranker |
| @@ -58,14 +59,42 @@ | ||
| 58 | 59 | def merge_results( |
| 59 | 60 | self, vector_results: pa.Table, fts_results: pa.Table, fill: float |
| 60 | 61 | ): |
| 61 | - # If both are empty then just return an empty table | |
| 62 | - if len(vector_results) == 0 and len(fts_results) == 0: | |
| 63 | - return vector_results | |
| 64 | - # If one is empty then return the other | |
| 62 | + # If one is empty then return the other and add _relevance_score | |
| 63 | + # column equal the existing vector or fts score | |
| 65 | 64 | if len(vector_results) == 0: |
| 66 | - return fts_results | |
| 65 | + results = fts_results.append_column( | |
| 66 | + "_relevance_score", | |
| 67 | + pa.array(fts_results["_score"], type=pa.float32()), | |
| 68 | + ) | |
| 69 | + if self.score == "relevance": | |
| 70 | + results = self._keep_relevance_score(results) | |
| 71 | + elif self.score == "all": | |
| 72 | + results = results.append_column( | |
| 73 | + "_distance", | |
| 74 | + pa.array([NaN] * len(fts_results), type=pa.float32()), | |
| 75 | + ) | |
| 76 | + return results | |
| 77 | + | |
| 67 | 78 | if len(fts_results) == 0: |
| 68 | - return vector_results | |
| 79 | + # invert the distance to relevance score | |
| 80 | + results = vector_results.append_column( | |
| 81 | + "_relevance_score", | |
| 82 | + pa.array( | |
| 83 | + [ | |
| 84 | + self._invert_score(distance) | |
| 85 | + for distance in vector_results["_distance"].to_pylist() | |
| 86 | + ], | |
| 87 | + type=pa.float32(), | |
| 88 | + ), | |
| 89 | + ) | |
| 90 | + if self.score == "relevance": | |
| 91 | + results = self._keep_relevance_score(results) | |
| 92 | + elif self.score == "all": | |
| 93 | + results = results.append_column( | |
| 94 | + "_score", | |
| 95 | + pa.array([NaN] * len(vector_results), type=pa.float32()), | |
| 96 | + ) | |
| 97 | + return results | |
| 69 | 98 | |
| 70 | 99 | # sort both input tables on _rowid |
| 71 | 100 | combined_list = [] |
added python/python/lancedb/rerankers/util.py +19 −0
| @@ -0,0 +1,19 @@ | ||
| 1 | +# SPDX-License-Identifier: Apache-2.0 | |
| 2 | +# SPDX-FileCopyrightText: Copyright The Lance Authors | |
| 3 | + | |
| 4 | +import pyarrow as pa | |
| 5 | + | |
| 6 | + | |
| 7 | +def check_reranker_result(result): | |
| 8 | + if not isinstance(result, pa.Table): # Enforce type | |
| 9 | + raise TypeError( | |
| 10 | + f"rerank_hybrid must return a pyarrow.Table, got {type(result)}" | |
| 11 | + ) | |
| 12 | + | |
| 13 | + # Enforce that `_relevance_score` column is present in the result of every | |
| 14 | + # rerank_hybrid method | |
| 15 | + if "_relevance_score" not in result.column_names: | |
| 16 | + raise ValueError( | |
| 17 | + "rerank_hybrid must return a pyarrow.Table with a column" | |
| 18 | + "named `_relevance_score`" | |
| 19 | + ) |
modified python/python/tests/test_rerankers.py +49 −8
| @@ -120,12 +120,14 @@ | ||
| 120 | 120 | ) |
| 121 | 121 | |
| 122 | 122 | assert len(result) == 30 |
| 123 | - err = ( | |
| 123 | + ascending_relevance_err = ( | |
| 124 | 124 | "The _relevance_score column of the results returned by the reranker " |
| 125 | 125 | "represents the relevance of the result to the query & should " |
| 126 | 126 | "be descending." |
| 127 | 127 | ) |
| 128 | - assert np.all(np.diff(result.column("_relevance_score").to_numpy()) <= 0), err | |
| 128 | + assert np.all( | |
| 129 | + np.diff(result.column("_relevance_score").to_numpy()) <= 0 | |
| 130 | + ), ascending_relevance_err | |
| 129 | 131 | |
| 130 | 132 | # Vector search setting |
| 131 | 133 | result = ( |
| @@ -135,7 +137,9 @@ | ||
| 135 | 137 | .to_arrow() |
| 136 | 138 | ) |
| 137 | 139 | assert len(result) == 30 |
| 138 | - assert np.all(np.diff(result.column("_relevance_score").to_numpy()) <= 0), err | |
| 140 | + assert np.all( | |
| 141 | + np.diff(result.column("_relevance_score").to_numpy()) <= 0 | |
| 142 | + ), ascending_relevance_err | |
| 139 | 143 | result_explicit = ( |
| 140 | 144 | table.search(query_vector, vector_column_name="vector") |
| 141 | 145 | .rerank(reranker=reranker, query_string=query) |
| @@ -158,7 +162,26 @@ | ||
| 158 | 162 | .to_arrow() |
| 159 | 163 | ) |
| 160 | 164 | assert len(result) > 0 |
| 161 | - assert np.all(np.diff(result.column("_relevance_score").to_numpy()) <= 0), err | |
| 165 | + assert np.all( | |
| 166 | + np.diff(result.column("_relevance_score").to_numpy()) <= 0 | |
| 167 | + ), ascending_relevance_err | |
| 168 | + | |
| 169 | + # empty FTS results | |
| 170 | + query = "abcxyz" * 100 | |
| 171 | + result = ( | |
| 172 | + table.search(query_type="hybrid", vector_column_name="vector") | |
| 173 | + .vector(query_vector) | |
| 174 | + .text(query) | |
| 175 | + .limit(30) | |
| 176 | + .rerank(reranker=reranker) | |
| 177 | + .to_arrow() | |
| 178 | + ) | |
| 179 | + | |
| 180 | + # should return _relevance_score column | |
| 181 | + assert "_relevance_score" in result.column_names | |
| 182 | + assert np.all( | |
| 183 | + np.diff(result.column("_relevance_score").to_numpy()) <= 0 | |
| 184 | + ), ascending_relevance_err | |
| 162 | 185 | |
| 163 | 186 | # Multi-vector search setting |
| 164 | 187 | rs1 = table.search(query, vector_column_name="vector").limit(10).with_row_id(True) |
| @@ -172,7 +195,7 @@ | ||
| 172 | 195 | result_deduped = reranker.rerank_multivector( |
| 173 | 196 | [rs1, rs2, rs1], query, deduplicate=True |
| 174 | 197 | ) |
| 175 | - assert len(result_deduped) < 20 | |
| 198 | + assert len(result_deduped) <= 20 | |
| 176 | 199 | result_arrow = reranker.rerank_multivector([rs1.to_arrow(), rs2.to_arrow()], query) |
| 177 | 200 | assert len(result) == 20 and result == result_arrow |
| 178 | 201 | |
| @@ -213,7 +236,7 @@ | ||
| 213 | 236 | .vector(query_vector) |
| 214 | 237 | .text(query) |
| 215 | 238 | .limit(30) |
| 216 | - .rerank(normalize="score") | |
| 239 | + .rerank(reranker, normalize="score") | |
| 217 | 240 | .to_arrow() |
| 218 | 241 | ) |
| 219 | 242 | assert len(result) == 30 |
| @@ -228,12 +251,30 @@ | ||
| 228 | 251 | table.search(query, query_type="hybrid", vector_column_name="vector").text( |
| 229 | 252 | query |
| 230 | 253 | ).to_arrow() |
| 231 | - | |
| 232 | - assert np.all(np.diff(result.column("_relevance_score").to_numpy()) <= 0), ( | |
| 254 | + ascending_relevance_err = ( | |
| 233 | 255 | "The _relevance_score column of the results returned by the reranker " |
| 234 | 256 | "represents the relevance of the result to the query & should " |
| 235 | 257 | "be descending." |
| 236 | 258 | ) |
| 259 | + assert np.all( | |
| 260 | + np.diff(result.column("_relevance_score").to_numpy()) <= 0 | |
| 261 | + ), ascending_relevance_err | |
| 262 | + | |
| 263 | + # Test with empty FTS results | |
| 264 | + query = "abcxyz" * 100 | |
| 265 | + result = ( | |
| 266 | + table.search(query_type="hybrid", vector_column_name="vector") | |
| 267 | + .vector(query_vector) | |
| 268 | + .text(query) | |
| 269 | + .limit(30) | |
| 270 | + .rerank(reranker=reranker) | |
| 271 | + .to_arrow() | |
| 272 | + ) | |
| 273 | + # should return _relevance_score column | |
| 274 | + assert "_relevance_score" in result.column_names | |
| 275 | + assert np.all( | |
| 276 | + np.diff(result.column("_relevance_score").to_numpy()) <= 0 | |
| 277 | + ), ascending_relevance_err | |
| 237 | 278 | |
| 238 | 279 | |
| 239 | 280 | @pytest.mark.parametrize("use_tantivy", [True, False]) |