law_ai

LegalAI - Legal Document Search & Analysis Platform

A comprehensive AI-powered legal document search and analysis platform built with FastAPI, React, and advanced AI models. This system provides semantic search, legal reasoning, document analysis, and intelligent categorization for legal professionals.

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Frontend (React)                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚   Search    β”‚ β”‚   Upload    β”‚ β”‚  Analysis   β”‚           β”‚
β”‚  β”‚   Interface β”‚ β”‚   System    β”‚ β”‚  Dashboard  β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                     β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
                     β”‚             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                Backend (FastAPI)                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚  Document    β”‚ β”‚   Vector     β”‚ β”‚      AI      β”‚        β”‚
β”‚  β”‚  Processor   β”‚ β”‚   Store      β”‚ β”‚   Analyzer   β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                 β”‚          β”‚          β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β” β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”
          β”‚PostgreSQLβ”‚ β”‚ChromaDBβ”‚ β”‚OpenAI  β”‚
          β”‚Database  β”‚ β”‚Vectors β”‚ β”‚Models  β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Features

🧠 AI-Powered Analysis

πŸ“„ Document Processing

πŸ“Š Analytics & Insights

πŸš€ Quick Start

Prerequisites

1. Clone the Repository

git clone <repository-url>
cd law_ai

2. Backend Setup

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp env.example .env
# Edit .env with your configuration

3. Frontend Setup

cd frontend
npm install

4. Database Setup

# For development (SQLite)
cd backend
python -m app.core.database

# For production (PostgreSQL)
# Ensure PostgreSQL is running and create database
createdb legal_ai

5. Start Development Servers

# Terminal 1: Backend
cd backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

# Terminal 2: Frontend
cd frontend
npm start

Visit http://localhost:3000 to access the application.

πŸ”§ Configuration

Environment Variables

Create a .env file in the root directory:

# OpenRouter AI Configuration
OPENROUTER_API_KEY=your_openrouter_api_key_here
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1

# AI Model Configuration
DOCUMENT_ANALYSIS_MODEL=anthropic/claude-3-sonnet
LEGAL_REASONING_MODEL=anthropic/claude-3-opus
EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2

# Database Configuration
DATABASE_URL=sqlite:///./legal_ai.db  # Development
# DATABASE_URL=postgresql://user:password@localhost/legal_ai  # Production

# Vector Database
CHROMA_HOST=localhost
CHROMA_PORT=8001
CHROMA_COLLECTION_NAME=legal_documents

# File Processing
MAX_FILE_SIZE_MB=50
SUPPORTED_FILE_TYPES=.pdf,.docx,.txt
CHUNK_SIZE=1000
CHUNK_OVERLAP=200

# Search Configuration
MAX_SEARCH_RESULTS=50
SIMILARITY_THRESHOLD=0.7
ENABLE_HYBRID_SEARCH=true

# Optional: Advanced Features
REDIS_URL=redis://localhost:6379  # For caching
ELASTICSEARCH_URL=http://localhost:9200  # For advanced search

Model Configuration

The system supports various AI models through OpenRouter:

πŸ“ Project Structure

law_ai/
β”œβ”€β”€ backend/                    # FastAPI backend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ api/               # API endpoints
β”‚   β”‚   β”œβ”€β”€ core/              # Core configuration
β”‚   β”‚   β”œβ”€β”€ models/            # Database models
β”‚   β”‚   β”œβ”€β”€ schemas/           # Pydantic schemas
β”‚   β”‚   └── services/          # Business logic
β”‚   └── requirements.txt
β”œβ”€β”€ frontend/                   # React frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/        # React components
β”‚   β”‚   β”œβ”€β”€ pages/             # Page components
β”‚   β”‚   └── styles/            # CSS/Tailwind
β”‚   └── package.json
β”œβ”€β”€ pdf/                       # Legal documents
└── README.md

🎯 Usage Guide

1. Document Upload

  1. Navigate to the Upload page
  2. Drag and drop PDF, DOCX, or TXT files
  3. Wait for processing to complete
  4. Documents are automatically indexed for search

2. Search Documents

Semantic Search:

"What are the requirements for establishing breach of contract?"

Keyword Search:

contract AND breach AND damages

Citation Search:

cite:"42 U.S.C. Β§ 1983"

3. AI Analysis

  1. Go to the Analysis page
  2. Enter your legal question
  3. Select analysis type (General, Case Law, Statutory, Precedent)
  4. Choose options (citations, counterarguments)
  5. Get comprehensive AI-powered analysis

4. Document Viewing

πŸ”Œ API Documentation

Authentication

Currently using API key authentication. Include your API key in the header:

curl -H "Authorization: Bearer YOUR_API_KEY" \
     http://localhost:8000/api/documents

Key Endpoints

Full API documentation is available at http://localhost:8000/docs when running the backend.

🐳 Docker Deployment

Using Docker Compose

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

# View logs
docker-compose logs -f

# Stop services
docker-compose down

Individual Container Build

# Backend
docker build -t legal-ai-backend ./backend

# Frontend
docker build -t legal-ai-frontend ./frontend

πŸ“Š Performance & Scalability

Database Optimization

Caching Strategy

Monitoring

πŸ§ͺ Testing

Backend Tests

cd backend
pytest tests/ -v

Frontend Tests

cd frontend
npm test

Integration Tests

# Run full test suite
npm run test:integration

🀝 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

πŸ“‹ Troubleshooting

Common Issues

β€œModule not found” errors:

# Ensure virtual environment is activated
source venv/bin/activate
pip install -r requirements.txt

Database connection errors:

# Check database configuration in .env
# Ensure PostgreSQL is running (production)

Frontend build errors:

cd frontend
rm -rf node_modules package-lock.json
npm install

Logs and Debugging

πŸ“„ License

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

πŸ™ Acknowledgments

πŸ“ž Support

For support, please:

  1. Check the documentation
  2. Search existing issues
  3. Create a new issue with detailed information
  4. Contact the development team

Built with ❀️ for the legal community