# kilan7mau/chatbotlawqa architecture

> Architecture diagram of the GitHub repository kilan7mau/chatbotlawqa, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kilan7mau/chatbotlawqa
- Repository: https://github.com/kilan7mau/chatbotlawqa

## Overview

#### 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`](https://github.com/kilan7mau/chatbotlawqa/blob/master/main.py) (FastAPI startup), [`services/chat_service.py`](https://github.com/kilan7mau/chatbotlawqa/blob/master/services/chat_service.py) (Chat orchestration), [`rag_components.py`](https://github.com/kilan7mau/chatbotlawqa/blob/master/rag_components.py) (Query preprocessing), [`db/weaviateDB.py`](https://github.com/kilan7mau/chatbotlawqa/blob/master/db/weaviateDB.py) (Weaviate access)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API Runtime"]
  node_main["FastAPI startup<br/>[main.py]"]
  node_chat_router["Chat REST &amp; SSE<br/>router<br/>[chat.py]"]
  node_chat_schemas["Chat schemas<br/>[chat.py]"]
  node_dependencies["Dependency wiring<br/>dependencies<br/>[dependencies.py]"]
  node_chat_service["Chat orchestration<br/>[chat_service.py]"]
end

subgraph group_rag["RAG Pipeline"]
  node_rag_components["Query preprocessing<br/>rag layer<br/>[rag_components.py]"]
  node_prompts["Prompt templates<br/>prompts<br/>[prompt_templete.py]"]
  node_synonyms["Vietnamese synonym map<br/>query expansion<br/>[synonym_map.py]"]
  node_retriever["AdvancedLawRetriever<br/>retrieval boundary"]
  node_reranker["Cross-encoder reranker<br/>ranking service"]
end

subgraph group_ingestion["Offline Ingestion"]
  node_ingestion_job["Vector-store build<br/>batch job"]
  node_corpus_processor["Legal corpus processor<br/>processing<br/>[process_data.py]"]
  node_legal_corpus["Legal source corpus<br/>source data"]
  node_processed_metadata[("Processed metadata<br/>local metadata")]
end

subgraph group_infra["Infrastructure"]
  node_weaviate_client["Weaviate access<br/>database adapter<br/>[weaviateDB.py]"]
  node_weaviate[("Weaviate Cloud<br/>vector database")]
  node_gemini{{"Google Gemini<br/>LLM"}}
  node_config["Environment configuration<br/>[config.py]"]
  node_logging["Central logging<br/>[logging_config.py]"]
end

node_main -->|"mounts"| node_chat_router
node_main -->|"initializes"| node_dependencies
node_main -->|"loads"| node_config
node_main -->|"configures"| node_logging
node_chat_router -->|"validates"| node_chat_schemas
node_chat_router -->|"handles requests"| node_chat_service
node_chat_service -->|"preprocesses"| node_rag_components
node_chat_service -->|"uses"| node_prompts
node_chat_service -->|"generates answers"| node_gemini
node_rag_components -->|"normalizes"| node_synonyms
node_rag_components -->|"rewrites and classifies"| node_gemini
node_rag_components -->|"legal_rag queries"| node_retriever
node_retriever -->|"reranks candidates"| node_reranker
node_retriever -->|"hybrid search"| node_weaviate_client
node_weaviate_client -->|"connects"| node_weaviate
node_ingestion_job -->|"reads"| node_legal_corpus
node_ingestion_job -->|"processes"| node_corpus_processor
node_corpus_processor -->|"persists"| node_processed_metadata
node_ingestion_job -->|"batch-loads embeddings"| node_weaviate_client
node_config -.->|"supplies connection settings"| node_weaviate_client
node_config -.->|"supplies credentials"| node_gemini

