graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
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
//! LazyGraphRAG Pipeline
//!
//! Complete end-to-end pipeline for LazyGraphRAG (Microsoft Research, 2025)
//! that achieves 0.1% of full GraphRAG indexing cost and 700x cheaper query costs.
//!
//! ## Key Features
//!
//! - **No LLM for Indexing**: Uses noun phrase extraction instead of LLM entity extraction
//! - **Fast Construction**: Builds concept graph from co-occurrence without summarization
//! - **Efficient Queries**: Iterative deepening search with bidirectional index
//! - **Zero Prior Summarization**: Works directly on text chunks
//!
//! ## Pipeline Stages
//!
//! 1. **Document Processing**: Load and chunk documents
//! 2. **Concept Extraction**: Extract concepts using NLP patterns (no LLM)
//! 3. **Graph Construction**: Build co-occurrence graph from concepts
//! 4. **Index Building**: Create bidirectional entity-chunk index
//! 5. **Query Processing**: Refine queries and retrieve using iterative deepening
//!
//! ## Example
//!
//! ```rust
//! use graphrag_core::lightrag::lazy_pipeline::{LazyGraphRAGPipeline, LazyPipelineConfig};
//!
//! let config = LazyPipelineConfig::default();
//! let mut pipeline = LazyGraphRAGPipeline::new(config);
//!
//! // Index documents
//! pipeline.index_document("doc1", "Text about machine learning and neural networks...");
//! pipeline.index_document("doc2", "More text about deep learning...");
//!
//! // Build the concept graph
//! pipeline.build_graph();
//!
//! // Query the graph
//! let results = pipeline.query("machine learning applications");
//! println!("Found {} relevant chunks", results.chunk_count());
//! ```

use crate::core::{ChunkId, TextChunk};
use crate::entity::BidirectionalIndex;
use crate::lightrag::concept_graph::{
    ConceptExtractor, ConceptExtractorConfig, ConceptGraph, ConceptGraphBuilder,
};
use crate::lightrag::iterative_deepening::{IterativeDeepeningSearch, SearchConfig, SearchResults};
use crate::lightrag::query_refinement::{QueryRefinementConfig, QueryRefiner};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration for LazyGraphRAG pipeline
#[derive(Debug, Clone)]
pub struct LazyPipelineConfig {
    /// Concept extraction configuration
    pub concept_extraction: ConceptExtractorConfig,

    /// Query refinement configuration
    pub query_refinement: QueryRefinementConfig,

    /// Search configuration
    pub search: SearchConfig,

    /// Chunk size for text splitting
    pub chunk_size: usize,

    /// Chunk overlap
    pub chunk_overlap: usize,

    /// Enable bidirectional indexing
    pub use_bidirectional_index: bool,
}

impl Default for LazyPipelineConfig {
    fn default() -> Self {
        Self {
            concept_extraction: ConceptExtractorConfig::default(),
            query_refinement: QueryRefinementConfig::default(),
            search: SearchConfig::default(),
            chunk_size: 512,
            chunk_overlap: 128,
            use_bidirectional_index: true,
        }
    }
}

/// LazyGraphRAG pipeline implementation
pub struct LazyGraphRAGPipeline {
    config: LazyPipelineConfig,
    concept_extractor: ConceptExtractor,
    graph_builder: ConceptGraphBuilder,
    concept_graph: Option<ConceptGraph>,
    bidirectional_index: Option<BidirectionalIndex>,
    query_refiner: QueryRefiner,
    search_engine: IterativeDeepeningSearch,
    chunks: HashMap<ChunkId, TextChunk>,
    document_count: usize,
}

impl LazyGraphRAGPipeline {
    /// Create a new LazyGraphRAG pipeline with configuration
    pub fn new(config: LazyPipelineConfig) -> Self {
        let concept_extractor = ConceptExtractor::with_config(config.concept_extraction.clone());
        let graph_builder = ConceptGraphBuilder::new();
        let query_refiner = QueryRefiner::new(config.query_refinement.clone());
        let search_engine = IterativeDeepeningSearch::new(config.search.clone());

        Self {
            config,
            concept_extractor,
            graph_builder,
            concept_graph: None,
            bidirectional_index: None,
            query_refiner,
            search_engine,
            chunks: HashMap::new(),
            document_count: 0,
        }
    }

