ragdefender


Nameragdefender JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/SecAI-Lab/RAGDefender
SummaryEfficient defense against knowledge corruption attacks on RAG systems
upload_time2025-10-25 02:44:39
maintainerNone
docs_urlNone
authorSecAI Lab
requires_python>=3.8
licenseMIT
keywords rag retrieval-augmented-generation security adversarial-defense nlp machine-learning knowledge-corruption llm
VCS
bugtrack_url
requirements torch transformers numpy pandas tqdm scikit-learn sentence-transformers
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # RAGDefender

[![PyPI version](https://badge.fury.io/py/ragdefender.svg)](https://badge.fury.io/py/ragdefender)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)

**Efficient defense against knowledge corruption attacks on RAG systems**

RAGDefender is a lightweight, efficient defense mechanism designed to protect Retrieval-Augmented Generation (RAG) systems from knowledge corruption attacks such as PoisonedRAG, Blind, and GARAG. It detects and isolates poisoned documents in retrieved contexts without requiring additional model training or fine-tuning.

📄 **Paper**: *"Rescuing the Unpoisoned: Efficient Defense against Knowledge Corruption Attacks on RAG Systems"* (ACSAC 2025)

🔗 **Repository**: [https://github.com/SecAI-Lab/RAGDefender](https://github.com/SecAI-Lab/RAGDefender)

## Features

- 🛡️ **Defense against multiple attack types**: PoisonedRAG, Blind, GARAG
- ⚡ **Efficient**: No additional model training required
- 🎯 **High accuracy**: Effectively identifies and removes poisoned documents
- 🔧 **Easy to integrate**: Simple API for existing RAG pipelines
- 🚀 **Two defense modes**: Single-hop and multi-hop query support
- 📊 **Comprehensive evaluation**: Built-in metrics and evaluation tools

## Installation

### Quick Install

```bash
pip install ragdefender
```

### Installation with GPU Support

```bash
pip install ragdefender[cuda]
```

## Quick Start

### Basic Usage

```python
from ragdefender import RAGDefender

# Initialize defender
defender = RAGDefender(device='cuda')

# Your retrieved documents (may contain poisoned content)
query = "Where is the capital of France?"
retrieved_docs = [
    "Paris serves as the heart of France, celebrated for its iconic landmarks as well as its influential role in art, fashion, and gastronomy.",
    "POISONED: Marseille is the capital of France, city renowned as a vibrant port city on the Mediterranean coast.",
    "POISONED: Strasbourg serves as the capital of France and hosts several important European institutions.",
    "POISONED: Toulouse, known as 'La Ville Rose', is recognized as the capital city of France.",
    "POISONED: Nice, the beautiful coastal city, functions as the capital of France.",
]

# Apply defense
clean_docs = defender.defend(
    query=query,
    retrieved_docs=retrieved_docs,
    mode='multihop'  # Use 'singlehop' for NQ/MSMARCO, 'multihop' for HotpotQA
)

print(f"Removed {len(retrieved_docs) - len(clean_docs)} poisoned documents")
```

### Command-Line Interface

```bash
# Apply defense
ragdefender defend --query "Your question" --corpus documents.json --mode multihop

# Evaluate performance
ragdefender evaluate --test-data test.json --attack poisonedrag --mode singlehop
```

## Defense Modes

RAGDefender uses different detection algorithms based on query type:

### Single-Hop Mode
- **Best for**: NQ, MSMARCO datasets (simple factual questions)
- **How it works**: Aggregation-based clustering with TF-IDF validation
- **Use when**: Query needs one document to answer

```python
clean = defender.defend(query, docs, mode='singlehop')
```

### Multi-Hop Mode
- **Best for**: HotpotQA dataset (complex multi-step reasoning)
- **How it works**: Similarity-based outlier detection
- **Use when**: Query requires multiple documents to answer

```python
clean = defender.defend(query, docs, mode='multihop')
```

**Key Insight**: Single-hop and multi-hop questions have different document similarity patterns, so RAGDefender adapts its detection strategy accordingly.

## Integration Example

```python
from ragdefender import RAGDefender

# Initialize defender
defender = RAGDefender(device='cuda')

def safe_rag_pipeline(query, retriever, llm):
    # Step 1: Retrieve documents
    retrieved_docs = retriever.retrieve(query, top_k=10)

    # Step 2: Apply RAGDefender
    clean_docs = defender.defend(
        query=query,
        retrieved_docs=retrieved_docs,
        mode='multihop',
        top_k=5
    )

    # Step 3: Generate response with clean documents
    response = llm.generate(query, clean_docs)
    return response
```

## Requirements

- Python ≥ 3.8
- PyTorch ≥ 1.9.0
- sentence-transformers ≥ 2.2.0
- scikit-learn ≥ 0.24.0

## Documentation

For detailed documentation, examples, and advanced usage:
- 📖 [GitHub Repository](https://github.com/SecAI-Lab/RAGDefender)
- 🚀 [Quick Start Guide](https://github.com/SecAI-Lab/RAGDefender/blob/main/QUICKSTART.md)
- 📝 [Examples](https://github.com/SecAI-Lab/RAGDefender/tree/main/examples)

## Citation

If you use RAGDefender in your research, please cite our paper:

```bibtex
@inproceedings{kim2025ragdefender,
  title={Rescuing the Unpoisoned: Efficient Defense against Knowledge Corruption Attacks on RAG Systems},
  author={Minseok Kim, Hankook Lee, Hyungjoon Koo},
  booktitle={Annual Computer Security Applications Conference (ACSAC) (to appear)},
  year={2025}
}
```

## License

This project is licensed under the MIT License - see the [LICENSE](https://github.com/SecAI-Lab/RAGDefender/blob/main/LICENSE) file for details.

## Support

- 📧 Email: for8821@g.skku.edu
- 🐛 Issues: [GitHub Issues](https://github.com/SecAI-Lab/RAGDefender/issues)
- 💬 Discussions: [GitHub Discussions](https://github.com/SecAI-Lab/RAGDefender/discussions)

---

**Disclaimer**: This tool is intended for research and defensive purposes only.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SecAI-Lab/RAGDefender",
    "name": "ragdefender",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Minseok Kim <for8821@g.skku.edu>",
    "keywords": "rag, retrieval-augmented-generation, security, adversarial-defense, nlp, machine-learning, knowledge-corruption, llm",
    "author": "SecAI Lab",
    "author_email": "SecAI Lab <for8821@g.skku.edu>",
    "download_url": "https://files.pythonhosted.org/packages/db/51/77a69e9e328b95e08e999cf4ca346f744b086a536df3f53b9ad7526f8e9e/ragdefender-0.1.1.tar.gz",
    "platform": null,
    "description": "# RAGDefender\n\n[![PyPI version](https://badge.fury.io/py/ragdefender.svg)](https://badge.fury.io/py/ragdefender)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)\n\n**Efficient defense against knowledge corruption attacks on RAG systems**\n\nRAGDefender is a lightweight, efficient defense mechanism designed to protect Retrieval-Augmented Generation (RAG) systems from knowledge corruption attacks such as PoisonedRAG, Blind, and GARAG. It detects and isolates poisoned documents in retrieved contexts without requiring additional model training or fine-tuning.\n\n\ud83d\udcc4 **Paper**: *\"Rescuing the Unpoisoned: Efficient Defense against Knowledge Corruption Attacks on RAG Systems\"* (ACSAC 2025)\n\n\ud83d\udd17 **Repository**: [https://github.com/SecAI-Lab/RAGDefender](https://github.com/SecAI-Lab/RAGDefender)\n\n## Features\n\n- \ud83d\udee1\ufe0f **Defense against multiple attack types**: PoisonedRAG, Blind, GARAG\n- \u26a1 **Efficient**: No additional model training required\n- \ud83c\udfaf **High accuracy**: Effectively identifies and removes poisoned documents\n- \ud83d\udd27 **Easy to integrate**: Simple API for existing RAG pipelines\n- \ud83d\ude80 **Two defense modes**: Single-hop and multi-hop query support\n- \ud83d\udcca **Comprehensive evaluation**: Built-in metrics and evaluation tools\n\n## Installation\n\n### Quick Install\n\n```bash\npip install ragdefender\n```\n\n### Installation with GPU Support\n\n```bash\npip install ragdefender[cuda]\n```\n\n## Quick Start\n\n### Basic Usage\n\n```python\nfrom ragdefender import RAGDefender\n\n# Initialize defender\ndefender = RAGDefender(device='cuda')\n\n# Your retrieved documents (may contain poisoned content)\nquery = \"Where is the capital of France?\"\nretrieved_docs = [\n    \"Paris serves as the heart of France, celebrated for its iconic landmarks as well as its influential role in art, fashion, and gastronomy.\",\n    \"POISONED: Marseille is the capital of France, city renowned as a vibrant port city on the Mediterranean coast.\",\n    \"POISONED: Strasbourg serves as the capital of France and hosts several important European institutions.\",\n    \"POISONED: Toulouse, known as 'La Ville Rose', is recognized as the capital city of France.\",\n    \"POISONED: Nice, the beautiful coastal city, functions as the capital of France.\",\n]\n\n# Apply defense\nclean_docs = defender.defend(\n    query=query,\n    retrieved_docs=retrieved_docs,\n    mode='multihop'  # Use 'singlehop' for NQ/MSMARCO, 'multihop' for HotpotQA\n)\n\nprint(f\"Removed {len(retrieved_docs) - len(clean_docs)} poisoned documents\")\n```\n\n### Command-Line Interface\n\n```bash\n# Apply defense\nragdefender defend --query \"Your question\" --corpus documents.json --mode multihop\n\n# Evaluate performance\nragdefender evaluate --test-data test.json --attack poisonedrag --mode singlehop\n```\n\n## Defense Modes\n\nRAGDefender uses different detection algorithms based on query type:\n\n### Single-Hop Mode\n- **Best for**: NQ, MSMARCO datasets (simple factual questions)\n- **How it works**: Aggregation-based clustering with TF-IDF validation\n- **Use when**: Query needs one document to answer\n\n```python\nclean = defender.defend(query, docs, mode='singlehop')\n```\n\n### Multi-Hop Mode\n- **Best for**: HotpotQA dataset (complex multi-step reasoning)\n- **How it works**: Similarity-based outlier detection\n- **Use when**: Query requires multiple documents to answer\n\n```python\nclean = defender.defend(query, docs, mode='multihop')\n```\n\n**Key Insight**: Single-hop and multi-hop questions have different document similarity patterns, so RAGDefender adapts its detection strategy accordingly.\n\n## Integration Example\n\n```python\nfrom ragdefender import RAGDefender\n\n# Initialize defender\ndefender = RAGDefender(device='cuda')\n\ndef safe_rag_pipeline(query, retriever, llm):\n    # Step 1: Retrieve documents\n    retrieved_docs = retriever.retrieve(query, top_k=10)\n\n    # Step 2: Apply RAGDefender\n    clean_docs = defender.defend(\n        query=query,\n        retrieved_docs=retrieved_docs,\n        mode='multihop',\n        top_k=5\n    )\n\n    # Step 3: Generate response with clean documents\n    response = llm.generate(query, clean_docs)\n    return response\n```\n\n## Requirements\n\n- Python \u2265 3.8\n- PyTorch \u2265 1.9.0\n- sentence-transformers \u2265 2.2.0\n- scikit-learn \u2265 0.24.0\n\n## Documentation\n\nFor detailed documentation, examples, and advanced usage:\n- \ud83d\udcd6 [GitHub Repository](https://github.com/SecAI-Lab/RAGDefender)\n- \ud83d\ude80 [Quick Start Guide](https://github.com/SecAI-Lab/RAGDefender/blob/main/QUICKSTART.md)\n- \ud83d\udcdd [Examples](https://github.com/SecAI-Lab/RAGDefender/tree/main/examples)\n\n## Citation\n\nIf you use RAGDefender in your research, please cite our paper:\n\n```bibtex\n@inproceedings{kim2025ragdefender,\n  title={Rescuing the Unpoisoned: Efficient Defense against Knowledge Corruption Attacks on RAG Systems},\n  author={Minseok Kim, Hankook Lee, Hyungjoon Koo},\n  booktitle={Annual Computer Security Applications Conference (ACSAC) (to appear)},\n  year={2025}\n}\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](https://github.com/SecAI-Lab/RAGDefender/blob/main/LICENSE) file for details.\n\n## Support\n\n- \ud83d\udce7 Email: for8821@g.skku.edu\n- \ud83d\udc1b Issues: [GitHub Issues](https://github.com/SecAI-Lab/RAGDefender/issues)\n- \ud83d\udcac Discussions: [GitHub Discussions](https://github.com/SecAI-Lab/RAGDefender/discussions)\n\n---\n\n**Disclaimer**: This tool is intended for research and defensive purposes only.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Efficient defense against knowledge corruption attacks on RAG systems",
    "version": "0.1.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/SecAI-Lab/RAGDefender/issues",
        "Documentation": "https://github.com/SecAI-Lab/RAGDefender/tree/main/docs",
        "Homepage": "https://github.com/SecAI-Lab/RAGDefender",
        "Paper": "https://arxiv.org/abs/YOUR_PAPER_URL",
        "Repository": "https://github.com/SecAI-Lab/RAGDefender.git"
    },
    "split_keywords": [
        "rag",
        " retrieval-augmented-generation",
        " security",
        " adversarial-defense",
        " nlp",
        " machine-learning",
        " knowledge-corruption",
        " llm"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5c6e620f114e3661ec4539f14b5692738bb978aaf06894c36361b77d704a1b57",
                "md5": "7acc0f91445ae305c5edba6ca050d556",
                "sha256": "ab5dbaab72b5b6db48b6352f3732b38a830928c35aa40d5c16095e37f57c3dfa"
            },
            "downloads": -1,
            "filename": "ragdefender-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7acc0f91445ae305c5edba6ca050d556",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 14000,
            "upload_time": "2025-10-25T02:44:37",
            "upload_time_iso_8601": "2025-10-25T02:44:37.940855Z",
            "url": "https://files.pythonhosted.org/packages/5c/6e/620f114e3661ec4539f14b5692738bb978aaf06894c36361b77d704a1b57/ragdefender-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "db5177a69e9e328b95e08e999cf4ca346f744b086a536df3f53b9ad7526f8e9e",
                "md5": "f5ea0375acdd9e041db0fe71dc46cf27",
                "sha256": "032d1cb4a079771a55bdc07702761bfa86f56ff820c1d77993607e8e718069fc"
            },
            "downloads": -1,
            "filename": "ragdefender-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "f5ea0375acdd9e041db0fe71dc46cf27",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 14844,
            "upload_time": "2025-10-25T02:44:39",
            "upload_time_iso_8601": "2025-10-25T02:44:39.495503Z",
            "url": "https://files.pythonhosted.org/packages/db/51/77a69e9e328b95e08e999cf4ca346f744b086a536df3f53b9ad7526f8e9e/ragdefender-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-25 02:44:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SecAI-Lab",
    "github_project": "RAGDefender",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "torch",
            "specs": [
                [
                    ">=",
                    "1.9.0"
                ]
            ]
        },
        {
            "name": "transformers",
            "specs": [
                [
                    ">=",
                    "4.20.0"
                ]
            ]
        },
        {
            "name": "numpy",
            "specs": [
                [
                    ">=",
                    "1.19.0"
                ]
            ]
        },
        {
            "name": "pandas",
            "specs": [
                [
                    ">=",
                    "1.2.0"
                ]
            ]
        },
        {
            "name": "tqdm",
            "specs": [
                [
                    ">=",
                    "4.60.0"
                ]
            ]
        },
        {
            "name": "scikit-learn",
            "specs": [
                [
                    ">=",
                    "0.24.0"
                ]
            ]
        },
        {
            "name": "sentence-transformers",
            "specs": [
                [
                    ">=",
                    "2.2.0"
                ]
            ]
        }
    ],
    "lcname": "ragdefender"
}
        
Elapsed time: 1.02783s