Legal Clause Analyzer
An AI-powered legal document analysis system that provides intelligent insights into legal clauses and contract terms using intent classification, vector similarity search, and asynchronous dataset ingestion.
- AI
- Legal
- Full-Stack

Context
Legal contract analysis often requires cross-referencing extensive clause libraries, identifying boilerplate terminology, and comparing subtle differences across contractual provisions. Manual review is time-consuming and prone to context loss.
Legal Clause Analyzer was built as a full-stack AI platform that unifies vector search, intent classification, and conversation memory. It allows legal professionals to upload custom clause datasets, query terms in plain English, and run automated comparative analyses.
The problem
Standard search tools in legal workflows rely on exact keyword matching, missing semantically related provisions or contextual variations across contracts. Furthermore, generic LLM queries lack grounding in verified corporate clause repositories.
From an architectural perspective, ingesting large legal clause datasets synchronously stalls web applications, while uniform LLM prompts struggle to differentiate between simple term definitions, multi-clause comparisons, and follow-up contextual questions.
Product strategy
The product strategy focuses on intent-driven agent routing grounded in local vector search. The system classifies user requests up-front, dynamically queries FAISS vector indices, and feeds structured context into dedicated prompt handlers.
Grounded intelligence
Generate answers from similarity-searched legal clauses rather than relying solely on raw LLM training memory.
Specialized intent routing
Route user inputs to specialized agent prompts based on query intent (definition, retrieval, comparison, follow-up) for precise structured outputs.
Non-blocking dataset ingestion
Offload CSV parsing, chunking, and embedding generation to background workers to maintain a responsive user interface.
AuraNode turns that strategy into one native workspace with a provider-neutral backend.
Intent-aware query execution
Classify queries into distinct intent types—DefinitionQuery, ClauseRetrieval, ComparativeAnalysis, or FollowUp—to dynamically assemble specialized response prompts.
Balanced comparative analysis
Extract comparison terms (e.g., 'X vs Y') to perform balanced vector retrievals for both concepts before synthesizing side-by-side legal evaluations.
Async bulk document processing
Upload custom CSV clause datasets that are processed asynchronously in background tasks, dynamically expanding the FAISS vector index.
Session-managed conversational state
Maintain organized, named conversation histories in PostgreSQL, enabling multi-turn contextual follow-ups without context fragmentation.
System architecture
A React SPA frontend communicates with a FastAPI backend, orchestrating intent classification via Gemini, vector search via FAISS, session memory via PostgreSQL, and asynchronous dataset ingestion through Celery and Redis.
React 18 single-page app
Vite and React 18 power a modern dark-themed interface managing session history, query interactions, markdown rendering, and CSV document uploads.
- React 18
- Vite
- CSS3
FastAPI backend
FastAPI accepts client requests, manages PostgreSQL session state, handles CSV uploads, and routes user prompts through intent classification and specialized agents.
- FastAPI
- Python
- SQLAlchemy
Intent classification engine
Gemini 1.5 Flash evaluates query intent (DefinitionQuery, ClauseRetrieval, ComparativeAnalysis, FollowUp, or fallback) to select the appropriate specialized prompt agent.
- Gemini 1.5 Flash
- Intent Classification
- Agent Routing
FAISS search & Gemini synthesis
The selected agent retrieves relevant clause chunks from FAISS vector storage and prompts Gemini 2.0 Flash to synthesize structured, grounded legal analysis.
- FAISS
- HuggingFace
- Gemini 2.0 Flash
FAISS vector store
Stores dense vector representations of legal clauses generated via HuggingFace all-MiniLM-L6-v2 embeddings for fast similarity retrieval.
Celery & Redis task queue
Parses CSV clause files asynchronously in background tasks, generating embeddings and building or dynamically updating the local FAISS index.
PostgreSQL database
Durable relational database managing active sessions, query-derived session titles, and complete turn-by-turn chat history for follow-up context.
Dataset ingestion lifecycle
When a user uploads a legal clause CSV dataset via the React frontend, FastAPI receives the file and immediately delegates processing to a Celery background worker backed by Redis. The worker parses the CSV text, extracts the clause content, generates dense vector representations using the HuggingFace all-MiniLM-L6-v2 embedding model, and either creates a new FAISS vector index or incrementally merges the new documents into the existing index stored on disk.
Query lifecycle
When a query is submitted, FastAPI passes the text to an intent classifier powered by Gemini 1.5 Flash. The prompt is categorized into one of five intents: DefinitionQuery, ClauseRetrieval, ComparativeAnalysis, FollowUp, or a sarcastic fallback handler (meanie).
Depending on the intent:
- For Definition and Retrieval queries, the system queries FAISS for top similarity matches, deduplicates results, and builds a grounded prompt for Gemini 2.0 Flash.
- For Comparative Analysis, the system parses terms being compared (e.g., “X vs Y”), performs balanced individual vector searches for each concept, and requests a side-by-side comparison structure.
- For Follow-up queries, the backend retrieves recent conversation turns from PostgreSQL and feeds both the chat history and new question to the model.
The resulting markdown answer is returned to the frontend and persisted into PostgreSQL chat history.
Separation of concerns
The React frontend remains completely decoupled from AI logic and vector mechanics, serving as a clean presentation layer. FastAPI acts as the central orchestrator protecting API keys and coordinating database, worker, and LLM calls. FAISS and HuggingFace provide isolated, sub-second vector search capabilities, Celery and Redis keep long-running embedding computations off the primary web thread, and PostgreSQL ensures conversation history remains durable across user sessions.
Key experiences
The user interface balances a dark glassmorphism aesthetic with high-utility workspace features tailored for contract review.
Dynamic session management
A persistent sidebar auto-titles sessions based on initial user prompts, allowing seamless switching and history deletion.
Asynchronous dataset upload
Drag-and-drop CSV ingestion with real-time task notifications lets users add custom clause libraries on the fly.
Structured markdown analysis
AI-generated responses render with rich markdown formatting, clean sectioning, summary bullets, and comparative tables.
Playful fallback interaction
Irrelevant or ambiguous prompts trigger a lighthearted agent fallback handler ('meanie') that maintains engagement while redirecting the user.
Engineering decisions
FastAPI for asynchronous backend routing
FastAPI provides low-latency REST endpoints for session CRUD, file upload processing, and intent classification dispatching.
Gemini 1.5/2.0 Flash for low-latency reasoning
Uses Gemini 1.5 Flash for fast query intent classification and Gemini 2.0 Flash for structured document analysis and comparative generation.
FAISS and HuggingFace for vector similarity
HuggingFace 'all-MiniLM-L6-v2' embeddings paired with FAISS provide sub-second local vector search over dense clause text datasets.
Celery & Redis for asynchronous worker queues
Background processing of CSV file uploads and incremental vector store updates prevents backend request timeouts.
PostgreSQL & SQLAlchemy for persistent chat context
Structured session tables and message histories back the conversational memory required for multi-turn follow-up queries.
React 18 & Vite with modern CSS glassmorphism
A fast Vite-powered single-page application built with custom dark glassmorphism styling and custom markdown renderers.
Complete legal AI pipeline with modular agent architecture.
Legal Clause Analyzer delivers a containerized microservice system combining FastAPI, React 18, Celery workers, PostgreSQL, and FAISS vector indexing.
The repository includes full multi-container deployment via Docker Compose, automated database schema creation, and sample dataset ingestion pipelines.
- Full-stack containerized architecture orchestrated via Docker Compose
- Multi-agent intent classification routing (Definition, Retrieval, Comparison, FollowUp)
- Async Celery CSV processing pipeline updating local FAISS indices
- PostgreSQL-backed conversation memory and session management
- Source reference available on GitLab