mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +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:
@@ -3,7 +3,7 @@
|
||||
|
||||
import { execFileSync, spawn } from "node:child_process";
|
||||
import * as path from "node:path";
|
||||
import { RecordBatch } from "apache-arrow";
|
||||
import { Field, Float64, RecordBatch, Schema } from "apache-arrow";
|
||||
import * as tmp from "tmp";
|
||||
import { Connection, Index, Table, connect, makeArrowTable } from "../lancedb";
|
||||
import { RRFReranker } from "../lancedb/rerankers";
|
||||
@@ -108,6 +108,122 @@ describe("rerankers", function () {
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("returns one-based RRF scores and the declared vector query schema", async function () {
|
||||
const query = table
|
||||
.vectorSearch([0.1, 0.1])
|
||||
.limit(2)
|
||||
.select(["text"])
|
||||
.rerank(await RRFReranker.create());
|
||||
const schema = await query.outputSchema();
|
||||
const result = await query.toArrow();
|
||||
|
||||
expect(result.schema.fields).toEqual(schema.fields);
|
||||
expect(schema.fields.map((field) => field.name)).toEqual([
|
||||
"text",
|
||||
"_distance",
|
||||
"_relevance_score",
|
||||
]);
|
||||
expect(result.getChild("text")?.toArray()).toEqual(["dog", "cat"]);
|
||||
const scores = result.getChild("_relevance_score")?.toArray();
|
||||
expect(scores?.[0]).toBeCloseTo(1 / 61, 8);
|
||||
expect(scores?.[1]).toBeCloseTo(1 / 62, 8);
|
||||
});
|
||||
|
||||
it("preserves the reranked schema for an empty vector result", async function () {
|
||||
const query = table
|
||||
.vectorSearch([0.1, 0.1])
|
||||
.where("text = 'absent'")
|
||||
.withRowId()
|
||||
.rerank(await RRFReranker.create());
|
||||
const schema = await query.outputSchema();
|
||||
const result = await query.toArrow();
|
||||
|
||||
expect(result.numRows).toBe(0);
|
||||
expect(result.schema.fields).toEqual(schema.fields);
|
||||
expect(result.schema.fields.map((field) => field.name)).toEqual(
|
||||
expect.arrayContaining(["_rowid", "_relevance_score"]),
|
||||
);
|
||||
});
|
||||
|
||||
it("dispatches to a custom vector callback with its declared output fields", async function () {
|
||||
const rerankedScore = 0.75;
|
||||
const declared = new Schema([
|
||||
new Field("_relevance_score", new Float64(), false),
|
||||
]);
|
||||
const rerankVector = jest.fn(
|
||||
async (_query: string, results: RecordBatch) => {
|
||||
expect(results.numRows).toBe(2);
|
||||
return makeArrowTable(
|
||||
// biome-ignore lint/style/useNamingConvention: this is the lance field name
|
||||
[{ _relevance_score: rerankedScore }],
|
||||
{ schema: declared },
|
||||
).batches[0];
|
||||
},
|
||||
);
|
||||
const rerankHybrid = jest.fn(async () => {
|
||||
throw new Error("unexpected hybrid callback");
|
||||
});
|
||||
const query = table
|
||||
.vectorSearch([0.1, 0.1])
|
||||
.limit(2)
|
||||
.rerank({
|
||||
rerankVector,
|
||||
rerankHybrid,
|
||||
outputSchema: () => declared,
|
||||
});
|
||||
expect((await query.outputSchema()).fields).toEqual(declared.fields);
|
||||
expect(rerankVector).not.toHaveBeenCalled();
|
||||
const result = await query.toArrow();
|
||||
expect(result.getChild("_relevance_score")?.toArray()).toEqual(
|
||||
new Float64Array([rerankedScore]),
|
||||
);
|
||||
expect(rerankVector).toHaveBeenCalledTimes(1);
|
||||
expect(rerankHybrid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports a rejected rerankVector callback to the query caller", async function () {
|
||||
const rerankVector = jest.fn(async () => {
|
||||
throw new Error("vector scoring failed");
|
||||
});
|
||||
const rerankHybrid = jest.fn(async () => {
|
||||
throw new Error("unexpected hybrid callback");
|
||||
});
|
||||
const query = table
|
||||
.vectorSearch([0.1, 0.1])
|
||||
.rerank({ rerankVector, rerankHybrid });
|
||||
|
||||
await expect(query.toArrow()).rejects.toThrow("vector scoring failed");
|
||||
expect(rerankVector).toHaveBeenCalledTimes(1);
|
||||
expect(rerankHybrid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("applies the query timeout while rerankVector is pending", async function () {
|
||||
let rejectPending: ((error: Error) => void) | undefined;
|
||||
const rerankVector = jest.fn(
|
||||
() =>
|
||||
new Promise<RecordBatch>((_resolve, reject) => {
|
||||
rejectPending = reject;
|
||||
}),
|
||||
);
|
||||
const rerankHybrid = jest.fn(async () => {
|
||||
throw new Error("unexpected hybrid callback");
|
||||
});
|
||||
const query = table
|
||||
.vectorSearch([0.1, 0.1])
|
||||
.rerank({ rerankVector, rerankHybrid });
|
||||
|
||||
try {
|
||||
await expect(query.toArrow({ timeoutMs: 500 })).rejects.toThrow(
|
||||
"Query timeout after 500 ms",
|
||||
);
|
||||
expect(rerankVector).toHaveBeenCalledTimes(1);
|
||||
expect(rerankHybrid).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
rejectPending?.(new Error("callback rejected after query timeout"));
|
||||
}
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
});
|
||||
|
||||
it("does not keep process alive after rerank query", async function () {
|
||||
const script = `
|
||||
import * as lancedb from "./dist/index.js";
|
||||
|
||||
Reference in New Issue
Block a user