Appearance
Step 8: AI Integration & Local RAG Architecture Deep Dive
Executive Summary: The Air-Gapped AI Philosophy
Unlike conventional AI wrappers that pass unvetted customer prompts over metered HTTPS connections to OpenAI, Anthropic, or Google APIs, the 21-BIS-Compliance-Engine is architected as an air-gapped, zero-cloud, deterministic RAG system. It operates entirely within local CPU memory using optimized ONNX runtimes and exact vector distance geometry.
1. Machine Learning Models & Embedding Pipeline
Dense Embedding Model: BAAI/bge-small-en-v1.5
- Architecture: Bidirectional Transformer Encoder optimized for dense retrieval.
- Dimensionality: 384 dimensions.
- Runtime Engine: Microsoft ONNX Runtime (CPU execution provider).
- Execution Memory: ~60 MB RAM.
- Embedding Latency: 12 to 15 milliseconds on a single modern CPU core.
- Why Chosen: Consistently ranks at the top of the Massive Text Embedding Benchmark (MTEB) for retrieval tasks while requiring a fraction of the compute and memory needed by 768- or 1536-dimensional models.
Vector Search Index: FAISS IndexFlatIP
- Type: Inverted Index Flat Inner Product (
faiss.IndexFlatIP). - Mathematical Principle: Cosine similarity is computed directly via the dot product of pre-normalized vectors: $$\text{Cosine Similarity}(\vec{u}, \vec{v}) = \frac{\vec{u} \cdot \vec{v}}{|\vec{u}|2 |\vec{v}|2} = \vec{u}{\text{norm}} \cdot \vec{v}{\text{norm}}$$ Because all corpus vectors are pre-normalized using
faiss.normalize_L2(embeddings)during index creation ([rebuild_faiss_index.py:L17](file:///Users/avishjain/avishjain9-repos/21-bis-compliance-engine/rebuild_faiss_index.py#L17)), the inner product calculationIndexFlatIPdirectly outputs the exact cosine similarity score without expensive square-root operations. - Precision: 100% exact nearest-neighbor search (zero recall degradation, unlike HNSW or IVF approximation graphs).
- Search Time: <0.2 milliseconds for $N=47$ corpus vectors.
Supervised Classification Model: bis_product_department_model_v3.pkl
- Type: Scikit-Learn serialized pipeline (
joblib). - Purpose: Direct administrative routing to regulatory departments (CED for Civil Engineering, MTD for Metallurgical/Steel, FAD for Food & Water, MoRTH_AIS for Automotive Homologation).
- Inference Time: <0.1 milliseconds.
2. Why a Pre-Trained Embedding API Instead of Training a Custom Foundation Model?
| Decision Dimension | Training a Custom Transformer Model | Using Pre-Trained Embedding + Local FAISS RAG (Chosen) |
|---|---|---|
| Compute Hardware Required | Cluster of 8x NVIDIA H100/A100 GPUs | Standard 2-Core / 4-Core Commodity CPU |
| Capital Expenditure (Cost) | $80,000 to $250,000+ in cloud compute | $0.00 |
| Training Dataset Size | Millions of paired regulatory text tokens | 47 curated, authoritative statutory entries |
| Engineering Time | 4 to 8 months of dataset cleaning & hyperparameter tuning | Ready for production in 1 week |
| Updating Knowledge | Requires full model re-training or parameter-efficient fine-tuning (LoRA) | Instant: append JSON entry and run [rebuild_faiss_index.py](file:///Users/avishjain/avishjain9-repos/21-bis-compliance-engine/rebuild_faiss_index.py) (0.8s) |
| Regulatory Hallucination Risk | High: Autoregressive decoders invent fake clause numbers | Zero: 100% deterministic retrieval from verified gazettes |
3. Corpus Text Representation & Semantic Construction
To maximize semantic retrieval accuracy across diverse phrasing, the text representation for each standard in [rebuild_faiss_index.py:L12](file:///Users/avishjain/avishjain9-repos/21-bis-compliance-engine/rebuild_faiss_index.py#L12) is synthesized using a high-density keyword concatenation formula:
$$\text{Embedding Document} = \text{Product Name} + \text{" "} + \text{Text Keywords / Scope} + \text{" "} + \text{Standard Code}$$
Real Example from model2_corpus.json:
- Standard:
IS 12269:2013 - Product Name:
Ordinary Portland Cement 53 Grade - Synthesized Text:
"Ordinary Portland Cement 53 grade OPC compressive strength clinker concrete structural IS 12269" - Why This Works: Captures both colloquial user search patterns ("OPC 53 concrete cement") and exact legal standard codes ("IS 12269"), guaranteeing cosine similarity scores $>0.80$ for both technical and non-technical queries.
4. Hallucination Elimination & Guardrailing
In standard LLMs, hallucination occurs because models predict the most probable subsequent token rather than verifying truth. The 21-BIS-Compliance-Engine completely eliminates hallucination through two architectural layers:
- Deterministic Retrieval Boundary: The engine never generates text tokens dynamically. The return payload is constructed directly from immutable JSON fields (
standard,qco_order,scheme,lab,citation). - 4-Tier Domain Guardrail: If an input fails to match an authentic regulatory domain or fails the 0.45 similarity threshold, the system rejects the prompt immediately rather than guessing.
5. Token & Cost Economics
Because the system relies on local ONNX vector inference, the economic cost model is flat:
- Cost per 1,000 queries: $0.00
- Monthly recurring cloud model subscription: $0.00
- Token rate limit constraints: None (bounded only by local CPU saturation)
- Air-gap capability: 100% functional without an active internet connection