    /// Create with default configuration
    pub fn default() -> Self {
        Self::new(LazyPipelineConfig::default())
    }

    /// Index a document by extracting concepts and building the graph
    ///
    /// This processes the document, extracts concepts, and adds them to the graph builder.
    /// Call `build_graph()` after indexing all documents to finalize the graph.
    pub fn index_document(&mut self, document_id: &str, text: &str) {
        // Chunk the document
        let chunks = self.chunk_text(text, document_id);

        // Extract concepts from each chunk
        for chunk in chunks {
            let concepts = self.concept_extractor.extract_concepts(&chunk.content);

            // Add to graph builder
            self.graph_builder
                .add_document_concepts(document_id, concepts.clone());
            self.graph_builder
                .add_chunk_concepts(&chunk.id.as_str(), concepts);

            // Store chunk
            self.chunks.insert(chunk.id.clone(), chunk);
        }

        self.document_count += 1;
    }

    /// Build the concept graph from all indexed documents
    ///
    /// This finalizes the graph construction and creates the bidirectional index.
    /// Must be called after indexing all documents and before querying.
    pub fn build_graph(&mut self) {
        // Build concept graph
        let graph_builder = std::mem::replace(&mut self.graph_builder, ConceptGraphBuilder::new());
        self.concept_graph = Some(graph_builder.build());

        // Build bidirectional index if enabled
        if self.config.use_bidirectional_index {
            let mut index = BidirectionalIndex::new();

            // Add all concept-chunk mappings to the index
            if let Some(ref graph) = self.concept_graph {
                for (concept_text, concept) in &graph.concepts {
                    let entity_id =
                        crate::core::EntityId::new(self.normalize_concept(concept_text));

                    for chunk_id in &concept.chunk_ids {
                        index.add_mapping(&entity_id, chunk_id);
                    }
                }
            }

            self.bidirectional_index = Some(index);
        }
    }

    /// Query the concept graph using iterative deepening search
    ///
    /// Returns search results with relevant chunks and search statistics.
    pub fn query(&self, query: &str) -> SearchResults {
        let graph = match &self.concept_graph {
            Some(g) => g,
            None => {
                // Return empty results if graph not built
                return SearchResults::new(query.to_string());
            },
        };

        let index = match &self.bidirectional_index {
            Some(i) => i,
            None => {
                // Return empty results if index not built
                return SearchResults::new(query.to_string());
            },
        };

        self.search_engine.search(query, graph, index)
    }

    /// Get chunks from search results
    pub fn get_chunks(&self, search_results: &SearchResults) -> Vec<TextChunk> {
        search_results
            .chunk_ids
            .iter()
            .filter_map(|chunk_id| self.chunks.get(chunk_id).cloned())
            .collect()
    }

    /// Get the concept graph
    pub fn get_concept_graph(&self) -> Option<&ConceptGraph> {
        self.concept_graph.as_ref()
    }

    /// Get the bidirectional index
    pub fn get_bidirectional_index(&self) -> Option<&BidirectionalIndex> {
        self.bidirectional_index.as_ref()
    }

    /// Get pipeline statistics
    pub fn get_statistics(&self) -> PipelineStatistics {
        let graph_stats = self.concept_graph.as_ref().map(|g| GraphStatistics {
            concept_count: g.concept_count(),
            relation_count: g.relation_count(),
        });

        let index_stats = self
            .bidirectional_index
            .as_ref()
            .map(|i| i.get_statistics());

        PipelineStatistics {
            document_count: self.document_count,
            chunk_count: self.chunks.len(),
            graph_statistics: graph_stats,
            index_statistics: index_stats,
        }
    }

