GraphRAG Core
The core library for GraphRAG-rs, providing portable functionality for both native and WASM deployments.
Overview
graphrag-core is the foundational library that powers GraphRAG-rs. It provides:
- Embedding Generation: 8 provider backends (HuggingFace, OpenAI, Voyage AI, Cohere, Jina, Mistral, Together AI, Ollama)
- Entity Extraction: TRUE LLM-based gleaning extraction with multi-round refinement (Microsoft GraphRAG-style)
- Graph Construction: Incremental updates, PageRank, community detection
- Retrieval Strategies: Vector, BM25, PageRank, hybrid, adaptive
- Configuration System: Hierarchical TOML-based configuration with environment variable overrides
- Cross-Platform: Works on native (Linux, macOS, Windows) and WASM
Quick Start (5 Lines!)
use *;
async
Or with detailed explanations:
let explained = graphrag.ask_explained.await?;
println!;
println!;
for step in &explained.reasoning_steps
Installation
Add to your Cargo.toml:
[]
# Choose a feature bundle:
= { = "0.1", = ["starter"] } # Basic setup
# OR
= { = "0.1", = ["full"] } # Production-ready
# OR
= { = "0.1", = ["research"] } # Advanced features
Feature Bundles
| Bundle | Description | Includes |
|---|---|---|
starter |
Minimal setup to get started | async, ollama, memory-storage, basic-retrieval |
full |
Production-ready with common features | starter + pagerank, lightrag, caching, parallel-processing, leiden |
wasm-bundle |
Browser-safe features only | memory-storage, basic-retrieval, leiden |
research |
Advanced experimental features | full + rograg, cross-encoder, incremental, monitoring |
Three Ways to Configure
1. TypedBuilder (Compile-Time Safety)
use *;
// Build won't compile until required fields are set!
let graphrag = new
.with_output_dir // Required
.with_ollama // Required: choose LLM backend
.with_chunk_size // Optional
.with_top_k // Optional
.build?;
Available LLM backends:
.with_ollama()- Local Ollama (recommended).with_ollama_custom("host", 8080, "model")- Custom Ollama config.with_hash_embeddings()- Offline, no LLM needed.with_candle_embeddings()- Local neural embeddings
2. Hierarchical Config (with figment)
Enable with the hierarchical-config feature:
// Loads configuration from 5 sources (in priority order):
// 1. Code defaults (lowest priority)
// 2. ~/.graphrag/config.toml (user config)
// 3. ./graphrag.toml (project config)
// 4. Environment variables (GRAPHRAG_*)
// 5. Builder overrides (highest priority)
let config = load?; // Automatically merges all sources
let graphrag = new?;
Environment variable overrides:
3. TOML Configuration File
# graphrag.toml
= "./output"
= "hybrid" # semantic, algorithmic, or hybrid
= 1000
= 200
[]
= "ollama"
= 768
= "nomic-embed-text:latest"
[]
= true
= "localhost"
= 11434
= "llama3.2:3b"
[]
= 0.7
= true
= 3
= ["PERSON", "ORGANIZATION", "LOCATION", "DATE", "EVENT"]
Load with:
let config = from_toml_file?;
let graphrag = new?;
Sectoral Templates
Pre-configured templates for specific domains:
| Template | Best For | Entity Types |
|---|---|---|
general.toml |
Mixed documents | PERSON, ORGANIZATION, LOCATION, DATE, EVENT |
legal.toml |
Contracts, agreements | PARTY, JURISDICTION, CLAUSE_TYPE, OBLIGATION |
medical.toml |
Clinical notes | PATIENT, DIAGNOSIS, MEDICATION, SYMPTOM |
financial.toml |
Reports, filings | COMPANY, TICKER, MONETARY_VALUE, METRIC |
technical.toml |
API docs, code | FUNCTION, CLASS, MODULE, API_ENDPOINT |
Using templates:
let config = from_toml_file?;
Or via CLI:
Explained Answers
Get transparency into how answers are generated:
let explained = graphrag.ask_explained.await?;
// Access detailed information:
println!;
println!;
// Reasoning trace
for step in &explained.reasoning_steps
// Source references
for source in &explained.sources
// Or get formatted output
println!;
Output:
**Answer:** John Smith founded Acme Corp in 2015.
**Confidence:** 85%
**Reasoning:**
1. Analyzed query: "Who founded the company?" (confidence: 95%)
2. Found 3 relevant entities (confidence: 85%)
3. Retrieved 5 relevant text chunks (confidence: 85%)
4. Synthesized answer from retrieved information (confidence: 85%)
**Sources:**
1. [TextChunk] chunk_123 (relevance: 92%)
2. [Entity] john_smith (relevance: 88%)
Error Handling
Errors implement standard std::error::Error and carry descriptive messages:
match graphrag.ask.await
CLI Setup Wizard
Interactive configuration wizard:
# With template:
# Custom output:
Wizard prompts:
- Select use case (General, Legal, Medical, Financial, Technical)
- Choose LLM provider (Ollama or pattern-based)
- Configure Ollama settings (if selected)
- Set output directory
Full Usage Example
use *;
async
Embedding Providers
GraphRAG Core supports 8 embedding backends:
| Provider | Cost | Quality | Feature Flag | Use Case |
|---|---|---|---|---|
| HuggingFace | Free | ★★★★ | huggingface-hub |
Offline, 100+ models |
| OpenAI | $0.13/1M | ★★★★★ | ureq |
Best quality |
| Voyage AI | Medium | ★★★★★ | ureq |
Anthropic recommended |
| Cohere | $0.10/1M | ★★★★ | ureq |
Multilingual (100+ langs) |
| Jina AI | $0.02/1M | ★★★★ | ureq |
Cost-optimized |
| Mistral | $0.10/1M | ★★★★ | ureq |
RAG-optimized |
| Together AI | $0.008/1M | ★★★★ | ureq |
Cheapest |
| Ollama | Free | ★★★★ | ollama + async |
Local GPU + LLM |
Advanced Features
LightRAG (Dual-Level Retrieval)
[]
= "hybrid"
= true # 6000x token reduction!
PageRank (Fast-GraphRAG)
[]
= true # 27x performance boost
RoGRAG (Logic Form Reasoning)
// Enable with feature flag: rograg
let answer = graphrag.ask_with_reasoning.await?;
Intelligent Caching
[]
= true # 80%+ hit rate, 6x cost reduction
Pipeline Architecture
GraphRAG uses a configurable pipeline with different methods for each phase:
┌─────────────────────────────────────────────────────────────────────────┐
│ build_graph() │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ │
│ │ CHUNKING │ TextProcessor splits document into chunks │
│ │ (always runs) │ Configurable: chunk_size, chunk_overlap │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ ENTITY EXTRACTION │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ Algorithmic │ │ Semantic │ │ Hybrid │ │ │
│ │ │ (pattern-based) │ │ (LLM-based) │ │ (both + fusion) │ │ │
│ │ │ ⚡ Fast │ │ 🎯 Accurate │ │ ⚖️ Balanced │ │ │
│ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ RELATIONSHIP EXTRACTION │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ Co-occurrence │ │ LLM-based │ │ Gleaning │ │ │
│ │ │ entity proximity│ │ GraphRAG method │ │ multi-round LLM │ │ │
│ │ │ ⚡ Fast │ │ 🎯 Semantic │ │ 🔄 Iterative │ │ │
│ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │
│ │ Optional: config.graph.extract_relationships = true/false │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ GRAPH │ Entities + Relationships → KnowledgeGraph │
│ │ CONSTRUCTION │ Supports: PageRank, Community Detection │
│ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────┐
│ ask() / query │
├─────────────────────────────────────────────────────────────────────────┤
│ ┌─────────────────┐ │
│ │ EMBEDDING │ Generated on-demand (lazy evaluation) │
│ │ GENERATION │ 8 providers: Ollama, OpenAI, HuggingFace, etc. │
│ └────────┬────────┘ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ RETRIEVAL STRATEGIES │ │
│ │ Vector │ BM25 │ PageRank │ Hybrid │ Adaptive │ LightRAG │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ ANSWER │ LLM synthesis (if Ollama enabled) │
│ │ GENERATION │ Or: concatenated search results │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
Phase Configuration Quick Reference
| Phase | Key Parameters | Config |
|---|---|---|
| 1. Chunking | chunk_size, chunk_overlap |
chunk_size = 1000 |
| 2. Entity Extraction | approach, entity_types, use_gleaning |
approach = "hybrid" |
| 3. Relationship Extraction | extract_relationships, use_gleaning |
[graph] extract_relationships = true |
| 4. Graph Construction | enable_pagerank, max_connections |
[graph] enable_pagerank = true |
| 5. Embedding | backend, dimension, model |
[embeddings] backend = "ollama" |
| 6. Retrieval | strategy, top_k |
[retrieval] strategy = "hybrid" |
| 7. Answer Generation | chat_model, temperature |
[ollama] enabled = true |
Method Selection by Phase
| Phase | Methods Available | Config Setting |
|---|---|---|
| Entity Extraction | Algorithmic / Semantic / Hybrid | approach = "algorithmic|semantic|hybrid" |
| Relationship Extraction | Co-occurrence / LLM-based / Gleaning | entities.use_gleaning = true|false |
| Embedding | Ollama / Hash / OpenAI / HuggingFace / 8 providers | embeddings.backend = "ollama" |
| Retrieval | Vector / BM25 / PageRank / Hybrid / Adaptive / LightRAG | retrieval.strategy = "hybrid" |
Key Notes
- Embedding is NOT part of
build_graph()- generated lazily during queries - Relationship extraction is optional - controlled by
config.graph.extract_relationships - Gleaning extracts entities AND relationships together in multi-round LLM calls
- See PIPELINE_ARCHITECTURE.md for full parameter reference
Module Structure
graphrag-core/
├── src/
│ ├── builder/ # TypedBuilder with type-state pattern
│ ├── config/ # Hierarchical configuration (figment)
│ ├── core/ # Core traits, errors with suggestions
│ ├── embeddings/ # 8 embedding providers
│ ├── entity/ # LLM-based gleaning extraction
│ ├── graph/ # Knowledge graph construction
│ ├── retrieval/ # ExplainedAnswer, search strategies
│ └── templates/ # Sectoral configuration templates
└── examples/
Testing
# Quick test with starter features
# Full test suite
# Test specific modules
Documentation
- QUICKSTART.md - 5-minute getting started guide
- PIPELINE_ARCHITECTURE.md - Pipeline phases and methods
- templates/README.md - Sectoral template guide
- EMBEDDINGS_CONFIG.md - Embedding configuration
- ENTITY_EXTRACTION.md - LLM-based extraction guide
- OLLAMA_INTEGRATION.md - Ollama setup guide
Cross-Platform Support
- ✅ Linux - Full support with all features
- ✅ macOS - Full support with Metal GPU acceleration
- ✅ Windows - Full support with CUDA GPU acceleration
- ✅ WASM - Core functionality (use
wasm-bundlefeature)
License
MIT License - see ../LICENSE for details.
Part of the GraphRAG-rs project | Main README | Quick Start