graphrag_core/graphrag/factory.rs
1#![allow(unused_imports)]
2
3use crate::config::Config;
4use crate::core::{
5 ChunkId, Document, DocumentId, Entity, EntityId, GraphRAGError, KnowledgeGraph, Relationship,
6 Result, TextChunk,
7};
8use crate::{critic, ollama, persistence, query, retrieval};
9
10#[cfg(feature = "parallel-processing")]
11#[allow(unused_imports)]
12use crate::parallel;
13
14use super::GraphRAG;
15
16impl GraphRAG {
17 // ================================
18 // CONVENIENCE CONSTRUCTORS
19 // ================================
20
21 /// Create GraphRAG from a JSON5 config file
22 ///
23 /// This is a convenience method that loads a JSON5 config file and creates a GraphRAG instance.
24 ///
25 /// # Examples
26 ///
27 /// ```rust,no_run
28 /// # #[cfg(feature = "json5-support")]
29 /// # async fn example() -> graphrag_core::Result<()> {
30 /// use graphrag_core::GraphRAG;
31 ///
32 /// let graphrag = GraphRAG::from_json5_file("config/templates/symposium_zero_cost.graphrag.json5")?;
33 /// # Ok(())
34 /// # }
35 /// ```
36 #[cfg(feature = "json5-support")]
37 pub fn from_json5_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
38 use crate::config::json5_loader::load_json5_config;
39 use crate::config::setconfig::SetConfig;
40
41 let set_config = load_json5_config::<SetConfig, _>(path)?;
42 let config = set_config.to_graphrag_config();
43 Self::new(config)
44 }
45
46 /// Create GraphRAG from a config file (auto-detect format: TOML, JSON5, YAML, JSON)
47 ///
48 /// This method automatically detects the config file format based on the file extension
49 /// and loads it appropriately.
50 ///
51 /// Supported formats:
52 /// - `.toml` - TOML format
53 /// - `.json5` - JSON5 format (requires `json5-support` feature)
54 /// - `.yaml`, `.yml` - YAML format
55 /// - `.json` - JSON format
56 ///
57 /// # Examples
58 ///
59 /// ```rust,no_run
60 /// # async fn example() -> graphrag_core::Result<()> {
61 /// use graphrag_core::GraphRAG;
62 ///
63 /// // Auto-detect format from extension
64 /// let graphrag = GraphRAG::from_config_file("config/templates/symposium_zero_cost.graphrag.json5")?;
65 /// # Ok(())
66 /// # }
67 /// ```
68 pub fn from_config_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
69 use crate::config::setconfig::SetConfig;
70
71 let set_config = SetConfig::from_file(path)?;
72 let config = set_config.to_graphrag_config();
73 Self::new(config)
74 }
75
76 /// Complete workflow: load config + process document + build graph
77 ///
78 /// This is the most convenient method for getting started with GraphRAG. It:
79 /// 1. Loads the config file (auto-detecting the format)
80 /// 2. Initializes the GraphRAG system
81 /// 3. Loads and processes the document
82 /// 4. Builds the knowledge graph
83 ///
84 /// After this method completes, the GraphRAG instance is ready to answer queries.
85 ///
86 /// # Examples
87 ///
88 /// ```rust,no_run
89 /// # #[cfg(feature = "async")]
90 /// # async fn example() -> graphrag_core::Result<()> {
91 /// use graphrag_core::GraphRAG;
92 ///
93 /// // Complete workflow in one call
94 /// let mut graphrag = GraphRAG::from_config_and_document(
95 /// "config/templates/symposium_zero_cost.graphrag.json5",
96 /// "docs-example/Symposium.txt"
97 /// ).await?;
98 ///
99 /// // Ready to query
100 /// let answer = graphrag.ask("What is Socrates' view on love?").await?;
101 /// println!("Answer: {}", answer);
102 /// # Ok(())
103 /// # }
104 /// ```
105 #[cfg(feature = "async")]
106 pub async fn from_config_and_document<P1, P2>(
107 config_path: P1,
108 document_path: P2,
109 ) -> Result<Self>
110 where
111 P1: AsRef<std::path::Path>,
112 P2: AsRef<std::path::Path>,
113 {
114 // Load config
115 let mut graphrag = Self::from_config_file(config_path)?;
116
117 // Initialize
118 graphrag.initialize()?;
119
120 // Load document
121 let content = std::fs::read_to_string(document_path).map_err(GraphRAGError::Io)?;
122
123 graphrag.add_document_from_text(&content)?;
124
125 // Build graph
126 graphrag.build_graph().await?;
127
128 Ok(graphrag)
129 }
130
131 /// Quick start: Create a ready-to-query GraphRAG instance from text in one call
132 ///
133 /// This is the simplest way to get started with GraphRAG. It:
134 /// 1. Creates a new instance with default or hierarchical configuration
135 /// 2. Initializes all components
136 /// 3. Processes your text document
137 /// 4. Builds the knowledge graph
138 ///
139 /// After this call, you can immediately use `ask()` to query the system.
140 ///
141 /// # Example: Hello World in 5 lines
142 /// ```rust,no_run
143 /// use graphrag_core::prelude::*;
144 ///
145 /// # async fn example() -> graphrag_core::Result<()> {
146 /// let mut graphrag = GraphRAG::quick_start("Your document text here").await?;
147 /// let answer = graphrag.ask("What is this document about?").await?;
148 /// println!("{}", answer);
149 /// # Ok(())
150 /// # }
151 /// ```
152 ///
153 /// # Configuration
154 /// - With `hierarchical-config` feature: Uses layered config (defaults → user → project → env)
155 /// - Without: Uses sensible defaults optimized for local Ollama setup
156 #[cfg(feature = "async")]
157 pub async fn quick_start(text: &str) -> Result<Self> {
158 // Load config (hierarchical if available, otherwise defaults)
159 let config = Config::load()?;
160
161 let mut graphrag = Self::new(config)?;
162 graphrag.initialize()?;
163 graphrag.add_document_from_text(text)?;
164 graphrag.build_graph().await?;
165
166 Ok(graphrag)
167 }
168
169 /// Quick start with custom configuration
170 ///
171 /// Like `quick_start()`, but allows you to customize the configuration
172 /// using the builder pattern before processing the document.
173 ///
174 /// # Example
175 /// ```rust,no_run
176 /// use graphrag_core::prelude::*;
177 ///
178 /// # async fn example() -> graphrag_core::Result<()> {
179 /// let mut graphrag = GraphRAG::quick_start_with_config(
180 /// "Your document text",
181 /// |builder| builder
182 /// .with_chunk_size(256)
183 /// .with_ollama_enabled(true)
184 /// ).await?;
185 /// # Ok(())
186 /// # }
187 /// ```
188 #[cfg(feature = "async")]
189 pub async fn quick_start_with_config<F>(text: &str, configure: F) -> Result<Self>
190 where
191 F: FnOnce(crate::builder::GraphRAGBuilder) -> crate::builder::GraphRAGBuilder,
192 {
193 let builder = configure(Self::builder());
194 let mut graphrag = builder.build()?;
195 graphrag.initialize()?;
196 graphrag.add_document_from_text(text)?;
197 graphrag.build_graph().await?;
198
199 Ok(graphrag)
200 }
201}