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
//! # Graph-Based Retriever
//!
//! Main retriever implementation that integrates graph-based algorithms with traditional retrieval methods.

use super::{
    algorithms::GraphAlgorithms,
    query_expansion::{ExpansionOptions, ExpansionStrategy, GraphQueryExpander, QueryExpander},
    storage::GraphStorage,
    GraphNode, KnowledgeGraph,
};
use crate::{
    retrieval_core::{IndexStats, QueryType},
    Document, DocumentChunk, Embedding, Retriever, RragResult, SearchQuery, SearchResult,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Graph-based retriever that combines traditional and graph-based search
pub struct GraphRetriever {
    /// Knowledge graph (using RwLock for interior mutability)
    graph: tokio::sync::RwLock<KnowledgeGraph>,

    /// Graph storage backend
    storage: tokio::sync::RwLock<Box<dyn GraphStorage>>,

    /// Query expander (using RwLock for interior mutability)
    query_expander: tokio::sync::RwLock<GraphQueryExpander>,

    /// Configuration
    config: GraphRetrievalConfig,

    /// PageRank scores cache
    pagerank_cache: tokio::sync::RwLock<Option<HashMap<String, f32>>>,

    /// Entity to document mapping (using RwLock for interior mutability)
    entity_document_map: tokio::sync::RwLock<HashMap<String, HashSet<String>>>,
}

/// Graph retrieval configuration
#[derive(Debug, Clone)]
pub struct GraphRetrievalConfig {
    /// Enable query expansion
    pub enable_query_expansion: bool,

    /// Enable PageRank scoring
    pub enable_pagerank_scoring: bool,

    /// Enable path-based retrieval
    pub enable_path_based_retrieval: bool,

    /// Weight for graph-based scores vs traditional similarity
    pub graph_weight: f32,

    /// Weight for traditional similarity scores
    pub similarity_weight: f32,

    /// Maximum number of graph hops for retrieval
    pub max_graph_hops: usize,

    /// Minimum graph score threshold
    pub min_graph_score: f32,

    /// Query expansion configuration
    pub expansion_options: ExpansionOptions,

    /// PageRank configuration
    pub pagerank_config: super::algorithms::PageRankConfig,

    /// Enable result diversification
    pub enable_diversification: bool,

    /// Diversification factor (0.0 to 1.0)
    pub diversification_factor: f32,
}

impl Default for GraphRetrievalConfig {
    fn default() -> Self {
        Self {
            enable_query_expansion: true,
            enable_pagerank_scoring: true,
            enable_path_based_retrieval: true,
            graph_weight: 0.4,
            similarity_weight: 0.6,
            max_graph_hops: 3,
            min_graph_score: 0.1,
            expansion_options: ExpansionOptions {
                strategies: vec![
                    ExpansionStrategy::Semantic,
                    ExpansionStrategy::Similarity,
                    ExpansionStrategy::CoOccurrence,
                ],
                max_terms: Some(10),
                min_confidence: 0.3,
                ..Default::default()
            },
            pagerank_config: super::algorithms::PageRankConfig::default(),
            enable_diversification: true,
            diversification_factor: 0.3,
        }
    }
}

/// Graph search result with additional graph-specific information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphSearchResult {
    /// Base search result
    pub search_result: SearchResult,

    /// Graph-based score
    pub graph_score: f32,

    /// PageRank score of associated entities
    pub pagerank_score: f32,

    /// Related entities found in the content
    pub related_entities: Vec<String>,

    /// Graph paths that led to this result
    pub graph_paths: Vec<GraphPath>,

    /// Expanded query terms that matched
    pub matched_expansions: Vec<String>,
}

/// Graph path information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphPath {
    /// Node IDs in the path
    pub nodes: Vec<String>,

    /// Path score
    pub score: f32,

    /// Path type/description
    pub path_type: String,

    /// Path length
    pub length: usize,
}

