GraphRAG: A Graph-based Retrieval-Augmented Generation System

repository·main·Indexed 12 days ago

https://github.com/microsoft/graphrag

A data pipeline and transformation suite that uses LLMs to extract structured knowledge graphs from unstructured text to enhance reasoning over private data. It includes modules for text chunking (SentenceChunker, TokenChunker), configuration management, LLM completion and embedding integration, and extensible storage providers including FileStorage, AzureBlobStorage, and AzureCosmosStorage.

Tokens
106.7K
Snippets
274
Records
398
Agent score
99%

What's inside GraphRAG

  1. Overview of the GraphRAG Indexing Dataflow

    main

    The default GraphRAG indexing workflow transforms raw text documents into a structured knowledge model through six distinct phases:

    1. Compose TextUnits: Documents are split into configurable chunks called TextUnits (default size is 1200 tokens).
    2. Document Processing: Links TextUnits back to their original Documents to maintain provenance.
    3. Graph Extraction: Extracts Entities, Relationships, and (optionally) Claims (Covariates) from TextUnits using an LLM.
    4. Graph Augmentation: Uses the Hierarchical Leiden Algorithm to detect a hierarchy of Communities within the graph.
    5. Community Summarization: Generates and summarizes Community Reports to provide high-level and low-level overviews of graph clusters.
    6. Text Embedding: Generates embeddings for TextUnits, entity descriptions, and community report content for vector search.
  2. Overview of GraphRAG

    main
    GraphRAG is a data pipeline and transformation suite designed to extract structured, meaningful data from unstructured text using Large Language Models (LLMs). It utilizes knowledge graph memory structures to enhance an LLM's ability to reason about private, narrative data.
  3. Supported file formats for GraphRAG Inputs

    main

    The graphrag-input package provides utilities for loading document data into GraphRAG. It supports the following standard file formats natively:

    • CSV: Tabular data (supports configurable column mappings).
    • JSON: JSON files (supports configurable property paths).
    • JSON Lines: Line-delimited JSON records.
    • Text: Plain text files.
  4. What is GraphRAG and how does it differ from Baseline RAG?

    main

    GraphRAG is a structured, hierarchical approach to Retrieval Augmented Generation (RAG) that uses knowledge graphs instead of plain text snippets.

    Key Differences

    • Baseline RAG: Uses vector similarity (semantic search) to find text snippets. It often struggles to 'connect the dots' between disparate pieces of information or to provide holistic summaries of large datasets.
    • GraphRAG: Extracts a knowledge graph from an input corpus, builds a community hierarchy, and generates summaries for these communities. This allows the system to reason about complex relationships and synthesized insights that baseline RAG might miss.

    The GraphRAG Process

    1. Index: Slices the corpus into TextUnits, extracts entities, relationships, and claims, performs hierarchical clustering (using the Leiden technique), and generates bottom-up community summaries.
    2. Query: Uses the indexed structures to augment LLM prompts via different search modes.
  5. What is GraphRAG Indexing?

    main

    GraphRAG Indexing is a configurable data pipeline and transformation suite designed to extract structured data from unstructured text using Large Language Models (LLMs).

    Key capabilities of the standard pipeline include:

    • Extracting entities, relationships, and claims from raw text.
    • Performing community detection on entities.
    • Generating community summaries and reports at multiple levels of granularity.
    • Embedding text into a vector space.

    By default, pipeline outputs are stored as Parquet tables, and embeddings are written to your configured vector store.

  6. What is Local Search in GraphRAG?

    main

    Local Search is a retrieval method that combines structured data from a knowledge graph with unstructured data from input documents to augment the LLM context.

    It is specifically designed for entity-based reasoning, making it ideal for questions that require understanding specific entities mentioned in documents (e.g., "What are the healing properties of chamomile?").

    How it works

    1. Entity Extraction: The method identifies entities from the knowledge graph that are semantically related to the user query.
    2. Graph Traversal: These entities act as access points to extract connected entities, relationships, entity covariates, and community reports.
    3. Text Retrieval: It extracts relevant text chunks from raw documents associated with the identified entities.
    4. Context Prioritization: Candidate data sources are ranked and filtered to fit within a pre-defined context window size to generate the final response.
  7. What is Global Search in GraphRAG?

    main

    Global Search is a retrieval method designed for 'whole dataset reasoning.' Unlike baseline RAG, which relies on vector similarity to find specific text chunks, Global Search uses the LLM-generated knowledge graph's community hierarchy to answer queries that require aggregation or thematic analysis (e.g., "What are the top 5 themes in the data?").

    It works by using pre-summarized community reports as context. This allows the system to understand the semantic structure of the entire dataset rather than just isolated pieces of text.

  8. What is Entity-based Question Generation

    main

    Entity-based Question Generation is a method that combines structured data from the knowledge graph with unstructured data from input documents to generate candidate questions related to specific entities.

    It uses a context-building approach similar to local search to extract and prioritize relevant data, including:

    • Entities
    • Relationships
    • Covariates
    • Community reports
    • Raw text chunks

    These records are fed into an LLM prompt to generate follow-up questions that represent the most important or urgent themes or information content within the data.

  9. What is DRIFT Search

    main

    DRIFT Search (Dynamic Reasoning and Inference with Flexible Traversal) is a retrieval method in GraphRAG that combines the strengths of both Global Search and Local Search.

    While standard Local Search focuses on specific entities and relationships, DRIFT Search incorporates community information into the local search process. This expands the breadth of the query's starting point, allowing the engine to use community insights to refine queries into detailed follow-up questions. This results in a higher variety of facts being retrieved and used in the final answer, balancing computational cost with high-quality, comprehensive outcomes.

  10. Configure chunking and metadata prepending

    main

    GraphRAG processes documents by splitting them into smaller "text units" (chunks) to fit within language model context windows. By default, chunking splits content evenly, which can cause loss of context if important information (like headlines or authors) only appears at the start of a document.

    To ensure every chunk contains shared document information, use the prepend_metadata setting in the chunks configuration block. This copies selected document fields to the start of every text chunk as key: value pairs on new lines.

    Key configuration settings:

    • chunk_size: The target size for each text unit.
    • overlap: The number of tokens to overlap between consecutive chunks.
    • prepend_metadata: A boolean that, when true, instructs the importer to include metadata at the start of every chunk.
    input:
        type: text
        metadata: [title]
    
    chunks:
        size: 100
        overlap: 0
        prepend_metadata: true
  11. Considerations for OpenAI o-series reasoning models

    main

    GraphRAG 2.2.0+ supports OpenAI o-series models, but they behave differently than standard models:

    • Response Control: GraphRAG has moved from using max_tokens to a prompted approach for controlling response length. While you can still use max_tokens (or max_completion_tokens for o-series) for budgetary limits, it is not recommended for controlling expected response length due to unknown reasoning token consumption.
    • Binary Questions: The previous method of using logit_bias to force binary (yes/no) answers is not compatible with reasoning models. GraphRAG now uses prompting to achieve this.
    • Prompt Tuning: o-series models use native chain-of-thought reasoning. Because GraphRAG's existing prompts sometimes include manual CoT techniques, you may need to tune or rewrite prompt templates (especially for graph and claim extraction) to avoid counterproductive results.
    • Cost/Speed: o-series models are generally slower and more expensive; consider an asymmetric model configuration (e.g., using standard models for indexing and o-series for querying).
  12. Files generated by graphrag init

    main

    Running graphrag init creates the following files and directories in your specified --root directory:

    • settings.yaml: The primary configuration settings file for GraphRAG.
    • .env: An environment variables file containing secrets and settings referenced by settings.yaml.
    • prompts/: A directory containing the default LLM prompts used by the system. You can manually modify these or use the Auto Prompt Tuning command to generate data-specific prompts.