MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度技术分析:Paparusi/legal-ai-agent

项目背景与痛点

在法律行业,智能化处理法律文档和案件分析需求日益增长。Paparusi/legal-ai-agent 正是响应这一需求而诞生的产品。这个开源工具致力于通过自然语言处理和机器学习技术,为企业用户提供智能的法律文档分析能力,从而减轻律师和法律顾问的工作负担,并提高工作效率。

主要面对的企业痛点包括:

  • 法律文档繁杂且不易处理。
  • 法律服务质量参差不齐,需大量手动检查。
  • 法律成本高昂,需要通过自动化手段降低人力成本。

该工具能够有效解决这些痛点,帮助企业在法律事务中更迅速准确地找到关键信息。

核心技术揭秘

Paparusi/legal-ai-agent 的实现涉及多种技术栈,包括自然语言处理算法、框架集成和数据流优化。下面从技术架构层面分析该项目的关键实现点。

graph TD subgraph "输入部分" A[用户请求] --> B{"参数校验"} end subgraph "处理部分" B -->|"通过"| C[文档预处理] C --> D[文本分析任务] D -->|"解析结果"| E[法律关键信息提取] E --> F{"处理逻辑分支"} F -->|"文本分析完成"| G[生成报告] F -->|"任务失败"| H[返回错误信息] end

此架构展示了从用户请求到最终生成法律分析报告的主要数据流。

  • 文档预处理:利用 NLP 技术清洗和格式化文本数据。
  • 文本分析任务:执行具体的文本分析流程,如实体识别和关系抽取。
  • 法律关键信息提取:通过定制化的算法抽取文档中的关键法务信息。

功能亮点与差异

相比同类产品,Paparusi/legal-ai-agent 的优势主要包括:

  • 定制化解决方案:支持针对特定领域和文档类型的自定义模型训练。
  • 模型可解释性:提供透明的预测结果解释,便于法务人员理解和验证。
  • 开发社区活跃:开源社区支持丰富,可基于社区反馈不断迭代模型和功能。

应用场景与落地建议

在生产环境中使用该工具时,需特别关注以下几点:

  • 数据隐私:确保遵守相关的数据保护法律法规,避免隐私泄露。
  • 模型准确率:虽然算法已通过验证,但仍需在实际应用中持续监控并改进。
  • 云计算资源:可能需要在较高配置的云服务器上运行,以确保性能满足应用需求。

适用于需要大批量处理法律文档的法务团队,尤其在合同审核、文件合规检查和法律研究等方面展现优势。

综合评价

Paparusi/legal-ai-agent 项目为法律行业引入了宝贵的科技应用能力。其核心算法设计巧妙地解决了复杂法律文档的理解和分析难题。然而,主要是其模型训练依赖较强,需要大量的法律文档标注数据来支撑。

优点:

  • 高效地处理文档,解放法务人员的劳动力。
  • 设计透明,模型可解释。

缺点:

  • 模型训练数据获取成本高。
  • 对运行环境配置有一定要求。

对于有充足资源的企业,这将会是个非常有价值的工具。而对于追求快速迭代和快速反馈的团队,它也体现了强大的潜力。

简要分析

热度分
399
价值分
155
活跃状态
活跃

语言占比

Dockerfile
PLpgSQL
Procfile
Python

贡献者

README

Stars License CI Python

🇻🇳 Tiếng Việt | 🇺🇸 English

Created by Lê Minh Hiếu — Trader turned Builder 🇻🇳

AI-powered legal assistant for Vietnamese businesses

An AI platform for legal research, contract review, and legal document drafting — all in a VSCode-style interface.

Stars Forks License Python FastAPI Claude


📸 Screenshots

Landing Page
Landing Page — Hero, features, pricing

Dashboard
Dashboard — VSCode-style 3-panel layout

AI Contract Review
AI Contract Review — Risk analysis, highlights, recommendations

Contract Upload
Contract Upload — Drag & drop, AI auto-analysis

✨ Features