click node_main "https://github.com/kilan7mau/chatbotlawqa/blob/master/main.py"
click node_chat_router "https://github.com/kilan7mau/chatbotlawqa/blob/master/routers/chat.py"
click node_chat_schemas "https://github.com/kilan7mau/chatbotlawqa/blob/master/schemas/chat.py"
click node_dependencies "https://github.com/kilan7mau/chatbotlawqa/blob/master/dependencies.py"
click node_chat_service "https://github.com/kilan7mau/chatbotlawqa/blob/master/services/chat_service.py"
click node_rag_components "https://github.com/kilan7mau/chatbotlawqa/blob/master/rag_components.py"
click node_prompts "https://github.com/kilan7mau/chatbotlawqa/blob/master/prompt_templete.py"
click node_synonyms "https://github.com/kilan7mau/chatbotlawqa/blob/master/utils/synonym_map.py"
click node_retriever "https://github.com/kilan7mau/chatbotlawqa/blob/master/utils/AdvancedLawRetriever.py"
click node_reranker "https://github.com/kilan7mau/chatbotlawqa/blob/master/services/reranker_service.py"
click node_ingestion_job "https://github.com/kilan7mau/chatbotlawqa/blob/master/build_vectorstore.py"
click node_corpus_processor "https://github.com/kilan7mau/chatbotlawqa/blob/master/utils/process_data.py"
click node_legal_corpus "https://github.com/kilan7mau/chatbotlawqa/tree/master/data/core"
click node_processed_metadata "https://github.com/kilan7mau/chatbotlawqa/tree/master/data/processed_files_metadata"
click node_weaviate_client "https://github.com/kilan7mau/chatbotlawqa/blob/master/db/weaviateDB.py"
click node_config "https://github.com/kilan7mau/chatbotlawqa/blob/master/config.py"
click node_logging "https://github.com/kilan7mau/chatbotlawqa/blob/master/core/logging_config.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_chat_router,node_chat_schemas,node_dependencies,node_chat_service toneBlue
class node_rag_components,node_prompts,node_synonyms,node_retriever,node_reranker toneAmber
class node_ingestion_job,node_corpus_processor,node_legal_corpus,node_processed_metadata toneMint
class node_weaviate_client,node_weaviate,node_gemini,node_config,node_logging toneRose
```

## Components

### API Runtime

- [FastAPI startup](https://github.com/kilan7mau/chatbotlawqa/blob/master/main.py): application · Application entry point and router wiring. · `main.py`
- [Chat REST & SSE](https://github.com/kilan7mau/chatbotlawqa/blob/master/routers/chat.py): router · Chat endpoints, including streaming responses. · `routers/chat.py`
- [Chat schemas](https://github.com/kilan7mau/chatbotlawqa/blob/master/schemas/chat.py): schemas · `schemas/chat.py`
- [Dependency wiring](https://github.com/kilan7mau/chatbotlawqa/blob/master/dependencies.py): dependencies · `dependencies.py`
- [Chat orchestration](https://github.com/kilan7mau/chatbotlawqa/blob/master/services/chat_service.py): service · Runs preprocessing, retrieval, generation, citations, and streaming. · `services/chat_service.py`

### RAG Pipeline

- [Query preprocessing](https://github.com/kilan7mau/chatbotlawqa/blob/master/rag_components.py): rag layer · Normalizes, rewrites, and routes legal versus general requests. · `rag_components.py`
- [Prompt templates](https://github.com/kilan7mau/chatbotlawqa/blob/master/prompt_templete.py): prompts · `prompt_templete.py`
- [Vietnamese synonym map](https://github.com/kilan7mau/chatbotlawqa/blob/master/utils/synonym_map.py): query expansion · `utils/synonym_map.py`
- [AdvancedLawRetriever](https://github.com/kilan7mau/chatbotlawqa/blob/master/utils/AdvancedLawRetriever.py): retrieval boundary · Performs constrained hybrid search and returns curated legal context. · `utils/AdvancedLawRetriever.py`
- [Cross-encoder reranker](https://github.com/kilan7mau/chatbotlawqa/blob/master/services/reranker_service.py): ranking service · `services/reranker_service.py`

### Offline Ingestion

- [Vector-store build](https://github.com/kilan7mau/chatbotlawqa/blob/master/build_vectorstore.py): batch job · Separate corpus ingestion entry point. · `build_vectorstore.py`
- [Legal corpus processor](https://github.com/kilan7mau/chatbotlawqa/blob/master/utils/process_data.py): processing · Extracts legal structure, chunks text, and enriches metadata. · `utils/process_data.py`
- [Legal source corpus](https://github.com/kilan7mau/chatbotlawqa/tree/master/data/core): source data · `data/core`
- [Processed metadata](https://github.com/kilan7mau/chatbotlawqa/tree/master/data/processed_files_metadata): local metadata · Traceability data for repeatable index rebuilds. · `data/processed_files_metadata`

### Infrastructure

- [Weaviate access](https://github.com/kilan7mau/chatbotlawqa/blob/master/db/weaviateDB.py): database adapter · `db/weaviateDB.py`
- **Weaviate Cloud**: vector database · Persistent legal vector-search collection.
- **Google Gemini**: LLM · Used for rewriting and cited answer generation.
- [Environment configuration](https://github.com/kilan7mau/chatbotlawqa/blob/master/config.py): configuration · Credentials and optional integrations are environment-driven. · `config.py`
- [Central logging](https://github.com/kilan7mau/chatbotlawqa/blob/master/core/logging_config.py): logging · `core/logging_config.py`

## Connections

- 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
