A production-grade agentic Retrieval-Augmented Generation (RAG) system built with FastAPI, LangGraph, pgvector, and Redis. The system features an autonomous self-healing loop that evaluates its own answers for faithfulness, relevance, and confidence, automatically adjusting its search parameters and query structure to correct itself when an answer fails validation.
Here is the real-time agent workflow orchestrated using LangGraph:
- Parsing & Tokenization: Documents (
.pdf,.md,.txt,.py,.js, etc.) are processed, sanitized, and broken down into chunks of 500 tokens (~2,000 characters) with a 50 token overlap. - Embeddings: Real-time vector representations are generated using a locally run, high-efficiency sentence embedding model (
sentence-transformers/all-MiniLM-L6-v2) running on CPU-optimized PyTorch. - Indexing: Chunks are stored in a PostgreSQL table backed by an HNSW cosine distance index for sub-millisecond similarity lookups.
-
Semantic Caching: Before running the agent, incoming queries are checked against a Redis cache using semantic similarity. If a similar question (score
$\ge 0.95$ ) was answered recently, the response is served instantly ($< 200\text{ms}$ ). - Retrieve Node: Performs vector or hybrid searches across the index to fetch relevant chunks.
- Reason Node: Synthesizes a candidate answer based only on retrieved context, appending cited source mappings.
-
Verify Node: Evaluates the response:
- Faithfulness Check: Determines if the generated answer contains hallucinated claims unsupported by the context.
- Relevance Check: Determines if the answer actually answers the user's question.
- Confidence Rating: The LLM scores its own confidence from 1 to 10.
-
Healing Routing:
- If all checks pass (relevance is high, faithful, confidence
$\ge 7/10$ ), the answer is cached and returned. - If any check fails and
retry_count$< 2$ : the agent rephrases the query, switches the retrieval strategy to Hybrid Reciprocal Rank Fusion (RRF), increases retrievaltop_kto 10, and loops back to Retrieve. - If retries are exhausted, it returns the final candidate flagged with a low-confidence badge.
- If all checks pass (relevance is high, faithful, confidence
- Backend API: FastAPI (Python 3.11+)
- Orchestration: LangGraph
- Vector Database: PostgreSQL 16 +
pgvectorextension - Caching Layer: Redis (semantic similarity index)
- Embeddings:
sentence-transformers/all-MiniLM-L6-v2(Local, CPU PyTorch) - LLM Inference: Groq API (Primary LPU Llama-3.3-70b/8b), Anthropic API (Reasoning Fallback)
- Frontend Interface: Next.js (TypeScript/React, Custom Vanilla CSS Glassmorphism)
- Deployments: Docker & Docker Compose
Copy the template configuration file:
cp .env.template .envOpen .env and fill in your keys:
GROQ_API_KEY: Get a free key from the Groq Console.ANTHROPIC_API_KEY: (Optional fallback).
To download dependencies, compile static assets, and launch all services in detached (background) mode:
docker-compose up --build -dOnce healthy, services will be accessible at:
- Web Dashboard: http://localhost:3000
- Backend API docs: http://localhost:8000/docs
- Postgres DB: Port
5432 - Redis Cache: Port
6379
- Direct File Upload: Go to the File Ingestion tab in the dashboard sidebar, click Select File, and select any
.pdf,.md,.txt, or source code file to upload and index it instantly. - Server Directory Ingestion: Type a folder path (like
/app/documentsto index pre-mounted files) in the "Folder Absolute Path" input and click Index Folder.
Ask questions in the chat box. You will see badges displaying verification traces:
β‘ Semantic Cache Hit (<200ms): Answer loaded directly from Redis.βοΈ Auto-healed (N retries): Shows that the answer failed verification first-pass and corrected itself.β οΈ Low Confidence Response: Returned if the context does not contain enough info.
docker-compose downNote: Databases and caches are preserved in Docker volumes, so your indexed documents are safe.
docker-compose up -dVerify security, prompt-injection guardrails, and verification node classification accuracy inside the container:
docker-compose exec -T backend python -m pytest