Ordinance
AI-powered RAG system for Hong Kong building regulations — hybrid search over 5,700+ regulation chunks across 224 government documents.
Overview
RAG system that answers questions about Hong Kong building regulations with cited, verifiable responses. Ingests PDFs from the Buildings Department, Fire Services Department, and Environmental Protection Department, then serves answers through a streaming API and browser UI.
Key numbers:
- 5,700+ regulation chunks across 224 documents
- 3 government departments (BD, FSD, EPD)
- 500x speedup on cached queries (8s to 15ms)
- Faithfulness scoring on every response
Technical highlights
Hybrid retrieval — Combines pgvector cosine similarity (3072-dim embeddings) with PostgreSQL full-text search. Reciprocal Rank Fusion merges both ranked lists, with optional Cohere reranking for precision.
Two-level query caching — Exact-match cache on normalized queries plus semantic cache using embedding similarity (0.95 cosine threshold). Repeated or near-identical questions resolve in ~15ms instead of the full 8s pipeline.
Structure-aware ingestion — Custom parser preserves Part > Section > Clause hierarchy from government PDFs. Chunks sized at 256-512 tokens with overlap, SHA-256 hashing for change detection across 200+ documents.
Citation-grounded generation — GPT-4o generates answers at temperature 0.1 with enforced citation format. Post-generation verification checks every cited section against retrieved context, flags phantom citations and uncited regulatory claims.
Quality assurance pipeline — LLM judge scores faithfulness 0-10 with reasoning. Citation verification, uncited claim detection, and per-query cost tracking run on every response.
Multi-query expansion — Query variants generated in parallel, fused via RRF. Live government web search runs concurrently for supplementary context.
Stack
TypeScript, Node.js, Express 5, PostgreSQL, pgvector, OpenAI (GPT-4o, text-embedding-3-large), Cohere Rerank, Zod, Railway