• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created about 1 year ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Official Implementation of ICLR 2024 paper: "Reasoning on Graphs: Faithful and Interpretable Large Language Model Reasoning"

Reasoning on Graphs (RoG)

Official Implementation of "Reasoning on Graphs: Faithful and Interpretable Large Language Model Reasoning".

Reasoning on graphs (RoG) synergizes LLMs with KGs to enable faithful and interpretable reasoning. We present a planning-retrieval-reasoning framework, where RoG first generates relation paths grounded by KGs as faithful plans. These plans are then used to retrieve valid reasoning paths from the KGs for LLMs to conduct faithful reasoning and generate interpretable results.

Requirements

pip install -r requirements.txt

Pre-trained weights

You can find the pre-trained weights here.

Datasets

RoG-WebQSP
RoG-CWQ

Subgraph Extraction

We extract the subgraphs from the Freebase following previous studies. The code can be found here.

Inference

Requirements: Any GPU with at least 12GB memory.

Step1: Planning (Generate relation paths)

Run: ./scripts/planning.sh

python src/qa_prediction/gen_rule_path.py \
        --model_name RoG \
        --model_path rmanluo/RoG \
        -d {RoG-webqsp,RoG-cwq} \
        --split test \
        --n_beam 3

Generated rules will be saved at: results/gen_rule_path/{dataset}/{model_name}/{split}

Step2: Reasoning (Generate answers with RoG)

Run: ./scripts/rog-reasoning.sh

python src/qa_prediction/predict_answer.py \
        --model_name RoG \
        --model_path rmanluo/RoG \
        -d {RoG-webqsp,RoG-cwq} \
        --prompt_path prompts/llama2_predict.txt \
        --add_rul \
        --rule_path {rule_path} \

Answers will be saved at: results/KGQA/{dataset}/{model_name}/{split}

Plug-and-play Reasoning (Generate answers with different LLMs)

Note: you need to set your openai key at .env to use ChatGPT.

Run: ./scripts/plug-and-play.sh

python src/qa_prediction/predict_answer.py \
        --model_name {gpt-3.5-turbo,alpaca,llama2-chat-hf,flan-t5} \
        -d {RoG-webqsp,RoG-cwq} \
        --prompt_path {prompt_path} \
        --add_rule \
        --rule_path {rule_path}

Interpretable Reasoning

Run: python scripts/interpretable_example.py

from transformers import pipeline, AutoTokenizer
import torch

MODEL_PATH_OR_NAME="rmanluo/RoG"

tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH_OR_NAME, use_fast=False)
model = pipeline("text-generation", model=MODEL_PATH_OR_NAME, tokenizer=tokenizer, device_map="auto", torch_dtype=torch.float16)

print("====EXAMPLE 1: ====")

INPUT_TEXT_1 = """Based on the reasoning paths, please answer the given question and explain why 

Reasoning Paths: 
Northern District -> location.administrative_division.first_level_division_of -> Israel -> government.form_of_government.countries -> Parliamentary system

Question: 
What type of government is used in the country with Northern District?"""

outputs = model(INPUT_TEXT_1, return_full_text=False)
print(outputs[0]['generated_text'])

Training

Training Datasets

You can download the processed datasets from RoG_train_data.tar.tz. Unzip the files and put them under datasets/ folder.

Process datasets
  1. Build question to relation path pairs.
python src/align_kg/build_align_qa_dataset.py -d {RoG-webqsp,RoG-cwq} --split {train,validation,test}
  1. Build joint-training datasets.
python src/joint_training/preprocess_align.py
python src/joint_training/preprocess_qa.py
  1. Build interpretable examples.
python src/joint_training/generate_explanation_results.py

Training RoG

2 A100-80GB GPUs are required for training RoG.

Run: ./scripts/train.sh

Results

Bibinfo

If you found this repo helpful, please help us by citing this paper:

@inproceedings{luo2024rog,
title={Reasoning on Graphs: Faithful and Interpretable Large Language Model Reasoning},
author={Luo, Linhao and Li, Yuan-Fang and Haffari, Gholamreza and Pan, Shirui},
booktitle={International Conference on Learning Representations},
  year={2024}
}

More Repositories

1

Awesome-LLM-KG

Awesome papers about unifying LLMs and KGs
1,833
star
2

FakePosition

Fake position for IOS17 虚拟定位 支持IOS17
Shell
130
star
3

ML_Malware_detect

阿里云安全恶意程序检测比赛
Python
116
star
4

CP-GNN

Official code implementation for CIKM 21 paper Detecting Communities from Heterogeneous Graphs: A Context Path-based Graph Neural Network Model
Python
33
star
5

crack-soga-v2ray

A crack version of soga v2ray backend
Shell
33
star
6

NP-FKGC

Official code implementation for SIGIR 23 paper Normalizing Flow-based Neural Process for Few-Shot Knowledge Graph Completion
Python
32
star
7

MAMDR

Official code implementation for ICDE 23 paper MAMDR: A Model Agnostic Learning Method for Multi-Domain Recommendation
Python
31
star
8

ChatRule

Mining Logical Rules with Large Language Models for Knowledge Graph Reasoning with 1 dollar.
Python
28
star
9

ciscn2019_final_web4

全国大学生信息安全竞赛决赛web4,SSTI+TensorFlow模型
Python
27
star
10

GSNOP

Official code implementation for WSDM 23 paper Graph Sequential Neural ODE Process for Link Prediction on Dynamic and Sparse Graphs.
Python
26
star
11

relay

高性能流量转发后端
Go
22
star
12

HITszOpeningTemplate

哈工大深圳本科毕设开题模板
PostScript
11
star
13

llm-facteval

Source code of paper "Systematic Assessment of Factual Knowledge in Large Language Models" - EMNLP Findings 2023
Python
10
star
14

SSPanel-VNet-V2ray

对接SSPanel的V2ray后端。
Go
8
star
15

2018CISCN_SOUTH

2018CISCN华南赛区WEB备份
PHP
7
star
16

MotifGNN

Official code implementation for paper A Motif-Based Graph Neural Network to Reciprocal Recommendation for Online Dating
Python
7
star
17

srun_auto_login

深澜校园网自动后台常驻保证在线,支持windows、linux、mac,妈妈再也不用担心电脑被挤掉线了。
Python
6
star
18

wh_migration

武汉人口迁移数据爬取
Python
4
star
19

GSim

Official code implementation of "GSim: A Graph Neural Network Based Relevance Measure for Heterogeneous Graphs"
Python
4
star
20

zhihuishu-auto

2018新版智慧树自动刷课脚本
Python
4
star
21

HITszMidtermTemplate

哈工大深圳本科毕设中期模板
PostScript
3
star
22

myblog

My Blog
HTML
1
star
23

RManLuo.github.io

My Homepage
Stylus
1
star
24

2018-HITCTF-Challenges

Source code and exploit(writeup) script of HITCTF 2018 hold by [Lilac](http://7hxzz.club/) & H1TerHub & Aya9
Python
1
star
25

SniperOJ-WriteUps

Python
1
star
26

Home_Monitor

A simple House Monitor programe with a WebUI
Python
1
star
27

RManLuo

1
star
28

code-similarity

An out of box code and text similarity computation package
Python
1
star
29

fishing_dectect

提取钓鱼页面特征,并方便SVM处理
Python
1
star
30

docker-openconnect-sso

Run your own anyconnect/openconnect VPN client with SSO in Docker.
Dockerfile
1
star