All work

02 / 04Case study · 2024 · AI · Legal tech

AI Document Review & Q&A Portal

RAG-powered legal document intelligence with e-signature workflows

Role
End-to-end (Frontend, RAG Pipeline, Ingestion Workers, Infrastructure)
Year
2024
Type
Multi-tenant SaaS with bring-your-own-database
Stack
Next.jsRAGLangChainpgvectorModalSupabase
Document Q&A Interface

Overview

This platform transforms how teams interact with large document sets. Instead of searching PDFs manually, users ask natural-language questions and receive cited, sourced answers in real-time. Built as a full-stack RAG system, it handles document ingestion (PDF, Word, Excel, PPT), embedding generation, vector search with reranking, and LLM-powered summarization. Additional modules include timeline visualization, due diligence checklists, party/risk analysis, and a complete e-signature workflow with templating and audit trails.

The Challenge

One question could mean reading hundreds of contracts.

Legal and M&A teams often work with hundreds of contracts, agreements, and diligence documents. Answering a single question—like 'What are the termination clauses?'—requires reading entire files, cross-referencing dates and parties, and summarizing findings. Traditional keyword search fails on synonyms and context. The goal was to build a system that could ingest any document format, understand semantic intent, retrieve relevant passages with high precision, and generate accurate, explainable answers with citations.

Approach

Four decisions that shaped the system.

  1. 01Built RAG pipeline with enhanced retrieval (BGE-M3 embeddings + reranker) and streaming LLM responses
  2. 02Designed multi-format document ingestion service with OCR fallback and batch chunking
  3. 03Implemented timeline extraction, due diligence checklists, and party/risk analysis
  4. 04Created full e-signature module with audit logs and guest-to-user conversion tracking

Technical Architecture

Technical stack

Frontend

  • Next.js (App Router)
  • React 19
  • TypeScript
  • TailwindCSS
  • shadcn/ui
  • React Email

RAG & AI

  • LangChain / LangGraph
  • Modal (vLLM hosting: Qwen2.5-32B)
  • BGE-M3 Embeddings + Reranker
  • @xenova/transformers (local fallback)
  • OpenAI-compatible endpoints

Data & Vector Search

  • Supabase (PostgreSQL + pgvector)
  • Full-text lexical search
  • Custom RPCs

Ingestion Workers

  • Modal Python workers
  • Marker PDF extraction
  • PyPDF2, textract, python-docx
  • OCR fallback

Infrastructure

  • Vercel (Frontend + API Routes + Cron)
  • Modal (LLM inference + ingestion)
  • Supabase Cloud / BYOD Supabase

Integrations

  • Clerk (Auth + Orgs)
  • Resend (Email)
  • pdfme (E-signatures)
  • Mixpanel (Analytics)
  • Sentry (Monitoring)

Key Features Built

What was built

  • 01Natural-language Q&A with streaming responses and inline source citations
  • 02Enhanced retrieval: BGE-M3 embeddings, reranking, lexical augmentation, and adaptive thresholds
  • 03Multi-format ingestion: PDF, Word, Excel, PowerPoint, TXT with OCR fallback
  • 04Timeline visualization: LLM-assisted extraction of dates, events, and parties from contracts
  • 05Due diligence checklists: Link documents to checklist items with completion tracking
  • 06Party and risk analysis: Extract entities, aliases, and assess risk via custom rules
  • 07Knowledge graph foundation: Items, links, and relationships
  • 08E-signature module: Templates (pdfme), guest signing, audit logs, final PDF storage
  • 09Multi-tenant SaaS with BYOD: Org-scoped data with encrypted customer Supabase credentials (AES-256-GCM)
  • 10Monitoring dashboards: Processing metrics, extraction timing, error tracking, cost summaries

Platform Screenshots

Inside the product

Hard problems

Where it got difficult, and what fixed it.

Challenge

Low retrieval precision on legal jargon and synonyms.

Solution

Switched to BGE-M3 embeddings (trained on domain-specific text) + reranker; improved precision by 40%.

Challenge

Large PDFs (1000+ pages) caused ingestion timeouts.

Solution

Batched chunking with progress callbacks; added job queue with retry logic.

Challenge

LLM hallucinations citing non-existent clauses.

Solution

Enforced strict context-only generation; added citation validation to ensure quoted text exists in source chunks.

Challenge

Multi-tenant data isolation with customer-owned DBs.

Solution

Built connection router with encrypted config storage (AES-256-GCM) and per-request DB client resolution.

Results

Review time went from hours to minutes, with citations.

<3sQuery response, streaming
~50Pages ingested per minute
85%+Retrieval precision
10K+Documents processed
  • 01Reduced document review time from hours to minutes with cited, explainable answers
  • 02Processed 1000+ page documents with sub-3-second query response times
  • 03Enabled multi-tenant SaaS with BYOD (Bring Your Own Database) for data sovereignty