AI Paralegal Platform
Self-hosted legal operations platform for OCR document ingestion, playbook contract review with Word Track Changes redlines, and citation-backed research Q&A.
01. Problem & Context
Legal teams handling contract review and litigation discovery must ingest messy PDFs, Word documents, and email threads while strictly protecting attorney-client privilege and sensitive PII. Generic cloud chatbots hallucinate citations, leak privileged files to external APIs, and fail to produce audit-ready work product.
02. Engineering Solution
Architected a self-hostable legal operations platform combining a FastAPI/Celery backend and PostgreSQL 16 (pgvector) database with a React 18 dashboard and Microsoft Word Office.js task-pane add-in. Features hybrid HNSW semantic + GIN full-text retrieval, native OOXML Track Changes redlines, automated PII and privilege guardrails, Bates stamping, Legal Hold enforcement, and tamper-evident SHA-256 hash-chained audit logs. All outputs are AI-assisted legal operations aids for licensed attorney review and do not constitute legal advice.
03. System Architecture
The platform isolates multi-tenant legal matters using organization-scoped access control and optional PostgreSQL Row-Level Security (FORCE ROW LEVEL SECURITY). Ingestion workers normalize PDFs, DOCX, EML threads, and scanned files via Tesseract OCR into pgvector chunks, while a per-task ModelRouter dispatches requests to local Ollama models or guarded Gemini endpoints and streams live progress over Redis Pub/Sub SSE.
Core Architectural Components
Hybrid Semantic + Full-Text Retrieval & Click-to-Verify Source Viewer
Combines PostgreSQL pgvector HNSW embeddings with GIN tsvector search via Reciprocal Rank Fusion and links every citation directly to highlighted spans in the interactive source viewer.
Playbook Contract Review, Risk Matrix & Word Track Changes Export
Evaluates contracts against reusable clause playbooks, compiles batch Portfolio Risk Matrices, and generates native OOXML Word (.docx) Track Changes (<w:del>/<w:ins>) redlines alongside an Office.js Word add-in.
Legal-Ops Privacy, PII & Privilege Guardrails
Detects sensitive PII (SSN, EIN, bank accounts, Luhn-verified cards) and attorney-client or work-product privilege markers, blocking privileged materials from outbound cloud LLM providers and redacting PII before external inference.
Multi-Tenant Governance, Legal Hold & Hash-Chained Audit Trail
Enforces PBKDF2/JWT bearer auth with refresh-token rotation and logout jti denylisting, OIDC JWKS role synchronization, sequential page-level Bates stamping, Legal Hold retention locks, and verifiable SHA-256 hash-chained audit logs.
Enterprise DMS Connectors, Escalation Webhooks & Observability
Integrates SharePoint, Google Drive, iManage, and NetDocuments connectors, SSRF-hardened HMAC-SHA256 escalation webhooks, S3 SSE-KMS presigned downloads, OpenTelemetry tracing, Prometheus metrics, and Kubernetes/Helm charts.
04. Notable Engineering Decisions
Outbound Cloud LLM Guardrails & Per-Task Local Model Routing
Law firms cannot risk sending privileged documents to external APIs. The ModelRouter defaults sensitive tasks to self-hosted Ollama models (Llama 3.1, Qwen 2.5, Mistral NeMo, DeepSeek-R1) and enforces hard privilege blocks plus PII redaction before any cloud Gemini call.
PostgreSQL Row-Level Security & Tamper-Evident SHA-256 Audit Chaining
Matter confidentiality and litigation holds demand provable isolation and chain-of-custody records. Transaction-scoped organization context with FORCE RLS policies and SHA-256 hash-chained audit logs detect any unauthorized access or record tampering.
Full-Stack CI with Live pgvector, Deterministic Legal Evals & Playwright E2E
Legal workflows break silently without multi-layer verification. GitHub Actions CI runs ruff, mypy, offline pytest, live PostgreSQL pgvector/Redis integration tests, deterministic eval benchmarks, Vitest specs, Playwright E2E workflows, and Docker Compose checks.
05. Engineering Challenges & Solutions
Challenge: Preventing privileged legal material and client PII from leaking to external cloud LLM APIs.
Engineering Solution: Implemented pre-inference privilege classification and pattern-based PII detection that hard-block privileged chunks from cloud providers, scrub PII prior to approved external calls, and route sensitive workloads to local Ollama models.
Challenge: Verifying AI-extracted clauses and research citations across scanned PDFs, DOCX files, and email threads.
Engineering Solution: Built upload magic-byte verification with pluggable Tesseract OCR fallback, Reciprocal Rank Fusion retrieval, citation verification against source spans, and a Click-to-Verify Source Viewer drawer.
06. Implementation Highlights
- FastAPI + async SQLAlchemy 2.x backend with PostgreSQL 16 pgvector HNSW + GIN hybrid search
- Native OOXML Word (.docx) Track Changes redline export and Microsoft Word Office.js task-pane add-in
- Automated PII/privilege guardrails, sequential Bates stamping, Privilege Log export, and Legal Hold locks
- OIDC JWKS role sync, refresh-token replay detection, PostgreSQL RLS, and SHA-256 hash-chained audit logs
- OpenTelemetry tracing, Prometheus/Grafana monitoring, Kubernetes manifests, and Helm v3 chart
- AI-assisted legal-ops scope: explicit attorney-review gates and non-legal-advice safeguards throughout