Skip to main content

graphrag_core/graphrag/
mod.rs

1//! Main `GraphRAG` orchestrator type.
2//!
3//! Phase 4 follow-up split: the struct + impl bodies that previously lived in a
4//! single 1753-LOC `graphrag.rs` are now distributed across per-concern files
5//! in this directory. The struct definition + private `ensure_initialized`
6//! helper + tests stay here; the impl methods live in:
7//!
8//! - [`lifecycle`] — constructors, `initialize`, workspace persistence, `clear_graph`
9//! - [`documents`] — `add_document_from_text`, `add_document`
10//! - [`build`] — async + sync `build_graph` pair
11//! - [`query`] — `ask*` family, `query_internal*`, semantic-answer synthesis, `ask_with_pagerank`
12//! - [`stats`] — getters (`config`, `knowledge_graph`, `get_entity`, etc.)
13//! - [`factory`] — `from_json5_file`, `from_config_file`, `quick_start*`
14//!
15//! `lib.rs` re-exports `GraphRAG` so external paths remain unchanged.
16
17#![allow(unused_imports)]
18
19use crate::config::Config;
20use crate::core::{
21    ChunkId, Document, DocumentId, Entity, EntityId, GraphRAGError, KnowledgeGraph, Relationship,
22    Result, TextChunk,
23};
24use crate::{critic, ollama, persistence, query, retrieval};
25
26#[cfg(feature = "parallel-processing")]
27#[allow(unused_imports)]
28use crate::parallel;
29
30mod ask;
31mod build;
32mod documents;
33mod factory;
34mod lifecycle;
35mod stats;
36
37/// This is the primary entry point for using GraphRAG. It orchestrates
38/// all components: knowledge graph, retrieval, generation, and caching.
39///
40/// # Examples
41///
42/// ```ignore
43/// use graphrag_core::{GraphRAG, Config};
44///
45/// # fn example() -> graphrag_core::Result<()> {
46/// let config = Config::default();
47/// let mut graphrag = GraphRAG::new(config)?;
48/// graphrag.initialize()?;
49///
50/// // Add documents
51/// graphrag.add_document_from_text("Your document text")?;
52///
53/// // Build knowledge graph
54/// graphrag.build_graph()?;
55///
56/// // Query
57/// let answer = graphrag.ask("Your question?")?;
58/// println!("Answer: {}", answer);
59/// # Ok(())
60/// # }
61/// ```
62pub struct GraphRAG {
63    config: Config,
64    knowledge_graph: Option<KnowledgeGraph>,
65    retrieval_system: Option<retrieval::RetrievalSystem>,
66    query_planner: Option<query::planner::QueryPlanner>,
67    #[cfg_attr(not(feature = "async"), allow(dead_code))]
68    critic: Option<critic::Critic>,
69    #[cfg(feature = "parallel-processing")]
70    #[allow(dead_code)]
71    parallel_processor: Option<parallel::ParallelProcessor>,
72}
73
74impl GraphRAG {
75    /// Ensure system is initialized
76    pub(super) fn ensure_initialized(&mut self) -> Result<()> {
77        if !self.is_initialized() {
78            self.initialize()
79        } else {
80            Ok(())
81        }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_builder_pattern() {
91        let graphrag = GraphRAG::builder()
92            .with_output_dir("./test_output")
93            .with_chunk_size(512)
94            .with_top_k(10)
95            .build();
96        assert!(graphrag.is_ok());
97    }
98}