reasonkit-mem 0.1.7

High-performance vector database & RAG memory layer - hybrid search, embeddings, RAPTOR trees, BM25 fusion, and semantic retrieval for AI systems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Result fusion strategies for hybrid retrieval
//!
//! Implements various fusion algorithms:
//! - RRF (Reciprocal Rank Fusion) - rank-based fusion
//! - Weighted Sum - score-based fusion (original implementation)
//! - RBF (Rank-Biased Fusion) - decay-based fusion

use crate::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Fusion strategy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FusionStrategy {
    /// Weighted sum of normalized scores
    WeightedSum {
        /// Weight for dense (vector) results (0.0-1.0)
        dense_weight: f32,
    },
    /// Reciprocal Rank Fusion (RRF)
    ReciprocalRankFusion {
        /// Constant k for RRF formula (typically 60)
        k: usize,
    },
    /// Rank-Biased Fusion (RBF)
    RankBiasedFusion {
        /// Persistence parameter (0.0-1.0, typically 0.8)
        rho: f32,
    },
}

impl Default for FusionStrategy {
    fn default() -> Self {
        Self::ReciprocalRankFusion { k: 60 }
    }
}

/// Result from a single retrieval method
#[derive(Debug, Clone)]
pub struct RankedResult {
    /// Item identifier (chunk ID)
    pub id: Uuid,
    /// Score from this retrieval method
    pub score: f32,
    /// Rank position (0-indexed)
    pub rank: usize,
}

/// Fused result combining multiple retrieval methods
#[derive(Debug, Clone)]
pub struct FusedResult {
    /// Item identifier
    pub id: Uuid,
    /// Combined fusion score
    pub fusion_score: f32,
    /// Individual scores from each method
    pub method_scores: HashMap<String, f32>,
}

/// Fusion engine for combining retrieval results
pub struct FusionEngine {
    strategy: FusionStrategy,
}

impl FusionEngine {
    /// Create a new fusion engine with the given strategy
    pub fn new(strategy: FusionStrategy) -> Self {
        Self { strategy }
    }

    /// Create with RRF strategy (recommended)
    pub fn rrf(k: usize) -> Self {
        Self::new(FusionStrategy::ReciprocalRankFusion { k })
    }

    /// Create with weighted sum strategy
    pub fn weighted(dense_weight: f32) -> Self {
        Self::new(FusionStrategy::WeightedSum { dense_weight })
    }

    /// Fuse results from multiple retrieval methods
    ///
    /// # Arguments
    /// * `results` - Map of method name to ranked results
    ///
    /// # Returns
    /// Fused results sorted by fusion score (descending)
    pub fn fuse(&self, results: HashMap<String, Vec<RankedResult>>) -> Result<Vec<FusedResult>> {
        match &self.strategy {
            FusionStrategy::WeightedSum { dense_weight } => {
                self.fuse_weighted_sum(results, *dense_weight)
            }
            FusionStrategy::ReciprocalRankFusion { k } => self.fuse_rrf(results, *k),
            FusionStrategy::RankBiasedFusion { rho } => self.fuse_rbf(results, *rho),
        }
    }

