Skip to main content
HB
AI & RAG Platforms
Freelance client

Multi-Stage Legal RAG with Azure AI Search

Azure AI Search · Hybrid BM25 · Reciprocal Rank Fusion · Cohere Rerank

Hasan Butt
By Hasan ButtPublished: February 2025
0.98Ragas Faithfulness Score (Up from 0.72)
01

The Challenge

Naive vector search on 3,145 complex statutory documents hallucinated on tables and nested clauses (0.72 faithfulness score). Standard RAG implementations chunk documents by token count, which acts as a hallucination factory for dense statutory texts. When schedules and rate tables are merged into text blobs, retrieval fails to isolate the exact numbers, causing LLMs to hallucinate.

02

The Approach

Built a 5-layer pipeline: (1) Structure-aware table parsing, (2) Hybrid BM25 keyword + dense vector search via Azure AI Search, (3) Reciprocal Rank Fusion, (4) Cohere Rerank layer, (5) Strict context guardrails. Rejected simple "top-k expansion" which blew token budgets. Chunks are demarcated by semantic section transitions rather than token limits, ensuring standalone schedule tables.

03

System Architecture

Loading architecture diagram...
04

Azure Infrastructure

Azure Infrastructure

Architecture for the Multi-Stage Legal RAG Pipeline. Click a node to view its configuration details.

HTTPS/SSERetrieveGenerateMemoryEmbedTriggerSearchNext.js FrontendSSE StreamingAzure App ServiceFastAPI BackendAzure AI SearchBM25 + Semantic RerankAzure AI FoundryGPT-4o AgentAzure OpenAIEmbeddings 3-largeCosmos DBConversation MemoryAzure BlobPDF StorageMonitoringTelemetry + Audit
Select a node to view infrastructure details
Primary data flow
Observability
05

Interactive Demo

Walk through the system design and data flow for this project step by step.

How the Legal RAG Pipeline Works

PDF3,145filespdfplumberper-page extractText PagesImage-OnlyAzure Blob StorageEvent Grid triggerAuto-pipeline on new upload

Step 1Document Ingestion and PDF Extraction

The system processes 3,145 Hong Kong and Singapore legal ordinances as PDF files. Each PDF is opened with pdfplumber and every page is extracted independently. Pages returning empty text are flagged as image-only content and logged for manual review since they are invisible to the retrieval system. When a new PDF is uploaded to Azure Blob Storage, an Event Grid trigger automatically kicks off the ingestion pipeline without any manual intervention.

Key Insight

Image-only pages are classified as a severity-high issue in the corpus audit system. These pages often contain scanned schedules or fee tables that are critical for answering queries but completely invisible to text-based retrieval. The audit identified and flagged these gaps across all 3,145 documents.

1 of 7

06

Overview

Built a 5-layer retrieval pipeline over 3,145 dense legal documents, fixing real failure modes at each step. Naive vector search on statutory documents hallucinated on tables and nested clauses (0.72 faithfulness score). Built a 5-layer pipeline: (1) Structure-aware table parsing, (2) Azure AI Search Hybrid BM25 keyword + dense vector search, (3) Reciprocal Rank Fusion, (4) Cohere Rerank layer, (5) Strict context guardrails on Azure AI Foundry. Faithfulness jumped from 0.72 to 0.98, context precision reached 0.94, and prompt token waste fell by 34%.

07

Business Impact

Faithfulness jumped from 0.72 to 0.98, context precision reached 0.94, and prompt token waste fell by 34%. The 200-question test suite passed with zero errors, zero hallucinations, and zero URL leaks across three consecutive identical runs. Structure-aware chunking resolved the core retrieval problem where schedule tables were being merged into surrounding text.

08

Technical Highlights

  • Structure-aware page classification into schedule, definition, preamble, and operative types
  • 10 deterministic injection triggers that bypass semantic search for known query patterns
  • BM25 keyword retrieval with Azure AI Search semantic reranking on 8,709+ schedule chunks
  • Jurisdiction detection with weighted regex scoring for Hong Kong and Singapore legal terms
  • Pre-agent interceptors block source disclosure probes and verify deep subsection references
  • Idempotent indexing with MD5-based chunk IDs and mergeOrUpload for safe re-indexing
  • Cosmos DB conversation memory with GPT-4o summarization every 4 turns
  • Corpus audit system detecting 5 classes of retrieval gaps across 3,145 PDFs
  • SSE streaming for real-time response delivery to the Next.js frontend
  • Azure Blob trigger auto-indexes new documents without manual intervention
09

Frequently Asked Questions

Q: What makes this Legal RAG system different from standard RAG implementations?

It uses a multi-stage approach featuring structure-aware page classification (preamble, schedule, operative, definitions) to chunk documents by semantic boundaries rather than token counts. This ensures large schedules and rate tables are kept intact, completely eliminating context-merge hallucinations.

Q: How do the deterministic injection triggers work?

I implemented ten deterministic query triggers that detect search patterns mapping to known legal structures (such as stamp duty rates). These bypass semantic vector search entirely, using OData filters to fetch and inject verified legal tables as context, guaranteeing 100% accurate ground truth for critical numbers.

Q: How was retrieval accuracy validated for the 3,145 Hong Kong and Singapore ordinances?

I created a 200-question automated validation suite covering edge cases (such as Reciprocating Countries lists and non-existent sections). The suite completed three consecutive clean runs with zero errors and zero hallucinations before the system went to production.

Want results like this for your infrastructure?

I specialize in taking complex AI pipelines and cloud setups from concept to high-availability production. Let's discuss how to optimize your workloads, secure your environment, and reduce cloud costs.