oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Advanced Retrieval Strategies for RAG
//!
//! Implements state-of-the-art retrieval techniques including:
//! - Dense Passage Retrieval (DPR)
//! - ColBERT-style late interaction
//! - BM25+ with learned parameters
//! - Learning-to-Rank (LTR)
//! - Query expansion and reformulation
//! - Multi-hop reasoning retrieval

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

use super::types::{QueryContext, RagDocument, SearchResult};

/// Advanced retrieval strategies configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdvancedRetrievalConfig {
    /// Enable dense passage retrieval
    pub enable_dpr: bool,
    /// Enable ColBERT late interaction
    pub enable_colbert: bool,
    /// Enable BM25+ with learned params
    pub enable_bm25_plus: bool,
    /// Enable learning-to-rank
    pub enable_ltr: bool,
    /// Enable query expansion
    pub enable_query_expansion: bool,
    /// Enable multi-hop retrieval
    pub enable_multi_hop: bool,
    /// Maximum expansion terms
    pub max_expansion_terms: usize,
    /// Multi-hop depth
    pub multi_hop_depth: usize,
    /// LTR model path (optional)
    pub ltr_model_path: Option<String>,
}

impl Default for AdvancedRetrievalConfig {
    fn default() -> Self {
        Self {
            enable_dpr: true,
            enable_colbert: false, // Expensive, off by default
            enable_bm25_plus: true,
            enable_ltr: true,
            enable_query_expansion: true,
            enable_multi_hop: false,
            max_expansion_terms: 10,
            multi_hop_depth: 2,
            ltr_model_path: None,
        }
    }
}

/// Advanced retrieval engine
pub struct AdvancedRetriever {
    config: AdvancedRetrievalConfig,
    dpr_retriever: Option<DensePassageRetriever>,
    colbert_retriever: Option<ColBERTRetriever>,
    bm25_plus: BM25Plus,
    ltr_ranker: Option<LearningToRank>,
    query_expander: QueryExpander,
}

impl AdvancedRetriever {
    /// Create a new advanced retriever
    pub fn new(config: AdvancedRetrievalConfig) -> Self {
        info!("Initializing advanced retrieval strategies");

        let dpr_retriever = if config.enable_dpr {
            Some(DensePassageRetriever::new())
        } else {
            None
        };

        let colbert_retriever = if config.enable_colbert {
            Some(ColBERTRetriever::new())
        } else {
            None
        };

        let ltr_ranker = if config.enable_ltr {
            Some(LearningToRank::new(config.ltr_model_path.clone()))
        } else {
            None
        };

        Self {
            config,
            dpr_retriever,
            colbert_retriever,
            bm25_plus: BM25Plus::new(),
            ltr_ranker,
            query_expander: QueryExpander::new(),
        }
    }

