Language: English | 🇨🇳 中文
An intelligent Chinese-law question-answering system built on Retrieval-Augmented Generation (RAG). Ask a legal question in natural language and get an answer grounded in cited statutory articles.
- 🔍 Hybrid retrieval — combines dense semantic search (vector) with sparse keyword search (BM25)
- 💬 Natural-language Q&A — answers Chinese legal questions in plain language with citations
- 📚 Statute-aware chunking — segments documents by article boundaries (
第X条) for precise retrieval - 🎯 Citation traceability — every answer is tied back to specific legal articles with similarity scores
- 🔄 Query rewriting — colloquial questions are rewritten to professional legal queries before search
- 🏛 Domain filtering — narrow searches to civil, criminal, labor, administrative, etc.
- ⚡ Pluggable vector store — ChromaDB by default; FAISS supported with one config change
- 🌐 Web UI + REST API — usable as a browser app or as a programmatic endpoint
- Python 3.9+
- 4 GB+ RAM
- ~1 GB free disk space
git clone https://github.com/Zsyyxrs/legal-rag-system.git
cd legal-rag-system
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt# macOS / Linux
./scripts/start.sh
# Windows
scripts\start.bat# 1. (First run only) build the knowledge base
python backend/services/document_processor.py
python backend/services/embedding_service.py
# 2. Start the backend
python backend/app.py # http://localhost:8000
# 3. In another terminal, start the frontend
cd frontend && python -m http.server 8080 # http://localhost:8080Open http://localhost:8080 and try:
- "劳动合同试用期最长是多久?" (Maximum probation period under a labor contract?)
- "公司能否随意解除劳动合同?" (Can an employer terminate at will?)
- "试用期工资有什么规定?" (How is probation salary regulated?)
All runtime knobs live in backend/config.py:
EMBEDDING_MODEL = "BAAI/bge-base-zh-v1.5"
LLM_MODEL = "Qwen3-8B"
VECTOR_DB_TYPE = "chroma" # or "faiss"
TOP_K = 5
SIMILARITY_THRESHOLD = 0.5
HYBRID_SEARCH_ALPHA = 0.5 # 1.0 = semantic only, 0.0 = BM25 onlyIf you use a hosted LLM, set the API key:
export ANTHROPIC_API_KEY="your-api-key-here"
# Windows PowerShell: $env:ANTHROPIC_API_KEY="your-api-key-here"- Drop
.txt/.pdf/.docx/.mdfiles intodata/raw/. - Re-run document processing and embedding:
python backend/services/document_processor.py python backend/services/embedding_service.py
For best chunking results, format documents with article markers:
法律名称
第一章 章节名称
第一条 条文内容...
第二条 条文内容...
| Endpoint | Method | Purpose |
|---|---|---|
/api/query |
POST | Ask a legal question |
/api/upload |
POST | Upload and index a new document |
/api/stats |
GET | Service stats (query count, latency) |
/health |
GET | Health probe |
/docs |
GET | Auto-generated Swagger UI |
Query example:
curl -X POST "http://localhost:8000/api/query" \
-H "Content-Type: application/json" \
-d '{
"question": "劳动合同试用期最长是多久?",
"domain": "劳动法",
"top_k": 5,
"use_rewrite": true
}'import requests
r = requests.post(
"http://localhost:8000/api/query",
json={"question": "试用期最长多久?", "top_k": 5, "use_rewrite": True},
)
data = r.json()
print(data["answer"])
print(data["confidence"])
for s in data["sources"]:
print(s["metadata"]["law_name"], s["metadata"].get("article_number"))pytest tests/
# or
python tests/test_system.py# Gunicorn + Uvicorn workers
pip install gunicorn
gunicorn backend.app:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000# Minimal Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "backend/app.py"]| Symptom | Fix |
|---|---|
| "No relevant information found" | Vector DB is empty. Re-run document_processor.py then embedding_service.py. |
| Frontend can't reach backend | Check backend is up at :8000/health; verify port not blocked. |
| Out of memory | Lower CHUNK_SIZE, use a smaller embedding model, or switch VECTOR_DB_TYPE to faiss. |
| Poor Chinese tokenization | pip install jieba and consider adding a custom dictionary. |
┌─────────────────────────┐
│ Browser UI (static) │ HTML5 / CSS3 / Vanilla JS
└────────────┬────────────┘
│ HTTP + JSON
┌────────────▼────────────┐
│ FastAPI Application │ REST + Swagger + CORS
└────────────┬────────────┘
│
┌─────────┼─────────────┬───────────────┐
│ │ │ │
┌──▼───┐ ┌───▼────┐ ┌──────▼─────┐ ┌──────▼─────┐
│ Doc │ │ Embed │ │ Retrieval │ │ QA / LLM │
│ Proc │ │ Service│ │ (Hybrid) │ │ Service │
└──┬───┘ └───┬────┘ └──────┬─────┘ └──────┬─────┘
│ │ │ │
│ ┌────▼──────────────▼────┐ ┌────▼────┐
│ │ Vector DB (Chroma) │ │ LLM │
│ │ / FAISS │ │ API │
└──►│ │ └─────────┘
└────────────────────────┘
Query flow:
question → [rewrite] → [hybrid retrieve] → [build context] → [LLM] → answer + citations
Project layout:
legal-rag-system/
├── backend/ # FastAPI service
│ ├── app.py # API routes + lifecycle
│ ├── config.py # Tunables
│ ├── models/ # Pydantic schemas
│ └── services/
│ ├── document_processor.py # Load · chunk · extract metadata
│ ├── embedding_service.py # Vectorize · index
│ ├── retrieval_service.py # Vector + BM25 + rerank
│ └── qa_service.py # Prompt assembly · LLM call
├── frontend/ # Static UI (HTML/CSS/JS)
├── data/
│ ├── raw/ # Source legal documents
│ ├── processed/ # Chunk JSON (generated)
│ └── chroma_db/ # Vector index (generated)
├── scripts/ # start.sh · start.bat
├── tests/ # Pytest suite
├── pyproject.toml
├── requirements.txt
└── LICENSE
Measured on a corpus of 100 statutes (≈1,234 chunks) over 50 evaluation questions:
| Metric | Value |
|---|---|
| Average response time | 2.3 s |
| Vector retrieval latency | < 100 ms |
| BM25 retrieval latency | < 50 ms |
| Retrieval precision | 87 % |
| Statute recall | 91 % |
| Article-citation accuracy | > 90 % |
| Memory footprint | ~ 2 GB |
| Concurrent queries (single node) | 50+ |
Q: 我和公司签了三年劳动合同,试用期公司说要定6个月,这合法吗?
A: 根据《劳动合同法》第十九条,三年期及以上的劳动合同,试用期最长可约定六个月。因此该约定合法。
- 3 个月以上不满 1 年 → 试用期最长 1 个月
- 1 年以上不满 3 年 → 试用期最长 2 个月
- 3 年以上 / 无固定期限 → 试用期最长 6 个月
Sources: 中华人民共和国劳动合同法 第十九条 (similarity 0.92) · 第二十条 (0.75) Confidence: High (92%)
Pull requests and issues are welcome.
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-idea - Commit changes following Conventional Commits
- Open a pull request against
main
Report bugs or request features at https://github.com/Zsyyxrs/legal-rag-system/issues.
Released under the MIT License © 2026 Shangyi Zhu.
This system is for informational and educational purposes only. Outputs do not constitute legal advice. For decisions on specific legal matters, consult a qualified attorney.