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
//! Multi-stage retrieval engine for the RAG system
//!
//! Implements semantic search, graph traversal, hybrid retrieval,
//! and intelligent document ranking and filtering.

use super::quantum::QuantumRanker;
use super::types::*;
use anyhow::Result;
use oxirs_core::Store;
use oxirs_vec::{Vector, VectorIndex};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info};

/// Multi-stage retrieval engine
pub struct MultiStageRetrieval {
    config: RetrievalConfig,
    semantic_retriever: SemanticRetriever,
    graph_retriever: GraphRetriever,
    hybrid_retriever: HybridRetriever,
    result_cache: Arc<RwLock<HashMap<String, Vec<SearchResult>>>>,
}

impl MultiStageRetrieval {
    /// Create a new multi-stage retrieval engine
    pub fn new(rag_config: &super::RAGConfig) -> Self {
        let config = RetrievalConfig {
            max_documents: rag_config.retrieval.max_results,
            similarity_threshold: rag_config.retrieval.similarity_threshold as f64,
            enable_reranking: true,
            reranking_model: None,
            enable_temporal_filtering: true,
            temporal_window: Some(std::time::Duration::from_secs(365 * 24 * 3600)),
        };

        Self {
            semantic_retriever: SemanticRetriever::new(),
            graph_retriever: GraphRetriever::new(),
            hybrid_retriever: HybridRetriever::new(),
            result_cache: Arc::new(RwLock::new(HashMap::new())),
            config,
        }
    }

    /// Perform multi-stage retrieval
    pub async fn retrieve(
        &self,
        query: &str,
        context: &QueryContext,
        vector_index: &Arc<dyn VectorIndex>,
        store: &Arc<dyn Store>,
    ) -> Result<Vec<SearchResult>> {
        let start_time = std::time::Instant::now();

        // Check cache first
        let cache_key = format!("{}:{}", query, context.session_id);
        let cache = self.result_cache.read().await;
        if let Some(cached_results) = cache.get(&cache_key) {
            debug!("Cache hit for query: {}", query);
            return Ok(cached_results.clone());
        }

        let mut all_results = Vec::new();

        // Stage 1: Semantic retrieval
        info!("Starting semantic retrieval for query: {}", query);
        let semantic_results = self
            .semantic_retriever
            .retrieve(query, context, vector_index)
            .await?;
        all_results.extend(semantic_results);

        // Stage 2: Graph traversal (if enabled)
        if context.query_intent != QueryIntent::Information {
            info!("Starting graph traversal");
            let graph_results = self.graph_retriever.retrieve(query, context, store).await?;
            all_results.extend(graph_results);
        }

        // Stage 3: Hybrid search (if enabled)
        info!("Starting hybrid retrieval");
        let hybrid_results = self
            .hybrid_retriever
            .retrieve(query, context, vector_index, store)
            .await?;
        all_results.extend(hybrid_results);

        // Stage 4: Deduplication and ranking
        let final_results = self.deduplicate_and_rank(all_results, query).await?;

        // Stage 5: Apply filters
        let filtered_results = self.apply_filters(final_results, context).await?;

        // Cache results
        let mut cache = self.result_cache.write().await;
        cache.insert(cache_key, filtered_results.clone());
        // Keep cache size reasonable
        if cache.len() > 1000 {
            cache.clear();
        }

        let retrieval_time = start_time.elapsed();
        info!(
            "Multi-stage retrieval completed in {:?}, found {} documents",
            retrieval_time,
            filtered_results.len()
        );

        Ok(filtered_results)
    }

    /// Update configuration
    pub fn update_config(&mut self, rag_config: &super::RAGConfig) {
        self.config.max_documents = rag_config.retrieval.max_results;
        self.config.similarity_threshold = rag_config.retrieval.similarity_threshold as f64;
    }

