Giter Site home page Giter Site logo

thu-keg / kb-plugin Goto Github PK

View Code? Open in Web Editor NEW
6.0 5.0 1.0 6.89 MB

This is the accompanying code & data for the paper "KB-Plugin: A Plug-and-play Framework for Large Language Models to Induce Programs over Low-resourced Knowledge Bases".

Python 98.84% Shell 1.16%

kb-plugin's Introduction

KB-Plugin: A Plug-and-play Framework for LLMs to Induce Programs over Low-resourced KBs

Contributions Welcome language-python3 paper

Program induction (PI) has become a promising paradigm for using knowledge bases (KBs) to help large language models (LLMs) answer complex knowledge-intensive questions. Nonetheless, PI typically relies on a large number of parallel question-program pairs to make the LLM aware of the schema of the given KB, and is thus challenging for many low-resourced KBs that lack annotated data. To this end, we propose KB-Plugin, a plug-and-play framework that enables LLMs to induce programs over any low-resourced KB. Firstly, KB-Plugin adopts self-supervised learning to encode the detailed schema information of a given KB into a pluggable module, namely schema plugin. Secondly, KB-Plugin utilizes abundant annotated data from a rich-resourced KB to train another pluggable module, namely PI plugin, which can help the LLM extract question-relevant schema information from the schema plugin of any KB and utilize this information to induce programs over this KB. Experiments on five heterogeneous KBQA datasets show that KB-Plugin achieves better or comparable performance with 25x smaller backbone LLM compared to SoTA PI methods for low-resourced KBs, and even approaches the performance of supervised methods.

This is the accompanying code & data for the paper "KB-Plugin: A Plug-and-play Framework for Large Language Models to Induce Programs over Low-resourced Knowledge Bases".

Plugin Learning and Transfer Framework

File Structure

ProbTree/
├─  data/: put downloaded datasets here
├─  checkpoints/: put trained or downloaded plugin checkpoints here
├─  models_hf/: put your Llama2-7B model here
├─  source_kqapro/: code for training source domain schema plugins and PI plugin using KQA Pro
├─  transfer_freebase/: code for experiments on GrailQA, GraphQ, and WebQSP
└──  transfer_domain_specific/: code for experiments on MetaQA and SoAyBench

Data & Model Preparation

To run experiments, please first download Llama2-7B model and put it under ./models_hf.

Our data and plugin checkpoints can be download from here.

To conduct evaluation on GrailQA, GraphQ, and WebQSP, please follow Freebase Setup to set up a Virtuoso triplestore service. After starting your virtuoso service, replace the url in ./transfer_freebase/utils/sparql_executer.py with your own.

Training on KQA Pro

KB Generation and Data Augmentation

cd ./source_kqapro/sft_llama

Use KQA Pro to generate 16 source KBs with different schemas and corresponding augmented data.

python get_diff_kb.py

Source KB Schema Plugin Learning

Firstly, sample factual triples from each source KBs to generate training data.

python get_el_data.py --metaqa 0 # set 1 to remove relations in metaqa
python process_el_data.py --metaqa 0 

Secondly, train an individual schema plugin for each source KB.

python get_el_data.py --metaqa 0 
python process_el_data.py --metaqa 0 
bash train_el_lora_0-7.sh
bash train_el_lora_8-15.sh

PI Plugin Learning

python mix_pi_data.py --metaqa 0
python process_pi_data.py --metaqa 0
bash train_pi_plugin.sh

Transfer the PI Plugin to Other KBs

Target KB Schema Plugin Learning

Train the schema plugin for each target KB.

cd ./transfer_freebase/sft_llama
python process_el_data.py --dataset grailqa # graphq, webq
bash train_el_lora.sh 

cd ./transfer_domain_specific/sft_llama
python process_el_data.py --dataset metaqa # soay
bash train_el_lora.sh 

Evaluation

cd ./transfer_freebase
python eval_kb_plugin.py --dataset grailqa # graphq, webq

cd ./transfer_domain_specific
python eval_kb_plugin.py --dataset metaqa # soaybench

Citation

If you find our work useful, please consider citing KB-Plugin:

@article{zhang2024kbplugin,
      title={KB-Plugin: A Plug-and-play Framework for Large Language Models to Induce Programs over Low-resourced Knowledge Bases}, 
      author={Jiajie Zhang and Shulin Cao and Linmei Hu and Ling Feng and Lei Hou and Juanzi Li},
      journal={arXiv preprint arXiv:2402.01619},
      year={2024}
}

kb-plugin's People

Contributors

neo-zhangjiajie avatar

Stargazers

Jeff Carpenter avatar Ben avatar Yaotian Liu avatar  avatar Shangqing Tu avatar Yushi Bai avatar

Watchers

Zhiyuan Liu avatar Lei Hou avatar 史佳欣 avatar Xin Lv avatar Shulin Cao avatar

Forkers

willer-lu

kb-plugin's Issues

About the number of generated source KBs

If I change the number of generated source KBs, for example, from 16 to 8, does it means the 'BS' in KB-Plugin/source_kqapro/sft_llama/process_pi_data.py , KB-Plugin/source_kqapro/sft_llama/mix_pi_data.py and KB-Plugin/source_kqapro/sft_llama/train_pi_plugin.sh should be set to 8?
I wonder if there is some relation between these variables.
In other words, if I change the number of source KBs from 16 to 8, when I train PI Plugin, which part of the codes should be changed?(Supposed that the files have been formed)

Experiment Setting

Hi!
What is the smallest gpu memory to reproduce your work?
or
which kind of GPU did you use?

关于结果的复现

当直接使用你们提供的checkpoint时,推理的结果与论文报告的结果基本上一样。
但是当我进行完整的训练流程再进行推理时,结果与论文差距很大,我只是将训练schma plugin的相关脚本文件中的bs由128改为了64来适应A100的40G显存。源知识图谱的训练只改了bs,目标知识图谱除了bs也只进行过并行设置(多卡并行)。
对于webqsp数据集而言,F1只有50.8,对于soaybench而言,f1只有80.7。
这个结果的波动似乎不太正常,是否是训练阶段引入了什么错误?
谢谢!!!

About hrt.json

For GrailQA, GraphQ and WebQ, how to get the file 'hrt.json' from the original dataset or knowledge base?
I know the file mentioned is provided, I just wonder how did it form.
What's more, can you offer the training_set of the above datasets?

关于一些数据文件的获取方式

如何获取对应于这些数据集的训练集的el_results?
例如对于webqsp的测试集而言,其对应的文件为
/transfer_freebase/el_results/webqsp_test_elq.json
如何从它的训练集获取对应文件?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.