How kruddra/lawrag works
Legal Chat is a US/Canada statutes-and-regulations RAG application. Users choose a jurisdiction and law categories, submit a question, and receive a scoped answer with citations and confidence information. The main runtime path runs from the chat UI through the API, hybrid retrieval, context assembly, and response generation. A separate ingestion path obtains official-source documents, normalizes and chunks them, then indexes them; the CLI and API expose different ingestion routes. The excerpts show key calls, but not every adapter and UI component, so some documented relationships are supported by the README.
Main entry points: routes.py API routes · pipeline.py Ingestion pipeline · adapters Official-source adapters · hybrid_search.py Hybrid search
Components 27
Chat interface
- Chat interface
frontend/components/ChatInterface.tsx - Chat state
frontend/hooks/useChat.ts - API client
frontend/lib/api.ts - Message display
frontend/components/MessageBubble.tsx - Confidence badge
frontend/components/ConfidenceBadge.tsx
Legal corpus ingestion
- Jurisdiction and categories
backend/jurisdictions.py - Category vocabulary
backend/categories.py - Source registry
backend/ingestion/source_registry.py - Official-source adapters
backend/ingestion/adapters - Ingestion pipeline
backend/ingestion/pipeline.py - Parse and normalize
backend/ingestion - Legal chunking
backend/ingestion/chunking.py - Content-hash cache
backend/ingestion/ingest_cache.py - Ingestion CLI
scripts/ingest.py
Scoped retrieval
- Vector store
backend/retrieval/vector_store.py - Query enhancement
backend/retrieval/query_enhancer.py - Hybrid search
backend/retrieval/hybrid_search.py - Context and cross-references
backend/retrieval/context.py
API and answers
- API routes
backend/api/routes.py - API and document models
backend/api/models.py - Answer prompts
backend/generation/prompts.py - LLM client
backend/generation/llm_client.py - Response safety
backend/generation/safety.py - Citations and formatting
backend/generation/formatter.py
Other components
- Legal questioner
- OpenAI service
- Government law sources
Connections 35
- Legal questioner → Chat interface: asks question
README.md - Chat interface → Chat state: sends message
frontend/components/ChatInterface.tsx - Chat state → API client: calls API
frontend/hooks/useChat.ts - API client → API routes: posts chat
- API routes → Query enhancement: enhances query
backend/api/routes.py - API routes → Hybrid search: searches law
backend/api/routes.py - Hybrid search → Query enhancement: enhances query
backend/retrieval/hybrid_search.py - Hybrid search → Vector store: retrieves chunks
backend/retrieval/hybrid_search.py - API routes → Context and cross-references: builds context
backend/api/routes.py - Context and cross-references → Vector store: loads crossrefs
backend/retrieval/context.py - API routes → Answer prompts: uses prompts
backend/api/routes.py - API routes → LLM client: generates answer
backend/api/routes.py - LLM client → OpenAI service: requests completion
backend/generation/llm_client.py - API routes → Citations and formatting: formats response
backend/api/routes.py - API routes → Response safety: checks safety
backend/api/routes.py - Citations and formatting → Context and cross-references: uses citations
backend/generation/formatter.py - API routes → API and document models: returns response
backend/api/routes.py - Chat interface → Message display: renders messages
frontend/components/ChatInterface.tsx - Message display → Confidence badge: shows confidence
frontend/components/MessageBubble.tsx - Ingestion CLI → Source registry: lists sources
scripts/ingest.py - Ingestion CLI → Official-source adapters: selects adapter
scripts/ingest.py - Official-source adapters → Government law sources: downloads law
README.md - Ingestion CLI → Ingestion pipeline: runs ingestion
scripts/ingest.py - Ingestion pipeline → Official-source adapters: reads documents
backend/ingestion/pipeline.py - Ingestion pipeline → Content-hash cache: skips unchanged
backend/ingestion/pipeline.py - Ingestion pipeline → Legal chunking: chunks documents
backend/ingestion/pipeline.py - Ingestion pipeline → Vector store: upserts chunks
backend/ingestion/pipeline.py - Parse and normalize → API and document models: creates documents
backend/ingestion/pipeline.py - Official-source adapters → Jurisdiction and categories: uses jurisdiction
backend/ingestion/adapters/base.py - Official-source adapters → Category vocabulary: assigns categories
backend/ingestion/adapters/justice_laws.py - Legal chunking → API and document models: creates chunks
backend/ingestion/chunking.py - API routes → Parse and normalize: processes uploads
backend/api/routes.py - API routes → Legal chunking: chunks for reindex
backend/api/routes.py - API routes → Vector store: indexes chunks
backend/api/routes.py - Hybrid search → Jurisdiction and categories: enforces jurisdiction