A full-stack application that transforms legal documents into reusable templates with smart variable extraction and automated drafting capabilities.
- Document Ingestion: Upload and process PDF/DOCX legal documents
- Smart Template Generation: Automatically extract variables and create reusable templates
- AI-Powered Drafting: Generate documents using natural language queries
- Variable Management: Define and manage template variables with validation
- Web Search Integration: Find relevant templates online when no local match exists
- Framework: FastAPI (Python)
- LLM: Google Gemini 1.5/Flash
- Database: PostgreSQL/SQLite
- Document Processing: Python-Docx, PyPDF2
- Vector Search: FAISS (for template similarity)
- Framework: Next.js 14 (App Router)
- UI: Shadcn UI + Tailwind CSS
- State Management: React Query
- Form Handling: React Hook Form + Zod
- Node.js 18+
- Python 3.10+
- PostgreSQL (or SQLite for development)
- Google AI API Key
- 
Clone the repository git clone https://github.com/yourusername/lexi.git cd lexi
- 
Backend Setup cd backend python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt 
- 
Frontend Setup cd frontend npm install
- 
Environment Variables Create .envfiles in bothbackend/andfrontend/directories:Backend (.env) DATABASE_URL=postgresql://user:password@localhost:5432/lexi GOOGLE_AI_API_KEY=your-google-ai-key EXA_API_KEY=your-exa-ai-key # For bonus featureFrontend (.env.local) NEXT_PUBLIC_API_URL=http://localhost:8000
- 
Start Backend cd backend uvicorn main:app --reload
- 
Start Frontend cd frontend npm run dev
- 
Access the Application - Frontend: http://localhost:3000
- API Docs: http://localhost:8000/docs
 
- 
Document Ingestor - Handles file uploads (PDF/DOCX)
- Extracts text and metadata
- Processes documents into clean text
 
- 
Template Engine - Identifies and extracts variables using Gemini
- Creates Markdown templates with YAML front-matter
- Manages template storage and retrieval
 
- 
Drafting System - Processes natural language queries
- Matches queries to templates
- Handles variable collection and validation
- Generates final documents
 
- 
Web Integration (Bonus) - Searches for relevant templates online
- Processes and ingests new templates on-the-fly
- Expands template library automatically
 
SYSTEM_PROMPT = """
You are a legal document templating assistant. Extract reusable fields from the provided legal document.
Return a JSON object with variables and their metadata.
"""TEMPLATE_MATCH_PROMPT = """
Given the user's request and available templates, select the most appropriate template.
Consider the document type, jurisdiction, and content.
Return the template_id and a confidence score.
"""CREATE TABLE templates (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    doc_type VARCHAR(100),
    jurisdiction VARCHAR(100),
    similarity_tags TEXT[],
    body_md TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE template_variables (
    id SERIAL PRIMARY KEY,
    template_id INTEGER REFERENCES templates(id),
    key VARCHAR(100) NOT NULL,
    label VARCHAR(255) NOT NULL,
    description TEXT,
    example TEXT,
    required BOOLEAN DEFAULT true,
    dtype VARCHAR(50),
    regex TEXT,
    enum_values TEXT[]
);- 
Upload a Document - Go to /templates/upload
- Select a legal document (PDF/DOCX)
- Review extracted variables
- Save as a new template
 
- Go to 
- 
Generate a Draft - Go to /draft
- Enter your request (e.g., "Draft a notice to insurer in India")
- Answer the variable prompts
- Download or copy the generated document
 
- Go to 
MIT
Created with ❤️ by UOIONHHC