Building an AI Security Analyst Assistant using Retrieval-Augmented Generation
A comprehensive, educational implementation of RAG techniques applied to AI/ML security, suitable for portfolio demonstration and learning advanced retrieval methods.
- Overview
- Key Features
- Learning Path
- What You'll Learn
- Project Structure
- Getting Started
- Notebooks
- Security Data Sources
- Advanced Techniques
- Demo Application
- Contributing
- License
- Resources
This project demonstrates how to build a production-ready RAG system for security applications, progressing from basic concepts to advanced techniques like RAPTOR and ColBERT. Each notebook is self-contained and educational, showing the complete implementation process with real security data sources.
π― 11 Comprehensive Notebooks - From basics to production deployment π Security-Focused - Real-world AI/ML security applications π Advanced Techniques - RAPTOR, ColBERT, RAG-Fusion, and more π Evaluation Framework - Quantitative metrics for comparison π‘οΈ Security Hardening - Adversarial detection, PII redaction, source verification π Production Ready - Streamlit app with Docker deployment π Educational - Clear explanations and visualizations throughout
New to RAG? Start here:
- RAG From Scratch by LangChain - Excellent introduction to core RAG concepts (indexing, retrieval, generation)
- Return to this repo for security-focused advanced techniques and production deployment
Already know RAG basics? Start directly with Notebook 1 to see how RAG applies to security applications with advanced retrieval methods, security hardening, and production-ready architecture.
- β RAG fundamentals with security-focused data (MITRE ATT&CK, OWASP, CVEs)
- β Advanced retrieval techniques (multi-query, RAG-fusion, decomposition)
- β Metadata filtering and query structuring for production systems
- β Intelligent re-ranking by security severity and relevance
- β Hierarchical knowledge organization with RAPTOR
- β Late interaction retrieval with ColBERT
- β Security hardening (adversarial query detection, source verification)
- β Evaluation metrics and benchmarking
- β Deployment as a working web application
security-rag-from-scratch/
βββ README.md # Project overview and setup
βββ LICENSE # MIT License
βββ CONTRIBUTING.md # Contribution guidelines
βββ SECURITY.md # Security policy
βββ CODE_OF_CONDUCT.md # Community guidelines
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose setup
βββ notebooks/ # Educational Jupyter notebooks (11 parts)
β βββ part_01_introduction.ipynb
β βββ part_02_basic_rag.ipynb
β βββ part_03_advanced_retrieval.ipynb
β βββ part_04_query_decomposition.ipynb
β βββ part_05_metadata_filtering.ipynb
β βββ part_06_reranking.ipynb
β βββ part_07_raptor.ipynb
β βββ part_08_colbert.ipynb
β βββ part_09_security_hardening.ipynb
β βββ part_10_evaluation.ipynb
β βββ part_11_deployment.ipynb
βββ data/ # Security datasets and knowledge bases
β βββ owasp_llm_top10/
β βββ mitre_attack/
β βββ README.md
βββ app/ # Production Streamlit application
βββ app.py # Main application
βββ utils.py # Utility functions
βββ README.md # App documentation
βββ .streamlit/
βββ config.toml # Streamlit configuration
- Python 3.10+
- OpenAI API key
- (Optional) Cohere API key for re-ranking
- (Optional) LangSmith account for tracing
- Clone this repository or navigate to the project directory:
cd security-rag-from-scratch- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
# Create a .env file with your API keys
echo "OPENAI_API_KEY=your-key-here" > .env
echo "COHERE_API_KEY=your-key-here" >> .env
echo "LANGCHAIN_API_KEY=your-key-here" >> .env
echo "LANGCHAIN_TRACING_V2=true" >> .env- Start with Part 1:
jupyter notebook notebooks/part_01_introduction.ipynbEach notebook is self-contained and builds upon previous concepts:
- Introduction & Use Case - Understanding the security analyst assistant use case and architecture overview
- Basic RAG - Implementing fundamental RAG with MITRE ATT&CK and OWASP data
- Advanced Retrieval - Multi-query and RAG-fusion for comprehensive results
- Query Decomposition - Breaking complex security questions into manageable sub-questions
- Metadata Filtering - Structured queries for CVE severity, dates, and affected systems
- Intelligent Ranking - Re-ranking by security priority using Cohere and custom logic
- RAPTOR - Hierarchical security knowledge organization (Tactics β Techniques β Procedures)
- ColBERT - Late interaction retrieval for precise matching of vulnerability patterns
- Security Hardening - Making the RAG system itself secure against adversarial queries
- Evaluation & Metrics - Measuring retrieval quality and generation performance
- Deployment - Building and deploying a working Streamlit application
This project uses authoritative security data sources:
- MITRE ATT&CK Framework - Adversary tactics, techniques, and procedures
- OWASP Top 10 for LLMs - LLM-specific vulnerabilities and mitigations
- National Vulnerability Database (NVD) - CVE records with CVSS scores
- Security Research Papers - Academic research on adversarial ML and defenses
- π Threat intelligence lookup and analysis
- π‘οΈ Vulnerability assessment guidance
- π Security framework navigation (MITRE ATT&CK, OWASP)
- π¨ Incident response recommendations
- π Security policy question answering
- π¬ Research paper summarization
Organizes security knowledge hierarchically, enabling both high-level overviews and detailed specifics.
Use Case: Navigate MITRE ATT&CK from tactics down to specific procedures, or OWASP categories to specific vulnerability examples.
Token-level embeddings with late interaction scoring for precise matching.
Use Case: Find similar code vulnerability patterns, match exploit signatures, search security logs with complex queries.
Run the interactive demo:
cd app
streamlit run app.pyFeatures:
- π¬ Natural language security queries
- π Source citations with confidence scores
- βοΈ Configurable retrieval methods
- π Visualization of retrieval process
- π Adversarial query detection
Contributions are welcome! This is an educational project focused on learning RAG techniques for security applications.
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
Quick contribution ideas:
- Add new security data sources
- Implement additional RAG techniques
- Improve documentation and examples
- Fix bugs or improve performance
- Enhance the evaluation framework
- LangChain Documentation
- RAG from Scratch (Original)
- MITRE ATT&CK
- OWASP Top 10 for LLMs
- ColBERT Paper
- RAPTOR Paper
MIT License - feel free to use for learning purposes.
Scott Thornton - AI/ML Security Engineer
Building intelligent systems for security operations
β If you found this project helpful, please consider giving it a star on GitHub!
Scott Thornton β AI Security Researcher
- Website: perfecxion.ai
- Email: scott@perfecxion.ai
- LinkedIn: linkedin.com/in/scthornton
- ORCID: 0009-0008-0491-0032
- GitHub: @scthornton
Security Issues: Please report via SECURITY.md