rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! # Advanced Reranking Module
//!
//! State-of-the-art reranking algorithms for improving retrieval relevance and precision.
//!
//! This module provides multiple reranking strategies to refine initial retrieval results,
//! ensuring the most relevant documents are ranked at the top. It supports various
//! approaches from simple score-based reranking to sophisticated neural models.
//!
//! ## Features
//!
//! - **Cross-Encoder Reranking**: Transformer-based relevance scoring
//! - **Neural Reranking**: Deep learning models for relevance prediction
//! - **Learning-to-Rank**: Machine learning ranking with feature engineering
//! - **Multi-Signal Fusion**: Combine multiple ranking signals
//! - **Diversity Reranking**: Ensure result diversity while maintaining relevance
//!
//! ## Performance
//!
//! - Batch processing for efficiency
//! - Async operations for non-blocking I/O
//! - Caching of model predictions
//! - GPU acceleration support (when available)
//!
//! ## Examples
//!
//! ### Cross-Encoder Reranking
//! ```rust
//! use rrag::reranking::{CrossEncoderReranker, CrossEncoderConfig};
//! use rrag::Document;
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let reranker = CrossEncoderReranker::new(
//!     CrossEncoderConfig::default()
//!         .with_model("cross-encoder/ms-marco-MiniLM-L-12-v2")
//!         .with_batch_size(32)
//! );
//!
//! let query = "What is machine learning?";
//! let documents = vec![
//!     Document::new("Machine learning is a subset of AI..."),
//!     Document::new("Deep learning uses neural networks..."),
//!     Document::new("Python is a programming language..."),
//! ];
//!
//! let reranked = reranker.rerank(query, documents).await?;
//! // Most relevant documents are now at the top
//! # Ok(())
//! # }
//! ```
//!
//! ### Learning-to-Rank
//! ```rust
//! use rrag::reranking::{LearningToRankReranker, LTRConfig};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let ltr_reranker = LearningToRankReranker::new(
//!     LTRConfig::default()
//!         .with_features(vec!["bm25", "tfidf", "semantic_similarity"])
//!         .with_model("lightgbm")
//! );
//!
//! let reranked = ltr_reranker.rerank_with_features(
//!     query,
//!     documents,
//!     feature_matrix
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Multi-Signal Reranking
//! ```rust
//! use rrag::reranking::{MultiSignalReranker, SignalWeight};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let multi_reranker = MultiSignalReranker::new()
//!     .add_signal("relevance", 0.6)
//!     .add_signal("recency", 0.2)
//!     .add_signal("popularity", 0.2);
//!
//! let reranked = multi_reranker.rerank_multi_signal(
//!     documents,
//!     signal_scores
//! ).await?;
//! # Ok(())
//! # }
//! ```

use crate::RragResult;

pub mod cross_encoder;
pub mod learning_to_rank;
pub mod multi_signal;
pub mod neural_reranker;

// Re-exports
pub use cross_encoder::{
    CrossEncoderConfig, CrossEncoderModel, CrossEncoderReranker, RerankedResult, RerankingStrategy,
    ScoreAggregation,
};
pub use learning_to_rank::{
    FeatureExtractor, FeatureType, LTRConfig, LTRFeatures, LTRModel, LearningToRankReranker,
    RankingFeature,
};
pub use multi_signal::{
    MultiSignalConfig, MultiSignalReranker, RelevanceSignal, SignalAggregation, SignalType,
    SignalWeight,
};
pub use neural_reranker::{
    AttentionMechanism, BertReranker, NeuralConfig, NeuralReranker, RobertaReranker,
    TransformerReranker,
};

/// Main reranking interface that coordinates different reranking strategies
pub struct AdvancedReranker {
    /// Cross-encoder for query-document relevance
    cross_encoder: Option<CrossEncoderReranker>,

    /// Learning-to-rank model
    ltr_model: Option<LearningToRankReranker>,

    /// Multi-signal aggregation
    multi_signal: Option<MultiSignalReranker>,

    /// Neural reranking models
    neural_reranker: Option<NeuralReranker>,

    /// Configuration
    config: AdvancedRerankingConfig,
}

/// Configuration for advanced reranking
#[derive(Debug, Clone)]
pub struct AdvancedRerankingConfig {
    /// Enable cross-encoder reranking
    pub enable_cross_encoder: bool,

    /// Enable learning-to-rank
    pub enable_ltr: bool,

    /// Enable multi-signal reranking
    pub enable_multi_signal: bool,

    /// Enable neural reranking
    pub enable_neural: bool,

    /// Maximum number of candidates to rerank
    pub max_candidates: usize,

    /// Minimum score threshold
    pub score_threshold: f32,

    /// Reranking strategy priority order
    pub strategy_order: Vec<RerankingStrategyType>,

    /// Score combination method
    pub score_combination: ScoreCombination,

    /// Cache reranking results
    pub enable_caching: bool,

    /// Batch size for neural models
    pub batch_size: usize,
}

impl Default for AdvancedRerankingConfig {
    fn default() -> Self {
        Self {
            enable_cross_encoder: true,
            enable_ltr: false,
            enable_multi_signal: true,
            enable_neural: false,
            max_candidates: 100,
            score_threshold: 0.1,
            strategy_order: vec![
                RerankingStrategyType::CrossEncoder,
                RerankingStrategyType::MultiSignal,
            ],
            score_combination: ScoreCombination::Weighted(vec![0.7, 0.3]),
            enable_caching: true,
            batch_size: 32,
        }
    }
}

/// Types of reranking strategies
#[derive(Debug, Clone, PartialEq)]
pub enum RerankingStrategyType {
    CrossEncoder,
    LearningToRank,
    MultiSignal,
    Neural,
}

/// Methods for combining scores from multiple rerankers
#[derive(Debug, Clone)]
pub enum ScoreCombination {
    /// Average all scores
    Average,
    /// Weighted combination
    Weighted(Vec<f32>),
    /// Maximum score
    Max,
    /// Minimum score
    Min,
    /// Learned combination (requires training)
    Learned,
}

/// Result from advanced reranking
#[derive(Debug, Clone)]
pub struct AdvancedRerankedResult {
    /// Document identifier
    pub document_id: String,

    /// Final combined score
    pub final_score: f32,

    /// Individual scores from each reranker
    pub component_scores: std::collections::HashMap<String, f32>,

    /// Original retrieval rank
    pub original_rank: usize,

    /// New rank after reranking
    pub new_rank: usize,

    /// Confidence in the reranking decision
    pub confidence: f32,

    /// Explanation of the reranking decision
    pub explanation: Option<String>,

    /// Processing metadata
    pub metadata: RerankingMetadata,
}

/// Metadata about the reranking process
#[derive(Debug, Clone)]
pub struct RerankingMetadata {
    /// Time taken for reranking
    pub reranking_time_ms: u64,

    /// Rerankers used
    pub rerankers_used: Vec<String>,

    /// Features extracted
    pub features_extracted: usize,

    /// Model versions used
    pub model_versions: std::collections::HashMap<String, String>,

    /// Warnings or notices
    pub warnings: Vec<String>,
}

impl AdvancedReranker {
    /// Create a new advanced reranker
    pub fn new(config: AdvancedRerankingConfig) -> Self {
        Self {
            cross_encoder: if config.enable_cross_encoder {
                Some(CrossEncoderReranker::new(CrossEncoderConfig::default()))
            } else {
                None
            },
            ltr_model: if config.enable_ltr {
                Some(LearningToRankReranker::new(LTRConfig::default()))
            } else {
                None
            },
            multi_signal: if config.enable_multi_signal {
                Some(MultiSignalReranker::new(MultiSignalConfig::default()))
            } else {
                None
            },
            neural_reranker: if config.enable_neural {
                Some(NeuralReranker::new(NeuralConfig::default()))
            } else {
                None
            },
            config,
        }
    }

    /// Rerank a list of initial retrieval results
    pub async fn rerank(
        &self,
        query: &str,
        initial_results: Vec<crate::SearchResult>,
    ) -> RragResult<Vec<AdvancedRerankedResult>> {
        let start_time = std::time::Instant::now();

        // Limit candidates if needed
        let candidates: Vec<_> = initial_results
            .into_iter()
            .take(self.config.max_candidates)
            .enumerate()
            .collect();

        let mut component_scores = std::collections::HashMap::new();
        let mut rerankers_used = Vec::new();
        let mut warnings = Vec::new();

        // Apply reranking strategies in order
        for strategy in &self.config.strategy_order {
            match strategy {
                RerankingStrategyType::CrossEncoder => {
                    if let Some(ref cross_encoder) = self.cross_encoder {
                        let candidate_results: Vec<_> = candidates
                            .iter()
                            .map(|(_, result)| result.clone())
                            .collect();
                        match cross_encoder.rerank(query, &candidate_results).await {
                            Ok(scores) => {
                                component_scores.insert("cross_encoder".to_string(), scores);
                                rerankers_used.push("cross_encoder".to_string());
                            }
                            Err(e) => {
                                warnings.push(format!("Cross-encoder failed: {}", e));
                            }
                        }
                    }
                }
                RerankingStrategyType::MultiSignal => {
                    if let Some(ref multi_signal) = self.multi_signal {
                        let candidate_results: Vec<_> = candidates
                            .iter()
                            .map(|(_, result)| result.clone())
                            .collect();
                        match multi_signal.rerank(query, &candidate_results).await {
                            Ok(scores) => {
                                component_scores.insert("multi_signal".to_string(), scores);
                                rerankers_used.push("multi_signal".to_string());
                            }
                            Err(e) => {
                                warnings.push(format!("Multi-signal failed: {}", e));
                            }
                        }
                    }
                }
                RerankingStrategyType::LearningToRank => {
                    if let Some(ref ltr) = self.ltr_model {
                        let candidate_results: Vec<_> = candidates
                            .iter()
                            .map(|(_, result)| result.clone())
                            .collect();
                        match ltr.rerank(query, &candidate_results).await {
                            Ok(scores) => {
                                component_scores.insert("ltr".to_string(), scores);
                                rerankers_used.push("ltr".to_string());
                            }
                            Err(e) => {
                                warnings.push(format!("LTR failed: {}", e));
                            }
                        }
                    }
                }
                RerankingStrategyType::Neural => {
                    if let Some(ref neural) = self.neural_reranker {
                        let candidate_results: Vec<_> = candidates
                            .iter()
                            .map(|(_, result)| result.clone())
                            .collect();
                        match neural.rerank(query, &candidate_results).await {
                            Ok(scores) => {
                                component_scores.insert("neural".to_string(), scores);
                                rerankers_used.push("neural".to_string());
                            }
                            Err(e) => {
                                warnings.push(format!("Neural reranker failed: {}", e));
                            }
                        }
                    }
                }
            }
        }

        // Combine scores
        let final_scores = self.combine_scores(&component_scores, candidates.len());

        // Create reranked results
        let mut reranked_results: Vec<_> = candidates
            .into_iter()
            .enumerate()
            .map(|(idx, (original_rank, result))| AdvancedRerankedResult {
                document_id: result.id.clone(),
                final_score: final_scores.get(&idx).copied().unwrap_or(result.score),
                component_scores: component_scores
                    .iter()
                    .map(|(name, scores)| (name.clone(), scores.get(&idx).copied().unwrap_or(0.0)))
                    .collect(),
                original_rank,
                new_rank: 0, // Will be filled after sorting
                confidence: self.calculate_confidence(&component_scores, idx),
                explanation: self.generate_explanation(&component_scores, idx),
                metadata: RerankingMetadata {
                    reranking_time_ms: start_time.elapsed().as_millis() as u64,
                    rerankers_used: rerankers_used.clone(),
                    features_extracted: 0, // Would be set by individual rerankers
                    model_versions: std::collections::HashMap::new(),
                    warnings: warnings.clone(),
                },
            })
            .collect();

        // Sort by final score
        reranked_results.sort_by(|a, b| {
            b.final_score
                .partial_cmp(&a.final_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Update new ranks
        for (idx, result) in reranked_results.iter_mut().enumerate() {
            result.new_rank = idx;
        }

        // Filter by score threshold
        reranked_results.retain(|result| result.final_score >= self.config.score_threshold);

        Ok(reranked_results)
    }

    /// Combine scores from different rerankers
    fn combine_scores(
        &self,
        component_scores: &std::collections::HashMap<String, std::collections::HashMap<usize, f32>>,
        num_candidates: usize,
    ) -> std::collections::HashMap<usize, f32> {
        let mut final_scores = std::collections::HashMap::new();

        for idx in 0..num_candidates {
            let scores: Vec<f32> = component_scores
                .values()
                .map(|scores| scores.get(&idx).copied().unwrap_or(0.0))
                .collect();

            let final_score = match &self.config.score_combination {
                ScoreCombination::Average => {
                    if scores.is_empty() {
                        0.0
                    } else {
                        scores.iter().sum::<f32>() / scores.len() as f32
                    }
                }
                ScoreCombination::Weighted(weights) => scores
                    .iter()
                    .zip(weights.iter())
                    .map(|(score, weight)| score * weight)
                    .sum::<f32>(),
                ScoreCombination::Max => scores.iter().fold(0.0f32, |a, &b| a.max(b)),
                ScoreCombination::Min => scores.iter().fold(1.0f32, |a, &b| a.min(b)),
                ScoreCombination::Learned => {
                    // Would use a learned combination model
                    if scores.is_empty() {
                        0.0
                    } else {
                        scores.iter().sum::<f32>() / scores.len() as f32
                    }
                }
            };

            final_scores.insert(idx, final_score);
        }

        final_scores
    }

    /// Calculate confidence in the reranking decision
    fn calculate_confidence(
        &self,
        component_scores: &std::collections::HashMap<String, std::collections::HashMap<usize, f32>>,
        idx: usize,
    ) -> f32 {
        // Simple confidence calculation based on score agreement
        let scores: Vec<f32> = component_scores
            .values()
            .map(|scores| scores.get(&idx).copied().unwrap_or(0.0))
            .collect();

        if scores.len() < 2 {
            return 0.5; // Low confidence with only one scorer
        }

        // Calculate standard deviation as inverse confidence
        let mean = scores.iter().sum::<f32>() / scores.len() as f32;
        let variance = scores
            .iter()
            .map(|score| (score - mean).powi(2))
            .sum::<f32>()
            / scores.len() as f32;
        let std_dev = variance.sqrt();

        // Convert to confidence (lower std_dev = higher confidence)
        (1.0 - std_dev.min(1.0)).max(0.0)
    }

    /// Generate explanation for reranking decision
    fn generate_explanation(
        &self,
        component_scores: &std::collections::HashMap<String, std::collections::HashMap<usize, f32>>,
        idx: usize,
    ) -> Option<String> {
        let scores: Vec<(String, f32)> = component_scores
            .iter()
            .map(|(name, scores)| (name.clone(), scores.get(&idx).copied().unwrap_or(0.0)))
            .collect();

        if scores.is_empty() {
            return None;
        }

        let mut explanations = Vec::new();

        for (reranker, score) in &scores {
            match reranker.as_str() {
                "cross_encoder" => {
                    explanations.push(format!("Cross-encoder relevance: {:.3}", score));
                }
                "multi_signal" => {
                    explanations.push(format!("Multi-signal analysis: {:.3}", score));
                }
                "ltr" => {
                    explanations.push(format!("Learning-to-rank: {:.3}", score));
                }
                "neural" => {
                    explanations.push(format!("Neural reranker: {:.3}", score));
                }
                _ => {
                    explanations.push(format!("{}: {:.3}", reranker, score));
                }
            }
        }

        Some(explanations.join("; "))
    }

    /// Update configuration
    pub fn update_config(&mut self, config: AdvancedRerankingConfig) {
        self.config = config;
    }

    /// Get current configuration
    pub fn get_config(&self) -> &AdvancedRerankingConfig {
        &self.config
    }
}