pub struct GraphRAG { /* private fields */ }Expand description
Main GraphRAG system
This is the primary entry point for using GraphRAG. It orchestrates all components: knowledge graph, retrieval, generation, and caching.
§Examples
use graphrag_core::{GraphRAG, Config};
let config = Config::default();
let mut graphrag = GraphRAG::new(config)?;
graphrag.initialize()?;
// Add documents
graphrag.add_document_from_text("Your document text")?;
// Build knowledge graph
graphrag.build_graph()?;
// Query
let answer = graphrag.ask("Your question?")?;
println!("Answer: {}", answer);Implementations§
Source§impl GraphRAG
impl GraphRAG
Sourcepub fn new(config: Config) -> Result<Self>
pub fn new(config: Config) -> Result<Self>
Create a new GraphRAG instance with the given configuration
Sourcepub fn initialize(&mut self) -> Result<()>
pub fn initialize(&mut self) -> Result<()>
Initialize the GraphRAG system
Sourcepub fn add_document_from_text(&mut self, text: &str) -> Result<()>
pub fn add_document_from_text(&mut self, text: &str) -> Result<()>
Add a document from text content
Sourcepub fn add_document(&mut self, document: Document) -> Result<()>
pub fn add_document(&mut self, document: Document) -> Result<()>
Add a document to the system
Sourcepub fn clear_graph(&mut self) -> Result<()>
pub fn clear_graph(&mut self) -> Result<()>
Clear all entities and relationships from the knowledge graph
This method preserves documents and text chunks but removes all extracted entities and relationships. Useful for rebuilding the graph from scratch without reloading documents.
Sourcepub async fn build_graph(&mut self) -> Result<()>
pub async fn build_graph(&mut self) -> Result<()>
Build the knowledge graph from added documents
This method implements dynamic pipeline selection based on the configured approach:
- Semantic (config.approach = “semantic”): Uses LLM-based entity extraction with gleaning for high-quality results. Requires Ollama to be enabled.
- Algorithmic (config.approach = “algorithmic”): Uses pattern-based entity extraction (regex + capitalization) for fast, resource-efficient processing.
- Hybrid (config.approach = “hybrid”): Combines both approaches with weighted fusion.
The selection is controlled by config.approach and mapped from TomlConfig’s [mode] section.
Sourcepub async fn ask(&mut self, query: &str) -> Result<String>
pub async fn ask(&mut self, query: &str) -> Result<String>
Query the system for relevant information
Sourcepub fn query_internal(&mut self, query: &str) -> Result<Vec<String>>
pub fn query_internal(&mut self, query: &str) -> Result<Vec<String>>
Internal query method (public for CLI access to raw results)
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Check if system is initialized
Sourcepub fn has_documents(&self) -> bool
pub fn has_documents(&self) -> bool
Check if documents have been added
Sourcepub fn knowledge_graph(&self) -> Option<&KnowledgeGraph>
pub fn knowledge_graph(&self) -> Option<&KnowledgeGraph>
Get a reference to the knowledge graph
Sourcepub fn get_entity(&self, entity_id: &str) -> Option<&Entity>
pub fn get_entity(&self, entity_id: &str) -> Option<&Entity>
Get entity details by ID
Sourcepub fn get_entity_relationships(&self, entity_id: &str) -> Vec<&Relationship>
pub fn get_entity_relationships(&self, entity_id: &str) -> Vec<&Relationship>
Get all relationships involving an entity
Sourcepub fn knowledge_graph_mut(&mut self) -> Option<&mut KnowledgeGraph>
pub fn knowledge_graph_mut(&mut self) -> Option<&mut KnowledgeGraph>
Get a mutable reference to the knowledge graph
Sourcepub fn from_config_file<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_config_file<P: AsRef<Path>>(path: P) -> Result<Self>
Create GraphRAG from a config file (auto-detect format: TOML, JSON5, YAML, JSON)
This method automatically detects the config file format based on the file extension and loads it appropriately.
Supported formats:
.toml- TOML format.json5- JSON5 format (requiresjson5-supportfeature).yaml,.yml- YAML format.json- JSON format
§Examples
use graphrag_core::GraphRAG;
// Auto-detect format from extension
let graphrag = GraphRAG::from_config_file("config/templates/symposium_zero_cost.graphrag.json5")?;Sourcepub async fn from_config_and_document<P1, P2>(
config_path: P1,
document_path: P2,
) -> Result<Self>
pub async fn from_config_and_document<P1, P2>( config_path: P1, document_path: P2, ) -> Result<Self>
Complete workflow: load config + process document + build graph
This is the most convenient method for getting started with GraphRAG. It:
- Loads the config file (auto-detecting the format)
- Initializes the GraphRAG system
- Loads and processes the document
- Builds the knowledge graph
After this method completes, the GraphRAG instance is ready to answer queries.
§Examples
use graphrag_core::GraphRAG;
// Complete workflow in one call
let mut graphrag = GraphRAG::from_config_and_document(
"config/templates/symposium_zero_cost.graphrag.json5",
"docs-example/Symposium.txt"
).await?;
// Ready to query
let answer = graphrag.ask("What is Socrates' view on love?").await?;
println!("Answer: {}", answer);Auto Trait Implementations§
impl Freeze for GraphRAG
impl RefUnwindSafe for GraphRAG
impl Send for GraphRAG
impl Sync for GraphRAG
impl Unpin for GraphRAG
impl UnsafeUnpin for GraphRAG
impl UnwindSafe for GraphRAG
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more