legal-contract-analyzer

Legal Contract Analyzer

AI-powered legal contract analysis platform using MindsDB Knowledge Bases for intelligent document processing, risk assessment, and automated reporting.

License Node.js Python MindsDB Docker

https://github.com/user-attachments/assets/b4ffa79e-f270-41a7-9309-f956062c6e33

⚡ Quick Deploy (2 minutes)

Try it now with one command - no setup required!

# Create .env file with your OpenAI or Gemini API key, then run:
curl -o docker-compose.yml https://raw.githubusercontent.com/ritwickrajmakhal/legal-contract-analyzer/main/docker-compose.prod.yml && docker-compose up -d

Access your AI contract analyzer at: http://localhost:3000

What You Get Instantly

Complete AI Contract Analysis Platform
Pre-configured MindsDB Knowledge Base
Multi-source Data Integration (PostgreSQL, SharePoint, Dropbox, etc.)
Interactive Chat Interface with Natural Language Queries
Risk Assessment & Compliance Monitoring
Automated Email Notifications & Reporting

🚀 Key Features

🏗️ Architecture

┌─────────────────────────────────────────────────────────────────────────────┐
│                          Frontend Layer (Next.js - Port 3000)               │
├─────────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │  Chat Interface │  │    Analytics    │  │  Integrations   │              │
│  │  • ChatContainer│  │  • Risk Gauges  │  │  • Data Sources │              │
│  │  • Composer     │  │  • Timeline     │  │  • KB Manager   │              │
│  │  • Message      │  │  • Charts       │  │  • Sync Status  │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
└─────────────────────────────────────────────────────────────────────────────┘
                                    │ HTTP/REST API
                                    ▼
┌─────────────────────────────────────────────────────────────────────────────┐
│                        Backend Layer (FastAPI - Port 8000)                  │
├─────────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │  Agent Routes   │  │ Integration API │  │   Upload API    │              │
│  │  • Chat API     │  │ • Data Sources  │  │ • PDF Upload    │              │
│  │  • Streaming    │  │ • Connection    │  │ • File Parsing  │              │
│  │  • Analytics    │  │ • Table Sync    │  │ • Validation    │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
│                                │                                            │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │   KB Manager    │  │ Email Actions   │  │ Core Services   │              │
│  │ • KB Operations │  │ • SMTP Service  │  │ • MindsDB Mgr   │              │
│  │ • Search API    │  │ • Notifications │  │ • Query Gen     │              │
│  │ • Sync Jobs     │  │ • Reporting     │  │ • Health Check  │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
└─────────────────────────────────────────────────────────────────────────────┘
                                    │ MindsDB SDK
                                    ▼
┌─────────────────────────────────────────────────────────────────────────────┐
│                       AI/Database Layer (MindsDB - Port 47334)              │
├─────────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │ Knowledge Base  │  │   AI Models     │  │   Integrations  │              │
│  │ • Vector Store  │  │ • OpenAI GPT-4  │  │ • PostgreSQL    │              │
│  │ • Document KB   │  │ • Google Gemini │  │ • SharePoint    │              │
│  │ • Search Index  │  │ • Embeddings    │  │ • Dropbox       │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
│                                │                                            │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │     Agents      │  │    Projects     │  │   Data Flows    │              │
│  │ • Contract AI   │  │ • Workspaces    │  │ • ETL Pipeline  │              │
│  │ • Risk Analysis │  │ • Permissions   │  │ • Auto Sync     │              │
│  │ • Q&A System    │  │ • Configs       │  │ • Notifications │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
└─────────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────────┐
│                              Data Sources Layer                             │
├─────────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │   Databases     │  │  Cloud Storage  │  │   Enterprise    │              │
│  │ • PostgreSQL    │  │ • SharePoint    │  │ • Salesforce    │              │
│  │ • Snowflake     │  │ • Dropbox       │  │ • Elasticsearch │              │
│  │ • Files/PDF     │  │ • GitHub        │  │ • Apache Solr   │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
│                                │                                            │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐              │
│  │ Communication   │  │   Productivity  │  │   Development   │              │
│  │ • Email/SMTP    │  │ • Notion        │  │ • GitLab        │              │
│  │ • Notifications │  │ • Workspaces    │  │ • Repositories  │              │
│  │ • Reporting     │  │ • Knowledge     │  │ • CI/CD         │              │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘              │
└─────────────────────────────────────────────────────────────────────────────┘

                              Data Flow & Security
┌─────────────────────────────────────────────────────────────────────────────┐
│ • PDF Upload → FastAPI → MindsDB Knowledge Base → Vector Embeddings         │
│ • User Query → Chat API → AI Agent → Knowledge Base Search → LLM Response   │
│ • Data Sync → Integration API → MindsDB → Automated Knowledge Base Update   │
│ • Risk Analysis → Agent API → ML Models → Interactive Analytics Dashboard   │
│ • Email Actions → SMTP Service → Automated Notifications & Reports          │
└─────────────────────────────────────────────────────────────────────────────┘

📋 Prerequisites

🚀 Quick Start

