# Self-RAG Agent
A Retrieval-Augmented Generation system that grades its own retrieved
documents and generated answers before responding — reducing hallucination
and irrelevant-context answers compared to standard RAG.
## How it works
1. **Retrieve** — pull top-k chunks from a FAISS vector store built from your PDF/notes
2. **Grade documents** — LLM checks each chunk for relevance to the question, discards irrelevant ones
3. **Generate** — answer is produced using only the relevant chunks
4. **Grade hallucination** — checks the answer is grounded in the retrieved context
5. **Grade usefulness** — checks the answer actually addresses the question
6. Retries generation (up to 2 times) if either check fails; returns "I don't know" if no relevant documents are found
## Setup
python -m venv venv
venv\\Scripts\\activate
pip install -r requirements.txt
cp .env.example .env # add your GROQ\_API\_KEY
Put a PDF or .txt file in data/sample.pdf (or update the path in the test scripts).
## Run
python tests/test\_rag.py # basic retriever test
python tests/test\_self\_rag.py # full self-correcting RAG loop
## Stack
LangChain, Groq (Llama 3.3 70B), FAISS, HuggingFace sentence-transformers, Pydantic