    /// Deduplicate and rank results
    async fn deduplicate_and_rank(
        &self,
        mut results: Vec<SearchResult>,
        query: &str,
    ) -> Result<Vec<SearchResult>> {
        // Deduplicate by document ID
        let mut seen_ids = HashSet::new();
        results.retain(|result| seen_ids.insert(result.document.id.clone()));

        // Apply quantum-inspired ranking if available
        let query_complexity = query.split_whitespace().count() as f64 / 10.0;
        if query_complexity > 0.5 {
            let documents: Vec<RagDocument> = results.iter().map(|r| r.document.clone()).collect();

            let mut quantum_ranker = QuantumRanker::new(query_complexity);
            let quantum_results = quantum_ranker.rank_documents(&documents);

            // Convert quantum results back to search results
            results = quantum_results
                .into_iter()
                .map(|qr| qr.to_classical())
                .collect();
        } else {
            // Standard ranking by score
            results.sort_by(|a, b| {
                b.score
                    .partial_cmp(&a.score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
        }

        // Limit to max documents
        results.truncate(self.config.max_documents);

        Ok(results)
    }

    /// Apply various filters to results
    async fn apply_filters(
        &self,
        mut results: Vec<SearchResult>,
        context: &QueryContext,
    ) -> Result<Vec<SearchResult>> {
        // Filter by similarity threshold
        results.retain(|result| result.score >= self.config.similarity_threshold);

        // Apply temporal filtering if enabled
        if self.config.enable_temporal_filtering {
            if let Some(window) = self.config.temporal_window {
                let cutoff = chrono::Utc::now() - chrono::Duration::from_std(window)?;
                results.retain(|result| result.document.timestamp >= cutoff);
            }
        }

        // Apply domain constraints
        if !context.domain_constraints.is_empty() {
            results.retain(|result| {
                context.domain_constraints.iter().any(|constraint| {
                    result.document.content.contains(constraint)
                        || result
                            .document
                            .metadata
                            .values()
                            .any(|v| v.contains(constraint))
                })
            });
        }

        Ok(results)
    }
}

/// Semantic retrieval using vector similarity
pub struct SemanticRetriever {
    embedding_cache: Arc<RwLock<HashMap<String, Vec<f32>>>>,
}

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

impl SemanticRetriever {
    pub fn new() -> Self {
        Self {
            embedding_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn retrieve(
        &self,
        query: &str,
        _context: &QueryContext,
        vector_index: &Arc<dyn VectorIndex>,
    ) -> Result<Vec<SearchResult>> {
        // Get query embedding (simplified - would use actual embedding model)
        let query_embedding_vec = self.get_query_embedding(query).await?;
        let query_embedding = Vector::new(query_embedding_vec);

        // Perform vector search
        let vector_results = vector_index.search_knn(&query_embedding, 20)?;

        // Convert to search results
        let search_results = vector_results
            .into_iter()
            .map(|(id, score)| {
                SearchResult::new(
                    RagDocument::new(
                        id.clone(),
                        format!("Content for document {id}"),
                        "vector_index".to_string(),
                    ),
                    score.into(),
                )
                .add_relevance_factor("semantic_similarity".to_string())
            })
            .collect();

        Ok(search_results)
    }

    async fn get_query_embedding(&self, query: &str) -> Result<Vec<f32>> {
        // Check cache first
        let cache = self.embedding_cache.read().await;
        {
            if let Some(embedding) = cache.get(query) {
                return Ok(embedding.clone());
            }
        }

        // Generate embedding (simplified implementation)
        let embedding = query
            .chars()
            .map(|c| (c as u8 as f32) / 255.0)
            .take(128)
            .collect::<Vec<f32>>();

        // Pad to fixed size
        let mut padded = embedding;
        padded.resize(128, 0.0);

        // Cache the result
        let mut cache = self.embedding_cache.write().await;
        {
            cache.insert(query.to_string(), padded.clone());
        }

        Ok(padded)
    }
}

/// Graph-based retrieval using SPARQL queries
pub struct GraphRetriever {
    query_cache: Arc<RwLock<HashMap<String, Vec<SearchResult>>>>,
}

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

impl GraphRetriever {
    pub fn new() -> Self {
        Self {
            query_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn retrieve(
        &self,
        query: &str,
        context: &QueryContext,
        _store: &Arc<dyn Store>,
    ) -> Result<Vec<SearchResult>> {
        // Check cache
        let cache_key = format!("graph:{}:{}", query, context.session_id);
        let cache = self.query_cache.read().await;
        {
            if let Some(cached) = cache.get(&cache_key) {
                return Ok(cached.clone());
            }
        }

        // Generate SPARQL query based on natural language query
        let sparql_query = self.generate_sparql_query(query, context)?;

        // Execute query (simplified implementation)
        let results = self.execute_sparql_query(&sparql_query).await?;

        // Cache results
        {
            let mut cache = self.query_cache.write().await;
            cache.insert(cache_key, results.clone());
        }

        Ok(results)
    }

    fn generate_sparql_query(&self, query: &str, _context: &QueryContext) -> Result<String> {
        // Simplified SPARQL generation
        let query_terms: Vec<&str> = query.split_whitespace().collect();

        let sparql = format!(
            r#"
            SELECT ?subject ?predicate ?object WHERE {{
                ?subject ?predicate ?object .
                FILTER(contains(lcase(str(?object)), "{}"))
            }}
            LIMIT 10
            "#,
            query_terms.join(" ").to_lowercase()
        );

        Ok(sparql)
    }

    async fn execute_sparql_query(&self, _query: &str) -> Result<Vec<SearchResult>> {
        // Placeholder implementation - would execute actual SPARQL query
        Ok(vec![SearchResult::new(
            RagDocument::new(
                "graph_result_1".to_string(),
                "Graph-based result content".to_string(),
                "knowledge_graph".to_string(),
            ),
            0.8,
        )
        .add_relevance_factor("graph_connectivity".to_string())])
    }
}

/// Hybrid retrieval combining multiple approaches
pub struct HybridRetriever {
    fusion_weights: HashMap<String, f64>,
}

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

impl HybridRetriever {
    pub fn new() -> Self {
        let mut weights = HashMap::new();
        weights.insert("semantic".to_string(), 0.6);
        weights.insert("keyword".to_string(), 0.3);
        weights.insert("graph".to_string(), 0.1);

        Self {
            fusion_weights: weights,
        }
    }

    pub async fn retrieve(
        &self,
        query: &str,
        context: &QueryContext,
        vector_index: &Arc<dyn VectorIndex>,
        _store: &Arc<dyn Store>,
    ) -> Result<Vec<SearchResult>> {
        let mut combined_results = Vec::new();

        // Keyword-based search
        let keyword_results = self.keyword_search(query, context).await?;
        combined_results.extend(keyword_results);

        // Additional semantic search with different parameters
        let additional_semantic = self.enhanced_semantic_search(query, vector_index).await?;
        combined_results.extend(additional_semantic);

        // Apply fusion scoring
        self.apply_fusion_scoring(&mut combined_results);

        Ok(combined_results)
    }

    async fn keyword_search(
        &self,
        query: &str,
        _context: &QueryContext,
    ) -> Result<Vec<SearchResult>> {
        // Simplified keyword search implementation
        let keywords: Vec<&str> = query.split_whitespace().collect();

        let results = keywords
            .iter()
            .enumerate()
            .map(|(i, &keyword)| {
                SearchResult::new(
                    RagDocument::new(
                        format!("keyword_result_{i}"),
                        format!("Document containing keyword: {keyword}"),
                        "keyword_search".to_string(),
                    ),
                    0.7 - (i as f64 * 0.1),
                )
                .add_relevance_factor(format!("keyword_match: {keyword}"))
            })
            .collect();

        Ok(results)
    }

    async fn enhanced_semantic_search(
        &self,
        query: &str,
        vector_index: &Arc<dyn VectorIndex>,
    ) -> Result<Vec<SearchResult>> {
        // Enhanced semantic search with query expansion
        let expanded_query = format!("{query} related context information");

        // Use semantic retriever logic (simplified)
        let semantic_retriever = SemanticRetriever::new();
        let dummy_context = QueryContext::new("hybrid_search".to_string());

        semantic_retriever
            .retrieve(&expanded_query, &dummy_context, vector_index)
            .await
    }

    fn apply_fusion_scoring(&self, results: &mut [SearchResult]) {
        for result in results.iter_mut() {
            let mut fused_score = 0.0;
            let mut total_weight = 0.0;

            // Apply weights based on retrieval method
            for factor in &result.relevance_factors {
                if factor.contains("semantic") {
                    fused_score +=
                        result.score * self.fusion_weights.get("semantic").unwrap_or(&0.0);
                    total_weight += self.fusion_weights.get("semantic").unwrap_or(&0.0);
                } else if factor.contains("keyword") {
                    fused_score +=
                        result.score * self.fusion_weights.get("keyword").unwrap_or(&0.0);
                    total_weight += self.fusion_weights.get("keyword").unwrap_or(&0.0);
                } else if factor.contains("graph") {
                    fused_score += result.score * self.fusion_weights.get("graph").unwrap_or(&0.0);
                    total_weight += self.fusion_weights.get("graph").unwrap_or(&0.0);
                }
            }

            if total_weight > 0.0 {
                result.score = fused_score / total_weight;
            }
        }

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

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

    #[test]
    fn test_semantic_retriever_creation() {
        let retriever = SemanticRetriever::new();
        assert!(retriever.embedding_cache.try_read().is_ok());
    }

    #[test]
    fn test_graph_retriever_sparql_generation() {
        let retriever = GraphRetriever::new();
        let context = QueryContext::new("test_session".to_string());
        let sparql = retriever.generate_sparql_query("test query", &context);
        assert!(sparql.is_ok());
        assert!(sparql.expect("should succeed").contains("SELECT"));
    }

    #[test]
    fn test_hybrid_retriever_weights() {
        let retriever = HybridRetriever::new();
        assert_eq!(retriever.fusion_weights.len(), 3);
        assert!(retriever.fusion_weights.contains_key("semantic"));
    }
}