YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

Agentic RAG Poisoning Framework

Autonomous Decision-Making LLM Agent with Data Poisoning Evaluation


Project Structure

agentic_rag_poisoning/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw_pdfs/                  ← source PDFs (generated by generate_pdfs.py)
β”‚   └── faiss_index/               ← saved vector store (auto-created)
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ ingestion.py               ← Phase 1 & 2: PDF β†’ chunks β†’ embeddings β†’ FAISS
β”‚   β”œβ”€β”€ rag_pipeline.py            ← Phase 3: retrieval + Ollama LLM generation
β”‚   β”œβ”€β”€ agent_monitor.py           ← Phase 4: autonomous validation & refinement
β”‚   β”œβ”€β”€ poisoning.py               ← Phase 5: 4 attack strategies
β”‚   └── evaluation.py              ← Phase 6: RAGAS metrics comparison
β”œβ”€β”€ results/
β”‚   └── evaluation_report.json     ← auto-generated after evaluation
β”œβ”€β”€ generate_pdfs.py               ← creates the 3 sample PDFs
β”œβ”€β”€ main.py                        ← unified pipeline runner
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env
└── README.md

FULL SETUP PROCEDURE (VS Code + Windows)

Follow every step in order. Commands marked [CMD] run in Command Prompt / PowerShell. Commands marked [VS CODE TERMINAL] run in the VS Code integrated terminal.


STEP 1 β€” Install Prerequisites

1a. Install Python 3.11

1b. Install Ollama (local LLM runtime)

  • Download from: https://ollama.com/download
  • Run the installer β€” it installs as a background service
  • Verify in CMD:
    ollama --version
    

1c. Install VS Code


STEP 2 β€” Create the Project Folder

In [CMD]:

mkdir C:\Projects\agentic_rag_poisoning
cd C:\Projects\agentic_rag_poisoning

Copy all project files into this folder so the structure matches above.


STEP 3 β€” Open in VS Code

In [CMD]:

code C:\Projects\agentic_rag_poisoning

OR: Open VS Code β†’ File β†’ Open Folder β†’ select agentic_rag_poisoning


STEP 4 β€” Create Virtual Environment

In [VS CODE TERMINAL] (Terminal β†’ New Terminal):

python -m venv venv

Activate it:

  • Windows:
    venv\Scripts\activate
    
  • Mac/Linux:
    source venv/bin/activate
    

You should see (venv) at the start of your terminal prompt.

Select the venv interpreter in VS Code:

  • Press Ctrl+Shift+P β†’ type "Python: Select Interpreter"
  • Choose the one that says .\venv\Scripts\python.exe

STEP 5 β€” Install Dependencies

In [VS CODE TERMINAL] (with venv active):

pip install --upgrade pip
pip install -r requirements.txt

This installs ~1.5 GB of packages. Takes 5–15 minutes depending on internet speed.

If torch fails (common on Windows CPU-only), install it separately:

pip install torch --index-url https://download.pytorch.org/whl/cpu

STEP 6 β€” Pull the Ollama LLM Model

Open a new [CMD] window (NOT the VS Code terminal β€” Ollama runs separately):

ollama pull mistral

Downloads ~4 GB. This is the Mistral 7B model. Takes 5–20 minutes.

Verify Ollama is running:

ollama serve

Leave this CMD window open while using the project.

In another CMD, test it:

ollama run mistral "Hello, are you working?"

STEP 7 β€” Generate Sample PDFs

Back in [VS CODE TERMINAL] (venv active):

python generate_pdfs.py

Expected output:

Created: data/raw_pdfs/machine_learning_intro.pdf
Created: data/raw_pdfs/rag_systems_guide.pdf
Created: data/raw_pdfs/data_poisoning_attacks.pdf

All 3 PDFs generated successfully in data/raw_pdfs/

You can open these PDFs in VS Code or any PDF viewer to inspect them.


STEP 8 β€” Run Phase 1 & 2: Ingestion

python src/ingestion.py

Expected output:

Loading PDFs from 'data/raw_pdfs'...
  βœ“ machine_learning_intro.pdf (5 pages)
  βœ“ rag_systems_guide.pdf (4 pages)
  βœ“ data_poisoning_attacks.pdf (4 pages)

Chunking complete: 13 pages β†’ ~80 chunks

Loading embedding model: sentence-transformers/all-MiniLM-L6-v2
  (First run downloads ~90 MB β€” subsequent runs use cache)

Building FAISS index from 80 chunks...
βœ“ Vector store saved to 'data/faiss_index'

STEP 9 β€” Run Phase 3: RAG Pipeline

Make sure Ollama is running (Step 6).

python src/rag_pipeline.py

You will see question β†’ answer β†’ source citations for 4 test questions.


STEP 10 β€” Run Phase 4: Agentic Monitor

python src/agent_monitor.py

Watch the agent validate each response and trigger refinement when needed.


STEP 11 β€” Run Phase 5: Poisoning Attacks

python src/poisoning.py

This runs all 4 attack strategies and shows you sample poisoned chunks.


STEP 12 β€” Run Phase 6: Evaluation

python src/evaluation.py

This builds clean + poisoned vector stores, runs all questions through both, and prints a comparison table like:

══════════════════════════════════════════════════════════════
  Evaluation: Clean RAG  vs  Poisoned RAG [Label Flipping]
══════════════════════════════════════════════════════════════
  Metric                 Clean    Poisoned      Drop
  ──────────────────────────────────────────────────────
  faithfulness           0.842       0.531    β–Ό 0.311
  answer_relevancy       0.791       0.624    β–Ό 0.167
  context_recall         0.768       0.592    β–Ό 0.176
  context_precision      0.803       0.641    β–Ό 0.162
══════════════════════════════════════════════════════════════

Results are saved to results/evaluation_report.json.


STEP 13 β€” Run Full Pipeline at Once

python main.py

Or run specific phases:

python main.py --phase ingestion
python main.py --phase rag
python main.py --phase agent
python main.py --phase poison
python main.py --phase eval
python main.py --rebuild    # force rebuild vector store

Adding Your Own PDFs

  1. Drop any PDF into data/raw_pdfs/
  2. Run: python src/ingestion.py (force rebuild)
  3. Or in code: run_ingestion(force_rebuild=True)

Common Errors & Fixes

Error Fix
ollama: connection refused Run ollama serve in a separate CMD window
ModuleNotFoundError Activate venv: venv\Scripts\activate
torch not found pip install torch --index-url https://download.pytorch.org/whl/cpu
No PDF files found Run python generate_pdfs.py first
FAISS index not found Run python src/ingestion.py first
out of memory Reduce CHUNK_SIZE in ingestion.py to 256

Tech Stack (All Free / Open Source)

Component Tool Version
LLM Mistral 7B via Ollama 0.3.x
Embeddings all-MiniLM-L6-v2 sentence-transformers 3.x
Vector DB FAISS (CPU) faiss-cpu 1.9
RAG Framework LangChain 0.3.7
PDF Loading PyMuPDF 1.24
Evaluation RAGAS 0.2.5
PDF Generation ReportLab 4.2
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support