🤖 AI Agent (24 Tools)

  • Legal search — Search across 40,000+ Vietnamese legal documents
  • Contract review — Risk analysis, missing clauses, amendment suggestions
  • Compliance check — Verify labor/commercial/service contracts against Vietnamese law
  • Clause drafting — Generate confidentiality, penalty, termination, force majeure clauses…
  • Contract summary — Quick summary of parties, value, duration
  • Contract comparison — Side-by-side diff of 2 contracts
  • Company memory — Remembers company context across chat sessions
  • 📂 Full document control — Read, write, edit, delete, organize documents autonomously

🦾 Agentic AI — Full Document Control

The AI is now “Cursor for lawyers” — full autonomous document manipulation capabilities.

Legal AI can autonomously manage your documents and contracts:

What it can do:
  • 📂 Browse and search — List all documents, search by folder/keyword/type
  • 📖 Read any document — View full content, extract specific sections
  • ✏️ Edit specific clauses — Find & replace text, track all changes
  • 📝 Generate new documents — Draft contracts, memos, reports from scratch using AI
  • 🔍 Compare documents — Side-by-side diff with similarity score
  • 📋 Review contracts for risks — Batch review multiple files at once
  • 🗂️ Organize with folders — Create folders, move files, tag documents
  • 📊 Batch operations — Review 10+ contracts simultaneously
  • 📜 Track edit history — Full audit trail of who changed what and when
  • 🗑️ Soft delete — Delete documents with 30-day recovery window
Example Commands:
"Liệt kê tất cả hợp đồng"
"Đọc hợp đồng thuê mặt bằng số 123"
"Sửa điều khoản phạt trong HĐ này cho đúng luật"
"Soạn NDA giữa công ty A và B, thời hạn 2 năm"
"So sánh bản cũ và bản mới của HĐ lao động"
"Review tất cả 5 hợp đồng trong thư mục Dự án X"
"Tạo thư mục 'Khách hàng ABC' và di chuyển 3 hợp đồng vào đó"
"Xem lịch sử chỉnh sửa của tài liệu này"
Multi-Step Autonomous Workflows:

The AI can chain multiple tools together to complete complex tasks:

User: "Sửa điều khoản phạt trong HĐ ABC cho đúng luật"
AI: 
  1. read_document → Gets current content
  2. search_law → Finds relevant penalty law (8% max per Commercial Law)
  3. edit_document → Replaces old penalty clause with compliant version
  4. document_history → Shows what changed
