PlanRAG: Plan-then-Retrieval Augmented Generation Framework

repository·master·Indexed 41 days ago

https://github.com/myeon9h/planrag

A Plan-then-Retrieval Augmented Generation framework for LLMs acting as decision makers. PlanRAG supports both relational (MySQL) and graph (Neo4j) databases to solve complex decision-making tasks in benchmarks like DQA. It includes support for various RAG techniques such as SingleRAG, IterRAG, and PlanRAG, and integrates with OpenAI, vLLM, and Hugging Face models.

Tokens
4.4K
Snippets
15
Records
17
Agent score
100%

What's inside PlanRAG

  1. Generate questions for the Building scenario

    master

    The building scenario requires Victoria 3 game savefiles (provided in /data/building/raw/). To generate simulated_questions.json in /data/building/questions/, run the simulator and example generator sequentially. This process takes approximately one hour.

    export PYTHONPATH=.
    python ./src/data_parsers/building/queries_gen/simulator.py
    python ./src/data_parsers/building/example_gen/main.py
  2. Install Neo4j Graph Database

    master

    PlanRAG uses Neo4j to access Labeled Property Graph (LPG) data. On Ubuntu, install Neo4j Enterprise and the Cypher shell using the following steps:

    1. Install dependencies and add the Neo4j repository.
    2. Install neo4j-enterprise and cypher-shell.
    3. Log in with the default credentials (neo4j/neo4j) and change the password immediately.
    # Ubuntu
    sudo apt update
    sudo apt install apt-transport-https ca-certificates curl software-properties-common
    sudo curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo apt-key add -
    sudo add-apt-repository "deb https://debian.neo4j.com stable 4.1"
    sudo apt install neo4j-enterprise
    sudo apt install cypher-shell
    
    # Login with the default user/password 'neo4j'/'neo4j'.
    cypher-shell -u neo4j
    # Then, change to a new password and enter ':exit' to return to the terminal.
  3. Install MySQL Relational Database

    master

    For relational data in the DQA benchmark, install MySQL on Ubuntu. You must create a database named DQA_rdb.

    Note: To allow the Python environment to access MySQL without sudo privileges, you should update the root user to use mysql_native_password.

    # Ubuntu
    wget https://dev.mysql.com/get/mysql-apt-config_0.8.14-1_all.deb
    sudo dpkg -i mysql-apt-config_0.8.14-1_all.deb
    # Then, select 'OK' to proceed to the next step.
    sudo apt update
    sudo apt install -y mysql-server
    
    # Login with root user to create the default database 'DQA_rdb'.
    sudo mysql -u root
    mysql> CREATE DATABASE DQA_rdb;
    # (Optional) Make MySQL accessible without sudo privilege in the Python environment.
    mysql> USE mysql;
    mysql> update user set plugin='mysql_native_password' where user='root';
    mysql> flush privileges;
    # Then, enter ':exit' to return to the terminal.
  4. Use open models with vLLM

    master

    To use open-source models, it is highly recommended to use vllm as the model method.

    1. Install vllm via pip.
    2. Deploy the model as an OpenAI-compatible server using vllm.entrypoints.openai.api_server.
    3. Run src/main.py with --model_method vllm.
    pip install vllm
    
    # Deploy model server
    python -m vllm.entrypoints.openai.api_server --model meta-llama/Llama-2-70b-chat-hf
    
    # Run experiment
    python src/main.py \
      --technique IterRAG \
      --scenario locating \
      --database relational \
      --question_num 1 \
      --model meta-llama/Llama-2-70b-chat-hf \
      --model_method vllm
  5. Generate questions for the Locating scenario

    master

    The locating scenario requires Europa Universalis IV game savefiles (provided in /data/locating/raw/). To generate simulated_question.json in /data/locating/questions/, run the simulator and example generator sequentially. This process takes approximately 30 minutes.

    export PYTHONPATH=.
    python ./src/data_parsers/locating/queries_gen/simulator.py
    python ./src/data_parsers/locating/example_gen/main.py
  6. Configure PlanRAG via config.json

    master

    Fill in the config.json file with your API keys and database connection details.

    Important Recommendations:

    • Use OpenAI's GPT-4.0 for best performance.
    • Do not use the default password neo4j for the Neo4j configuration.
    {
        "OPENAI_API_KEY": "Your OpenAI API key",
        "HUGGINGFACEHUB_API_TOKEN": "Your Hugging Face Hub API key", 
        "NEO4J": {
            "HOST": "Neo4j host address (default: 'bolt://localhost:7687')", 
            "USER": "User id (default: 'neo4j')",
            "PASSWORD": "User password (Do not fill with the default password 'neo4j')"
        },
        "MYSQL": {
            "HOST": "MySQL host address (default: 'localhost')",
            "USER": "User id (default: 'root')"
        }
    }
  7. Supported RAG Techniques

    master

    PlanRAG supports different reasoning and retrieval strategies through its TaskManager abstraction. Depending on the --technique flag, the application initializes different logic:

    • SingleRAG: Uses TaskManagerSingleRAG.
    • IterRAG: Uses TaskManagerChain from the IterRAG module.
    • PlanRAG: Uses TaskManagerChain from the PlanRAG module.
    • PlanRAG_woReplan: Uses TaskManagerChain from the PlanRAG_woReplan module.
  8. Run the LangChain server locally using docker-compose

    master

    You can run the LangChain server locally by executing the main() function in src/langchain/server.py. This script uses docker-compose to pull the necessary images and start the services defined in the local docker-compose.yaml file. It relies on the langsmith.cli.main.get_docker_compose_command utility to construct the correct command.

    To run the server, execute the script directly from your terminal.

    python src/langchain/server.py
  9. Configure the LangChain stack via Docker Compose

    master

    The src/langchain/docker-compose.yaml file defines a multi-container setup for running a LangChain environment consisting of a frontend, a backend, and a PostgreSQL database.

    Services Overview

    • langchain-frontend: The user interface. It maps port 4173 on the host to 4173 in the container. It depends on langchain-backend.
    • langchain-backend: The core logic service. It maps port 8000 on the host to 8000 in the container. It depends on langchain-db.
    • langchain-db: A PostgreSQL 14.1 database service. It exposes port 5432 internally.
    version: '3'
    services:
      langchain-frontend:
        image: notlangchain/langchainplus-frontend:latest
        ports:
          - 4173:4173
        environment:
          - BACKEND_URL=http://langchain-backend:8000
          - PUBLIC_BASE_URL=http://localhost:8000
          - PUBLIC_DEV_MODE=true
        depends_on:
          - langchain-backend
      langchain-backend:
        image: notlangchain/langchainplus:latest
        environment:
          - PORT=8000
          - LANGCHAIN_ENV=local
        ports:
          - 8000:8000
        depends_on:
          - langchain-db
      langchain-db:
        image: postgres:14.1
        environment:
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_DB=postgres
        expose:
          - 5432
  10. Generate locating-related examples with json_example_generator

    master

    The json_example_generator function creates a JSON file containing structured examples for locating-related tasks. It processes raw CSV data (containing country-to-answer mappings) and uses parsers to extract geographical context (countries, provinces, and trade provinces) to build complex prompt components including business rules, questions, and goals.

    Parameters:

    • file_dir (str): The destination path for the generated JSON file. Defaults to ./data/locating/questions/questions.json.
    • raw_dirs (list of str): A list of paths to raw CSV files. Each file is expected to contain a Country column and an Answer column. Defaults to `[
  11. Run PlanRAG experiments via CLI

    master

    Execute experiments using src/main.py. You can specify the technique (e.g., PlanRAG, IterRAG, SingleRAG), the scenario (locating or building), and the database type (relational or graph).

    Available Techniques:

    • PlanRAG: The core proposed method.
    • IterRAG: Iterative RAG.
    • SingleRAG: Single-turn RAG.
    # PlanRAG: locating scenario with relational database
    python src/main.py \
        --technique PlanRAG \
        --scenario locating \
        --database relational \
        --question_num 1 \
        --model gpt-4 \
        --model_method openai
    
    # PlanRAG: building scenario with graph database
    python src/main.py \
        --technique PlanRAG \
        --scenario building \
        --database graph \
        --question_num 1 \
        --model gpt-4 \
        --model_method openai
    
    # Iterative RAG
    python src/main.py \
        --technique IterRAG \
        --scenario locating \
        --database relational \
        --question_num 1 \
        --model gpt-4 \
        --model_method openai