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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! # Query Expander
//!
//! Intelligent query expansion using synonyms, related terms, and semantic similarity.
//! Improves recall by adding relevant terms that might appear in target documents.

use crate::RragResult;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Query expander for adding related terms
pub struct QueryExpander {
    /// Configuration
    config: ExpansionConfig,

    /// Synonym dictionary
    synonyms: HashMap<String, Vec<String>>,

    /// Related terms dictionary
    related_terms: HashMap<String, Vec<String>>,

    /// Domain-specific expansions
    domain_expansions: HashMap<String, HashMap<String, Vec<String>>>,
}

/// Configuration for query expansion
#[derive(Debug, Clone)]
pub struct ExpansionConfig {
    /// Maximum number of synonyms to add
    pub max_synonyms: usize,

    /// Maximum number of related terms to add
    pub max_related_terms: usize,

    /// Enable synonym expansion
    pub enable_synonyms: bool,

    /// Enable related term expansion
    pub enable_related_terms: bool,

    /// Enable semantic expansion
    pub enable_semantic_expansion: bool,

    /// Enable domain-specific expansion
    pub enable_domain_expansion: bool,

    /// Minimum relevance score for expansions
    pub min_relevance_score: f32,
}

impl Default for ExpansionConfig {
    fn default() -> Self {
        Self {
            max_synonyms: 3,
            max_related_terms: 2,
            enable_synonyms: true,
            enable_related_terms: true,
            enable_semantic_expansion: true,
            enable_domain_expansion: true,
            min_relevance_score: 0.6,
        }
    }
}

/// Expansion strategies
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExpansionStrategy {
    /// Add synonyms
    Synonyms,
    /// Add related terms
    RelatedTerms,
    /// Semantic expansion using embeddings
    Semantic,
    /// Domain-specific expansion
    DomainSpecific,
    /// Contextual expansion
    Contextual,
}

/// Result of query expansion
#[derive(Debug, Clone)]
pub struct ExpansionResult {
    /// Original query
    pub original_query: String,

    /// Expanded query
    pub expanded_query: String,

    /// Terms that were added
    pub added_terms: Vec<String>,

    /// Expansion strategy used
    pub expansion_type: ExpansionStrategy,

    /// Confidence score (0.0 to 1.0)
    pub confidence: f32,

    /// Relevance scores for added terms
    pub term_scores: HashMap<String, f32>,
}

impl QueryExpander {
    /// Create a new query expander
    pub fn new(config: ExpansionConfig) -> Self {
        let synonyms = Self::init_synonyms();
        let related_terms = Self::init_related_terms();
        let domain_expansions = Self::init_domain_expansions();

        Self {
            config,
            synonyms,
            related_terms,
            domain_expansions,
        }
    }

    /// Expand a query using all enabled strategies
    pub async fn expand(&self, query: &str) -> RragResult<Vec<ExpansionResult>> {
        let mut results = Vec::new();

        // Tokenize query
        let tokens = self.tokenize(query);

        // Apply synonym expansion
        if self.config.enable_synonyms {
            if let Some(result) = self.expand_with_synonyms(query, &tokens) {
                if result.confidence >= self.config.min_relevance_score {
                    results.push(result);
                }
            }
        }

        // Apply related terms expansion
        if self.config.enable_related_terms {
            if let Some(result) = self.expand_with_related_terms(query, &tokens) {
                if result.confidence >= self.config.min_relevance_score {
                    results.push(result);
                }
            }
        }

        // Apply semantic expansion
        if self.config.enable_semantic_expansion {
            if let Some(result) = self.expand_semantically(query, &tokens) {
                if result.confidence >= self.config.min_relevance_score {
                    results.push(result);
                }
            }
        }

        // Apply domain-specific expansion
        if self.config.enable_domain_expansion {
            let domain_results = self.expand_domain_specific(query, &tokens);
            results.extend(
                domain_results
                    .into_iter()
                    .filter(|r| r.confidence >= self.config.min_relevance_score),
            );
        }

        Ok(results)
    }

