
This repository provides the official implementation of POISONCRAFT: Practical Poisoning of Retrieval-Augmented Generation for Large Language Models.
This repository provides the official implementation of POISONCRAFT: Practical Poisoning of Retrieval-Augmented Generation for Large Language Models.

POISONCRAFT aims to demonstrate how a malicious actor can plant “poisoned” content into the corpus used by Retrieval-Augmented Generation (RAG) pipelines, thereby misleading a Large Language Model into hallucinating or referencing malicious content. This codebase provides scripts to:
Prepare and preprocess standard datasets (e.g., Natural Questions, MS MARCO, HotpotQA).
Inject adversarial suffixes (i.e., “poisons”) into query-like text in order to degrade or manipulate retrieval results.
Evaluate the poisoning effectiveness under different retrievers (e.g., Contriever, SimCSE, and BGE) and measure transferability.
Below is a high-level guide to configuring the environment. The exact requirements are listed in requirements.txt. We recommend creating a new virtual environment to avoid conflicts:
# Example using conda
conda create -n PoisonCraft python=3.10
conda activate PoisonCraft
# Install dependencies
pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1 --index-url https://download.pytorch.org/whl/cu118
pip install -r requirements.txt
Certain parts of this project, such as domain classification and query categorization, rely on API calls (e.g., OpenAI GPT models). You need to configure the API key and model settings as follows:
"api_key_info": {
"api_keys": ["your_openai_api_key"],
"api_key_use": 0
}
Note: This project supports multiple LLM providers (e.g., OpenAI, Llama, and PaLM2). Ensure you select the appropriate configuration file for your model.
We provide various bash scripts under the scripts folder for different stages of the pipeline. Below is a step-by-step reference:
1. Download and Prepare Datasets
Run scripts/run_prepare_dataset.sh to download and unzip the datasets, and to create training/test splits:
bash scripts/run_prepare_dataset.sh
This script internally calls:
2. Preprocess Datasets
To calculate top-k ground truth similarity scores for each domain, run:
bash scripts/run_process_data.sh
3. [Optional]Evaluating BEIR for Ground Truth
Execute this step only if you want to evaluate a retriever model, as it may take considerable time. Follow these steps:
python src/beir_eval/eval_beir.py --model_code bge-small --dataset nq --top_k 100
bash scripts/run_process_data.sh
To generate adversarial suffixes (i.e., “poisons”), you can use:
bash scripts/run_multi_poisoncraft.sh
You may customize parameters such as:
After generating adversarial suffixes, you can evaluate how many queries get “poisoned” references by running:
bash scripts/run_retrieval_attack.sh
This script:
Alternatively, for LLM-level evaluation (i.e., final text generation with top-k retrieval results included), you can use:
bash scripts/run_target_attack.sh