User: "Soạn NDA giữa công ty A và B, lưu vào thư mục Khách hàng A"
AI:
  1. generate_document → Creates NDA from requirements
  2. create_folder → Creates "Khách hàng A" folder (if doesn't exist)
  3. write_document → Saves NDA to folder
New Agentic Tools (11):
  1. list_documents — List all documents/contracts with search & filter
  2. read_document — Read full content or specific sections
  3. write_document — Create new documents with metadata & tags
  4. edit_document — Find & replace text, track changes
  5. compare_documents — Diff two documents (summary/detailed/clause-by-clause)
  6. create_folder — Create organizational folders for cases/projects
  7. move_document — Move documents between folders
  8. delete_document — Soft delete (recoverable for 30 days)
  9. generate_document — AI drafts legal documents from requirements
  10. batch_review — Review multiple documents for risks simultaneously
  11. document_history — View full edit history and audit trail

📋 Contract Review AI

Upload contracts for instant AI-powered review:

  • ⚠️ Risk identification and scoring — 10 risk categories analyzed
  • ⚖️ Vietnamese law compliance check — Civil Code, Commercial Law, Labor Law
  • 💡 Revision suggestions — Specific amendments with legal references
  • 📊 Clause-by-clause analysis — Risk levels: LOW / MEDIUM / HIGH / CRITICAL
  • 📄 8+ contract templates — Ready-to-use Vietnamese templates

Risk Categories Analyzed

  1. Điều khoản bất lợi — One-sided clauses favoring one party
  2. Phạt vi phạm cao — Excessive penalty clauses (>8% per Vietnamese law)
  3. Thời hạn bất hợp lý — Unreasonable deadlines/terms
  4. Thiếu điều khoản bảo vệ — Missing protective clauses
  5. Mâu thuẫn với luật — Clauses contradicting Vietnamese law
  6. Điều khoản tự động gia hạn — Auto-renewal traps
  7. Giới hạn trách nhiệm — Liability limitations
  8. Bảo mật và SHTT — IP/confidentiality issues
  9. Giải quyết tranh chấp — Dispute resolution (arbitration vs court)
  10. Force majeure — Missing or weak force majeure

Supported Contract Types

  • Employment (Hợp đồng lao động)
  • Lease (Hợp đồng thuê mặt bằng)
  • Sale (Hợp đồng mua bán)
  • Service (Hợp đồng dịch vụ)
  • NDA / Confidentiality (Bảo mật thông tin)
  • Loan (Hợp đồng vay)
  • Agency (Hợp đồng đại lý)
  • Business Cooperation (Hợp đồng hợp tác kinh doanh)

API Usage

Review a contract:

POST /v1/contracts/{contract_id}/review-ai

Get review results:

GET /v1/contracts/{contract_id}/review-ai

Response structure:

{
  "review_id": "review_20250319_143000",
  "contract_title": "Hợp đồng thuê mặt bằng",
  "contract_type": "lease",
  "parties": ["Công ty A", "Công ty B"],
  "risk_score": 72,
  "risk_level": "HIGH",
  "summary": "Hợp đồng có 5 điều khoản rủi ro cao...",
  "clauses": [
    {
      "clause_number": "Điều 5",
      "title": "Phạt vi phạm",
      "content": "Bên B phải trả phạt 20%...",
      "risk_level": "CRITICAL",
      "risk_score": 95,
      "issue": "Mức phạt 20% vượt quá quy định",
      "law_reference": "Điều 301 Luật TM 2005: phạt ≤ 8%",
      "suggestion": "Giảm mức phạt xuống ≤ 8%"
    }
  ],
  "missing_clauses": [
    {
      "clause": "Force Majeure",
      "importance": "HIGH",
      "suggestion": "Thêm Điều 156 BLDS 2015"
    }
  ],
  "compliance": {
    "civil_code": {"status": "PARTIAL", "issues": 2},
    "commercial_law": {"status": "VIOLATION", "issues": 1},
    "labor_law": {"status": "N/A"}
  },
  "recommendations": [
    {
      "priority": 1,
      "action": "Sửa Điều 5: giảm phạt 20% → 8%",
      "reason": "Vi phạm Điều 301 Luật TM 2005"
    }
  ]
}

📊 Dashboard & Analytics

  • Risk Dashboard — Overview of risks across all contracts
  • Contract Calendar — Monthly contract schedule
  • Usage Analytics — Usage stats, top queries
  • Audit Log — Activity journal

🎯 Enterprise Features

  • Batch upload (10 files at a time)
  • Report export (.docx)
  • Contract versioning & notes
  • Smart suggestions (AI-powered contract improvements)
  • Bulk analysis (analyze 20 contracts simultaneously)
  • Universal search (contracts + docs + laws + chats)
  • Template auto-fill
  • Onboarding wizard

🏗️ Platform Administration

Self-hosted deployments include a full Platform Super Admin panel for system administration:

Access

Navigate to /platform-admin (requires superadmin role)

Features
  • 📊 Dashboard — Real-time platform statistics, usage trends, top companies
  • 🏢 Multi-tenant Management — Manage all companies, change plans, set quotas, activate/deactivate
  • 👥 User Management — View all users across tenants, change roles, manage permissions
  • ⚙️ System Settings — Configure LLM provider, file limits, registration settings, feature flags
  • 💰 LLM Usage & Cost Tracking — Token usage by provider/company, estimated monthly costs
  • 📋 Audit Logs — Full platform-level action logging with user attribution
  • 🔧 Maintenance Tools — DB statistics, cleanup scripts, reindex operations

The Platform Admin panel provides complete control over your self-hosted Legal AI deployment:

# Navigate to platform admin
https://your-domain.com/platform-admin

# Available stats:
- Total companies, users, documents, contracts
- Vietnamese law database size (60K+ documents, 117K+ chunks)
- Daily/monthly query volumes
- Active users, storage usage
- Usage trends (30-day charts)
- Revenue estimates by plan

# Management capabilities:
- Create/edit/deactivate companies
- Change subscription plans (trial → starter → pro → enterprise)
- Adjust quota limits per company
- Reset user passwords, change roles
- View company-specific usage history
- Configure system-wide settings
- Track LLM costs per company
- Full audit trail of admin actions

📱 Modern UI

  • VSCode-style 3-panel layout
  • Dark/Light theme
  • Mobile responsive (bottom tab bar)
  • PWA installable
  • SSE streaming chat
  • Command palette (Ctrl+K)
  • Keyboard shortcuts

🕷️ Data Crawler (Powered by CrawlKit)

Legal AI Agent can automatically crawl Vietnamese legal websites to build and update your document database.

Supported Sources
  • 📚 Thư Viện Pháp Luật (thuvienphapluat.vn) — Largest Vietnamese legal document database
  • 🏛️ Văn Bản Pháp Luật Chính Phủ (vbpl.vn) — Official government legal portal
  • 📰 Công Báo (congbao.chinhphu.vn) — Official Gazette of Vietnam
  • 🌐 Any legal website URL — Custom legal document sources
Setup
  1. Get your free API key at crawlkit.org
  2. Add to .env:
    CRAWLKIT_API_KEY=your_api_key_here
    
  3. Start crawling!
Usage

Via API:

POST /crawler/crawl
{
  "url": "https://thuvienphapluat.vn/van-ban/..."
}

Via AI Chat:

"Crawl văn bản tại https://thuvienphapluat.vn/van-ban/123"

Other endpoints:

  • GET /crawler/sources — List supported legal sources
  • POST /crawler/discover — Discover legal document links from a page
  • POST /crawler/batch — Batch crawl multiple URLs
  • GET /crawler/status — Check CrawlKit configuration
Pricing
  • Free: 100 requests/day (perfect for getting started)
  • Starter: $19/mo — 10,000 requests
  • Pro: $79/mo — 100,000 requests

Get your free CrawlKit API key →


🚀 Quick Start

Prerequisites

1. Clone & Install

git clone https://github.com/Paparusi/legal-ai-agent.git
cd legal-ai-agent
pip install -r requirements.txt

2. Configure

cp .env.example .env
# Edit .env with your credentials

3. Database Setup

# Run migrations
python scripts/run_migration.py

# Load Vietnamese law data (optional, ~40K documents)
python scripts/load_law_data.py
python scripts/index_chunks.py

4. Run

uvicorn src.api.main:app --host 0.0.0.0 --port 8080

Open http://localhost:8080/static/app.html

# 1. Clone
git clone https://github.com/Paparusi/legal-ai-agent.git
cd legal-ai-agent

# 2. Configure
cp .env.example .env
nano .env  # Add your ANTHROPIC_API_KEY

# 3. Start (PostgreSQL + App)
docker compose up -d

# 4. Open
# http://localhost:8080/static/app.html

This starts PostgreSQL 15 (with pgvector) and the FastAPI app automatically.

⚠️ Note: The database schema is created automatically on first startup, but law documents are empty by default. To populate the legal database:

# Option 1: Crawl Vietnamese legal websites (recommended)
docker compose exec app python scripts/crawl_thuvien.py

# Option 2: Load from backup (if you have one)
docker compose exec app python scripts/load_law_data.py

Without legal data, the AI can still review/draft contracts, but legal search won’t work.

🖥️ Self-hosted (NAS / Xpenology / Synology)

Works on any Docker-capable device — NAS, Raspberry Pi, VPS, or local server.

# SSH into your NAS/server
git clone https://github.com/Paparusi/legal-ai-agent.git
cd legal-ai-agent
cp .env.example .env

# Edit .env — only ANTHROPIC_API_KEY is required
nano .env

# Start
docker compose up -d

# Access from any device on your network:
# http://NAS_IP:8080/static/app.html

System requirements:

  • Docker + Docker Compose
  • 512MB RAM minimum (1GB recommended)
  • 1GB disk space
  • Any CPU (x86_64 or ARM64)

Ports: 8080 (web UI), 5432 (PostgreSQL, optional external access)

Persistent data: PostgreSQL data is stored in a Docker volume (pgdata). Your data survives container restarts and updates.

Update to latest version:

git pull
docker compose build
docker compose up -d

📁 Project Structure

├── src/
│   ├── api/
│   │   ├── main.py              # FastAPI app + all routes
│   │   ├── routes/              # Route modules
│   │   │   ├── auth.py          # Login, register, API keys
│   │   │   ├── contracts.py     # Contract CRUD
│   │   │   ├── documents.py     # Document upload
│   │   │   ├── chats.py         # Chat history
│   │   │   ├── company.py       # Company management
│   │   │   └── admin.py         # Admin dashboard
│   │   └── middleware/
│   │       ├── auth.py          # API key verification
│   │       └── logging.py       # Usage logging
│   └── agents/
│       ├── legal_agent.py       # AI agent with 11 tools
│       └── company_memory.py    # Company context memory
├── static/
│   ├── app.html                 # Main SPA (~5600 lines)
│   ├── index.html               # Landing page
│   ├── admin.html               # Admin dashboard
│   └── manifest.json            # PWA manifest
├── scripts/
│   ├── load_law_data.py         # Import law documents
│   ├── index_chunks.py          # Chunk & index for search
│   └── run_migration.py         # DB migrations
├── docker-compose.yml           # One-command deploy
├── Dockerfile                   # Container build
├── .env.example                 # Environment template
├── requirements.txt
└── Procfile                     # Railway/Heroku deploy

🔧 API Endpoints

Auth

Method Endpoint Description
POST /v1/auth/register Register
POST /v1/auth/login Login
POST /v1/auth/api-key Generate API key

AI Chat

Method Endpoint Description
POST /v1/legal/ask Ask AI agent
POST /v1/legal/ask-stream Ask AI (SSE streaming)

Contracts

Method Endpoint Description
GET /v1/contracts List contracts
POST /v1/contracts/upload Upload contract
POST /v1/contracts/batch-upload Batch upload contracts
POST /v1/contracts/{id}/review AI contract review
POST /v1/contracts/{id}/report Export Word report
POST /v1/contracts/{id}/diff Compare 2 contracts
GET /v1/contracts/{id}/suggestions AI suggestions
POST /v1/contracts/bulk-analyze Bulk analysis
GET /v1/contracts/calendar Contract calendar
GET /v1/contracts/risk-overview Risk overview
Method Endpoint Description
GET /v1/legal/search Search laws
GET /v1/search/all Search everything

Analytics

Method Endpoint Description
GET /v1/analytics Usage statistics
GET /v1/audit-log Activity log
GET /v1/insights AI insights

🤖 Multi-LLM Provider Support

Bring Your Own LLM — Connect your preferred AI provider with API Key or OAuth:

Supported Providers

Provider Models Auth Methods Context
🔵 Anthropic Claude Sonnet 4, Opus 4, Haiku 3.5 API Key 200K tokens
🟢 OpenAI GPT GPT-4o, GPT-4o Mini, O1 API Key, OAuth 128-200K tokens
🔴 Google Gemini Gemini 2.5 Pro/Flash, 2.0 Flash API Key, OAuth 1M tokens
⚫ Custom/Local Ollama, vLLM, LM Studio API Key Variable

Configuration

  1. Via Dashboard: Settings → AI Provider → Choose provider → Enter API key
  2. Via API: POST /v1/llm/configure with your API key
  3. OAuth (OpenAI/Gemini): Click “Connect with [Provider]” → Authorize → Done

API Endpoints

Method Endpoint Description
GET /v1/llm/providers List all providers + models
POST /v1/llm/configure Set API key (manual)
POST /v1/llm/test Test current connection
GET /v1/llm/status Current provider status
GET /v1/llm/models List models
POST /v1/llm/model Set model
GET /v1/llm/oauth/{provider}/authorize Start OAuth flow
GET /v1/llm/oauth/callback OAuth callback
POST /v1/llm/oauth/{provider}/refresh Refresh OAuth token

Features

  • ✅ Unified Interface: Agent works with any LLM — no code changes
  • 🔒 Encrypted Storage: API keys encrypted with Fernet (AES-256)
  • 🔄 OAuth Support: Automated token management for OpenAI & Google
  • 🛠️ Tool Normalization: Function calling formats normalized across providers
  • 💾 Company-Level Config: Each company can use different LLM
  • 🔁 Fallback: Defaults to ANTHROPIC_API_KEY env var if not configured

Environment Variables

# Encryption key for API keys (generate with: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())")
LLM_ENCRYPTION_KEY=your-32-byte-key

# OAuth credentials (optional, for OAuth flow)
OPENAI_CLIENT_ID=your-openai-client-id
OPENAI_CLIENT_SECRET=your-openai-client-secret
GEMINI_CLIENT_ID=your-google-client-id
GEMINI_CLIENT_SECRET=your-google-client-secret
OAUTH_REDIRECT_URI=http://localhost:8080/v1/llm/oauth/callback

# Default fallback (if no provider configured)
ANTHROPIC_API_KEY=your-anthropic-key

💰 Pricing

Tier USD VND Queries/day Contracts Key Features
Free $0 0₫ 10 1 Basic search, 2 templates
Starter $29/mo 725K₫ 100 20 AI review, all templates
Professional $99/mo 2.5M₫ 500 Unlimited API, custom LLM, analytics
Enterprise $499/mo 12.5M₫ Unlimited Unlimited SLA 99.9%, dedicated support

Discounts: Annual -20% · Startups -30% · NGOs -50%

GET /v1/pricing  # Get pricing tiers

📄 Contract Templates

8 ready-to-use Vietnamese contract templates:

Template Law Reference
Hợp đồng lao động BLLĐ 2019
Hợp đồng thuê mặt bằng BLDS 2015
Hợp đồng mua bán Luật TM 2005
Hợp đồng dịch vụ BLDS 2015
Hợp tác kinh doanh (BCC) Luật ĐT 2020
NDA / Bảo mật Luật SHTT 2005
Hợp đồng vay BLDS 2015
Hợp đồng đại lý Luật TM 2005

All templates include {{fillable_fields}}, legal notes, and specific law article references.

GET /v1/templates              # List all templates
GET /v1/templates/{id}         # Get template content
POST /v1/templates/generate    # AI-fill template

🌐 Multi-Language (i18n)

Support Vietnamese and English:

# Vietnamese (default)
curl -H "Accept-Language: vi" /v1/pricing

# English
curl -H "Accept-Language: en" /v1/pricing

🛠️ Tech Stack

  • Backend: FastAPI + Python
  • AI: Multi-LLM (Claude, GPT, Gemini, Custom) via unified provider interface
  • Database: PostgreSQL (Supabase) with pgvector
  • Search: Full-text search + synonym expansion + TF-IDF ranking
  • Frontend: Vanilla JS SPA (single HTML file)
  • Deploy: Railway / Docker / any container host

📝 Vietnamese Law Database

The search engine indexes Vietnamese legal documents including:

  • Labor Code 2019 (Bộ luật Lao động)
  • Civil Code 2015 (Bộ luật Dân sự)
  • Enterprise Law 2020 (Luật Doanh nghiệp)
  • Commercial Law 2005 (Luật Thương mại)
  • Corporate Income Tax, Personal Income Tax, VAT Laws
  • And 40,000+ more…

🤝 Contributing

See CONTRIBUTING.md for guidelines. Areas that need help:

  • More Vietnamese legal document sources
  • Better NLP for Vietnamese text
  • Test coverage
  • Multi-language support

📄 License

MIT — free to use, including commercially.

⚠️ Disclaimer

This is an assistive tool and does not replace professional legal advice. Always consult a qualified lawyer for important legal decisions.


💖 Sponsors

Love this project? Become a sponsor! 🙏

Your support helps maintain and expand this open-source Vietnamese legal AI platform. By sponsoring, you’re supporting:

  • 🇻🇳 Vietnamese open-source development
  • ⚖️ Democratized legal tech for small businesses
  • 📚 Free legal AI tools for everyone
  • 🚀 New features and improvements
Tier Monthly Benefits
☕ Coffee $5 Your name in sponsors list
🥉 Bronze $25 Logo in README + priority issue response (24h)
🥈 Silver $100 Direct support channel + feature request priority
🥇 Gold $500 Prominent logo + custom features + quarterly calls
🏢 Enterprise Custom SLA, white-label, custom development, private hosting

👉 View all tiers & sponsor now

For Enterprise inquiries: GitHub Issues or @gau_trader on Telegram

🌟 Current Sponsors

No sponsors yet — be the first! Your logo could be here. 🚀

See .github/SPONSORS.md for full details.


Made with ❤️ by Lê Minh Hiếu 🇻🇳

评论

暂无评论