https://github.com/betswish/mirage
Easy-to-use MIRAGE code for faithful answer attribution in RAG applications. Paper: https://aclanthology.org/2024.emnlp-main.347/
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
✓DOI references
Found 1 DOI reference(s) in README -
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.2%) to scientific vocabulary
Keywords
Repository
Easy-to-use MIRAGE code for faithful answer attribution in RAG applications. Paper: https://aclanthology.org/2024.emnlp-main.347/
Basic Info
Statistics
- Stars: 21
- Watchers: 1
- Forks: 2
- Open Issues: 1
- Releases: 0
Topics
Metadata Files
README.md
Toward faithful answer attribution with model internals 🌴
Latest update: Our paper has been accepted by the EMNLP 2024 Main Conference! 🎉 Also check our demo here!
Authors (* Equal contribution): Jirui Qi* • Gabriele Sarti* • Raquel Fernández • Arianna Bisazza
[!NOTE] This repository provides an easy-to-use MIRAGE framework for analyzing the groundedness of RAG generation to the retrieved documents. To reproduce the paper results, please take a look at this repo.
Abstract: Ensuring the verifiability of model answers is a fundamental challenge for retrieval-augmented generation (RAG) in the question answering (QA) domain. Recently, self-citation prompting was proposed to make large language models (LLMs) generate citations to supporting documents along with their answers. However, self-citing LLMs often struggle to match the required format, refer to non-existent sources, and fail to faithfully reflect LLMs' context usage throughout the generation. In this work, we present MIRAGE --Model Internals-based RAG Explanations -- a plug-and-play approach using model internals for faithful answer attribution in RAG applications. MIRAGE detects context-sensitive answer tokens and pairs them with retrieved documents contributing to their prediction via saliency methods. We evaluate our proposed approach on a multilingual extractive QA dataset, finding high agreement with human answer attribution. On open-ended QA, MIRAGE achieves citation quality and efficiency comparable to self-citation while also allowing for a finer-grained control of attribution parameters. Our qualitative evaluation highlights the faithfulness of MIRAGE's attributions and underscores the promising application of model internals for RAG answer attribution.
If you find the paper helpful and use the content, we kindly suggest you cite through:
bibtex
@inproceedings{qi-etal-2024-model,
title = "Model Internals-based Answer Attribution for Trustworthy Retrieval-Augmented Generation",
author = "Qi, Jirui and
Sarti, Gabriele and
Fern{\'a}ndez, Raquel and
Bisazza, Arianna",
editor = "Al-Onaizan, Yaser and
Bansal, Mohit and
Chen, Yun-Nung",
booktitle = "Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing",
month = nov,
year = "2024",
address = "Miami, Florida, USA",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2024.emnlp-main.347",
doi = "10.18653/v1/2024.emnlp-main.347",
pages = "6037--6053",
abstract = "Ensuring the verifiability of model answers is a fundamental challenge for retrieval-augmented generation (RAG) in the question answering (QA) domain. Recently, self-citation prompting was proposed to make large language models (LLMs) generate citations to supporting documents along with their answers. However, self-citing LLMs often struggle to match the required format, refer to non-existent sources, and fail to faithfully reflect LLMs{'} context usage throughout the generation. In this work, we present MIRAGE {--} Model Internals-based RAG Explanations {--} a plug-and-play approach using model internals for faithful answer attribution in RAG applications. MIRAGE detects context-sensitive answer tokens and pairs them with retrieved documents contributing to their prediction via saliency methods. We evaluate our proposed approach on a multilingual extractive QA dataset, finding high agreement with human answer attribution. On open-ended QA, MIRAGE achieves citation quality and efficiency comparable to self-citation while also allowing for a finer-grained control of attribution parameters. Our qualitative evaluation highlights the faithfulness of MIRAGE{'}s attributions and underscores the promising application of model internals for RAG answer attribution. Code and data released at https://github.com/Betswish/MIRAGE.",
}
Environment:
For a quick start, you may load our environment easily with Conda:
conda env create -f MIRAGE.yaml
Alternatively, you can install all packages by yourself:
Python: 3.9.19
Packages: pip install -r requirements.txt
Quick Start
For a quick start, you only need to put your RAG Data file in data_input/ folder and run the following one command to get the LLM outputs with answer attribution (e.g. LLaMA2 with standard prompt):
python mirage.py --f data_input/EXAMPLE.json --config configs/llama2_standard_prompt.yaml
The data file should be in JSON format. For example, suppose you have two questions, each provided with two retrieved docs:
json
[
{
"question": "YOUR QUESTION",
"docs": [
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
},
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
}
]
},
{
"question": "YOUR QUESTION",
"docs": [
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
},
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
}
]
}
]
The data with LLM's raw outputs will be saved in the folder data_input_with_ans/, and the attributed answers will be saved in res_AA/ folder.
You may also check internal_res/ for the model internals obtained by MIRAGE. The internal of each instance is saved as an individual JSON file, which can be used for advanced functions below.
Full functions
The all parameters for mirage.py are listed below:
- f: Path to the input file.
- config: Path to the configuration file, containing the generation parameters and prompts.
- CTI: CTI threshold. This means how many standard deviations over average, default 1.
- CCI: CCI threshold. Using Top k strategy if k > 0; otherwise Top (-k)% if k < 0, default -5.
Advanced Feature 1
If you already have LLM generations (e.g. LLaMA2 with standard prompt) in the data file, like:
json
[
{
"question": "YOUR QUESTION",
"docs": [
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
},
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
}
],
"output": "LLM GENERATED ANSWER"
},
{
"question": "YOUR QUESTION",
"docs": [
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
},
{
"title": "TITLE OF RETRIEVED DOC",
"text": "TEXT OF RETRIEVED DOC"
}
],
"output": "LLM GENERATED ANSWER"
}
]
put it in the data_input_with_ans/ folder and specify --f_with_ans to get the answer attribution for the LLM outputs you already have:
bash
mkdir data_input_with_ans
python mirage.py --f data_input_with_ans/EXAMPLE_WITH_ANS.json --config configs/llama2_standard_prompt.yaml --f_with_ans
Advanced Feature 2
After a first run, you should have got:
- a JSON file containing LLM outputs in data_input_with_ans/, whether generated by our code or uploaded by yourself, e.g. EXAMPLE_WITH_ANS.json.
- a JSON file in the res_AA/ folder with MIRAGE-attributed answers, named after res_AA/EXAMPLE.json.mirage_CTI_X_CCI_X.
- multiple JSON files saving model internals obtained by MIRAGE at internal_res/, like internal_res/EXAMPLE_WITH_ANS-0.json, internal_res/EXAMPLE_WITH_ANS-1.json, etc.
If you plan to try different CTI and CCI thresholds, set the data file with LLM outputs as --f and specify --f_with_ans and --only_cite.
It allows you to try different combinations of CTI and CCI thresholds with the existing model internal files, instead of analyzing model internals from scratch, saving you treasure time. :)
bash
python mirage.py --f data_input_with_ans/EXAMPLE_WITH_ANS.json --config configs/llama2_standard_prompt.yaml --f_with_ans --only_cite --CTI X --CCI X
Owner
- Login: Betswish
- Kind: user
- Repositories: 1
- Profile: https://github.com/Betswish
GitHub Events
Total
- Watch event: 13
- Issue comment event: 1
- Push event: 17
- Pull request event: 1
- Fork event: 3
Last Year
- Watch event: 13
- Issue comment event: 1
- Push event: 17
- Pull request event: 1
- Fork event: 3
Dependencies
- Cython ==3.0.8
- Jinja2 ==3.1.3
- MarkupSafe ==2.1.5
- PyYAML ==6.0.1
- Pygments ==2.17.2
- absl-py ==2.1.0
- accelerate ==0.27.2
- aiohttp ==3.9.3
- aiosignal ==1.3.1
- annotated-types ==0.6.0
- async-timeout ==4.0.3
- attrs ==23.2.0
- bitsandbytes ==0.42.0
- blis ==0.7.11
- captum ==0.7.0
- catalogue ==2.0.10
- causal-conv1d ==1.2.0.post2
- certifi ==2024.2.2
- charset-normalizer ==3.3.2
- click ==8.1.7
- cloudpathlib ==0.16.0
- coloredlogs ==15.0.1
- confection ==0.1.4
- contourpy ==1.2.0
- cycler ==0.12.1
- cymem ==2.0.8
- datasets ==2.19.2
- dill ==0.3.8
- einops ==0.8.0
- faiss-cpu ==1.8.0
- filelock ==3.13.1
- flatbuffers ==23.5.26
- fonttools ==4.49.0
- frozenlist ==1.4.1
- fsspec ==2024.2.0
- huggingface-hub ==0.23.2
- humanfriendly ==10.0
- idna ==3.6
- importlib_resources ==6.1.2
- jaxtyping ==0.2.27
- joblib ==1.3.2
- jsonlines ==4.0.0
- kiwisolver ==1.4.5
- langcodes ==3.3.0
- lightgbm ==4.3.0
- mamba-ssm ==1.2.0.post1
- markdown-it-py ==3.0.0
- matplotlib ==3.8.3
- mauve-text ==0.3.0
- mdurl ==0.1.2
- mpmath ==1.3.0
- multidict ==6.0.5
- multiprocess ==0.70.16
- murmurhash ==1.0.10
- networkx ==3.2.1
- ninja ==1.11.1.1
- nltk ==3.8.1
- nmslib ==2.1.1
- numpy ==1.26.4
- nvidia-cublas-cu12 ==12.1.3.1
- nvidia-cuda-cupti-cu12 ==12.1.105
- nvidia-cuda-nvrtc-cu12 ==12.1.105
- nvidia-cuda-runtime-cu12 ==12.1.105
- nvidia-cudnn-cu12 ==8.9.2.26
- nvidia-cufft-cu12 ==11.0.2.54
- nvidia-curand-cu12 ==10.3.2.106
- nvidia-cusolver-cu12 ==11.4.5.107
- nvidia-cusparse-cu12 ==12.1.0.106
- nvidia-nccl-cu12 ==2.19.3
- nvidia-nvjitlink-cu12 ==12.3.101
- nvidia-nvtx-cu12 ==12.1.105
- onnxruntime ==1.17.1
- openai ==0.27.4
- packaging ==23.2
- pandas ==2.2.1
- pillow ==10.2.0
- preshed ==3.0.9
- protobuf ==3.20.2
- psutil ==5.9.8
- pyarrow ==16.1.0
- pyarrow-hotfix ==0.6
- pybind11 ==2.6.1
- pydantic ==2.6.2
- pydantic_core ==2.16.3
- pyjnius ==1.6.1
- pyparsing ==3.1.2
- pyserini ==0.21.0
- python-dateutil ==2.8.2
- pytz ==2024.1
- regex ==2023.12.25
- requests ==2.32.3
- rich ==13.7.1
- rouge-score ==0.1.2
- safetensors ==0.4.2
- scikit-learn ==1.4.1.post1
- scipy ==1.12.0
- sentence-transformers ==2.2.2
- sentencepiece ==0.2.0
- six ==1.16.0
- smart-open ==6.4.0
- spacy ==3.7.4
- spacy-legacy ==3.0.12
- spacy-loggers ==1.0.5
- srsly ==2.4.8
- sympy ==1.12
- thinc ==8.2.3
- threadpoolctl ==3.3.0
- tokenizers ==0.19.1
- torch ==2.2.1
- torchvision ==0.17.1
- tqdm ==4.66.2
- triton ==2.2.0
- typeguard ==2.13.3
- typer ==0.9.0
- typing_extensions ==4.10.0
- tzdata ==2024.1
- urllib3 ==2.2.1
- wasabi ==1.1.2
- weasel ==0.3.4
- xxhash ==3.4.1
- yarl ==1.9.4
- zipp ==3.17.0