Deploy instantly with one command:

  1. Create your .env file
    # Required: AI Model API Key (choose one)
    OPENAI_API_KEY=your-openai-api-key-here
    # OR
    GEMINI_API_KEY=your-gemini-api-key-here
    
    # Optional: Email notifications
    SMTP_USER=your-email@gmail.com
    SMTP_PASSWORD=your-app-password
    
  2. Run the deployment command:
    curl -o docker-compose.yml https://raw.githubusercontent.com/ritwickrajmakhal/legal-contract-analyzer/main/docker-compose.prod.yml && docker-compose up -d
    
  3. Access the application
    • Frontend: http://localhost:3000
    • Backend API: http://localhost:8000
    • MindsDB: http://localhost:47334

Local Development Setup

  1. Clone and setup
    git clone https://github.com/ritwickrajmakhal/legal-contract-analyzer.git
    cd legal-contract-analyzer
    cp .env.example .env
    # Edit .env and add your API keys
    
  2. Start all services
    docker-compose up -d
    
  3. Access the application
    • Frontend: http://localhost:3000
    • Backend API: http://localhost:8000
    • MindsDB: http://localhost:47334

📖 Usage

Getting Started

  1. Launch the Application
    • Open http://localhost:3000 in your browser
    • The app opens to a clean chat interface with suggested actions
  2. Create Your First Conversation
    • Click “Start New Conversation” or use the sidebar to create a new chat
    • Each conversation maintains its own context for focused contract analysis

Core Workflows

1. Upload & Analyze Contracts

Upload PDF Files:

Example:

Upload a vendor agreement PDF, then ask: "Analyze the risk level of this contract"

2. Connect Data Sources

Access Integrations:

Sync Data:

3. Ask Questions & Get Insights

Natural Language Queries: Use the chat interface to ask questions in plain English:

"What are my highest-risk vendor contracts?"
"Show contracts expiring in the next 3 months"
"Compare liability clauses across all agreements"
"Find contracts with payment terms over 60 days"

Quick Actions:

4. Interactive Analytics

View Visual Insights:

Example Queries:

"Show me a risk distribution chart for all contracts"
"Display contract renewal timeline for Q1 2025"
"Provide portfolio metrics and KPIs"

5. Automated Email Actions

Smart Email Suggestions: The AI automatically suggests email actions based on analysis results:

Execute Email Actions:

6. Knowledge Base Management

Access KB Manager:

Advanced Search:

Keyboard Shortcuts

Best Practices

  1. Start Specific: Begin with focused questions about particular contract types or time periods
  2. Upload Context: Upload relevant PDFs before asking detailed questions about them
  3. Use Quick Actions: Leverage predefined prompts for common analysis tasks
  4. Connect Sources: Set up data source integrations for comprehensive portfolio analysis
  5. Regular Sync: Keep data synchronized with the “Sync Now” button
  6. Review Analytics: Use visual insights to identify patterns and high-risk areas

🧪 Example Queries

Risk Analysis

"Identify contracts with high liability exposure"
"Show me agreements missing termination clauses"
"Find vendors with uncapped indemnification terms"

Compliance Monitoring

"Check GDPR compliance across data processing agreements"
"Verify all contracts have proper governing law clauses"
"Find agreements missing required insurance provisions"

Financial Analysis

"Calculate total contract value by vendor"
"Show payment terms distribution across portfolio"
"Identify contracts with penalty clauses over $50k"

Timeline Management

"List all contracts expiring in Q1 2025"
"Show renewal deadlines requiring 60-day notice"
"Find overdue contract renewals"

🔧 Development

Project Structure

legal-contract-analyzer/
├── app/                     # Next.js pages (layout, globals, main page)
├── components/              # React UI components (chat, analytics, integrations)
├── lib/                     # TypeScript utilities and hooks
├── python-services/         # FastAPI backend
│   ├── api/                 # API routes (agent, integrations, uploads, kb)
│   ├── mindsdb_manager.py   # MindsDB integration
│   ├── kb_manager.py        # Knowledge base operations
│   └── requirements.txt     # Python dependencies
├── mdb_data/                # MindsDB persistent storage
├── docker-compose.yml       # Local development
├── docker-compose.prod.yml  # Production deployment
└── package.json            # Node.js dependencies

Available Scripts

Frontend

npm run dev          # Start development server
npm run build        # Build for production
npm run start        # Start production server
npm run lint         # Run ESLint
npm run type-check   # TypeScript type checking

Backend

npm run python:install  # Install Python dependencies
npm run python:dev      # Start FastAPI development server

API Testing

Test the backend API:

curl http://localhost:8000/health
curl -X POST http://localhost:8000/api/agent/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello, analyze my contracts"}'

🐳 Docker Deployment

Using Docker Hub images (no building required):

# Download production configuration
curl -o docker-compose.yml https://raw.githubusercontent.com/ritwickrajmakhal/legal-contract-analyzer/main/docker-compose.prod.yml

# Start all services
docker-compose up -d

Using local build:

# Build and start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

Available Docker Images

The platform is published on Docker Hub with optimized production images:

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
    
  3. Commit your changes
    git commit -m 'Add amazing feature'
    
  4. Push to the branch
    git push origin feature/amazing-feature
    
  5. Open a Pull Request

Development Guidelines

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

📞 Support


Built with ❤️ for legal professionals and contract managers