feat(agent): selectable fusion; adopt the measured 0.4/0.6 default weights
BENCHMARKS.md has recorded since the weight sweep that the 0.7/0.3 default is
strictly dominated by 0.4/0.6 over the full LongMemEval haystack, but the
shipping code never adopted it: unified_search and the OpenClaw backend both
passed 0.7/0.3. Re-running the sweep here (500 questions, real MiniLM
embeddings on a GPU) reproduces it — turn-level Hit@1 51.6% vs 44.2%, Hit@5
81.4% vs 79.2%, Hit@10 87.8% vs 85.8%, MRR 0.6430 vs 0.5856 — so both now use
hybrid::DEFAULT_FUSION, which is that operating point and carries the
reasoning. A unit test pins it.
Fusion is also selectable now. hybrid::Fusion is either Weighted { vector,
keyword } or Rrf { k }; hybrid::fuse applies either to one candidate list per
stage, and merge_vector_keyword / hybrid_search delegate to it, so the public
API is unchanged. New HDF5Memory::hybrid_search_with and
hybrid::hybrid_search_fused take a Fusion. Reciprocal rank fusion was
implemented but reachable only as a free function over a linear scan, so it
had never been compared with the weighted sum on equal terms; it is now a mode
in the LongMemEval bench (measurement to follow).
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
@@ -20,8 +20,7 @@ impl HDF5Memory {
|
||||
query_embedding: &[f32],
|
||||
query_text: &str,
|
||||
bm25: &bm25::BM25Index,
|
||||
vector_weight: f32,
|
||||
keyword_weight: f32,
|
||||
fusion: hybrid::Fusion,
|
||||
k: usize,
|
||||
) -> Vec<(usize, f32)> {
|
||||
self.ensure_hnsw_fresh();
|
||||
@@ -38,23 +37,16 @@ impl HDF5Memory {
|
||||
// Fusion normalises over every keyword match, so it needs all
|
||||
// the scores — but not ranked.
|
||||
let kw_scores = bm25.scores(query_text);
|
||||
hybrid::merge_vector_keyword(
|
||||
vec_scores,
|
||||
kw_scores,
|
||||
vector_weight,
|
||||
keyword_weight,
|
||||
k,
|
||||
)
|
||||
hybrid::fuse(vec_scores, kw_scores, fusion, k)
|
||||
}
|
||||
_ => hybrid::hybrid_search(
|
||||
_ => hybrid::hybrid_search_fused(
|
||||
query_embedding,
|
||||
query_text,
|
||||
&self.cache.embeddings,
|
||||
&self.cache.chunks,
|
||||
&self.cache.tombstones,
|
||||
bm25,
|
||||
vector_weight,
|
||||
keyword_weight,
|
||||
fusion,
|
||||
k,
|
||||
),
|
||||
}
|
||||
@@ -66,19 +58,17 @@ impl HDF5Memory {
|
||||
query_embedding: &[f32],
|
||||
query_text: &str,
|
||||
bm25: &bm25::BM25Index,
|
||||
vector_weight: f32,
|
||||
keyword_weight: f32,
|
||||
fusion: hybrid::Fusion,
|
||||
k: usize,
|
||||
) -> Vec<(usize, f32)> {
|
||||
hybrid::hybrid_search(
|
||||
hybrid::hybrid_search_fused(
|
||||
query_embedding,
|
||||
query_text,
|
||||
&self.cache.embeddings,
|
||||
&self.cache.chunks,
|
||||
&self.cache.tombstones,
|
||||
bm25,
|
||||
vector_weight,
|
||||
keyword_weight,
|
||||
fusion,
|
||||
k,
|
||||
)
|
||||
}
|
||||
@@ -91,20 +81,36 @@ impl HDF5Memory {
|
||||
vector_weight: f32,
|
||||
keyword_weight: f32,
|
||||
k: usize,
|
||||
) -> Vec<SearchResult> {
|
||||
self.hybrid_search_with(
|
||||
query_embedding,
|
||||
query_text,
|
||||
hybrid::Fusion::Weighted {
|
||||
vector: vector_weight,
|
||||
keyword: keyword_weight,
|
||||
},
|
||||
k,
|
||||
)
|
||||
}
|
||||
|
||||
/// [`HDF5Memory::hybrid_search`] with the fusion method chosen explicitly.
|
||||
///
|
||||
/// [`hybrid::DEFAULT_FUSION`] is what the weighted form defaults to;
|
||||
/// [`hybrid::Fusion::Rrf`] combines the two stages by rank instead of by
|
||||
/// score.
|
||||
pub fn hybrid_search_with(
|
||||
&mut self,
|
||||
query_embedding: &[f32],
|
||||
query_text: &str,
|
||||
fusion: hybrid::Fusion,
|
||||
k: usize,
|
||||
) -> Vec<SearchResult> {
|
||||
// The keyword index lives for the life of the store and is updated
|
||||
// incrementally. Take it out for the duration of the call so the
|
||||
// vector stage can borrow `self` mutably, then put it back.
|
||||
self.ensure_bm25_fresh();
|
||||
let bm25 = self.bm25.take().expect("ensure_bm25_fresh leaves an index");
|
||||
let scored = self.vector_keyword_search(
|
||||
query_embedding,
|
||||
query_text,
|
||||
&bm25,
|
||||
vector_weight,
|
||||
keyword_weight,
|
||||
k,
|
||||
);
|
||||
let scored = self.vector_keyword_search(query_embedding, query_text, &bm25, fusion, k);
|
||||
let mut results: Vec<SearchResult> = scored
|
||||
.into_iter()
|
||||
.map(|(idx, score)| {
|
||||
|
||||
Reference in New Issue
Block a user