How bhavjeetsingh/contractiq works
1. System shape
ContractIQ is a FastAPI-based document intelligence service with a thin HTML frontend. The browser-facing layer lives in templates/ and static/, while almost all business logic sits under src/. The app is centered on contract PDFs and exposes analysis, comparison, and chat-style Q&A workflows.
2. Request entry point
api/main.py is the main orchestration layer. It wires routes, file upload handling, template rendering, and health checks. From an architecture standpoint, this is the boundary between HTTP requests and the internal document-processing pipelines.
3. Document ingestion boundary
src/document_ingestion/ handles the first stage of every document workflow: saving uploaded files, reading PDF content page by page, and preparing content for downstream processing. This is the shared entry path for analysis, comparison, and chat indexing, so it acts as the normalization layer for all document inputs.
4. Analysis pipeline
src/document_analyzer/data_analysis.py is responsible for extracting structured contract intelligence such as metadata, summary, sentiment/tone, and obligations. It uses LLM prompting plus structured output models from model/models.py, so the output is not just free text but schema-shaped data suitable for API responses.
5. Comparison pipeline
src/document_compare/document_comparator.py compares two document versions, likely page-by-page or section-by-section, and asks the model to identify differences and meaningful changes. This is a separate pipeline from analysis because it consumes two inputs and produces a delta-oriented result rather than a single-document summary.
6. Conversational RAG subsystem
src/document_chat/ implements the multi-document Q&A flow. retrieval.py and hybrid_retrieval.py provide retrieval logic, while sse_streaming.py suggests optional streaming response support. The README indicates a session-scoped FAISS index, so chat is built around document indexing per session followed by retrieval-augmented generation on query.
7. LLM and prompt layer
The model-agnostic orchestration is concentrated in prompt/prompt_library.py, utils/model_loader.py, and config/config.yaml. This layer configures the actual model providers described in the README: Groq-hosted LLMs for generation and Google Gemini embeddings for vectorization. LangChain is the integration framework connecting prompts, parsers, and model calls.
8. Structured outputs and domain schemas
model/models.py defines the Pydantic schemas that constrain responses from analysis and related workflows. This is an important architectural boundary: unstructured LLM output is forced into typed application models before it reaches API consumers.
9. Cross-cutting platform services
Logging, exceptions, and rate control are implemented as shared infrastructure rather than feature code. logger/custom_logger.py provides structured JSON logs, exception/custom_exception.py captures rich traceback context, and utils/rate_limiter.py suggests request throttling support. These apply across routes rather than inside one business feature.
10. Session, auth, and caching hooks
src/auth/jwt_handler.py and src/cache/redis_cache.py indicate planned or partial supporting services for identity and response caching. Even if not central today, they define architectural seams for future user isolation, protected routes, and reduced LLM latency.
11. Evaluation and tracing
src/eval/ragas_evaluator.py, run_eval.py, and utils/langsmith_tracer.py form the quality/observability lane. This is separate from production request handling and is used to score or inspect retrieval-generation quality, which matters for a contract-analysis product where answer fidelity is critical.
12. Deployment and runtime
Production deployment is containerized with Docker and described in infrastructure/document-portal-cf.yaml. The repo also includes AWS ECS Fargate, ECR, Secrets Manager, and CloudWatch integration, so the runtime boundary is a managed container service with externalized secret management and centralized logs. GitHub Actions in .github/workflows/ci.yaml handles CI/CD.
13. Frontend and UX boundary
The HTML templates (templates/index.html, templates/login.html) and static/style.css suggest a server-rendered UI rather than a separate SPA. The frontend’s role is primarily to submit PDFs, render results, and drive chat/comparison workflows against the FastAPI backend.
14. Testing and validation
tests/test_routes.py and tests/test_unit_cases.py verify both API behavior and core utility logic. This shows the repo treats route correctness and internal processing as separate test surfaces, which is useful when diagramming trust boundaries around HTTP input and document-processing internals.
Main entry points: main.py API app · retrieval.py Chat RAG · data_analysis.py Analysis · data_ingestion.py Ingestion
Components 22
HTTP App
FastAPI entrypoint, routes, and browser-facing templates/static assets.
- API app · Wires HTTP routes, uploads, templates, and health checks.
api/main.py - UI templates · Thin HTML frontend for landing, login, and workflow pages.
templates/index.html - Styles · Static browser assets for the rendered UI.
static/style.css
Core Pipelines
Document ingestion and the main contract intelligence workflows.
- Ingestion · Saves uploads and reads PDF content into normalized document data.
src/document_ingestion/data_ingestion.py - Analysis · Extracts metadata, summary, tone, and obligations from a single contract.
src/document_analyzer/data_analysis.py - Compare docs · Compares two document versions and identifies meaningful changes.
src/document_compare/document_comparator.py - Chat RAG · Coordinates session-scoped retrieval and answer generation over documents.
src/document_chat/retrieval.py - Hybrid search · Combines retrieval strategies for better context selection.
src/document_chat/hybrid_retrieval.py - SSE stream · Streams chat or generation responses to the client.
src/document_chat/sse_streaming.py
LLM Layer
Prompting, model loading, and structured output schemas.
- Prompt library · Centralizes model prompts used across analysis and chat workflows.
prompt/prompt_library.py - Model loader · Initializes configured LLM and embedding providers from app settings.
utils/model_loader.py - Schemas · Defines typed outputs for analysis and other structured responses.
model/models.py - Config · Holds model and application configuration values.
config/config.yaml
Platform Services
Shared infrastructure, auth, cache, logging, and rate control.
- Logging · Shared JSON logging for application events and errors.
logger/custom_logger.py - Exceptions · Wraps failures with rich traceback and application context.
exception/custom_exception.py - JWT auth · Handles token-based identity and route protection hooks.
src/auth/jwt_handler.py - Redis cache · Provides shared caching for responses or session data.
src/cache/redis_cache.py - Rate limit · Applies request throttling across API surfaces.
utils/rate_limiter.py
Ops & Quality
Evaluation, tracing, deployment, and CI/CD.
- RAG eval · Scores retrieval and generation quality for experiments and regression checks.
src/eval/ragas_evaluator.py - Tracing · Connects runs to LangSmith-style tracing and inspection.
utils/langsmith_tracer.py - Deploy · Container and cloud deployment definitions for runtime execution.
infrastructure/document-portal-cf.yaml - CI/CD · Continuous integration workflow for validation and delivery.
.github/workflows/ci.yaml
Connections 26
- API app → Ingestion: uploads · HTTP file uploads enter the ingestion boundary.
- API app → Analysis: analyzes · Routes invoke the single-document analysis pipeline.
- API app → Compare docs: compares · Routes trigger document-to-document comparison.
- API app → Chat RAG: serves chat · API exposes conversational document Q&A.
- API app → UI templates: renders · Server-side HTML is rendered from FastAPI handlers.
- UI templates → Styles: styles · Templates are paired with static browser assets.
- Ingestion → Analysis: feeds · Normalized documents are passed into analysis.
- Ingestion → Compare docs: feeds · Both versions are prepared by ingestion before diffing.
- Ingestion → Chat RAG: indexes · Ingested content is indexed for retrieval and Q&A.
- Chat RAG → Hybrid search: retrieves · Chat uses retrieval logic to select relevant context.
- Chat RAG → SSE stream: streams · Chat responses can be emitted incrementally.
- Analysis → Prompt library: uses · Analysis relies on prompt templates and instructions.
- Analysis → Schemas: returns · Model output is constrained to structured application schemas.
- Compare docs → Prompt library: uses · Comparison prompts the model to identify deltas.
- Chat RAG → Prompt library: uses · RAG answers are generated with shared prompt assets.
- Prompt library → Model loader: executes on · Prompts are run through configured model providers.
- Model loader → Config: reads · Provider and model settings come from configuration.
- Analysis → Logging: logs · Analysis pipeline emits structured logs.
- API app → Exceptions: handles errors · Request failures are wrapped in application exceptions.
- API app → JWT auth: protects · JWT hooks can guard authenticated workflows.
- API app → Redis cache: caches · Shared cache support sits behind request workflows.
- API app → Rate limit: throttles · API traffic can be rate limited at the boundary.
- RAG eval → Chat RAG: evaluates · Quality tooling measures retrieval-generation behavior.
- Tracing → Chat RAG: observes · Tracing inspects chat and model execution.
- Deploy → API app: runs · Deployment targets the FastAPI application runtime.
- CI/CD → Deploy: delivers · CI/CD supports build and deployment workflows.