Skip to main content

A Python package for explaining RAG pipelines.

Project description

RAG-E

Deploy PyPI

This project provides a framework for building and evaluating interpretable Retrieval-Augmented Generation (RAG) pipelines. The core of the project is the ability to explain the contribution of retrieved documents and query parts to the final generated output, using Shapley values.

Features

  • Interpretable RAG Pipeline: A RAG pipeline that can explain the importance of retrieved documents and query components.
  • Shapley Value-based Explanations: Uses Shapley values to quantify the contribution of each input feature (e.g., retrieved documents, query tokens) to the generated output.
  • Multiple Explanation Aggregations: Supports different aggregation methods for Shapley values, such as token-level, sequence-level, and bag-of-words.
  • Online and Offline Retrieval: Supports both online and offline retrieval methods.
  • Extensible and Modular: The code is organized in a modular way, making it easy to extend and adapt to different models and datasets.

Installation

Install source code fom GitHub

This option is mainly useful for development or replication our experiments. First clone this repository. After cloning or downloading the repository, run the Linux shell script ./setup.sh. It will initialize the workspace by performing the following steps:

  1. It will install the required Python modules by running pip install -r "./requirements.txt"
  2. It will download the necessary Python code to compute the BARTScore by Yuan et al. (2021) to "./resources/bart_score.py".
  3. It will download the necessary Python code to compute the LongDocFACTScore by Bishop et al. (2024) to "./resources/ldfacts.py".

Usage

How to use RAG-E:

The classes ExplainableAutoModelForRetrieval and ExplainableAutoModelForGeneration wrap Hugging Face transformers models and provide the functionality to compute attribution scores. For retrieval, RAG-E supports the following token attribution methods:

  • .grad(...): Raw gradients towards the inputs of the last batch
  • .aGrad(...): AGrad (da ⊙ a) by Liu et al. (2021) of the last batch.
  • .gradIn(...): Gradient times input (dx ⊙ x) scores of the last batch.
  • .intGrad(...): Integrated Gradients by Sundararajan et al. (2017) of the last batch.

For generation RAG-E supports precise and Kernel-SHAP approximated Shapley Values for document attribution.

The ExplainableAutoModelForRAG class combines these models to create the full interpretable RAG pipeline. Here is an Example with Llama 3.1 8B and Snowflake Arctic Embed v2 (more examples in scripts/demos):

from src.Interpretable_RAG.rag import ExplainableAutoModelForRAG

# Load Pipeline:
model = ExplainableAutoModelForRAG(
    # Retriever info:
    query_encoder_name_or_path='Snowflake/snowflake-arctic-embed-l-v2.0',
    retriever_query_format='query: {query}',
    retriever_token_processor=lambda s: s.replace('▁', ' '),
    retriever_kwargs={'add_pooling_layer':False},

    # Generator info:
    generator_name_or_path='meta-llama/Llama-3.1-8B-Instruct',
    generator_token_processor=lambda s: s.replace('Ġ', ' ').strip('Ċ'),
    generator_kwargs={'device_map':'auto', 'torch_dtype':torch.bfloat16}
)

# MSMarco query and passage as an example:
query =  "Where was Marie Curie born?"
contexts = [
    "Maria Skłodowska, later known as Marie Curie, was born on November 7, 1867.",
    "Born in Paris on 15 May 1859, Pierre Curie was the son of Eugène Curie, a doctor of French Catholic origin from Alsace.",
    "Maria Skłodowska was born in Warsaw, in Congress Poland in the Russian Empire, as the fifth and youngest child of well-known teachers Bronisława, née Boguska, and Władysław Skłodowski.",
    "While a French citizen, Marie Skłodowska Curie, who used both surnames, never lost her sense of Polish identity. She taught her daughters the Polish language and took them on visits to Poland.",
    "Marie Curie founded the Curie Institute in Paris in 1920, and the Curie Institute in Warsaw in 1932.",
]

# Generate answer:
output = model(
    query=query,
    contexts=contexts,
    k=5,
    generator_kwargs={
        'max_new_tokens':256,
        'do_sample':False,
        'top_p':1,
        'num_beams':1,
        'batch_size':64,
        'max_samples_query':32,
        'max_samples_context':32,
        'conditional':True
    }
)

# Explain retriever:
ret_attributions = model.retriever.intGrad()