    /// Retrieve documents using advanced strategies
    pub async fn retrieve(
        &self,
        query: &str,
        context: &QueryContext,
        candidate_docs: &[RagDocument],
    ) -> Result<Vec<SearchResult>> {
        let mut all_results = Vec::new();

        // Query expansion
        let expanded_query = if self.config.enable_query_expansion {
            self.query_expander.expand(query, context).await?
        } else {
            query.to_string()
        };

        debug!("Expanded query: {} -> {}", query, expanded_query);

        // Dense Passage Retrieval
        if let Some(ref dpr) = self.dpr_retriever {
            let dpr_results = dpr.retrieve(&expanded_query, candidate_docs).await?;
            all_results.extend(dpr_results);
        }

        // ColBERT late interaction
        if let Some(ref colbert) = self.colbert_retriever {
            let colbert_results = colbert.retrieve(&expanded_query, candidate_docs).await?;
            all_results.extend(colbert_results);
        }

        // BM25+ with learned parameters
        if self.config.enable_bm25_plus {
            let bm25_results = self
                .bm25_plus
                .retrieve(&expanded_query, candidate_docs)
                .await?;
            all_results.extend(bm25_results);
        }

        // Multi-hop retrieval
        if self.config.enable_multi_hop {
            let multi_hop_results = self
                .multi_hop_retrieve(&expanded_query, candidate_docs)
                .await?;
            all_results.extend(multi_hop_results);
        }

        // Learning-to-Rank reranking
        let final_results = if let Some(ref ltr) = self.ltr_ranker {
            ltr.rerank(all_results, &expanded_query, context).await?
        } else {
            // Default ranking by score
            let mut sorted = all_results;
            sorted.sort_by(|a, b| {
                b.score
                    .partial_cmp(&a.score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
            sorted
        };

        Ok(final_results)
    }

    /// Multi-hop retrieval for complex queries
    async fn multi_hop_retrieve(
        &self,
        query: &str,
        candidate_docs: &[RagDocument],
    ) -> Result<Vec<SearchResult>> {
        let mut hop_results = Vec::new();
        let mut current_query = query.to_string();

        for hop in 0..self.config.multi_hop_depth {
            debug!("Multi-hop retrieval: hop {}", hop);

            // Retrieve documents for current query
            let hop_docs = self
                .bm25_plus
                .retrieve(&current_query, candidate_docs)
                .await?;

            if hop_docs.is_empty() {
                break;
            }

            // Extract key information from top documents for next hop
            let top_doc = &hop_docs[0].document;
            current_query = format!(
                "{} {}",
                current_query,
                top_doc
                    .content
                    .split_whitespace()
                    .take(20)
                    .collect::<Vec<_>>()
                    .join(" ")
            );

            hop_results.extend(hop_docs);
        }

        Ok(hop_results)
    }
}

/// Dense Passage Retrieval (DPR)
pub struct DensePassageRetriever {
    // TODO: Integrate with actual embedding model
    // For now, use similarity-based approach
}

impl DensePassageRetriever {
    pub fn new() -> Self {
        Self {}
    }

    pub async fn retrieve(
        &self,
        query: &str,
        documents: &[RagDocument],
    ) -> Result<Vec<SearchResult>> {
        // Simplified DPR - in production, use actual dual encoder model
        let query_embedding = self.encode_query(query)?;

        let mut results = Vec::new();
        for doc in documents {
            let doc_embedding = self.encode_passage(&doc.content)?;
            let score = self.compute_similarity(&query_embedding, &doc_embedding);

            let mut result = SearchResult::new(doc.clone(), score);
            result.relevance_factors.push("DPR".to_string());
            results.push(result);
        }

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

        Ok(results)
    }

    fn encode_query(&self, query: &str) -> Result<Vec<f32>> {
        // Simplified encoding - use actual embedding model in production
        Ok(query
            .split_whitespace()
            .enumerate()
            .map(|(i, _)| (i as f32 + 1.0) / query.split_whitespace().count() as f32)
            .collect())
    }

    fn encode_passage(&self, passage: &str) -> Result<Vec<f32>> {
        // Simplified encoding - use actual embedding model in production
        Ok(passage
            .split_whitespace()
            .enumerate()
            .map(|(i, _)| (i as f32 + 1.0) / passage.split_whitespace().count() as f32)
            .collect())
    }

    fn compute_similarity(&self, query_emb: &[f32], doc_emb: &[f32]) -> f64 {
        // Cosine similarity
        let dot_product: f32 = query_emb
            .iter()
            .zip(doc_emb.iter())
            .map(|(a, b)| a * b)
            .sum();

        let query_norm: f32 = query_emb.iter().map(|x| x * x).sum::<f32>().sqrt();
        let doc_norm: f32 = doc_emb.iter().map(|x| x * x).sum::<f32>().sqrt();

        if query_norm > 0.0 && doc_norm > 0.0 {
            (dot_product / (query_norm * doc_norm)) as f64
        } else {
            0.0
        }
    }
}

impl Default for DensePassageRetriever {
    fn default() -> Self {
        Self::new()
    }
}

/// ColBERT-style late interaction retriever
pub struct ColBERTRetriever {
    // Token-level representations for late interaction
}

impl ColBERTRetriever {
    pub fn new() -> Self {
        Self {}
    }

    pub async fn retrieve(
        &self,
        query: &str,
        documents: &[RagDocument],
    ) -> Result<Vec<SearchResult>> {
        // Simplified ColBERT - use actual ColBERT model in production
        let query_tokens = self.tokenize_and_encode(query)?;

        let mut results = Vec::new();
        for doc in documents {
            let doc_tokens = self.tokenize_and_encode(&doc.content)?;
            let score = self.max_sim_interaction(&query_tokens, &doc_tokens);

            let mut result = SearchResult::new(doc.clone(), score);
            result.relevance_factors.push("ColBERT".to_string());
            results.push(result);
        }

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

        Ok(results)
    }

    fn tokenize_and_encode(&self, text: &str) -> Result<Vec<Vec<f32>>> {
        // Simplified tokenization and encoding
        Ok(text
            .split_whitespace()
            .map(|token| {
                token
                    .chars()
                    .enumerate()
                    .map(|(i, _)| (i as f32 + 1.0) / token.len() as f32)
                    .collect()
            })
            .collect())
    }

    fn max_sim_interaction(&self, query_tokens: &[Vec<f32>], doc_tokens: &[Vec<f32>]) -> f64 {
        // MaxSim late interaction
        let mut total_score = 0.0;

        for q_token in query_tokens {
            let mut max_sim = 0.0;
            for d_token in doc_tokens {
                let sim = self.cosine_similarity(q_token, d_token);
                if sim > max_sim {
                    max_sim = sim;
                }
            }
            total_score += max_sim;
        }

        total_score / query_tokens.len() as f64
    }

    fn cosine_similarity(&self, a: &[f32], b: &[f32]) -> f64 {
        if a.is_empty() || b.is_empty() {
            return 0.0;
        }

        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm_a > 0.0 && norm_b > 0.0 {
            (dot / (norm_a * norm_b)) as f64
        } else {
            0.0
        }
    }
}

impl Default for ColBERTRetriever {
    fn default() -> Self {
        Self::new()
    }
}

/// BM25+ with learned parameters
pub struct BM25Plus {
    k1: f64,    // Term frequency saturation parameter
    b: f64,     // Length normalization parameter
    delta: f64, // BM25+ delta parameter
}

impl BM25Plus {
    pub fn new() -> Self {
        Self {
            k1: 1.5,    // Learned parameter
            b: 0.75,    // Learned parameter
            delta: 0.5, // BM25+ improvement
        }
    }

    pub async fn retrieve(
        &self,
        query: &str,
        documents: &[RagDocument],
    ) -> Result<Vec<SearchResult>> {
        let query_terms: Vec<&str> = query.split_whitespace().collect();
        let avg_doc_length = self.compute_avg_doc_length(documents);

        let mut results = Vec::new();
        for doc in documents {
            let score = self.compute_bm25_plus_score(
                &query_terms,
                &doc.content,
                avg_doc_length,
                documents.len(),
            );

            let mut result = SearchResult::new(doc.clone(), score);
            result.relevance_factors.push("BM25+".to_string());
            results.push(result);
        }

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

        Ok(results)
    }

    fn compute_avg_doc_length(&self, documents: &[RagDocument]) -> f64 {
        if documents.is_empty() {
            return 0.0;
        }

        let total_length: usize = documents
            .iter()
            .map(|d| d.content.split_whitespace().count())
            .sum();

        total_length as f64 / documents.len() as f64
    }

    fn compute_bm25_plus_score(
        &self,
        query_terms: &[&str],
        document: &str,
        avg_doc_length: f64,
        corpus_size: usize,
    ) -> f64 {
        let doc_terms: Vec<&str> = document.split_whitespace().collect();
        let doc_length = doc_terms.len() as f64;

        let mut score = 0.0;

        for term in query_terms {
            let term_freq = doc_terms.iter().filter(|&&t| t == *term).count() as f64;

            // IDF component (simplified - should count document frequency)
            let idf = ((corpus_size as f64 + 1.0) / 2.0).ln();

            // BM25+ score
            let tf_component = (term_freq * (self.k1 + 1.0))
                / (term_freq + self.k1 * (1.0 - self.b + self.b * doc_length / avg_doc_length));

            // BM25+ delta improvement
            score += idf * (tf_component + self.delta);
        }

        score
    }
}

impl Default for BM25Plus {
    fn default() -> Self {
        Self::new()
    }
}

/// Learning-to-Rank (LTR) reranker
pub struct LearningToRank {
    model_path: Option<String>,
    feature_weights: HashMap<String, f64>,
}

impl LearningToRank {
    pub fn new(model_path: Option<String>) -> Self {
        // Initialize with default feature weights
        let mut feature_weights = HashMap::new();
        feature_weights.insert("semantic_score".to_string(), 0.4);
        feature_weights.insert("bm25_score".to_string(), 0.3);
        feature_weights.insert("query_term_coverage".to_string(), 0.2);
        feature_weights.insert("document_freshness".to_string(), 0.1);

        Self {
            model_path,
            feature_weights,
        }
    }

    pub async fn rerank(
        &self,
        results: Vec<SearchResult>,
        query: &str,
        _context: &QueryContext,
    ) -> Result<Vec<SearchResult>> {
        let mut reranked = results;

        // Extract features and compute LTR scores
        for result in &mut reranked {
            let features = self.extract_features(&result.document, query);
            let ltr_score = self.compute_ltr_score(&features);
            result.score = ltr_score;
        }

        // Re-sort by LTR scores
        reranked.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(reranked)
    }

    fn extract_features(&self, document: &RagDocument, query: &str) -> HashMap<String, f64> {
        let mut features = HashMap::new();

        // Query term coverage
        let query_terms: Vec<&str> = query.split_whitespace().collect();
        let doc_terms: Vec<&str> = document.content.split_whitespace().collect();
        let coverage = query_terms
            .iter()
            .filter(|&term| doc_terms.contains(term))
            .count() as f64
            / query_terms.len() as f64;
        features.insert("query_term_coverage".to_string(), coverage);

        // Document freshness (days since creation)
        let age_days = (chrono::Utc::now() - document.timestamp).num_days() as f64;
        let freshness = 1.0 / (1.0 + age_days / 365.0); // Decay over year
        features.insert("document_freshness".to_string(), freshness);

        // Semantic score (placeholder - would use actual embeddings)
        features.insert("semantic_score".to_string(), 0.5);

        // BM25 score (placeholder)
        features.insert("bm25_score".to_string(), 0.5);

        features
    }

    fn compute_ltr_score(&self, features: &HashMap<String, f64>) -> f64 {
        let mut score = 0.0;

        for (feature, value) in features {
            if let Some(&weight) = self.feature_weights.get(feature) {
                score += weight * value;
            }
        }

        score
    }
}

/// Query expansion engine
pub struct QueryExpander {
    expansion_terms: HashMap<String, Vec<String>>,
}

impl QueryExpander {
    pub fn new() -> Self {
        // Initialize with common expansions
        let mut expansion_terms = HashMap::new();

        // Example expansions
        expansion_terms.insert(
            "search".to_string(),
            vec!["find".to_string(), "look".to_string(), "query".to_string()],
        );
        expansion_terms.insert(
            "person".to_string(),
            vec![
                "people".to_string(),
                "individual".to_string(),
                "human".to_string(),
            ],
        );

        Self { expansion_terms }
    }

    pub async fn expand(&self, query: &str, _context: &QueryContext) -> Result<String> {
        let mut expanded_terms = Vec::new();

        for term in query.split_whitespace() {
            expanded_terms.push(term.to_string());

            // Add expansion terms if available
            if let Some(expansions) = self.expansion_terms.get(&term.to_lowercase()) {
                expanded_terms.extend(expansions.iter().take(2).cloned());
            }
        }

        Ok(expanded_terms.join(" "))
    }
}

impl Default for QueryExpander {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_bm25_plus_scoring() {
        let bm25 = BM25Plus::new();
        let docs = [
            RagDocument {
                id: "1".to_string(),
                content: "machine learning artificial intelligence".to_string(),
                embedding: None,
                metadata: HashMap::new(),
                timestamp: Utc::now(),
                source: "test".to_string(),
            },
            RagDocument {
                id: "2".to_string(),
                content: "deep learning neural networks".to_string(),
                embedding: None,
                metadata: HashMap::new(),
                timestamp: Utc::now(),
                source: "test".to_string(),
            },
        ];

        let score =
            bm25.compute_bm25_plus_score(&["machine", "learning"], &docs[0].content, 10.0, 2);

        assert!(score > 0.0);
    }

    #[tokio::test]
    async fn test_query_expansion() {
        let expander = QueryExpander::new();
        let expanded = expander
            .expand("search person", &QueryContext::new("test".to_string()))
            .await
            .expect("should succeed");

        assert!(expanded.contains("search"));
        assert!(expanded.contains("person"));
        assert!(expanded.split_whitespace().count() > 2);
    }
}