    /// Expand query with synonyms
    fn expand_with_synonyms(&self, query: &str, tokens: &[String]) -> Option<ExpansionResult> {
        let mut added_terms = Vec::new();
        let mut term_scores = HashMap::new();

        for token in tokens {
            if let Some(synonyms) = self.synonyms.get(&token.to_lowercase()) {
                for synonym in synonyms.iter().take(self.config.max_synonyms) {
                    if !tokens
                        .iter()
                        .any(|t| t.to_lowercase() == synonym.to_lowercase())
                    {
                        added_terms.push(synonym.clone());
                        term_scores.insert(synonym.clone(), 0.8); // Fixed score for synonyms
                    }
                }
            }
        }

        if !added_terms.is_empty() {
            let expanded_query = format!("{} {}", query, added_terms.join(" "));
            Some(ExpansionResult {
                original_query: query.to_string(),
                expanded_query,
                added_terms,
                expansion_type: ExpansionStrategy::Synonyms,
                confidence: 0.8,
                term_scores,
            })
        } else {
            None
        }
    }

    /// Expand query with related terms
    fn expand_with_related_terms(&self, query: &str, tokens: &[String]) -> Option<ExpansionResult> {
        let mut added_terms = Vec::new();
        let mut term_scores = HashMap::new();

        for token in tokens {
            if let Some(related) = self.related_terms.get(&token.to_lowercase()) {
                for term in related.iter().take(self.config.max_related_terms) {
                    if !tokens
                        .iter()
                        .any(|t| t.to_lowercase() == term.to_lowercase())
                    {
                        added_terms.push(term.clone());
                        term_scores.insert(term.clone(), 0.7); // Slightly lower than synonyms
                    }
                }
            }
        }

        if !added_terms.is_empty() {
            let expanded_query = format!("{} {}", query, added_terms.join(" "));
            Some(ExpansionResult {
                original_query: query.to_string(),
                expanded_query,
                added_terms,
                expansion_type: ExpansionStrategy::RelatedTerms,
                confidence: 0.7,
                term_scores,
            })
        } else {
            None
        }
    }

    /// Expand query semantically
    fn expand_semantically(&self, query: &str, _tokens: &[String]) -> Option<ExpansionResult> {
        // For now, implement a simple semantic expansion
        // In production, this would use word embeddings or language models
        let semantic_expansions = self.get_semantic_expansions(query);

        if !semantic_expansions.is_empty() {
            let mut term_scores = HashMap::new();
            for term in &semantic_expansions {
                term_scores.insert(term.clone(), 0.6);
            }

            let expanded_query = format!("{} {}", query, semantic_expansions.join(" "));
            Some(ExpansionResult {
                original_query: query.to_string(),
                expanded_query,
                added_terms: semantic_expansions,
                expansion_type: ExpansionStrategy::Semantic,
                confidence: 0.6,
                term_scores,
            })
        } else {
            None
        }
    }

    /// Apply domain-specific expansions
    fn expand_domain_specific(&self, query: &str, tokens: &[String]) -> Vec<ExpansionResult> {
        let mut results = Vec::new();

        // Detect domain
        let domain = self.detect_domain(tokens);

        if let Some(domain_dict) = self.domain_expansions.get(&domain) {
            for token in tokens {
                if let Some(expansions) = domain_dict.get(&token.to_lowercase()) {
                    let mut term_scores = HashMap::new();
                    for term in expansions {
                        term_scores.insert(term.clone(), 0.75);
                    }

                    let expanded_query = format!("{} {}", query, expansions.join(" "));
                    results.push(ExpansionResult {
                        original_query: query.to_string(),
                        expanded_query,
                        added_terms: expansions.clone(),
                        expansion_type: ExpansionStrategy::DomainSpecific,
                        confidence: 0.75,
                        term_scores,
                    });
                }
            }
        }

        results
    }

