An advanced, self-correcting RAG (Retrieval-Augmented Generation) agent built with LangGraph to assist students and faculty at XYZ National University. The agent provides accurate information on course catalogs, academic policies, and faculty details while actively preventing hallucinations.
- Adaptive Retrieval: Classifies queries to decide whether to search the internal knowledge base (University data) or provide a general response.
- Structure-Aware Chunking: Custom regex-based parsing of PDF catalogs to ensure course descriptions and faculty entries stay intact.
- Relevance Grading: Automatically filters out irrelevant documents before generation to minimize noise and token costs.
- Self-Correction (Hallucination Check): Verifies every generated claim against the retrieved context. If an answer isn't grounded, the agent automatically retries generation or falls back to web search.
- Web Search Fallback: Integrates with Tavily to provide supplementary information when internal documents are insufficient.
- Streamlit UI: A clean, modern interface for real-time interaction and execution tracing.
- Orchestration: LangGraph
- LLM: Meta Llama 3 (via OpenRouter)
- Vector Store: FAISS
- Embeddings: OpenAI
text-embedding-3-small - UI: Streamlit
git clone git@github.com:abeeranajam31/Self-Rag-.git
cd Self-Rag-python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txtCreate a .env file in the root directory:
OPENROUTER_API_KEY=your_key_here
TAVILY_API_KEY=your_key_here
DATA_DIR=./Data_share
VECTORSTORE_PATH=./vectorstore_indexRun the agent directly in your terminal:
python self_rag_agent.pyIf you update the PDFs in Data_share, rebuild the knowledge base:
python self_rag_agent.py --rebuild-indexLaunch the web interface:
streamlit run app.pyThe agent has been verified across 5 critical scenarios:
- General Queries: Instant grounded responses without retrieval.
- Specific Retrieval: Precise course/policy lookups (e.g., "Prerequisites for EE-201").
- Web Fallback: Handling queries outside the local KB (e.g., "International rankings").
- Hallucination Protection: Self-correction loop for unsupported claims.
- Cross-Document Synthesis: Combining faculty contact info with course catalog details.
Detailed execution traces can be found in evaluation_results.md.
This project uses environment variables for all API keys. Hardcoded secrets have been removed to comply with security best practices.