A sophisticated multi-agent AI system for legal contract analysis and document processing. This application combines OCR technology, AI agents, and a modern web interface to provide comprehensive document analysis capabilities.
Agentic-Contract-Analysis.mp4
- Ambiguity Detector: Identifies unclear language and suggests improvements
- Framework Analyzer: Maps governing law provisions and identifies conflicts
- Summarizer: Extracts key details and provides comprehensive summaries
- Deadline & Obligation Tracker: Extracts critical dates and obligations
- Risk Assessment Agent: Analyzes potential risks and compliance issues
- OCR Support: Extract text from PDFs and images using pytesseract
- Multiple Formats: Support for PDF, PNG, JPG, JPEG, GIF, BMP, WebP
- Large File Support: Handle documents up to 10MB
- Text Storage: Persistent storage using ChromaDB for efficient retrieval
- Real-time Analysis: Ask questions about uploaded documents
- Agent-specific Actions: Trigger specialized analysis with dedicated buttons
- Chat History: Maintain conversation context across sessions
- Modern UI: Clean, responsive interface with light theme
- PDF Export: Download complete chat analysis as PDF
- Contract Rewriting: Rephrase contracts with ambiguities resolved
- Structured Output: Formatted results for easy reading
- Environment Variables: Secure API key management
- CORS Support: Cross-origin resource sharing for frontend-backend communication
- Error Handling: Comprehensive error management and user feedback
- Loading States: Visual feedback during processing
- FastAPI: Modern, fast web framework for building APIs
- CrewAI: Multi-agent orchestration framework
- OpenAI GPT-3.5-turbo: Language model for AI agents
- ChromaDB: Vector database for document storage and retrieval
- pytesseract: OCR engine for text extraction
- pdf2image: PDF to image conversion
- fpdf: PDF generation for exports
- uvicorn: ASGI server for FastAPI
- React 18: Modern React with hooks and functional components
- TypeScript: Type-safe JavaScript development
- Vite: Fast build tool and development server
- Tailwind CSS: Utility-first CSS framework
- Zustand: Lightweight state management
- Axios: HTTP client for API communication
- React Dropzone: File upload functionality
- Lucide React: Beautiful icon library
- ESLint: Code linting and formatting
- PostCSS: CSS processing
- Autoprefixer: CSS vendor prefixing
- Python 3.8 or higher
- Node.js 16 or higher
- npm or yarn
- Tesseract OCR (for text extraction)
-
Clone the repository
git clone https://github.com/yourusername/agentic-document-analysis.git cd agentic-document-analysis -
Backend Setup
# Install Python dependencies pip install -r requirements.txt # Create environment file cp .env.example .env # Edit .env with your OpenAI API key
-
Frontend Setup
cd contract-chat-ai-assist npm install -
Environment Configuration
Option A: Use the setup script (recommended)
python setup_env.py
Option B: Create manually Create a
.envfile in the project root:OPENAI_API_KEY=your_openai_api_key_here OPENAI_MODEL=gpt-4o OPENAI_API_BASE=https://api.openai.com/v1
⚠️ Important: Replaceyour_openai_api_key_herewith your actual OpenAI API key. The.envfile is already in.gitignoreto prevent accidental commits.
-
Start the Backend Server
# From project root uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000 -
Start the Frontend Development Server
# From contract-chat-ai-assist directory npm run dev -
Access the Application
- Frontend: http://localhost:5173
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
User Upload → OCR Processing → Text Extraction → ChromaDB Storage
- Users upload PDF or image files through the web interface
- Backend processes files using pytesseract for OCR
- Extracted text is stored in ChromaDB for efficient retrieval
- Document metadata is tracked for analysis
User Query → Agent Selection → CrewAI Orchestration → AI Analysis → Response
- Users can trigger specific agents or ask general questions
- CrewAI orchestrates the selected agents with specialized prompts
- Each agent has specific expertise and constraints
- Results are formatted and returned to the user
- Identifies vague or unclear language in contracts
- Suggests specific improvements and clarifications
- Provides follow-up questions for contract rewriting
- Maps legal frameworks and governing laws
- Identifies potential conflicts between jurisdictions
- Analyzes compliance requirements
- Extracts key terms and conditions
- Provides executive summaries
- Highlights important clauses and deadlines
- Identifies critical dates and deadlines
- Tracks obligations for all parties
- Provides timeline analysis
- Analyzes potential legal and compliance risks
- Identifies problematic clauses
- Provides risk mitigation suggestions
- Users can download complete chat analysis as PDF
- Contract rewriting feature creates improved versions
- All exports are properly formatted and structured
POST /upload- Upload and process documentsGET /contract-text/{document_id}- Retrieve original text
POST /chat- General chat with AI assistantPOST /analyze/{document_id}- Run specific agent analysis
POST /export-pdf/{document_id}- Generate PDF of chat analysisPOST /rewrite-contract/{document_id}- Create improved contract version
- Upload a legal contract (PDF or image)
- Use the "Summarizer" agent to get an overview
- Ask specific questions about the document
- Download the analysis as PDF
- Upload multiple related documents
- Use "Framework Analyzer" to identify legal conflicts
- Run "Risk Assessment" for compliance analysis
- Use "Ambiguity Detector" to improve contract language
- Export rewritten contract with improvements
- Ask general legal questions
- Get explanations of legal terms
- Request analysis of specific clauses
- Compare different contract provisions
legal-agentic2/
├── backend/
│ ├── main.py # FastAPI application
│ ├── ocr_module.py # OCR processing
│ ├── crew_agents.py # AI agent definitions
│ ├── qa_module.py # Agent orchestration
│ └── text_store.py # ChromaDB integration
├── contract-chat-ai-assist/
│ ├── src/
│ │ ├── components/ # React components
│ │ ├── services/ # API services
│ │ ├── store/ # State management
│ │ └── hooks/ # Custom hooks
│ └── package.json
├── requirements.txt # Python dependencies
└── README.md
- Define agent in
backend/crew_agents.py - Add task configuration
- Update
AGENT_MAPinbackend/qa_module.py - Add frontend button in
AgentActions.tsx
- Modify agent prompts in
crew_agents.py - Adjust analysis parameters in
qa_module.py - Update UI components for new features
- API Keys: Never commit
.envfiles to version control - File Uploads: Implement proper file validation and size limits
- CORS: Configure appropriate CORS settings for production
- Rate Limiting: Consider implementing rate limiting for API endpoints
- Set up production environment variables
- Configure CORS for your domain
- Set up proper logging and monitoring
- Use production-grade ASGI server (Gunicorn)
- Configure reverse proxy (Nginx)
# Example Dockerfile for backend
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY backend/ .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- CrewAI for multi-agent orchestration
- OpenAI for language model capabilities
- ChromaDB for vector storage
- FastAPI for the excellent web framework
- React and Tailwind CSS for the modern UI


