How sleeepeer/poisonedrag works
Project Type & Purpose
• This is a research codebase supporting experiments on attacking RAG systems as detailed in a USENIX Security paper.
• Its purpose is to orchestrate evaluations of various language models and retrieval methods when subjected to knowledge-based corruption attacks. The experiments utilize different model configurations (e.g., GPT3.5, GPT4, PaLM2, LLaMA, Vicuna) and a retrieval system based on Contriever (a Facebook Research project).
Major Components
a. Experiment Orchestrators and Entry Points:
• Files like run.py, main.py, and evaluate_beir.py serve as the main drivers. They coordinate experiments and reproduction of results.
b. Dataset & Preprocessing:
• prepare_dataset.py fetches and prepares data (with the expected beir format) for experiments.
• The results directory indicates outcomes from both “adv_targeted” and “beir” experiment runs.
c. Model Configuration and API Integration:
• The model_configs directory holds JSON files with API key settings and parameters for each supported language model. This serves as an abstraction layer for model integration.
d. Retrieval Module (Contriever):
• Located in src/contriever_src, this component encapsulates the retrieval functionalities (loading data, indexing, evaluation, finetuning data generation, etc.). It’s an essential element in the RAG framework.
e. Attack Modules:
• The src/attack.py script embodies the logic for performing the knowledge corruption (or poisoning) attacks.
• The attack module interacts with both the retrieval module and the language model modules.
f. Language Model Interfaces:
• Found in src/models (files like GPT.py, Llama.py, PaLM2.py, Vicuna.py, and Model.py), these modules abstract the interaction with the corresponding external APIs or frameworks.
g. Utility Modules:
• src/utils.py and similar helper modules (e.g., prompts.py) support the orchestration logic, formatting, and any additional pre-/post-processing.
Relationships & Data Flows
• The experiment script loads configuration (API keys and hyperparameters) from model_configs.
• It then ensures datasets are present (or triggers prepare_dataset.py if needed) which feed into the evaluation.
• The retrieval component (Contriever in src/contriever_src) indexes and retrieves data while the attack module (src/attack.py) crafts corrupted input queries.
• The language model interface then sends these queries (using API keys from model_configs) to external services (e.g., OpenAI, Google’s generative AI, HuggingFace for LLaMA 2).
• Results from both retrieval and model outputs are aggregated and saved under results/ for further analysis.
• Evaluation routines may compare “truth” versus “attacked” results using additional evaluation and scoring modules.
Architectural Patterns & Design Principles
• Modularity: The project is clearly separated into modules for model interactions, retrieval logic, attack logic, data preparation, and evaluation.
• Configurability: With external JSON configuration files, the system supports pluggable components (different LLMs and retrieval models).
• Experiment Reproducibility: The orchestration script (run.py) encapsulates hyperparameters and diverse experiment settings explicitly.
b. Branch out from this layer:
• One branch for Dataset & Preprocessing (prepare_dataset.py).
• Another branch for the Attack & Evaluation Flow that connects to the LLM Interfaces.
• The Retrieval Component (src/contriever_src).
• The Attack Module (src/attack.py).
d. That both these modules interact with the Language Model Interface (src/models), which in turn communicates with external LLM services (noting API key usage from model_configs).
e. That the output from the LLMs is processed and stored in the “Results” directory.
f. Utility modules like src/utils.py, src/prompts.py, etc., as support blocks feeding into both the experiment orchestration and the model interfacing modules.