    /// Reciprocal Rank Fusion (RRF)
    ///
    /// Formula: score(d) = sum over all methods of: 1 / (k + rank(d))
    /// where k is a constant (typically 60)
    ///
    /// Reference: "Reciprocal Rank Fusion outperforms Condorcet and
    /// individual Rank Learning Methods" (Cormack et al., 2009)
    fn fuse_rrf(
        &self,
        results: HashMap<String, Vec<RankedResult>>,
        k: usize,
    ) -> Result<Vec<FusedResult>> {
        let mut scores: HashMap<Uuid, (f32, HashMap<String, f32>)> = HashMap::new();

        for (method_name, method_results) in results {
            for result in method_results {
                let rrf_score = 1.0 / (k as f32 + result.rank as f32 + 1.0);

                let (total_score, method_scores) = scores
                    .entry(result.id)
                    .or_insert_with(|| (0.0, HashMap::new()));

                *total_score += rrf_score;
                method_scores.insert(method_name.clone(), result.score);
            }
        }

        let mut fused_results: Vec<FusedResult> = scores
            .into_iter()
            .map(|(id, (fusion_score, method_scores))| FusedResult {
                id,
                fusion_score,
                method_scores,
            })
            .collect();

        // Sort by fusion score descending
        fused_results.sort_by(|a, b| {
            b.fusion_score
                .partial_cmp(&a.fusion_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(fused_results)
    }

    /// Weighted sum fusion with score normalization
    ///
    /// Normalizes scores from each method to [0, 1] range using min-max normalization,
    /// then combines using weighted sum.
    fn fuse_weighted_sum(
        &self,
        results: HashMap<String, Vec<RankedResult>>,
        dense_weight: f32,
    ) -> Result<Vec<FusedResult>> {
        let sparse_weight = 1.0 - dense_weight;

        // Normalize scores for each method
        let mut normalized_results: HashMap<String, Vec<(Uuid, f32)>> = HashMap::new();

        for (method_name, method_results) in results {
            if method_results.is_empty() {
                continue;
            }

            // Find min and max scores for normalization
            let min_score = method_results
                .iter()
                .map(|r| r.score)
                .min_by(|a, b| a.partial_cmp(b).unwrap())
                .unwrap_or(0.0);
            let max_score = method_results
                .iter()
                .map(|r| r.score)
                .max_by(|a, b| a.partial_cmp(b).unwrap())
                .unwrap_or(1.0);

            let score_range = max_score - min_score;

            let normalized: Vec<(Uuid, f32)> = if score_range > 1e-6 {
                method_results
                    .into_iter()
                    .map(|r| {
                        let norm_score = (r.score - min_score) / score_range;
                        (r.id, norm_score)
                    })
                    .collect()
            } else {
                // All scores are the same, normalize to 1.0
                method_results.into_iter().map(|r| (r.id, 1.0)).collect()
            };

            normalized_results.insert(method_name, normalized);
        }

        // Combine normalized scores
        let mut scores: HashMap<Uuid, (f32, HashMap<String, f32>)> = HashMap::new();

        for (method_name, method_results) in normalized_results {
            let weight = if method_name == "dense" {
                dense_weight
            } else {
                sparse_weight
            };

            for (id, norm_score) in method_results {
                let (total_score, method_scores) =
                    scores.entry(id).or_insert_with(|| (0.0, HashMap::new()));

                *total_score += norm_score * weight;
                method_scores.insert(method_name.clone(), norm_score);
            }
        }

        let mut fused_results: Vec<FusedResult> = scores
            .into_iter()
            .map(|(id, (fusion_score, method_scores))| FusedResult {
                id,
                fusion_score,
                method_scores,
            })
            .collect();

        fused_results.sort_by(|a, b| {
            b.fusion_score
                .partial_cmp(&a.fusion_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(fused_results)
    }

    /// Rank-Biased Fusion (RBF)
    ///
    /// Formula: score(d) = sum over all methods of: rho^rank(d)
    /// where rho is the persistence parameter (0 < rho < 1)
    ///
    /// Reference: "Rank-Biased Precision for Measurement of Retrieval
    /// Effectiveness" (Moffat & Zobel, 2008)
    fn fuse_rbf(
        &self,
        results: HashMap<String, Vec<RankedResult>>,
        rho: f32,
    ) -> Result<Vec<FusedResult>> {
        let mut scores: HashMap<Uuid, (f32, HashMap<String, f32>)> = HashMap::new();

        for (method_name, method_results) in results {
            for result in method_results {
                // RBF score: rho^rank
                let rbf_score = rho.powi(result.rank as i32);

                let (total_score, method_scores) = scores
                    .entry(result.id)
                    .or_insert_with(|| (0.0, HashMap::new()));

                *total_score += rbf_score;
                method_scores.insert(method_name.clone(), result.score);
            }
        }

        let mut fused_results: Vec<FusedResult> = scores
            .into_iter()
            .map(|(id, (fusion_score, method_scores))| FusedResult {
                id,
                fusion_score,
                method_scores,
            })
            .collect();

        fused_results.sort_by(|a, b| {
            b.fusion_score
                .partial_cmp(&a.fusion_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(fused_results)
    }
}

/// Convert scored results to ranked results
pub fn to_ranked_results(results: Vec<(Uuid, f32)>) -> Vec<RankedResult> {
    results
        .into_iter()
        .enumerate()
        .map(|(rank, (id, score))| RankedResult { id, score, rank })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rrf_fusion() {
        let engine = FusionEngine::rrf(60);

        let mut results = HashMap::new();

        // Sparse results
        results.insert(
            "sparse".to_string(),
            vec![
                RankedResult {
                    id: Uuid::nil(),
                    score: 10.0,
                    rank: 0,
                },
                RankedResult {
                    id: Uuid::from_u128(1),
                    score: 8.0,
                    rank: 1,
                },
                RankedResult {
                    id: Uuid::from_u128(2),
                    score: 6.0,
                    rank: 2,
                },
            ],
        );

        // Dense results (different order)
        results.insert(
            "dense".to_string(),
            vec![
                RankedResult {
                    id: Uuid::from_u128(1),
                    score: 0.9,
                    rank: 0,
                },
                RankedResult {
                    id: Uuid::nil(),
                    score: 0.8,
                    rank: 1,
                },
                RankedResult {
                    id: Uuid::from_u128(3),
                    score: 0.7,
                    rank: 2,
                },
            ],
        );

        let fused = engine.fuse(results).unwrap();

        // Both Uuid::from_u128(1) and Uuid::nil() have same RRF score
        // (each is rank 0 in one and rank 1 in another)
        // Check that both appear in top 2 results
        assert!(fused.len() >= 2);
        let top_ids: Vec<_> = fused.iter().take(2).map(|r| r.id).collect();
        assert!(top_ids.contains(&Uuid::from_u128(1)));
        assert!(top_ids.contains(&Uuid::nil()));
    }

    #[test]
    fn test_weighted_sum_fusion() {
        let engine = FusionEngine::weighted(0.7);

        let mut results = HashMap::new();

        results.insert(
            "sparse".to_string(),
            vec![
                RankedResult {
                    id: Uuid::nil(),
                    score: 10.0,
                    rank: 0,
                },
                RankedResult {
                    id: Uuid::from_u128(1),
                    score: 5.0,
                    rank: 1,
                },
            ],
        );

        results.insert(
            "dense".to_string(),
            vec![
                RankedResult {
                    id: Uuid::from_u128(1),
                    score: 0.9,
                    rank: 0,
                },
                RankedResult {
                    id: Uuid::nil(),
                    score: 0.5,
                    rank: 1,
                },
            ],
        );

        let fused = engine.fuse(results).unwrap();

        assert_eq!(fused.len(), 2);
        // Check that scores are normalized and combined
        assert!(fused[0].fusion_score <= 1.0);
    }

    #[test]
    fn test_to_ranked_results() {
        let scored = vec![
            (Uuid::nil(), 0.9),
            (Uuid::from_u128(1), 0.8),
            (Uuid::from_u128(2), 0.7),
        ];

        let ranked = to_ranked_results(scored);

        assert_eq!(ranked.len(), 3);
        assert_eq!(ranked[0].rank, 0);
        assert_eq!(ranked[1].rank, 1);
        assert_eq!(ranked[2].rank, 2);
    }
}