π€ Advanced AI-powered legal analysis system for Indonesian Criminal Code (KUHP)
- Interactive Legal Consultation - Dynamic questioning system that adapts based on case complexity
- Multi-Mode Analysis - Simple chat, interactive analysis, 8-item analysis, and comprehensive flowchart modes
- Criminal Code Article Search - Intelligent search through Indonesian Penal Code articles
- Case Law Integration - Analysis incorporating relevant Indonesian case law
- Penalty Assessment - Automated calculation of potential sentences and legal consequences
An intelligent Next.js application that provides comprehensive criminal law analysis using AI and vector databases. Features interactive legal consultation, PDF document processing, and advanced case analysis frameworks.
Progressive questioning system for systematic legal analysis
Basic legal consultation with AI-powered responses
PDF upload, processing, and management interface
- Node.js 18.0 or higher
- OpenAI API Key (for GPT-4 analysis)
- Qdrant Vector Database (Docker recommended)
git clone https://github.com/yourusername/criminalcode-ai-nextjs.git
cd criminalcode-ai-nextjsnpm installCreate a .env.local file in the root directory:
# OpenAI Configuration (Required)
OPENAI_API_KEY=your_openai_api_key
# Qdrant Vector Database Configuration (Required)
QDRANT_HOST=localhost
QDRANT_PORT=6333
# For Qdrant Cloud (Production - Optional)
# QDRANT_URL=https://your-cluster.qdrant.tech:6333
# QDRANT_API_KEY=your_qdrant_cloud_api_key
# Next.js Configuration (Required)
NEXTAUTH_SECRET=your_random_secret_string_32_chars
NEXTAUTH_URL=http://localhost:3000# Vercel Blob Storage (Optional - for production file storage)
# If not provided, uses local filesystem storage
BLOB_READ_WRITE_TOKEN=your_vercel_blob_token_here
# File Upload Limits (Optional)
MAX_FILE_SIZE=52428800 # 50MB default
# Development Settings (Optional)
NODE_ENV=development# === REQUIRED CONFIGURATION ===
# OpenAI API Configuration
OPENAI_API_KEY=sk-proj-abcd1234...your-actual-openai-key
# Qdrant Vector Database (Local Development)
QDRANT_HOST=localhost
QDRANT_PORT=6333
# Next.js Security
NEXTAUTH_SECRET=super-secret-string-32-characters-long
NEXTAUTH_URL=http://localhost:3000
# === OPTIONAL CONFIGURATION ===
# Production Qdrant Cloud (uncomment for production)
# QDRANT_URL=https://your-cluster.qdrant.tech:6333
# QDRANT_API_KEY=qr_your-qdrant-cloud-api-key
# Vercel Blob Storage (uncomment for production)
# BLOB_READ_WRITE_TOKEN=vercel_blob_rw_your-token
# File Upload Settings
MAX_FILE_SIZE=52428800
# Environment
NODE_ENV=development- Create account at Qdrant Cloud
- Create a new cluster
- Get your cluster URL and API key
- Update your
.env.localwith Qdrant Cloud credentials
# Start Qdrant with Docker
docker run -p 6333:6333 qdrant/qdrant
# Or with persistent storage
docker run -p 6333:6333 -v $(pwd)/qdrant_storage:/qdrant/storage qdrant/qdrant
# Verify Qdrant is running
curl http://localhost:6333/healthCreate docker-compose.yml:
version: '3.8'
services:
qdrant:
image: qdrant/qdrant
ports:
- "6333:6333"
volumes:
- qdrant_data:/qdrant/storage
environment:
- QDRANT__SERVICE__HTTP_PORT=6333
volumes:
qdrant_data:Then run:
docker-compose up -dnpm run devNavigate to http://localhost:3000
You should see the Criminal Code AI interface ready for use! π
Progressive questioning system that guides users through systematic legal analysis:
- Stage 1: Initial assessment and potential criminal categories
- Stage 2: Targeted fact gathering with critical questions
- Stage 3: Narrowing down to specific articles with penalty assessment
- Stage 4: Final determination with complete legal recommendations
- Basic AI-powered legal consultation
- Immediate responses for quick guidance
- Enhanced with vector database context
- General legal education and information
- PDF Upload: Process criminal code documents
- Auto-Extraction: Automatically parse articles and chapters
- Text Search: Find specific articles with PDF.js integration
- Vector Storage: Store content for semantic search
- Crime Name Master: Searchable criminal offense database
- Criminal Code Articles: Full-text legal provisions
- Case Law Master: Legal precedents and court decisions
- Semantic Search: Find relevant content by meaning, not just keywords
- 8-Item Model: Structured criminal case framework
- Constituent Element Flowcharts: Interactive decision trees
- Multi-Database Search: Cross-reference multiple legal sources
- Real-time Analysis: Instant legal insights with AI
| Component | Technology | Purpose |
|---|---|---|
| Frontend | Next.js 15, React 18, Tailwind CSS | Modern, responsive UI with SSR |
| Backend | Node.js, API Routes | Server-side processing and APIs |
| AI/ML | OpenAI GPT-4, text-embedding-ada-002 | Legal analysis and semantic embeddings |
| Vector DB | Qdrant (Rust-based) | High-performance semantic search and metadata storage |
| PDF Processing | PDF.js, react-pdf, pdf-lib | Document parsing, viewing, and text extraction |
| File Storage | Hybrid (Local/Vercel Blob) | Automatic storage detection and fallback |
| UI Components | Lucide Icons, Custom Components | Beautiful, accessible interface |
| Styling | Tailwind CSS, CSS Modules | Responsive design and dark mode |
| State Management | React Hooks, Context API | Client-side state and data management |
criminalcode-ai-nextjs/
βββ src/
β βββ app/ # Next.js App Router
β β βββ api/
β β β βββ chat/ # Simple chat API endpoint
β β β βββ legal/
β β β β βββ analyze/ # Interactive analysis API
β β β β βββ article-content/ # Article content fetching API
β β β β βββ pdfs/ # PDF management API
β β β βββ upload/ # PDF upload processing API
β β β βββ init/ # Database initialization API
β β βββ globals.css # Global styles and Tailwind
β β βββ layout.js # App layout and metadata
β β βββ page.js # Main application page
β βββ components/
β β βββ ChatInterface.js # Main chat interface with condensed responses
β β βββ PDFViewer.js # In-browser PDF viewer with search
β β βββ PDFManager.js # Document management interface
β β βββ TabNavigation.js # Tab navigation component
β βββ lib/
β βββ legal/
β β βββ legalAnalyzer.js # Core AI legal analysis logic
β β βββ documentProcessor.js # PDF processing and extraction
β β βββ eightItemModel.js # 8-Item analysis framework
β β βββ constituentFlowchart.js # Decision tree logic
β βββ storage/
β β βββ fileStorage.js # Hybrid storage system (Local/Blob)
β βββ vector/
β βββ qdrant.js # Vector database operations
βββ public/
β βββ uploads/ # Local file storage directory
β βββ pdf.worker.min.js # PDF.js worker file
β βββ pdf.worker.min.mjs # PDF.js worker (ES module)
β βββ ... # Static assets
βββ docs/
β βββ images/ # Screenshots and documentation
β βββ ... # Project documentation
βββ .env.local # Environment configuration
βββ vercel.json # Vercel deployment configuration
βββ package.json # Dependencies and scripts
βββ tailwind.config.js # Tailwind CSS configuration
βββ next.config.mjs # Next.js configuration
βββ README.md # Project documentation
- Hybrid storage detection - Automatic Vercel Blob vs Local storage
- PDF processing pipeline - Upload β Parse β Vector storage
- Interactive analysis engine - Multi-stage legal consultation
- Article content retrieval - Dynamic article fetching with metadata
- Condensed response UI - Smart content parsing with expandable details
- In-browser PDF viewer - Search, navigate, and highlight articles
- Real-time file management - Upload progress, enable/disable documents
- Responsive design - Mobile-friendly interface with dark mode
- AI-powered analysis - GPT-4 integration with legal frameworks
- Vector search engine - Semantic search through legal documents
- Hybrid file storage - Automatic detection and fallback mechanisms
- Document processing - PDF parsing with article extraction
Endpoint: POST /api/legal/analyze
const response = await fetch('/api/legal/analyze', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
mode: 'interactive',
data: {
description: 'A person was found with a stolen motorcycle...'
},
conversationHistory: [
'Previous user input',
'Previous AI response'
]
})
});const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: 'What constitutes theft under criminal law?',
useAdvancedAnalysis: true
})
});Upload: POST /api/upload
List: GET /api/legal/pdfs
Delete: DELETE /api/legal/pdfs
Fetch Article: POST /api/legal/article-content
{
"articleNumber": "362",
"enabledPDFs": ["filename.pdf"]
}- Start the application following the Quick Start guide
- Upload your criminal code PDF documents via the Document Management tab
- Enable the PDFs you want to use for analysis
- Switch to the Legal Analysis tab to begin consultations
- Choose "Interactive Analysis Mode" for systematic legal consultation
- Describe your case in detail
- Answer the AI's targeted questions one by one
- Receive progressive analysis until final determination
- Choose "Simple Chat Mode" for quick consultations
- Ask general legal questions or describe cases
- Get immediate AI-powered responses
- Click article badges to view full legal texts
- Upload PDF files (max 50MB each)
- Enable/disable documents for analysis
- Delete unnecessary files
- View upload status and file information
- Click any article badge (e.g., "Article 362") in AI responses
- View detailed legal provisions and penalties
- Use "Open & Search PDF" to jump to exact article location
- Access original PDF with highlighted search terms
# === REQUIRED CONFIGURATION ===
# OpenAI API Configuration
OPENAI_API_KEY=sk-proj-your-actual-openai-api-key
# Qdrant Vector Database (Local Development)
QDRANT_HOST=localhost
QDRANT_PORT=6333
# Next.js Security Configuration
NEXTAUTH_SECRET=your-secure-random-32-character-string
NEXTAUTH_URL=http://localhost:3000
# === OPTIONAL CONFIGURATION ===
# File Upload Settings
MAX_FILE_SIZE=52428800 # 50MB default
# Development Mode
NODE_ENV=development# === REQUIRED CONFIGURATION ===
# OpenAI API Configuration
OPENAI_API_KEY=sk-proj-your-actual-openai-api-key
# Qdrant Cloud (Production)
QDRANT_URL=https://your-cluster.qdrant.tech:6333
QDRANT_API_KEY=qr_your-qdrant-cloud-api-key
# Next.js Security Configuration
NEXTAUTH_SECRET=your-secure-random-32-character-string
NEXTAUTH_URL=https://your-domain.vercel.app
# === OPTIONAL CONFIGURATION ===
# Vercel Blob Storage (for file uploads)
BLOB_READ_WRITE_TOKEN=vercel_blob_rw_your-token
# File Upload Settings
MAX_FILE_SIZE=52428800 # 50MB default
# Production Mode
NODE_ENV=productionThe system uses a hybrid storage approach:
- Files: Stored in
public/uploads/directory - Metadata: Stored in Qdrant vector database
- Auto-detection: No BLOB token = local storage
- With BLOB_READ_WRITE_TOKEN: Uses Vercel Blob Storage
- Without BLOB token: Falls back to local storage
- Metadata: Always stored in Qdrant vector database
# Basic setup
docker run -p 6333:6333 qdrant/qdrant
# With persistent storage
docker run -p 6333:6333 -v $(pwd)/qdrant_storage:/qdrant/storage qdrant/qdrantversion: '3.8'
services:
qdrant:
image: qdrant/qdrant
ports:
- "6333:6333"
volumes:
- qdrant_data:/qdrant/storage
environment:
- QDRANT__SERVICE__HTTP_PORT=6333
volumes:
qdrant_data:- Sign up at Qdrant Cloud
- Create a new cluster
- Copy the cluster URL and API key
- Update your
.env.localwith the cloud credentials
βοΈ Important Notice: This system is designed for:
- Legal education and research purposes
- Academic analysis and study
- Professional legal assistance (not replacement)
- Case preparation and preliminary analysis
This system does NOT provide legal advice. Always consult qualified legal counsel for actual legal matters.
- Ensure Qdrant is running on port 6333
- Check firewall settings
- Verify QDRANT_HOST and QDRANT_PORT in .env.local
- Verify your OPENAI_API_KEY is valid
- Check your OpenAI account has credits
- Ensure API key has GPT-4 access
- Check file size (max 50MB)
- Ensure PDF is not password protected
- Verify upload directory permissions
- Try uploading the correct criminal code document
- Check if PDF contains the article number
- Enable more PDF documents in Document Management
npx kill-port 3000
# or
lsof -ti:3000 | xargs killcurl -X DELETE http://localhost:6333/collections/criminal_code_articles
curl -X DELETE http://localhost:6333/collections/crime_name_masterWe welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch
git checkout -b feature/amazing-feature
- Make your changes
- Commit your changes
git commit -m 'Add amazing feature' - Push to the branch
git push origin feature/amazing-feature
- Open a Pull Request
- Follow existing code style
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting
This project is licensed under the MIT License - see the LICENSE file for details.
- Multi-language support (Japanese criminal code)
- Advanced UI for 8-Item Model and Flowcharts
- Case law precedent integration
- Document comparison and analysis
- Export to legal document formats
- Mobile app development
- Integration with legal practice management systems
- Batch document processing capabilities
- Documentation: Check this README and inline code comments
- Issues: Use GitHub Issues for bug reports and feature requests
- Questions: Start a GitHub Discussion for usage questions
Built with βοΈ for legal professionals, researchers, and students
Criminal Code AI - Making legal analysis accessible through artificial intelligence