# Explain generator:
gen_attributions = model.generator.get_shapley_values()

To visualize these explanations, RAG-E includes easy-to-use plotting functions:

from src.Interpretable_RAG.plotting import visualize_importance_retriever, visualize_importance_generator, plot_document_importance_rag, higlight_importance_rag

# Functions to normalize tokens:
retriever_token_processor=lambda s: s.replace('▁', ' '),
generator_token_processor=lambda s: s.replace('Ġ', ' ').strip('Ċ')

# Generate highlighted tokens for the retriever:
visualize_importance_retriever(model.retriever, method='intGrad', token_processor=retriever_token_processor, show:bool=True)

# Generate highlighted tokens for the generator:
visualize_attribution_generator(model.generator, aggregation='token', token_processor=generator_token_processor, show:bool=True)

# Generate highlighted tokens for the rag pipeline:
higlight_importance_rag(model, retriever_method='intGrad', show:bool=True,
        retriever_token_processor=retriever_token_processor,
        generator_token_processor=generator_token_processor
)

# Plot document importance for the rag pipeline:
plot_document_importance_rag(model, show:bool=True)

Replicating our experiments:

Replication our experiments requires installation Option 2. The main entry point for running experiments is the scripts/run_pipeline.py script. Here is an example of how to run it:

python scripts/run_pipeline.py \
    --topics_path /path/to/your/topics.tsv \
    --ranked_list_path /path/to/your/ranked_list.csv \
    --collection_path /path/to/your/collection.tsv \
    --output_path /path/to/your/output_directory/ \
    --model_id meta-llama/Llama-3.1-8B-Instruct \
    --num_docs_context 6 \
    --max_gen_len 300 \
    --run_original \
    --run_randomized

Arguments:

  • --topics_path: Path to the file with the queries.
  • --ranked_list_path: Path to the ranked list from the retrieval.
  • --collection_path: Path to the collection of passages.
  • --output_path: Base directory to save all the results.
  • --model_id: ID of the generative model from Hugging Face.
  • --num_docs_context: Number of documents to use as context.
  • --max_gen_len: Maximum length of the generated response.
  • --run_original: Run the experiment with the original contexts.
  • --run_randomized: Run with contexts in random order.
  • --run_no_duplicates: Run with contexts without duplicates.

Project Structure

  • src/Interpretable_RAG: Contains the core source code for the interpretable RAG pipeline.
  • scripts: Contains scripts for running experiments, building indexes, and performing analysis.
  • data: Contains the data used for the experiments.
  • resources: Contains additional resources, such as evaluation scripts.
  • outputs_evaluation: Contains the evaluation outputs.
  • outputs_retrieved: Contains the retrieved outputs.
  • results: Contains the results of the experiments.
  • plots: Contains plots for the analysis of the results.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the GPL v3 License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rag_exp-0.2.tar.gz (86.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rag_exp-0.2-py3-none-any.whl (89.8 kB view details)

Uploaded Python 3

File details

Details for the file rag_exp-0.2.tar.gz.

File metadata

  • Download URL: rag_exp-0.2.tar.gz
  • Upload date:
  • Size: 86.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rag_exp-0.2.tar.gz
Algorithm Hash digest
SHA256 0d27e1812e0e1eab194881f257d35b6c44ab6f365e3c21eea0a5ae79de899f73
MD5 b23b1bd609f44534b5612814a88fe445
BLAKE2b-256 99be64b6fef8eeafed77c2303cb72dc5c56dd97e13345d575bd3bbe415ef6827

See more details on using hashes here.

Provenance

The following attestation bundles were made for rag_exp-0.2.tar.gz:

Publisher: python-publish.yml on k-randl/Interpretable_RAG

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rag_exp-0.2-py3-none-any.whl.

File metadata

  • Download URL: rag_exp-0.2-py3-none-any.whl
  • Upload date:
  • Size: 89.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rag_exp-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d75a781ebc61e5c4b7c97f5108078893c1a1145bdac278ba424aa0e24eaa251c
MD5 030c55770b3828ec7bea0d05e34a2215
BLAKE2b-256 2d12b6437065fc4d21cdb997d3ac9b49747d429ade71e122b2369f1e12dfc9ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for rag_exp-0.2-py3-none-any.whl:

Publisher: python-publish.yml on k-randl/Interpretable_RAG

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page