    /// Chunk text into smaller pieces
    fn chunk_text(&self, text: &str, document_id: &str) -> Vec<TextChunk> {
        let mut chunks = Vec::new();
        let text_len = text.len();

        if text_len == 0 {
            return chunks;
        }

        let mut start = 0;
        let mut chunk_index = 0;

        while start < text_len {
            let end = (start + self.config.chunk_size).min(text_len);
            let chunk_text = text[start..end].to_string();

            let chunk = TextChunk::new(
                ChunkId::new(format!("{}_{}", document_id, chunk_index)),
                crate::core::DocumentId::new(document_id.to_string()),
                chunk_text,
                start,
                end,
            );

            chunks.push(chunk);

            // Move to next chunk with overlap
            if end >= text_len {
                break;
            }

            start = end - self.config.chunk_overlap;
            chunk_index += 1;
        }

        chunks
    }

    /// Normalize concept for entity ID
    fn normalize_concept(&self, concept: &str) -> String {
        concept
            .to_lowercase()
            .chars()
            .filter(|c| c.is_alphanumeric() || *c == '_')
            .collect::<String>()
            .replace(' ', "_")
    }
}

/// Pipeline statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineStatistics {
    /// Number of documents indexed
    pub document_count: usize,

    /// Number of chunks created
    pub chunk_count: usize,

    /// Concept graph statistics
    pub graph_statistics: Option<GraphStatistics>,

    /// Bidirectional index statistics
    pub index_statistics: Option<crate::entity::IndexStatistics>,
}

/// Concept graph statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphStatistics {
    /// Number of concepts in graph
    pub concept_count: usize,

    /// Number of relationships in graph
    pub relation_count: usize,
}

impl SearchResults {
    pub(crate) fn new(query: String) -> Self {
        Self {
            query,
            depth_reached: 0,
            total_chunks: 0,
            total_concepts_explored: 0,
            depth_results: Vec::new(),
            chunk_ids: Vec::new(),
            stop_reason: crate::lightrag::iterative_deepening::StopReason::MaxDepthReached,
        }
    }
}

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

    #[test]
    fn test_lazy_pipeline_creation() {
        let config = LazyPipelineConfig::default();
        let pipeline = LazyGraphRAGPipeline::new(config);

        assert_eq!(pipeline.document_count, 0);
        assert_eq!(pipeline.chunks.len(), 0);
    }

    #[test]
    fn test_index_and_build() {
        let mut pipeline = LazyGraphRAGPipeline::default();

        // Index a simple document
        pipeline.index_document(
            "test_doc",
            "Machine Learning is a subset of Artificial Intelligence. \
             Neural Networks are used in Deep Learning.",
        );

        assert_eq!(pipeline.document_count, 1);
        assert!(!pipeline.chunks.is_empty());

        // Build the graph
        pipeline.build_graph();

        assert!(pipeline.concept_graph.is_some());
        assert!(pipeline.bidirectional_index.is_some());
    }

    #[test]
    fn test_query_before_build() {
        let pipeline = LazyGraphRAGPipeline::default();

        // Query without building graph should return empty results
        let results = pipeline.query("machine learning");

        assert_eq!(results.total_chunks, 0);
        assert_eq!(results.chunk_ids.len(), 0);
    }

    #[test]
    fn test_pipeline_statistics() {
        let mut pipeline = LazyGraphRAGPipeline::default();

        pipeline.index_document("doc1", "Test document with some content");
        pipeline.build_graph();

        let stats = pipeline.get_statistics();

        assert_eq!(stats.document_count, 1);
        assert!(stats.chunk_count > 0);
        assert!(stats.graph_statistics.is_some());
    }

    #[test]
    fn test_chunking() {
        let config = LazyPipelineConfig {
            chunk_size: 10,
            chunk_overlap: 2,
            ..Default::default()
        };

        let pipeline = LazyGraphRAGPipeline::new(config);

        let text = "This is a test document";
        let chunks = pipeline.chunk_text(text, "test_doc");

        assert!(!chunks.is_empty());
        assert!(chunks[0].content.len() <= 10);
    }
}