impl GraphRetriever {
    /// Create a new graph retriever
    pub fn new(
        graph: KnowledgeGraph,
        storage: Box<dyn GraphStorage>,
        config: GraphRetrievalConfig,
    ) -> RragResult<Self> {
        let query_expander = GraphQueryExpander::new(
            graph.clone(),
            super::query_expansion::ExpansionConfig::default(),
        );

        let mut entity_document_map = HashMap::new();

        // Build entity-document mapping
        for (_, node) in &graph.nodes {
            for doc_id in &node.source_documents {
                entity_document_map
                    .entry(node.id.clone())
                    .or_insert_with(HashSet::new)
                    .insert(doc_id.clone());
            }
        }

        let retriever = Self {
            graph: tokio::sync::RwLock::new(graph),
            storage: tokio::sync::RwLock::new(storage),
            query_expander: tokio::sync::RwLock::new(query_expander),
            config,
            pagerank_cache: tokio::sync::RwLock::new(None),
            entity_document_map: tokio::sync::RwLock::new(entity_document_map),
        };

        Ok(retriever)
    }

    /// Update the knowledge graph
    pub async fn update_graph(&self, graph: KnowledgeGraph) -> RragResult<()> {
        *self.graph.write().await = graph.clone();
        self.query_expander
            .write()
            .await
            .update_graph(graph.clone())
            .await;

        // Rebuild entity-document mapping
        let mut entity_map = self.entity_document_map.write().await;
        entity_map.clear();
        for (_, node) in &graph.nodes {
            for doc_id in &node.source_documents {
                entity_map
                    .entry(node.id.clone())
                    .or_insert_with(HashSet::new)
                    .insert(doc_id.clone());
            }
        }

        // Clear PageRank cache
        *self.pagerank_cache.write().await = None;

        // Update storage
        self.storage.write().await.store_graph(&graph).await?;

        Ok(())
    }

    /// Get or compute PageRank scores
    async fn get_pagerank_scores(&self) -> RragResult<HashMap<String, f32>> {
        let mut cache = self.pagerank_cache.write().await;

        if cache.is_none() {
            let graph = self.graph.read().await;
            let scores = GraphAlgorithms::pagerank(&*graph, &self.config.pagerank_config)?;
            *cache = Some(scores);
        }

        Ok(cache.clone().unwrap())
    }

    /// Expand query using graph structure
    async fn expand_query(&self, query: &str) -> RragResult<Vec<String>> {
        if !self.config.enable_query_expansion {
            return Ok(vec![query.to_string()]);
        }

        let expansion_result = self
            .query_expander
            .read()
            .await
            .expand_query(query, &self.config.expansion_options)
            .await?;

        let mut terms = vec![query.to_string()];
        terms.extend(expansion_result.expanded_terms.into_iter().map(|t| t.term));

        Ok(terms)
    }

    /// Find entities related to query
    async fn find_query_entities(&self, query: &str) -> Vec<String> {
        let query_lower = query.to_lowercase();
        let mut entities = Vec::new();

        let graph = self.graph.read().await;

        // Find entities that match query terms
        for (entity_id, node) in &graph.nodes {
            let label_lower = node.label.to_lowercase();
            if query_lower.contains(&label_lower) || label_lower.contains(&query_lower) {
                entities.push(entity_id.clone());
            }
        }

        entities
    }

    /// Add documents and their entities to the graph
    pub async fn add_document_with_entities(
        &self,
        document: &Document,
        entities: Vec<GraphNode>,
        relationships: Vec<super::GraphEdge>,
    ) -> RragResult<()> {
        let mut graph = self.graph.write().await;

        // Add document node
        let doc_node = GraphNode::new(format!("doc_{}", document.id), super::NodeType::Document)
            .with_source_document(document.id.clone())
            .with_attribute(
                "title",
                serde_json::Value::String(
                    document
                        .metadata
                        .get("title")
                        .and_then(|v| v.as_str())
                        .unwrap_or(&document.id)
                        .to_string(),
                ),
            );

        graph.add_node(doc_node.clone())?;

        // Add entities and connect them to the document
        for entity in entities {
            let entity_id = entity.id.clone();
            graph.add_node(entity)?;

            // Create containment edge from document to entity
            let containment_edge = super::GraphEdge::new(
                doc_node.id.clone(),
                entity_id.clone(),
                "contains",
                super::EdgeType::Contains,
            );
            graph.add_edge(containment_edge)?;

            // Update entity-document mapping
            self.entity_document_map
                .write()
                .await
                .entry(entity_id)
                .or_insert_with(HashSet::new)
                .insert(document.id.clone());
        }

        // Add relationships
        for relationship in relationships {
            graph.add_edge(relationship)?;
        }

        // Clear PageRank cache
        *self.pagerank_cache.write().await = None;

        // Update storage
        self.storage.write().await.store_graph(&*graph).await?;

        Ok(())
    }
}

