How kilan7mau/chatbotlawqa works
Purpose and runtime
LawBot/JuriBot is a Python 3.10+ FastAPI backend for Vietnamese legal question answering. It exposes REST and SSE chat APIs, retrieves statutory text through RAG, and generates cited answers using Google Gemini. Primary startup is main.py; ingestion is run separately via build_vectorstore.py.
Paths: main.py, build_vectorstore.py, config.py.
API composition and application wiring
The FastAPI application delegates HTTP concerns to routers and business operations to services. Chat is the primary workload; document and user routes indicate supporting management workflows, while health endpoints expose runtime readiness. Shared components are initialized through dependency wiring and typed request/response schemas.
Paths: routers/chat.py, services/chat_service.py, dependencies.py, schemas/chat.py.
Query preprocessing and routing
A unified RAG layer normalizes Vietnamese input, incorporates conversation history to rewrite standalone questions, and classifies requests into legal retrieval (legal_rag) versus general chat. Prompt definitions separate LLM task instructions from pipeline orchestration.
Paths: rag_components.py, prompt_templete.py, utils/synonym_map.py.
Legal retrieval and relevance ranking
AdvancedLawRetriever is the legal-domain retrieval boundary. It expands/extracts keywords, applies metadata constraints, runs hybrid BM25/vector search against Weaviate, then reranks candidates with a cross-encoder. The result is curated legal context and source metadata rather than raw database output.
Paths: utils/AdvancedLawRetriever.py, services/reranker_service.py, db/weaviateDB.py.
Answer generation and streaming
The chat service drives the end-to-end request flow: preprocess → optionally retrieve/rerank → construct QA context → invoke Gemini/LangChain answer generation → return citations and processing metadata. The chat router additionally exposes SSE streaming, requiring the generation path to emit incremental response content while preserving the same retrieval boundary.
Paths: services/chat_service.py, routers/chat.py, rag_components.py.
Corpus preparation and vector-store ingestion
The offline ingestion path reads Vietnamese legal source files, identifies legal structure (such as articles and clauses), enriches chunks with document metadata, generates embeddings, and batch-loads them into the configured Weaviate collection. Processed metadata is persisted locally alongside source corpus material, enabling traceability and repeatable rebuilds.
Paths: build_vectorstore.py, utils/process_data.py, data/core, data/processed_files_metadata.
Infrastructure and external dependencies
Weaviate Cloud is the persistent vector-search system. Google Gemini provides preprocessing and answer-generation LLM calls. Hugging Face Vietnamese bi-encoder embeddings and a cross-encoder reranker are material local/model dependencies. Optional integrations include Groq, LlamaCloud document parsing, Redis, and JWT/Google-based authentication; their actual activation is environment-driven.
Paths: .env.example, config.py, db/weaviateDB.py.
Operational boundaries
Configuration and credentials are environment-based; logging is centralized. The corpus and vector index are distinct lifecycle domains: changes under data/ require the ingestion process, whereas API startup serves the existing Weaviate collection. This makes Weaviate availability and Gemini credentials critical runtime dependencies.
Paths: config.py, core/logging_config.py, .env.example.
Main entry points: main.py FastAPI startup · chat_service.py Chat orchestration · rag_components.py Query preprocessing · weaviateDB.py Weaviate access
Components 19
API Runtime
- FastAPI startup · Application entry point and router wiring.
main.py - Chat REST & SSE · Chat endpoints, including streaming responses.
routers/chat.py - Chat schemas
schemas/chat.py - Dependency wiring
dependencies.py - Chat orchestration · Runs preprocessing, retrieval, generation, citations, and streaming.
services/chat_service.py
RAG Pipeline
- Query preprocessing · Normalizes, rewrites, and routes legal versus general requests.
rag_components.py - Prompt templates
prompt_templete.py - Vietnamese synonym map
utils/synonym_map.py - AdvancedLawRetriever · Performs constrained hybrid search and returns curated legal context.
utils/AdvancedLawRetriever.py - Cross-encoder reranker
services/reranker_service.py
Offline Ingestion
- Vector-store build · Separate corpus ingestion entry point.
build_vectorstore.py - Legal corpus processor · Extracts legal structure, chunks text, and enriches metadata.
utils/process_data.py - Legal source corpus
data/core - Processed metadata · Traceability data for repeatable index rebuilds.
data/processed_files_metadata
Infrastructure
- Weaviate access
db/weaviateDB.py - Weaviate Cloud · Persistent legal vector-search collection.
- Google Gemini · Used for rewriting and cited answer generation.
- Environment configuration · Credentials and optional integrations are environment-driven.
config.py - Central logging
core/logging_config.py
Connections 21
- FastAPI startup → Chat REST & SSE: mounts
- FastAPI startup → Dependency wiring: initializes
- FastAPI startup → Environment configuration: loads
- FastAPI startup → Central logging: configures
- Chat REST & SSE → Chat schemas: validates
- Chat REST & SSE → Chat orchestration: handles requests
- Chat orchestration → Query preprocessing: preprocesses
- Chat orchestration → Prompt templates: uses
- Chat orchestration → Google Gemini: generates answers
- Query preprocessing → Vietnamese synonym map: normalizes
- Query preprocessing → Google Gemini: rewrites and classifies
- Query preprocessing → AdvancedLawRetriever: legal_rag queries
- AdvancedLawRetriever → Cross-encoder reranker: reranks candidates
- AdvancedLawRetriever → Weaviate access: hybrid search
- Weaviate access → Weaviate Cloud: connects
- Vector-store build → Legal source corpus: reads
- Vector-store build → Legal corpus processor: processes
- Legal corpus processor → Processed metadata: persists
- Vector-store build → Weaviate access: batch-loads embeddings
- Environment configuration → Weaviate access: supplies connection settings
- Environment configuration → Google Gemini: supplies credentials