πŸ€— HiRAG: Retrieval-Augmented Generation with Hierarchical Knowledge

June 13, 2026 Β· View on GitHub

πŸ€— 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