GraphRAG-rs

repository·main·Indexed 19 days ago

https://github.com/automataia/graphrag-rs

A high-performance, modular Rust implementation of Graph-based Retrieval Augmented Generation. It features a meta-crate bundling graphrag-core and graphrag-cli, supporting Server-Only, WASM-Only, and Hybrid deployment modes with GPU acceleration via WebGPU. The library includes specialized configuration templates for narrative, technical, academic, legal, and web content, and provides a TUI for interactive querying, graph management, and benchmarking.

Tokens
71.9K
Snippets
223
Records
321
Agent score
67%

What's inside graphrag-rs

  1. Overview of GraphRAG-RS

    main

    GraphRAG-RS is a modular and portable implementation of GraphRAG written in Rust. It automates the creation of a knowledge graph from documents through a pipeline of chunking, embeddings, entity and relationship extraction, and community detection. Once the graph is built, it allows for answering questions over the graph with citations.

    Key features include:

    • Runtime Configuration: Choose between pattern-only (no LLM, < 10 ms/chunk), LLM + KV-cache enrichment (e.g., via Ollama), or a hybrid approach using the Config object.
    • Dual Environments: The core library (graphrag-core) is designed to run both natively and in the browser via WebAssembly (WASM). The WASM build utilizes the Voy vector store.
    • Modular Architecture: The project is split into specialized crates including graphrag-core, graphrag-cli, graphrag-server, and graphrag-wasm.
  2. Check Cross-Platform Support for GraphRAG

    main

    GraphRAG Core supports the following platforms with varying levels of acceleration:

    • Linux: Full support with all features.
    • macOS: Full support with Metal GPU acceleration.
    • Windows: Full support with CUDA GPU acceleration.
    • WASM: Core functionality available via the wasm-bundle feature.
  3. Implementation status and roadmap

    main

    Phase 1: Core Implementation (COMPLETE)

    • Modular Architecture: 50,000+ lines across 25+ modules.
    • Retrieval Engines: Includes Fast-GraphRAG (PageRank-based) and LightRAG (dual-level retrieval).
    • Hybrid Retrieval: Combines Semantic, Keyword, BM25, and Graph retrieval.
    • Reasoning: Supports ROGRAG (query decomposition), temporal reasoning, and causal reasoning.
    • Server: graphrag-server provides a REST API using Actix-web 4.9 and Apistos (OpenAPI 3.0.3).
    • Integrations: Qdrant vector database and Ollama embeddings.

    Phase 2: WASM & Web UI (IN PROGRESS)

    • graphrag-wasm: WASM bindings for browser-native RAG.
    • Inference: Uses ONNX Runtime Web for GPU embeddings and WebLLM for GPU LLM.
    • UI: A 3-column Nordic-Minimal UI built with Leptos featuring citations and SVG subgraph views.

    Phase 3 & 4: Planned Features

    • Performance: Distributed caching (Redis), OpenTelemetry, and multi-model embeddings.
    • Analytics: Community detection, entity clustering, and quality metrics.
    • Data: Bulk import (CSV, JSON, RDF) and connectors (Notion, Confluence).
    • Enterprise: High availability, horizontal scaling, and multi-language SDKs (Python, TypeScript, Go).
  4. Choose the right GraphRAG-rs crate for your deployment

    main

    GraphRAG-rs is organized into a 5-crate Cargo workspace. Select the crate that matches your intended use case:

    • graphrag-core: The core library containing all GraphRAG logic. It is available as both a native library (rlib) and a WASM library (cdylib).
    • graphrag-cli: A turnkey CLI and TUI binary for in-process use. It uses the core library directly without an HTTP layer.
    • graphrag-server: An Actix-web REST API that includes OpenAPI support and optional integration with Qdrant.
    • graphrag-wasm: Browser bindings designed for client-side execution using Voy vector store, WebLLM, and ONNX.
    • graphrag: A meta-crate that re-exports graphrag-core, intended for a simple "hello-world" experience.
  5. What is GraphRAG and how does it differ from traditional RAG?

    main
    GraphRAG (Graph-based Retrieval-Augmented Generation) transforms unstructured text into an interconnected knowledge graph of people, places, concepts, and relationships. Unlike traditional RAG, which relies on flat vector chunks and semantic similarity, GraphRAG enables multi-hop reasoning through graph traversal and provides better context awareness by understanding hierarchies and relationships. This approach can lead to significant improvements in accuracy and token efficiency.
  6. How ChunkingStrategy and TextProcessor work together

    main

    The chunking architecture uses a trait-based pattern to decouple the text processing logic from specific splitting algorithms.

    1. ChunkingStrategy Trait: Defines the interface for any splitting logic. Available implementations include HierarchicalChunkingStrategy (boundary preservation), SemanticChunkingStrategy (embedding-based), and RustCodeChunkingStrategy (AST-based).
    2. TextProcessor: Acts as the orchestrator. You use the chunk_with_strategy method to apply a specific strategy to a Document.

    This design allows you to swap strategies (e.g., using Tree-sitter for code and Hierarchical for prose) without changing the core processing pipeline.

    // Hierarchical - preserves paragraph/sentence boundaries
    let strategy = HierarchicalChunkingStrategy::new(1000, 100, document.id.clone());
    let chunks = processor.chunk_with_strategy(&document, &strategy)?;
    
    // Tree-sitter - preserves syntactic boundaries for code
    let code_strategy = RustCodeChunkingStrategy::new(50, document_id);
    let code_chunks = code_strategy.chunk(rust_code)?;
  7. Relationship between graphrag-wasm and graphrag-core

    main

    The graphrag-wasm crate is a functional implementation that links to graphrag-core (via path dependency) to drive a real graphrag_core::GraphRAG instance.

    What is shared from graphrag-core:

    • Document ingestion (add_document_from_text)
    • Knowledge-graph types (Entity, Relationship)
    • Leiden community detection
    • Adaptive query routing

    What is reimplemented in WASM for browser compatibility:

    • Embeddings: onnx_embedder.rs (ONNX Runtime Web / WebGPU)
    • Entity extraction: entity_extractor.rs (WebLLM or rule-based)
    • Vector search: vector_search.rs (pure-Rust cosine similarity)

    Note: src/lib.rs also exposes a separate wasm_bindgen GraphRAG wrapper for direct JavaScript use (e.g., new GraphRAG(384)), which is distinct from the graphrag_core::GraphRAG instance.

  8. Configure the GraphRAG-rs pipeline via TOML

    main

    GraphRAG-rs uses a configuration-driven architecture where the behavior of the pipeline is determined by a TOML file. You can switch between a fast, pattern-based system (no LLM required) and a high-accuracy AI system (LLM-based) simply by changing settings. This allows for dynamic stage selection for text chunking, embeddings, entity extraction, relationships, retrieval, and generation without changing the codebase.

    # Example 1: Fast, No-LLM Pipeline
    [entity_extraction]
    use_gleaning = false          # ← Pattern-based extraction
    
    [ollama]
    enabled = false               # ← No LLM required
  9. Understand the GraphRAG Pipeline Stages

    main

    The GraphRAG-rs pipeline transforms raw text into an intelligent knowledge graph through seven distinct stages:

    1. Text: The raw input document.
    2. Chunks: Breaking text into manageable segments.
    3. Vectors: Generating embeddings for chunks.
    4. Entities: Extracting key concepts, people, and relationships.
    5. Graph: Constructing the knowledge graph from entities and relationships.
    6. Retrieval: Searching the graph and vectors for relevant context.
    7. Query & Answer: Processing the user query against the retrieved context to generate a final response.
  10. Optimize Answer Generation with Caching

    main

    Answer generation synthesizes retrieved context into a natural language response using LLM backends like Ollama or WebLLM. To reduce costs and latency, GraphRAG-rs implements semantic caching.

    Caching Benefits:

    • Cost Reduction: Up to 6x reduction.
    • Latency Reduction: From ~100ms down to ~5ms.
    • Hit Rate: Typically 80%+ in production workloads.

    Implementation uses a cached_client that generates a semantic key from the prompt to check for existing responses before calling the LLM.

  11. Deployment Architecture: WASM-Only (Client-Side)

    main

    The WASM-Only architecture runs 100% client-side in the browser. It is ideal for privacy-first applications, offline tools, and zero-infrastructure edge deployment. It uses ONNX Runtime Web for GPU-accelerated embeddings and WebLLM (Phi-3-mini) for LLM synthesis.

    # Install build tools
    cargo install trunk wasm-bindgen-cli
    
    # Build and run
    cd graphrag-wasm
    trunk serve --open
  12. Compare Storage Backends

    main

    Select a storage backend based on your deployment needs:

    • Qdrant: Best for production and large-scale deployments (100M+ vectors). Requires a separate server (Docker/Cloud). Supports distributed deployment and advanced filtering.
    • LanceDB: Best for desktop or embedded applications. No server required (embedded), zero-copy access, and works offline. Data is stored locally.
    • In-Memory: Best for development and testing. No dependencies and very fast, but data is lost on restart and it does not scale.