Skip to content

BUPT-GAMMA/PathRAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d9be087 · Mar 14, 2025

History

11 Commits
Mar 14, 2025
Mar 4, 2025
Feb 18, 2025
Mar 4, 2025

Repository files navigation

The code for the paper "PathRAG: Pruning Graph-based Retrieval Augmented Generation with Relational Paths".

Install

cd PathRAG
pip install -e .

Quick Start

  • You can quickly experience this project in the v1_test.py file.
  • Set OpenAI API key in environment if using OpenAI models: api_key="sk-...". in the v1_test.py and llm.py file
  • Prepare your retrieval document "text.txt".
  • Use the following Python snippet in the "v1_text.py" file to initialize PathRAG and perform queries.
import os
from PathRAG import PathRAG, QueryParam
from PathRAG.llm import gpt_4o_mini_complete

WORKING_DIR = "./your_working_dir"
api_key="your_api_key"
os.environ["OPENAI_API_KEY"] = api_key
base_url="https://api.openai.com/v1"
os.environ["OPENAI_API_BASE"]=base_url


if not os.path.exists(WORKING_DIR):
    os.mkdir(WORKING_DIR)

rag = PathRAG(
    working_dir=WORKING_DIR,
    llm_model_func=gpt_4o_mini_complete,  
)

data_file="./text.txt"
question="your_question"
with open(data_file) as f:
    rag.insert(f.read())

print(rag.query(question, param=QueryParam(mode="hybrid")))

Parameter modification

You can adjust the relevant parameters in the base.py and operate.py files.

Batch Insert

import os
folder_path = "your_folder_path"  

txt_files = [f for f in os.listdir(folder_path) if f.endswith(".txt")]
for file_name in txt_files:
    file_path = os.path.join(folder_path, file_name)
    with open(file_path, "r", encoding="utf-8") as file:
        rag.insert(file.read())

Cite

Please cite our paper if you use this code in your own work:

@article{chen2025pathrag,
  title={PathRAG: Pruning Graph-based Retrieval Augmented Generation with Relational Paths},
  author={Chen, Boyu and Guo, Zirui and Yang, Zidan and Chen, Yuluo and Chen, Junze and Liu, Zhenghao and Shi, Chuan and Yang, Cheng},
  journal={arXiv preprint arXiv:2502.14902},
  year={2025}
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages