mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 15:55:19 +00:00
feat: support vector-only and FTS-only reranking (#3024)
Add rerank_vector() and rerank_fts() methods to Reranker trait, allowing rerankers to work with single search modes instead of only hybrid search. Implemented for: - RRFReranker (Rust and Python) - LinearCombinationReranker (Python) Resolves TODO at rust/lancedb/src/rerankers.rs:55 Changes: - Extended Reranker trait with default implementations - Added unit tests for new methods - Updated documentation with examples --------- Co-authored-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
@@ -668,6 +668,8 @@ distance between the query vector and the actual uncompressed vector.
|
||||
rerank(reranker): VectorQuery
|
||||
```
|
||||
|
||||
Apply a reranker to this vector or hybrid query.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* **reranker**: [`Reranker`](../namespaces/rerankers/interfaces/Reranker.md)
|
||||
|
||||
@@ -33,6 +33,26 @@ rerankHybrid(
|
||||
|
||||
***
|
||||
|
||||
### rerankVector()
|
||||
|
||||
```ts
|
||||
rerankVector(query, results): Promise<RecordBatch<any>>
|
||||
```
|
||||
|
||||
Score vector search results by their one-based rank.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* **query**: `string`
|
||||
|
||||
* **results**: `RecordBatch`<`any`>
|
||||
|
||||
#### Returns
|
||||
|
||||
`Promise`<`RecordBatch`<`any`>>
|
||||
|
||||
***
|
||||
|
||||
### create()
|
||||
|
||||
```ts
|
||||
|
||||
@@ -6,8 +6,29 @@
|
||||
|
||||
# Interface: Reranker
|
||||
|
||||
Reranks vector or hybrid search results.
|
||||
|
||||
## Methods
|
||||
|
||||
### outputSchema()?
|
||||
|
||||
```ts
|
||||
optional outputSchema(input): Schema<any> | Promise<Schema<any>>
|
||||
```
|
||||
|
||||
Declare the output fields for vector reranking. The default keeps the
|
||||
input fields and appends a non-nullable Float32 `_relevance_score` field.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* **input**: `Schema`<`any`>
|
||||
|
||||
#### Returns
|
||||
|
||||
`Schema`<`any`> \| `Promise`<`Schema`<`any`>>
|
||||
|
||||
***
|
||||
|
||||
### rerankHybrid()
|
||||
|
||||
```ts
|
||||
@@ -28,3 +49,23 @@ rerankHybrid(
|
||||
#### Returns
|
||||
|
||||
`Promise`<`RecordBatch`<`any`>>
|
||||
|
||||
***
|
||||
|
||||
### rerankVector()?
|
||||
|
||||
```ts
|
||||
optional rerankVector(query, results): Promise<RecordBatch<any>>
|
||||
```
|
||||
|
||||
Rerank one vector result list. The query string is empty for vector queries.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* **query**: `string`
|
||||
|
||||
* **results**: `RecordBatch`<`any`>
|
||||
|
||||
#### Returns
|
||||
|
||||
`Promise`<`RecordBatch`<`any`>>
|
||||
|
||||
@@ -106,6 +106,15 @@ model-based reranker from TypeScript or Rust, you must implement the hybrid rera
|
||||
yourself.
|
||||
</Note>
|
||||
|
||||
{/*
|
||||
Draft for the single-mode reranking SDK release. Replace the hybrid-only SDK
|
||||
guidance in the note above with this paragraph.
|
||||
|
||||
Rust supports vector, full-text and hybrid reranking. TypeScript supports vector
|
||||
and hybrid reranking. Both SDKs provide `RRFReranker` and let you implement a
|
||||
[custom reranker](#custom).
|
||||
*/}
|
||||
|
||||
|
||||
### Multi-vector reranking {#multi-vector-reranking}
|
||||
Use `rerank_multivector()` to combine result sets from several vector queries.
|
||||
@@ -142,6 +151,14 @@ Python custom rerankers implement `rerank_hybrid()` and can optionally implement
|
||||
`rerank_vector()` and `rerank_fts()`. TypeScript and Rust expose hybrid reranking
|
||||
interfaces. See [the interface requirements](#custom) before implementing one.
|
||||
|
||||
{/*
|
||||
Draft for the single-mode reranking SDK release. Replace the TypeScript and
|
||||
Rust sentence above with this sentence.
|
||||
|
||||
For single-mode searches, Rust exposes vector and full-text callbacks and
|
||||
TypeScript exposes a vector callback.
|
||||
*/}
|
||||
|
||||
## Algorithm options
|
||||
|
||||
The following sections describe the Python constructor options and examples.
|
||||
@@ -168,6 +185,26 @@ why it's the default reranker for LanceDB hybrid search. The implementation foll
|
||||
> Because RRF fuses two or more ranked lists, it can't rerank a single vector or full-text result set
|
||||
> on its own. Calling `rerank_vector` or `rerank_fts` on an `RRFReranker` raises `NotImplementedError`.
|
||||
|
||||
{/*
|
||||
Draft for the single-mode reranking SDK release. Replace the supported query
|
||||
types note above with these paragraphs.
|
||||
|
||||
Use `RRFReranker` with vector-only, full-text-only or hybrid search in Python
|
||||
and Rust, or with vector-only or hybrid search in TypeScript. Python also
|
||||
supports [multi-vector reranking](/reranking#multi-vector-reranking).
|
||||
|
||||
On a single ranked list, `RRFReranker` keeps retrieval order and computes
|
||||
`_relevance_score` as `1 / (rank + K)`, starting at rank 1. It scores by position
|
||||
without requiring `_rowid` and includes the score column on empty results. In
|
||||
Python, `return_score="all"` also keeps `_distance` for vector search or
|
||||
`_score` for full-text search. When passing a precomputed vector to the Python
|
||||
query builder, supply the text with `.rerank(reranker, query_string=query)`.
|
||||
|
||||
For independent searches against a single-vector column, submit one query
|
||||
vector per search. A multivector column combines multiple query vectors into
|
||||
one ranked list.
|
||||
*/}
|
||||
|
||||
|
||||
<CodeGroup>
|
||||
<CodeBlock filename="Python" language="Python" icon="python">
|
||||
@@ -363,6 +400,27 @@ The Python base class exposes hybrid, vector-only, and FTS-only rerank hooks. Ty
|
||||
currently expose the custom reranker interface for hybrid reranking. In Rust, a custom reranker must
|
||||
also satisfy the trait bounds `Debug + Send + Sync`.
|
||||
|
||||
{/*
|
||||
Draft for the single-mode reranking SDK release. Keep the Python sentence and
|
||||
replace the TypeScript and Rust guidance above with this section.
|
||||
|
||||
- In Rust, implement `Reranker` with `rerank_hybrid()` and override
|
||||
`rerank_vector()` or `rerank_fts()` for the single-mode queries you support.
|
||||
Pass the reranker to `.rerank(...)` on a vector or full-text query. The vector
|
||||
callback receives an empty query string and the full-text callback receives
|
||||
the search text. The trait requires `Debug + Send + Sync`.
|
||||
- In TypeScript, implement `rerankHybrid()` and add `rerankVector()` to use the
|
||||
reranker with `table.vectorSearch(...).rerank(...)`. The vector callback
|
||||
receives an empty query string.
|
||||
|
||||
Hybrid search calls the hybrid callback once with both lists. In Rust and
|
||||
TypeScript, single-mode callbacks receive Arrow record batches and return a
|
||||
batch in relevance order. By default the output schema keeps the
|
||||
input fields and adds a non-nullable Float32 `_relevance_score`. If your callback
|
||||
changes the output fields, declare its schema with `output_schema()` in Rust or
|
||||
`outputSchema()` in TypeScript.
|
||||
*/}
|
||||
|
||||
### Interface {#custom-interface}
|
||||
|
||||
The `Reranker` base interface comes with a `merge_results()` method that can be used to combine the
|
||||
|
||||
Reference in New Issue
Block a user