    /// Get semantic expansions for a query
    fn get_semantic_expansions(&self, query: &str) -> Vec<String> {
        // Simple rule-based semantic expansion
        // In production, use proper semantic models
        let mut expansions = Vec::new();

        let query_lower = query.to_lowercase();

        if query_lower.contains("learn") || query_lower.contains("study") {
            expansions.extend_from_slice(&["education", "training", "tutorial"]);
        }

        if query_lower.contains("build") || query_lower.contains("create") {
            expansions.extend_from_slice(&["develop", "construct", "implement"]);
        }

        if query_lower.contains("fast") || query_lower.contains("quick") {
            expansions.extend_from_slice(&["rapid", "efficient", "performance"]);
        }

        if query_lower.contains("problem") || query_lower.contains("issue") {
            expansions.extend_from_slice(&["solution", "fix", "troubleshoot"]);
        }

        expansions.into_iter().map(String::from).collect()
    }

    /// Detect the domain of a query
    fn detect_domain(&self, tokens: &[String]) -> String {
        let tech_terms = [
            "code",
            "programming",
            "software",
            "api",
            "database",
            "algorithm",
        ];
        let business_terms = ["market", "sales", "revenue", "customer", "profit"];
        let science_terms = ["research", "study", "experiment", "theory", "analysis"];

        let tokens_lower: Vec<String> = tokens.iter().map(|t| t.to_lowercase()).collect();

        let tech_count = tech_terms
            .iter()
            .filter(|&&term| tokens_lower.iter().any(|t| t.contains(term)))
            .count();
        let business_count = business_terms
            .iter()
            .filter(|&&term| tokens_lower.iter().any(|t| t.contains(term)))
            .count();
        let science_count = science_terms
            .iter()
            .filter(|&&term| tokens_lower.iter().any(|t| t.contains(term)))
            .count();

        if tech_count > business_count && tech_count > science_count {
            "technology".to_string()
        } else if business_count > science_count {
            "business".to_string()
        } else if science_count > 0 {
            "science".to_string()
        } else {
            "general".to_string()
        }
    }

    /// Tokenize query into individual terms
    fn tokenize(&self, query: &str) -> Vec<String> {
        query
            .to_lowercase()
            .split_whitespace()
            .map(|s| s.trim_matches(|c: char| !c.is_alphanumeric()))
            .filter(|s| !s.is_empty())
            .filter(|s| s.len() > 2) // Filter out very short words
            .map(String::from)
            .collect()
    }

    /// Initialize synonym dictionary
    fn init_synonyms() -> HashMap<String, Vec<String>> {
        let mut synonyms = HashMap::new();

        // Technology synonyms
        synonyms.insert(
            "fast".to_string(),
            vec![
                "quick".to_string(),
                "rapid".to_string(),
                "speedy".to_string(),
            ],
        );
        synonyms.insert(
            "big".to_string(),
            vec![
                "large".to_string(),
                "huge".to_string(),
                "massive".to_string(),
            ],
        );
        synonyms.insert(
            "small".to_string(),
            vec![
                "tiny".to_string(),
                "little".to_string(),
                "compact".to_string(),
            ],
        );
        synonyms.insert(
            "good".to_string(),
            vec![
                "excellent".to_string(),
                "great".to_string(),
                "quality".to_string(),
            ],
        );
        synonyms.insert(
            "bad".to_string(),
            vec![
                "poor".to_string(),
                "terrible".to_string(),
                "awful".to_string(),
            ],
        );
        synonyms.insert(
            "simple".to_string(),
            vec![
                "easy".to_string(),
                "basic".to_string(),
                "straightforward".to_string(),
            ],
        );
        synonyms.insert(
            "difficult".to_string(),
            vec![
                "hard".to_string(),
                "challenging".to_string(),
                "complex".to_string(),
            ],
        );
        synonyms.insert(
            "method".to_string(),
            vec![
                "approach".to_string(),
                "technique".to_string(),
                "way".to_string(),
            ],
        );
        synonyms.insert(
            "create".to_string(),
            vec![
                "build".to_string(),
                "make".to_string(),
                "develop".to_string(),
            ],
        );
        synonyms.insert(
            "use".to_string(),
            vec![
                "utilize".to_string(),
                "employ".to_string(),
                "apply".to_string(),
            ],
        );

        synonyms
    }