#[async_trait]
impl Retriever for GraphRetriever {
    fn name(&self) -> &str {
        "graph_retriever"
    }

    async fn search(&self, query: &SearchQuery) -> RragResult<Vec<SearchResult>> {
        let query_text = match &query.query {
            QueryType::Text(text) => text,
            QueryType::Embedding(_) => {
                // For embedding queries, we can't do text-based entity extraction
                // Fall back to basic similarity search (would need embedding-based entity matching)
                return Ok(Vec::new());
            }
        };

        // Expand query if enabled
        let expanded_terms = self.expand_query(query_text).await?;
        let expanded_query = expanded_terms.join(" ");

        // Find entities in the (expanded) query
        let query_entities = self.find_query_entities(&expanded_query).await;

        // For simplicity, return basic results based on entity matching
        let mut results = Vec::new();

        let entity_map = self.entity_document_map.read().await;
        let pagerank_scores = if self.config.enable_pagerank_scoring {
            self.get_pagerank_scores().await?
        } else {
            HashMap::new()
        };

        // Find documents connected to query entities
        let mut candidate_docs = HashSet::new();
        for entity_id in &query_entities {
            if let Some(doc_ids) = entity_map.get(entity_id) {
                candidate_docs.extend(doc_ids.clone());
            }
        }

        // Create search results for candidate documents
        for (rank, doc_id) in candidate_docs.iter().enumerate() {
            // Calculate graph-based score
            let mut graph_score = 0.5; // Base score

            // Add PageRank contribution
            for entity_id in &query_entities {
                if let Some(doc_ids) = entity_map.get(entity_id) {
                    if doc_ids.contains(doc_id) {
                        let pagerank_score = pagerank_scores.get(entity_id).copied().unwrap_or(0.0);
                        graph_score += pagerank_score * 0.3;
                    }
                }
            }

            if graph_score >= self.config.min_graph_score {
                let result = SearchResult {
                    id: doc_id.clone(),
                    content: format!("Document {}", doc_id), // Placeholder
                    score: graph_score,
                    rank,
                    metadata: {
                        let mut metadata = HashMap::new();
                        metadata.insert("graph_score".to_string(), serde_json::json!(graph_score));
                        metadata
                    },
                    embedding: None,
                };

                results.push(result);
            }
        }

        // Sort by score and apply limits
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.retain(|result| result.score >= query.min_score);
        results.truncate(query.limit);

        // Update ranks
        for (i, result) in results.iter_mut().enumerate() {
            result.rank = i;
        }

        Ok(results)
    }

    async fn add_documents(&self, documents: &[(Document, Embedding)]) -> RragResult<()> {
        // This would typically involve:
        // 1. Extracting entities and relationships from documents
        // 2. Adding them to the graph
        // 3. Updating storage
        // For now, just add document nodes

        let mut graph = self.graph.write().await;
        let mut nodes = Vec::new();

        for (document, _embedding) in documents {
            let doc_node =
                GraphNode::new(format!("doc_{}", document.id), super::NodeType::Document)
                    .with_source_document(document.id.clone());

            nodes.push(doc_node.clone());
            graph.add_node(doc_node)?;
        }

        self.storage.write().await.store_nodes(&nodes).await?;

        Ok(())
    }

    async fn add_chunks(&self, chunks: &[(DocumentChunk, Embedding)]) -> RragResult<()> {
        // Similar to add_documents but for chunks
        let mut graph = self.graph.write().await;
        let mut nodes = Vec::new();

        for (chunk, _embedding) in chunks {
            let chunk_node = GraphNode::new(
                format!("chunk_{}_{}", chunk.document_id, chunk.chunk_index),
                super::NodeType::DocumentChunk,
            )
            .with_source_document(chunk.document_id.clone());

            nodes.push(chunk_node.clone());
            graph.add_node(chunk_node)?;
        }

        self.storage.write().await.store_nodes(&nodes).await?;

        Ok(())
    }

