DIGIMON GraphRAG

repository·master·Indexed 23 days ago

https://github.com/jaylzhou/graphrag

A modularized framework for the deep analysis of Graph-based Retrieval-Augmented Generation (RAG) systems. DIGIMON provides a unified interface to run and compare various GraphRAG methods, including RAPTOR, HippoRAG, LightRAG, and MS GraphRAG. It supports multiple graph types (Chunk Tree, Passage Graph, KG, TKG, RKG) and five categories of retrieval operators (Entity, Relationship, Chunk, Subgraph, and Community). The framework integrates with OpenAI and local LLM backends via Ollama and LlamaFactory.

Tokens
2.1K
Snippets
7
Records
12
Agent score
32%

What's inside DIGIMON

  1. Understand the five types of retrieval operators in GraphRAG

    master

    DIGIMON classifies the retrieval stage of GraphRAG into five distinct categories of operators. These operators are used to build retrieval modules by combining one or more specific methods to identify query-relevant content from graph-based data.

    1. Entity Operators: Retrieve entities (people, places, organizations) relevant to a query.
    2. Relationship Operators: Extract useful relationships for a given query.
    3. Chunk Operators: Retrieve the most relevant text segments (chunks) related to the query.
    4. Subgraph Operators: Extract a relevant subgraph for a given query.
    5. Community Operators: Identify high-level information (specifically used for MS GraphRAG implementations).
  2. Understand Graph Types in DIGIMON

    master

    DIGIMON categorizes graphs based on the entities and relations they contain. Understanding these types is crucial for selecting the right method:

    • Chunk Tree: A tree structure formed by document content and summary.
    • Passage Graph: A relational network composed of passages, tables, and other elements within documents.
    • KG (Knowledge Graph): Constructed by extracting entities and relationships (triples) from chunks.
    • TKG (Textual Knowledge Graph): A specialized KG that enriches entities with detailed descriptions and type information.
    • RKG (Rich Knowledge Graph): A graph that further incorporates keywords associated with relations.

    Comparison of Graph Attributes:

    AttributeChunk TreePassage GraphKGTKGRKG
    Original Content
    Entity Name
    Entity Type
    Entity Description
    Relation Name
    Relation Keyword
    Relation Description
    Edge Weight
  3. Run a GraphRAG method

    master

    You can execute different GraphRAG methods by providing a specific configuration file (.yaml) and a dataset name using the main.py entry point.

    Command Format:

    python main.py -opt Option/Method/<METHOD>.yaml -dataset_name your_dataset

    Available Methods:

    • Dalk
    • GR
    • LGraphRAG (Local search in GraphRAG)
    • GGraphRAG (Global search in GraphRAG)
    • HippoRAG
    • KGP
    • LightRAG
    • RAPTOR
    • ToG
    • GraphRAG (via Option/Method/GraphRAG.yaml)

    Example (Running RAPTOR):

    python main.py -opt Option/Method/RAPTOR.yaml -dataset_name your_dataset
    python main.py -opt Option/Method/RAPTOR.yaml -dataset_name your_dataset
  4. Install DIGIMON GraphRAG from source

    master

    To use DIGIMON, clone the repository from GitHub and navigate to the project directory.

    # Clone the repository from GitHub
    git clone https://github.com/JayLZhou/GraphRAG.git
    cd GraphRAG
    # Clone the repository from GitHub
    git clone https://github.com/JayLZhou/GraphRAG.git
    cd GraphRAG
  5. Configure LLM backends (OpenAI or Local)

    master

    DIGIMON supports cloud-based models (OpenAI) and locally deployed models (Ollama and LlamaFactory).

    To use a local model, set api_type to open_llm in your configuration file. For local models, you must provide the base_url and the model name, while api_key is not strictly required but can be set to any placeholder string.

    Configuration Schema (config.yaml):

    llm:
      api_type: "openai/open_llm"  # Options: "openai" or "open_llm" (For Ollama and LlamaFactory)
      model: "YOUR_LOCAL_MODEL_NAME"
      base_url: "YOUR_LOCAL_URL"  # Change this for local models
      api_key: "YOUR_API_KEY"  # Not required for local models
    llm:
      api_type: "openai/open_llm"  # Options: "openai" or "open_llm" (For Ollama and LlamaFactory) 
      model: "YOUR_LOCAL_MODEL_NAME"
      base_url: "YOUR_LOCAL_URL"  # Change this for local models
      api_key: "YOUR_API_KEY"  # Not required for local models
  6. Initialize and run GraphRAG via main.py

    master

    The main.py script serves as the primary entrypoint for running the DIGIMON system. It orchestrates the full lifecycle: loading configuration, inserting a corpus into the GraphRAG system, executing queries from a dataset, and evaluating the results.

    To run the system, you must provide a path to a YAML configuration file and a dataset name via CLI arguments.

    Workflow:

    1. Configuration: Load settings using Config.parse().
    2. Initialization: Instantiate GraphRAG(config=opt).
    3. Data Ingestion: Load a RAGQueryDataset and use digimon.insert(corpus) to populate the graph.
    4. Querying: Use wrapper_query to iterate through questions and collect answers.
    5. Evaluation: Use wrapper_evaluation to compute metrics based on the query results.
  7. How existing GraphRAG methods combine operators

    master

    Existing GraphRAG algorithms are constructed by combining different types of operators. Here are representative examples of how these combinations work:

    • HippoRAG: Uses the Chunk (Aggregator) operator.
    • LightRAG: Combines Chunk (FromRel), Entity (RelNode), and Relationship (VDB).
    • FastGraphRAG: Combines Chunk (Aggregator), Entity (PPR), and Relationship (Aggregator).
    | Name | Operators |
    |---|---|
    | **HippoRAG** | Chunk (Aggregator) |
    | **LightRAG** | Chunk (FromRel) + Entity (RelNode) + Relationship (VDB) |
    | **FastGraphRAG** | Chunk (Aggregator) + Entity (PPR) + Relationship (Aggregator) |
  8. Use GraphRAG for data insertion and querying

    master

    The GraphRAG class is the core engine of the system. It supports asynchronous operations for both ingesting data and retrieving answers.

    • insert(corpus): An asynchronous method used to ingest a list of documents (the corpus) into the graph structure.
    • query(question): An asynchronous method that takes a string question and returns the generated answer based on the graph context.

    Example usage:

    from Core.GraphRAG import GraphRAG
    from Option.Config2 import Config
    import asyncio
    
    # Setup
    opt = Config.parse(path_to_yaml, dataset_name="my_dataset")
    digimon = GraphRAG(config=opt)
    
    # Ingest data
    corpus = ["Document 1 content", "Document 2 content"]
    asyncio.run(digimon.insert(corpus))
    
    # Query data
    answer = asyncio.run(digimon.query("Who is Fred Gehrke?"))
    print(answer)
    digimon = GraphRAG(config=opt)
    asyncio.run(digimon.insert(corpus))
    res = asyncio.run(digimon.query("Who is Fred Gehrke?"))
  9. Execute batch queries with wrapper_query

    master

    The wrapper_query function is a utility for running an entire dataset of questions through the GraphRAG instance and saving the results.

    Parameters:

    • query_dataset: An instance of RAGQueryDataset containing the questions.
    • digimon: An initialized GraphRAG instance.
    • result_dir: The directory where the results should be saved.

    Output: Returns the file path to a results.json file containing the questions and their corresponding outputs in JSON Lines format.

    save_path = wrapper_query(query_dataset, digimon, result_dir)
  10. Evaluate RAG performance with wrapper_evaluation

    master

    The wrapper_evaluation function automates the evaluation process using the Evaluator class.

    Parameters:

    • path: The file path to the results.json generated by wrapper_query.
    • opt: The configuration object.
    • result_dir: The directory where the metrics.json file will be saved.

    It initializes an Evaluator with the results path and the dataset_name, runs the evaluation asynchronously, and writes the resulting metrics dictionary to metrics.json inside the result_dir.

    async def wrapper_evaluation(path, opt, result_dir):
        eval = Evaluator(path, opt.dataset_name)
        res_dict = await eval.evaluate()
        save_path = os.path.join(result_dir, "metrics.json")
        with open(save_path, "w") as f:
            f.write(str(res_dict))