    /// Initialize related terms dictionary
    fn init_related_terms() -> HashMap<String, Vec<String>> {
        let mut related = HashMap::new();

        // Technology related terms
        related.insert(
            "programming".to_string(),
            vec![
                "coding".to_string(),
                "development".to_string(),
                "software".to_string(),
            ],
        );
        related.insert(
            "database".to_string(),
            vec![
                "data".to_string(),
                "storage".to_string(),
                "query".to_string(),
            ],
        );
        related.insert(
            "algorithm".to_string(),
            vec![
                "logic".to_string(),
                "computation".to_string(),
                "optimization".to_string(),
            ],
        );
        related.insert(
            "machine".to_string(),
            vec![
                "learning".to_string(),
                "ai".to_string(),
                "model".to_string(),
            ],
        );
        related.insert(
            "web".to_string(),
            vec![
                "website".to_string(),
                "internet".to_string(),
                "browser".to_string(),
            ],
        );
        related.insert(
            "api".to_string(),
            vec![
                "interface".to_string(),
                "endpoint".to_string(),
                "service".to_string(),
            ],
        );
        related.insert(
            "security".to_string(),
            vec![
                "encryption".to_string(),
                "authentication".to_string(),
                "protection".to_string(),
            ],
        );
        related.insert(
            "performance".to_string(),
            vec![
                "speed".to_string(),
                "optimization".to_string(),
                "efficiency".to_string(),
            ],
        );

        related
    }

    /// Initialize domain-specific expansions
    fn init_domain_expansions() -> HashMap<String, HashMap<String, Vec<String>>> {
        let mut domains = HashMap::new();

        // Technology domain
        let mut tech_expansions = HashMap::new();
        tech_expansions.insert(
            "ml".to_string(),
            vec![
                "machine learning".to_string(),
                "artificial intelligence".to_string(),
            ],
        );
        tech_expansions.insert(
            "ai".to_string(),
            vec![
                "artificial intelligence".to_string(),
                "machine learning".to_string(),
                "neural networks".to_string(),
            ],
        );
        tech_expansions.insert(
            "nlp".to_string(),
            vec![
                "natural language processing".to_string(),
                "text analysis".to_string(),
            ],
        );
        tech_expansions.insert(
            "api".to_string(),
            vec![
                "rest".to_string(),
                "endpoint".to_string(),
                "microservice".to_string(),
            ],
        );
        tech_expansions.insert(
            "db".to_string(),
            vec![
                "database".to_string(),
                "sql".to_string(),
                "storage".to_string(),
            ],
        );

        domains.insert("technology".to_string(), tech_expansions);

        // Business domain
        let mut business_expansions = HashMap::new();
        business_expansions.insert(
            "roi".to_string(),
            vec![
                "return on investment".to_string(),
                "profitability".to_string(),
            ],
        );
        business_expansions.insert(
            "kpi".to_string(),
            vec![
                "key performance indicator".to_string(),
                "metrics".to_string(),
            ],
        );
        business_expansions.insert(
            "b2b".to_string(),
            vec!["business to business".to_string(), "enterprise".to_string()],
        );
        business_expansions.insert(
            "b2c".to_string(),
            vec!["business to consumer".to_string(), "retail".to_string()],
        );

        domains.insert("business".to_string(), business_expansions);

        domains
    }
}

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

    #[tokio::test]
    async fn test_synonym_expansion() {
        let expander = QueryExpander::new(ExpansionConfig::default());

        let results = expander.expand("fast algorithm").await.unwrap();

        let synonym_result = results
            .iter()
            .find(|r| r.expansion_type == ExpansionStrategy::Synonyms);
        assert!(synonym_result.is_some());

        let result = synonym_result.unwrap();
        assert!(result.expanded_query.contains("quick") || result.expanded_query.contains("rapid"));
    }

    #[tokio::test]
    async fn test_domain_expansion() {
        let expander = QueryExpander::new(ExpansionConfig::default());

        let results = expander.expand("ML model").await.unwrap();

        let domain_result = results
            .iter()
            .find(|r| r.expansion_type == ExpansionStrategy::DomainSpecific);
        assert!(domain_result.is_some());

        let result = domain_result.unwrap();
        assert!(result.expanded_query.contains("machine learning"));
    }
}