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
//! # Graph-Based Retrieval Module
//!
//! Advanced knowledge graph construction and graph-based retrieval for RAG systems.
//!
//! This module enables sophisticated reasoning over structured knowledge by building
//! knowledge graphs from documents and leveraging graph traversal algorithms for
//! enhanced retrieval. It provides both automatic graph construction and manual
//! graph management capabilities.
//!
//! ## Features
//!
//! - **Knowledge Graph Construction**: Automatic entity and relationship extraction
//! - **Graph Algorithms**: PageRank, community detection, path finding
//! - **Entity Recognition**: Multi-type entity extraction with confidence scoring
//! - **Relationship Extraction**: Semantic relationship detection between entities
//! - **Graph Storage**: Efficient storage and indexing for large graphs
//! - **Query Expansion**: Graph-based query enhancement and expansion
//! - **Hybrid Retrieval**: Combine graph and vector retrieval
//! - **Graph Analytics**: Centrality measures, clustering, and graph statistics
//!
//! ## Use Cases
//!
//! - **Knowledge Base Construction**: Build structured knowledge from documents
//! - **Question Answering**: Multi-hop reasoning across connected entities
//! - **Recommendation Systems**: Find related entities and concepts
//! - **Fact Verification**: Verify claims using graph-based evidence
//! - **Research Discovery**: Find connections between research topics
//!
//! ## Examples
//!
//! ### Building a Knowledge Graph
//! ```rust
//! use rrag::graph_retrieval::{GraphBuilder, EntityExtractor, RelationshipExtractor};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let mut builder = GraphBuilder::new()
//! .with_entity_extraction(true)
//! .with_relationship_detection(true)
//! .build();
//!
//! // Add documents to build the graph
//! let documents = vec![
//! "Albert Einstein was born in Germany in 1879.",
//! "Einstein developed the theory of relativity.",
//! "The theory of relativity revolutionized physics."
//! ];
//!
//! for doc in documents {
//! builder.add_document(doc).await?;
//! }
//!
//! let graph = builder.build().await?;
//! println!("Built graph with {} nodes and {} edges",
//! graph.node_count(),
//! graph.edge_count());
//! # Ok(())
//! # }
//! ```
//!
//! ### Graph-Based Query Expansion
//! ```rust
//! use rrag::graph_retrieval::{GraphQueryExpander, ExpansionStrategy};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let expander = GraphQueryExpander::new(graph)
//! .with_strategy(ExpansionStrategy::SemanticPath)
//! .with_max_hops(2);
//!
//! let original_query = "Einstein's theories";
//! let expanded = expander.expand_query(original_query).await?;
//!
//! println!("Original: {}", original_query);
//! println!("Expanded: {:?}", expanded.expanded_terms);
//! // Output might include: ["theory of relativity", "special relativity",
//! // "general relativity", "physics", "German physicist"]
//! # Ok(())
//! # }
//! ```
//!
//! ### Multi-Hop Reasoning
//! ```rust
//! use rrag::graph_retrieval::{GraphRetriever, TraversalStrategy};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let retriever = GraphRetriever::new(graph)
//! .with_traversal_strategy(TraversalStrategy::BreadthFirst)
//! .with_max_depth(3);
//!
//! // Find connections between entities
//! let connections = retriever.find_path_between(
//! "Einstein",
//! "quantum mechanics"
//! ).await?;
//!
//! for connection in connections {
//! println!("Path: {}", connection.format_path());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Entity-Centric Retrieval
//! ```rust
//! use rrag::graph_retrieval::{EntityCentricRetriever, RetrievalOptions};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let retriever = EntityCentricRetriever::new(graph);
//!
//! let query = "What did Einstein contribute to physics?";
//! let results = retriever.retrieve_with_entities(
//! query,
//! RetrievalOptions::new()
//! .with_entity_expansion(true)
//! .with_relationship_traversal(true)
//! ).await?;
//!
//! for result in results {
//! println!("Document: {}", result.content);
//! println!("Related entities: {:?}", result.entities);
//! println!("Relationship path: {:?}", result.path);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Graph Analytics
//! ```rust
//! use rrag::graph_retrieval::{GraphAnalyzer, CentralityMetric};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let analyzer = GraphAnalyzer::new(graph);
//!
//! // Find most important entities
//! let pagerank_scores = analyzer.compute_centrality(
//! CentralityMetric::PageRank
//! ).await?;
//!
//! let top_entities = pagerank_scores.top_k(10);
//! for (entity, score) in top_entities {
//! println!("Entity: {}, Importance: {:.3}", entity, score);
//! }
//!
//! // Detect communities
//! let communities = analyzer.detect_communities().await?;
//! for (idx, community) in communities.iter().enumerate() {
//! println!("Community {}: {:?}", idx, community.entities);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Optimization
//!
//! - **Parallel Processing**: Multi-threaded entity extraction
//! - **Batch Operations**: Process multiple documents together
//! - **Graph Indexing**: Pre-built indexes for fast traversal
//! - **Caching**: Cache frequently accessed graph patterns
//! - **Memory Mapping**: Efficient storage for large graphs
//! - **Incremental Updates**: Add nodes/edges without rebuilding
// Re-exports
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use GraphStorageConfig;
pub use ;
use crateRragError;
/// Graph-based retrieval error types