Agentic RAG pipeline — LangGraph retrieve→grade→rewrite loop, hybrid BM25 + Supabase pgvector search with Reciprocal Rank Fusion, FastAPI, Gemini embeddings, LangSmith tracing. Domain: smart metering & mini-grids in rural Africa.
# asase-rag-pipeline
A production-grade **Agentic RAG** pipeline built with LangGraph, backed by Supabase pgvector and hybrid BM25 + semantic retrieval. The domain is smart electricity metering and mini-grid deployment in rural Africa.
---
## What this is
This repository implements a complete retrieval-augmented generation system as a FastAPI service. The core idea: instead of a naive retrieve-then-generate loop, the agent _evaluates_ what it retrieves, rewrites the query if relevance is low, and retries — only generating an answer when it has grounding it can trust.
The knowledge base covers technical material on mini-grids, prepaid metering (STS/PAYG), solar sizing, battery storage, and rural electrification in Sub-Saharan Africa.
---
## Architecture
```
POST /chat
│
├─ Rate limiter (slowapi — 20 req/min per IP, returns 429 on breach)
│
├─ LangSmith trace (wraps the full request for end-to-end observability)
│
├─ Input security (prompt injection detection, PII masking)
│ └─ 400 if blocked
│
├─ Response cache (TTL-based, keyed on cleaned query — returns early on hit)
│
├─ AgenticRAG (LangGraph)
│ │
│ ├─ retrieve Hybrid search: BM25 + pgvector, fused via RRF
│ ├─ grade LLM scores each retrieved doc 0–1 for relevance
│ ├─ rewrite ──► If score < threshold and retries remain,
│ │ └─ retrieve reformulate query and search again
│ ├─ generate Grounded answer from primary LLM (Gemini Flash)
│ │ Falls back to secondary model on failure
│ └─ fallback Graceful message when retrieval fails completely
│
├─ Output security (validates and sanitises the generated response)
│
├─ Cache store (write validated response for future cache hits)
│
├─ Metrics (latency, token estimates, error rate, cache hit rate)
│
└─ Structured log (thread ID, model used, latency, sources retrieved)
│
▼
JSON response (response, source …