    async fn remove_documents(&self, document_ids: &[String]) -> RragResult<()> {
        let mut graph = self.graph.write().await;

        // Remove document nodes and update entity mappings
        let doc_node_ids: Vec<_> = document_ids
            .iter()
            .map(|doc_id| format!("doc_{}", doc_id))
            .collect();

        for node_id in &doc_node_ids {
            graph.remove_node(node_id)?;
        }

        // Update entity-document mapping
        let mut entity_map = self.entity_document_map.write().await;
        for doc_id in document_ids {
            for entity_docs in entity_map.values_mut() {
                entity_docs.remove(doc_id);
            }
        }

        self.storage
            .write()
            .await
            .delete_nodes(&doc_node_ids)
            .await?;

        Ok(())
    }

    async fn clear(&self) -> RragResult<()> {
        *self.graph.write().await = KnowledgeGraph::new();
        self.entity_document_map.write().await.clear();
        *self.pagerank_cache.write().await = None;
        self.storage.write().await.clear().await?;
        Ok(())
    }

    async fn stats(&self) -> RragResult<IndexStats> {
        let storage_stats = self.storage.read().await.get_stats().await?;
        let graph = self.graph.read().await;
        let _graph_metrics = graph.calculate_metrics();

        Ok(IndexStats {
            total_items: storage_stats.total_nodes,
            size_bytes: storage_stats.storage_size_bytes,
            dimensions: 0, // Graph doesn't have fixed dimensions
            index_type: "graph_based".to_string(),
            last_updated: storage_stats.last_updated,
        })
    }

    async fn health_check(&self) -> RragResult<bool> {
        // Check graph consistency and storage health
        let graph = self.graph.read().await;
        Ok(!graph.nodes.is_empty() || self.storage.read().await.get_stats().await.is_ok())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph_retrieval::{storage::InMemoryGraphStorage, EdgeType, GraphEdge, NodeType};

    #[tokio::test]
    async fn test_graph_retriever_creation() {
        let graph = KnowledgeGraph::new();
        let storage = Box::new(InMemoryGraphStorage::new());
        let config = GraphRetrievalConfig::default();

        let retriever = GraphRetriever::new(graph, storage, config).unwrap();
        assert_eq!(retriever.name(), "graph_retriever");
    }

    #[tokio::test]
    async fn test_query_expansion() {
        let mut graph = KnowledgeGraph::new();

        // Create test graph with related entities
        let node1 = GraphNode::new("machine learning", NodeType::Concept);
        let node2 = GraphNode::new("artificial intelligence", NodeType::Concept);
        let node1_id = node1.id.clone();
        let node2_id = node2.id.clone();

        graph.add_node(node1).unwrap();
        graph.add_node(node2).unwrap();

        graph
            .add_edge(
                GraphEdge::new(
                    node1_id.clone(),
                    node2_id.clone(),
                    "part_of",
                    EdgeType::Semantic("part_of".to_string()),
                )
                .with_confidence(0.8),
            )
            .unwrap();

        let storage = Box::new(InMemoryGraphStorage::new());
        let config = GraphRetrievalConfig::default();

        let retriever = GraphRetriever::new(graph, storage, config).unwrap();

        // Test query expansion
        let expanded = retriever.expand_query("machine learning").await.unwrap();
        assert!(!expanded.is_empty());
        assert!(expanded.contains(&"machine learning".to_string()));
    }

    #[tokio::test]
    async fn test_find_query_entities() {
        let mut graph = KnowledgeGraph::new();

        let node = GraphNode::new("neural networks", NodeType::Concept);
        let node_id = node.id.clone();
        graph.add_node(node).unwrap();

        let storage = Box::new(InMemoryGraphStorage::new());
        let config = GraphRetrievalConfig::default();

        let retriever = GraphRetriever::new(graph, storage, config).unwrap();

        let entities = retriever
            .find_query_entities("neural networks deep learning")
            .await;
        assert!(!entities.is_empty());
        assert!(entities.contains(&node_id));
    }

    #[tokio::test]
    async fn test_health_check() {
        let graph = KnowledgeGraph::new();
        let storage = Box::new(InMemoryGraphStorage::new());
        let config = GraphRetrievalConfig::default();

        let retriever = GraphRetriever::new(graph, storage, config).unwrap();
        let is_healthy = retriever.health_check().await.unwrap();
        assert!(is_healthy);
    }
}