ROGRAG: Robustly Optimized GraphRAG

repository·main·Indexed 41 days ago

https://github.com/tpoisonooo/rograg

A robust Graph Retrieval-Augmented Generation (GraphRAG) implementation designed to improve LLM accuracy on specialized, domain-specific knowledge. It features a two-stage retrieval mechanism (dual-level and logic form methods), incremental database construction, and enhanced fuzzy matching. The system integrates with TuGraph for graph storage and supports v1 and v2 APIs for response generation.

Tokens
11.7K
Snippets
40
Records
52
Agent score
51%

What's inside ROGRAG

  1. Overview of ROGRAG

    main

    ROGRAG is a robust GraphRAG (Graph Retrieval-Augmented Generation) approach designed to enhance LLM performance on specialized topics. It utilizes a two-stage retrieval mechanism (dual-level and logic form methods) to improve accuracy without increasing computational costs.

    Key features include:

    • Two-stage retrieval: Enhances robustness in finding relevant information.
    • Incremental database construction: Allows for efficient knowledge updates.
    • Enhanced fuzzy matching and structured reasoning: Improves the precision of retrieved knowledge.

    ROGRAG has demonstrated significant performance improvements on the SeedBench benchmark compared to vanilla RAG, LangChain, BM25, and RQ-RAG.

  2. What is ROGRAG?

    main

    ROGRAG is a robustly optimized GraphRAG implementation that merges multiple open-source projects (HuixiangDou, KAG, LightRAG, and DB-GPT) to improve retrieval accuracy. It is designed to overcome limitations in standard RAG pipelines by using a two-stage retrieval process that strengthens fuzzy matching and logical reasoning retrieval. It also supports incremental knowledge base construction.

    Key features include:

    • Two-stage retrieval: Enhances fuzzy matching and logical reasoning.
    • Incremental knowledge base construction: Allows for efficient updates to the graph.
    • High accuracy: Demonstrated significant improvements over vanilla RAG and LangChain on specialized test sets.
  3. Understand the RAG Project Structure

    main

    The project is organized into several top-level directories that separate implementation, testing, and configuration:

    • huixiangdou/: The core implementation of the system.
    • config.ini.example: A configuration template (note: this file uses TOML format despite the .ini extension).
    • evaluation/: Tools for testing pipeline accuracy.
    • tests/: Verification of specific code snippets.
    • unittest/: Standard unit tests for the codebase.
    .
    ├── config.ini.example  # Configuration example
    ├── evaluation          # Pipeline accuracy testing tool
    ├── huixiangdou         # Implementation
    ├── tests               # Code snippet verification
    └── unittest            # Unit tests
  4. Understand the project directory structure

    main

    The project is organized into several top-level directories that separate configuration, implementation, testing, and evaluation:

    • config.ini.example: A template configuration file (note: the actual format is TOML, despite the .ini extension).
    • huixiangdou/: The core implementation of the project.
    • evaluation/: Tools for testing pipeline accuracy.
    • tests/: Verification of specific code snippets.
    • unittest/: Standard unit tests.
    .
    ├── config.ini.example  # 配置样例
    ├── evaluation          # pipeline 精度测试工具
    ├── huixiangdou         # 实现
    ├── tests               # 代码片段验证
    └── unittest            # 单元测试
  5. Choosing a Text Splitter

    main

    When setting up your RAG pipeline, the choice of splitter affects the quality of the retrieved chunks. Based on evaluation results:

    • For Chinese priority: Use ChineseTextSplitter. This results in centrifugal values.
    • For English priority: Use langchain.RecursiveTextSplitter. This cuts Chinese corpus more finely but lacks centrifugal values.
    • Avoid: CharacterTextSplitter, as it does not actually slice the text effectively and should not be used for direct slicing.
  6. Explore the huixiangdou Module Components

    main

    The huixiangdou directory contains the core logic, ranging from low-level primitives to high-level interfaces:

    • Interfaces & Access Methods:

      • main.py: Entry point for Command-line (CLI) usage.
      • server.py: Entry point for HTTP API usage.
      • gradio_ui.py: Access method via Gradio UI.
      • frontend/: Integration methods for platforms like WeChat and Feishu.
    • Logic Layers:

      • pipeline/: High-level logic for building knowledge bases and managing query pipelines.
      • service/: Middle-level logic (e.g., graph implementations).
      • primitive/: Low-level utility tools such as Chunk definitions, splitting methods, and Embedder encapsulation. These are designed to be portable and can be reused in other repositories.
    • Retrieval Methods (located within the retriever sub-module):

      • bm25.py: BM25 retrieval.
      • dense.py: Dense retrieval.
      • inverted.py: Inverted index retrieval.
      • knowledge.py: General graph retrieval.
      • logic/: Logical reasoning graph retrieval.
      • web.py: Web retrieval.
      • pool.py: A factory class used to instantiate the retrieval methods above.
  7. Splitter Selection Guidance

    main

    When choosing a splitter for text processing, consider the following trade-offs observed during testing:

    • ChineseTextSplitter: Prioritizes Chinese text but may result in outlier values (离心值).
    • langchain.RecursiveTextSplitter: Good for English; when used on Chinese corpora, it results in smaller/more fragmented chunks but avoids outlier values.
    • CharacterTextSplitter: Does not effectively perform splitting in practice; avoid using it directly.
  8. Getting started with HuixiangDou

    main

    To quickly familiarize yourself with HuixiangDou, follow these steps in order:

    1. Run the basic version: Follow the instructions in the main README.md to get the system up and running with default settings.
    2. Enhance performance: Refer to the advanced tutorial to optimize and enhance the overall effect of the system.

    For a more detailed walkthrough, consult the doc_how_to_run.md guide in the Quick Start section.

  9. Configure HuixiangDou2 via config.ini

    main

    Before running the pipeline, you must configure the config.ini file. This includes setting your LLM API key (e.g., from SiliconCloud).

    1. Copy the example configuration file to create your active config:
      cp config.ini.example config.ini
    2. Open config.ini and fill in your SiliconCloud SK (API Key).

    For a detailed breakdown of all configuration options, refer to the Configuration Guide.

    cp config.ini.example config.ini
  10. Run Rejection Accuracy Tests

    main

    To perform rejection accuracy testing (evaluating the upper and lower bounds of chunksize for text2vec models), follow these steps:

    1. Prepare Data: Populate gt_bad.txt and gt_good.txt with your annotated positive and negative examples.
    2. Execute Processing: Run the filtering script to calculate tokenized lengths in debug mode:
      python3 evaluation/rejection/build_fs_and_filter.py
    3. Visualize Results: Use the plotting script to generate F1 score charts for different chunksize and throttle values:
      python3 plot.py
    python3 evaluation/rejection/build_fs_and_filter.py
    python3 plot.py
  11. Retrieve information via CLI, Gradio, or Swagger API

    main

    Once the services are running, you can perform retrieval using several interfaces:

    1. Command Line Interface

    Run the main.py module to execute query samples directly in the terminal:

    python3 -m huixiangdou.main

    2. Gradio UI

    To use the Gradio web interface, you must first start the server, then start the client:

    # Start the server
    python3 -m huixiangdou.server --lgraph-data-path /path/to/lgraph-data
    
    # Start the Gradio client
    python3 -m huixiangdou.gradio_ui --port 8888

    Access the UI at http://<host>:8888 for streaming responses.

    3. Swagger API

    To use the REST API via Swagger:

    # Start the server
    python3 -m huixiangdou.server
    
    # Run the client test script
    python3 huixiangdou/client.py

    Access the SwaggerUI at port 8888.

    # Gradio Setup
    python3 -m huixiangdou.server --lgraph-data-path /path/to/lgraph-data
    python3 -m huixiangdou.gradio_ui --port 8888
  12. Build the Knowledge Base

    main

    After starting VLLM, you can build the knowledge base using documents located in HuixiangDou/tests/data.

    1. Copy the test data to the repodir directory:
      cd HuixiangDou
      cp -rf tests/data repodir
    2. Run the pipeline store module:
      python3 -m huixiangdou.pipeline.store

    Once built, you can view the resulting graph by opening port 17070 in a browser. Use credentials admin / 73@TuGraph to log in.

    cd HuixiangDou
    cp -rf tests/data repodir
    python3 -m huixiangdou.pipeline.store