Home

Awesome

KG-LLM-Doc

This repository includes the code and demo of Knowledge Graph Prompting for Multi-Document Question Answering.

Folder Architecture

All model checkpoints and real datasets are separately stored in the Dropbox! Due to limited number of times for the folder to be viewed by readers in a day, here I create another link sharing the folder, feel free to use this one as well

Environment Configuration

conda install -c anaconda python=3.8
pip install -r requirements.txt
pip install langchain
pip install nltk
pip install -U scikit-learn
pip install rank_bm25
pip install -U sentence-transformers
pip install -U pip setuptools wheel
pip install -U spacy
python -m spacy download en_core_web_lg
pip install torch-scatter
pip install Levenshtein
pip install openai==0.28
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install sentencepiece
pip install transformers

Work Flow

1. Document Collection

We query Wikipedia based on the QA data from existing literature.

cd Data-Collect/{dataset}
python3 process.py

2. Knowledge Graph Construction

We generate KG for each set of documents (paired with a question) using TF-IDF/KNN/TAGME of various densities. For the main Table experiment, we only use the graph constructed by TAGME with prior_prob 0.8.

cd Data-Collect
bash run_{dataset}.sh

3. DPR

We fine-tune the DPR model based on training queries and supporting passages and further use it for obtaining DPR baseline performance.

cd DPR
bash run.sh

After run.sh, you will get model.pt and retrieved contexts for supporting facts for questions in test_docs.json.

4. MDR

We fine-tune the MDR model based on training queries and supporting passages and further use it for obtaining DPR baseline performance. Please note that we only fine-tune MDR for HotpotQA and MuSiQue as we have access to their multi-hop inference training data while for Wiki2MQA and IIRC, we directly use model.pt from HotpotQA to retrieve contexts.

cd MDR
bash run.sh

After run.sh, you will get model.pt and retrieved contexts for supporting facts for questions in test_docs.json.

5. T5

See README.md in T5 Folder

6. LLaMA

See README.md in LLaMA Folder

7. Reproducing Main Table Results.

After completing all the above steps, we need to put the testing documents, retrieved contexts from the model, and generated knowledge graph in the corresponding place. Then, we have everything ready for the main Table in the paper. Specifically, see README.md in Pipeline to configure the files.

Then run the following commands:

cd Pipeline
bash run_{dataset}.sh

[!important]
We use multi-parallel processing to call LLM to process each question for each set of documents, which would incur a large amount of consumption when calling with OpenAI API call, therefore, please adaptively change the number of CPUs when parallel calling API according to your budget.

For final evaluation the generated answer:

cd evaluation
jupyter notebook eval.ipynb
Run the corresponding kernels