HiRAG

hhy-huangMIT更新放缓

基于层次化知识图谱的 Graph RAG 系统,把文档组织成分层结构来提升检索增强生成的召回与准确度,开箱即用。

[EMNLP'25 findings] An easy-to-use Graph RAG system using hierarchical knowledge.

暂无版本555star81fork同步于 2026-11-07 07:29 UTC

怎么上手

由 README 提炼,执行前请确认
cd HiRAG
pip install -e .
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于分层知识图谱的 Graph RAG 系统,用层次化知识组织提升检索增强生成效果

基于层次化知识图谱的 Graph RAG 系统,把文档组织成分层结构来提升检索增强生成的召回与准确度,开箱即用。

核心能力

  • 构建分层知识图谱组织文档内容
  • 支持 hi、naive、hi_nobridge、hi_local、hi_global、hi_bridge 多种检索模式
  • 可接入 DeepSeek、ChatGLM、OpenAI 等第三方 LLM API
  • 提供 LLM 缓存与异步嵌入加速索引
  • 附带 UltraDomain 数据集与批量评测脚本

容易踩的坑

需先在 config.yaml 中配置 API key 与 LLM 参数;评测数据集需从 Hugging Face 的 UltraDomain 单独下载;不同 LLM 后端需替换对应的 insert/test 脚本(如 insert_context_openai.py、test_glm.py)

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    🤗 HiRAG: Retrieval-Augmented Generation with Hierarchical Knowledge

    Model Pipeline

    image-20240129111934589

    Install

    # remember clone this repo first
    cd HiRAG
    pip install -e .
    

    Quick Start

    You can just utilize the following code to perform a query with HiRAG.

    graph_func = HiRAG(
        working_dir="./your_work_dir",
        enable_llm_cache=True,
        enable_hierachical_mode=True, 
        embedding_batch_num=6,
        embedding_func_max_async=8, # according to your machine
        enable_naive_rag=True
        )
    # indexing
    with open("path_to_your_context.txt", "r") as f:
        graph_func.insert(f.read())
    # retrieval & generation
    print("Perform hi search:")
    print(graph_func.query("The question you want to ask?", param=QueryParam(mode="hi")))
    

    Or if you want to employ HiRAG with DeepSeek, ChatGLM, or other third-party retrieval api, here are the examples in ./hi_Search_deepseek.py, ./hi_Search_glm.py, and ./hi_Search_openai.py. The API keys and the LLM configurations can be set at ./config.yaml.

    Evaluation

    We take the procedure in Mix dataset as an example.

    cd ./HiRAG/eval
    
    1. Extract context from original QA datasets.
    python extract_context.py -i ./datasets/mix -o ./datasets/mix
    
    1. Insert context to Graph Database.
    python insert_context_deepseek.py
    

    Note that the script insert_context_deepseek.py is for the setting of generation with DeepSeek-v3 api, you can replace that with insert_context_openai.py or insert_context_glm.py.

    1. Test with different versions of HiRAG.
    # there are different retrieval options
    # If you want to employ HiRAG approach, just run:
    python test_deepseek.py -d mix -m hi
    # If you want to employ naive RAG approach, just run:
    python test_deepseek.py -d mix -m naive
    # If you want to employ HiRAG approach w/o bridge, just run:
    python test_deepseek.py -d mix -m hi_nobridge
    # If you want to employ HiRAG approach with retrieving only local knowledge, just run:
    python test_deepseek.py -d mix -m hi_local
    # If you want to employ HiRAG approach with retrieving only global knowledge, just run:
    python test_deepseek.py -d mix -m hi_global
    # If you want to employ HiRAG approach with retrieving only bridge knowledge, just run:
    python test_deepseek.py -d mix -m hi_bridge
    

    Note that the dataset mix can be replaced to any other datasets in Hugging Face link. And the script test_deepseek.py is for the setting of generation with DeepSeek-v3 api, you can replace that with test_openai.py or test_glm.py.

    1. Evaluate the generated answers.

    First step, request for evaluations.

    python batch_eval.py -m request -api openai
    python batch_eval.py -m request -api deepseek
    

    Second step, get the results.

    python batch_eval.py -m result -api openai
    python batch_eval.py -m result -api deepseek
    

    Results

    Compare with Naive RAG:

    With the config output_file set as f"./datasets/{DATASET}/{DATASET}_eval_hi_naive.jsonl", just run the command:

    python batch_eval.py -m result -api openai
    
    DatasetDimensionNaiveRAG %HiRAG %
    Mix
    Comprehensiveness16.683.4
    Empowerment11.688.4
    Diversity12.787.3
    Overall12.487.6
    CS
    Comprehensiveness30.070.0
    Empowerment29.071.0
    Diversity14.585.5
    Overall26.573.5
    Legal
    Comprehensiveness32.567.5
    Empowerment25.075.0
    Diversity22.078.0
    Overall22.574.5
    Agriculture
    Comprehensiveness34.066.0
    Empowerment31.069.0
    Diversity21.079.0
    Overall28.571.5

    Compare with GraphRAG:

    With the config output_file set as f"./datasets/{DATASET}/{DATASET}_eval_hi_graphrag.jsonl", just run the command:

    python batch_eval.py -m result -api openai
    
    DatasetDimensionGraphRAG %HiRAG %
    Mix
    Comprehensiveness42.157.9
    Empowerment35.164.9
    Diversity40.559.5
    Overall35.964.1
    CS
    Comprehensiveness40.559.5
    Empowerment38.561.5
    Diversity30.569.5
    Overall36.064.0
    Legal
    Comprehensiveness48.551.5
    Empowerment43.556.5
    Diversity47.053.0
    Overall45.554.5
    Agriculture
    Comprehensiveness49.051.0
    Empowerment48.551.5
    Diversity45.554.5
    Overall46.054.0

    Compare with LightRAG:

    With the config output_file set as f"./datasets/{DATASET}/{DATASET}_eval_hi_lightrag.jsonl", just run the command:

    python batch_eval.py -m result -api openai
    
    DatasetDimensionLightRAG %HiRAG %
    Mix
    Comprehensiveness36.863.2
    Empowerment34.965.1
    Diversity34.165.9
    Overall34.165.9
    CS
    Comprehensiveness44.555.5
    Empowerment41.558.5
    Diversity33.067.0
    Overall41.059.0
    Legal
    Comprehensiveness49.051.0
    Empowerment43.556.5
    Diversity63.037.0
    Overall48.052.0
    Agriculture
    Comprehensiveness38.561.5
    Empowerment36.563.5
    Diversity37.562.5
    Overall38.561.5

    Compare with FastGraphRAG:

    With the config output_file set as f"./datasets/{DATASET}/{DATASET}_eval_hi_fastgraphrag.jsonl", just run the command:

    python batch_eval.py -m result -api openai
    
    DatasetDimensionFastGraphRAG %HiRAG %
    Mix
    Comprehensiveness0.899.2
    Empowerment0.899.2
    Diversity0.899.2
    Overall0.899.2
    CS
    Comprehensiveness0.0100.0
    Empowerment0.0100.0
    Diversity0.599.5
    Overall0.0100.0
    Legal
    Comprehensiveness1.099.0
    Empowerment0.0100.0
    Diversity1.598.5
    Overall0.0100.0
    Agriculture
    Comprehensiveness0.0100.0
    Empowerment0.0100.0
    Diversity0.0100.0
    Overall0.0100.0

    Compare with KAG:

    With the config output_file set as f"./datasets/{DATASET}/{DATASET}_eval_hi_kag.jsonl", just run the command:

    python batch_eval.py -m result -api openai
    
    DatasetDimensionKAG %HiRAG %
    Mix
    Comprehensiveness2.397.7
    Empowerment3.596.5
    Diversity3.896.2
    Overall2.397.7
    CS
    Comprehensiveness1.099.0
    Empowerment4.595.5
    Diversity5.095.0
    Overall1.598.5
    Legal
    Comprehensiveness16.583.5
    Empowerment9.091.0
    Diversity11.089.0
    Overall8.591.5
    Agriculture
    Comprehensiveness5.095.0
    Empowerment5.095.0
    Diversity3.596.5
    Overall0.0100.0

    Acknowledgement

    We gratefully acknowledge the use of the following open-source projects in our work:

    • nano-graphrag: a simple, easy-to-hack GraphRAG implementation

    • RAPTOR: a novel approach to retrieval-augmented language models by constructing a recursive tree structure from documents.

    💡 Cite Us

    @article{huang2025retrieval,
      title={Retrieval-Augmented Generation with Hierarchical Knowledge},
      author={Huang, Haoyu and Huang, Yongfeng and Yang, Junjie and Pan, Zhenyu and Chen, Yongqiang and Ma, Kaili and Chen, Hongzhi and Cheng, James},
      journal={arXiv preprint arXiv:2503.10150},
      year={2025}
    }
    

    🌟 Star History

    Star History Chart