Architecture & Routing Flow
How incoming questions are contextualized, routed, retrieved, reranked, and verified.
1. How Routes Are Decided (2-Stage Hybrid Router)
Follow-up Rewriting
If conversation history exists, pronouns and follow-ups (e.g. "What about L5?") are rewritten into a standalone query before routing.
Heuristic + BM25 Probe
Trivial greetings go straight to Route A with zero LLM calls. A fast Okapi BM25 probe checks keyword overlap against uploaded docs.
Doc-Aware Classifier
LangChain ChatPromptTemplate | llm.with_structured_output(RouteDecision) inspects the query, doc manifest, and BM25 hint to output validated route, confidence, and reason.
Asymmetric Fallback
If confidence < 0.70 or the query references uploaded documents, it falls back to Route B so retrieval verifies or abstains instead of hallucinating.
2. Three Execution Strategies
0 Retrieval Steps
Used for general ML, coding, and concept questions unrelated to uploaded docs. Saves retrieval latency and tokens.
Hybrid RRF + Rerank + CRAG
Retrieves top candidates via BM25 + Gemini Embeddings (RRF), grades relevance (0-10), retries once with a rewritten query if weak, or safely abstains.
Decomposition + Synthesis
Splits complex cross-document scenarios into 2-3 sub-queries, runs hybrid search per hop, deduplicates & reranks chunks, and synthesizes cited answers.
3. Key Engineering Details
LangChain Hybrid Search (BM25 + RRF)
Chunks documents with RecursiveCharacterTextSplitter and combines sparse BM25Okapi
with persisted LangChain InMemoryVectorStore (GoogleGenerativeAIEmbeddings):
RRF(d) = 1 / (60 + rank_bm25) + 1 / (60 + rank_dense)
Scaling to Production (10k QPS)
The doc-aware LLM router works zero-shot on any newly uploaded PDF. At high traffic, logged routing decisions + outcomes can be distilled into a local DeBERTa / ModernBERT classifier (<15 ms CPU, zero API cost), following Jeong et al. (NAACL 2024).