How mayankjhaprojects/risk-analyzer works
Purpose and topology
AI Contract Analyzer is a three-process contract-analysis system: a React client, Express API, and FastAPI retrieval/LLM service. It accepts PDF contracts, retains metadata and conversations, indexes contract text semantically, and returns contract-grounded answers and risk findings. Main entry points are frontend/src/main.jsx, backend/index.js, and ai-engine/main.py.
Frontend client
The Vite/React SPA provides login, upload/listing dashboard, and per-contract chat/risk views. Routing and application composition live in frontend/src/App.jsx; API calls use a shared client in frontend/src/api.js, which attaches the JWT held in browser local storage. Primary workflow pages are frontend/src/pages/Dashboard.jsx and frontend/src/pages/Contract.jsx.
Backend API and identity boundary
Express is the system-of-record-facing API. backend/index.js initializes API routing and MongoDB connectivity. Authentication is handled by backend/routes/auth.js and enforced on protected requests by backend/middleware/auth.js; JWT separates each user’s contract access. MongoDB persistence is modeled by backend/models/User.js, backend/models/Contract.js, and backend/models/Chat.js.
Contract ingestion orchestration
backend/routes/contract.js receives PDF uploads, writes files beneath the runtime backend/uploads/ location, persists contract metadata, then calls the AI service’s /upload endpoint with the contract ID and file path. This creates a filesystem boundary: the AI engine must be able to access the path supplied by the backend. The backend-to-engine call is local HTTP (127.0.0.1:8000 per README), not an in-process integration.
AI indexing pipeline
FastAPI endpoints in ai-engine/main.py coordinate ingestion. PDFs are extracted by ai-engine/pdf_loader.py, segmented in ai-engine/chunker.py, and embedded through ai-engine/embedder.py. ai-engine/chroma_store.py writes chunk embeddings and contract-associated metadata to persistent ChromaDB storage at ai-engine/vector_db/, allowing retrieval after engine restarts. ai-engine/vector_store.py is a legacy FAISS-oriented helper rather than the primary persistence path.
Question answering and chat flow
The contract page sends a question and contract ID through the backend QA route, backend/routes/qa.js. The backend forwards it to AI /ask; the engine uses ai-engine/retriever.py plus ChromaDB to select relevant chunks, then ai-engine/reasoning.py builds an LLM request and produces an answer. The backend stores the resulting interaction in backend/models/Chat.js; backend/routes/chat.js exposes contract-specific history.
Risk-analysis flow and external dependencies
Risk requests enter through backend/routes/qa.js and AI /analyze-risks in ai-engine/main.py. The engine performs LLM-backed analysis via ai-engine/reasoning.py, with ai-engine/risk_rules.py providing fallback rule evaluation. Material services are MongoDB, persistent local ChromaDB, a sentence-transformers embedding model, and a GitHub-hosted or OpenAI-compatible LLM endpoint configured in ai-engine/config.py. Node.js 18+, Python 3.11+, and separate frontend/backend/AI-engine runtimes are required.
Main entry points: main.py FastAPI service · index.js Express API · contract.js Contract ingestion · qa.js QA and risk proxy
Components 17
React client
- Browser session · Stores JWT in local storage.
- React SPA · Login, dashboard, contract chat, and risk views.
frontend/src/App.jsx - API client · Attaches the browser JWT to API requests.
frontend/src/api.js
Express API
- Express API · System-of-record-facing API and MongoDB bootstrap.
backend/index.js - Auth boundary · Authenticates users and scopes protected contract access.
backend/routes/auth.js - Contract ingestion · Persists metadata, writes PDFs, and invokes engine ingestion.
backend/routes/contract.js - QA and risk proxy · Forwards questions and risk requests to the AI engine.
backend/routes/qa.js - Chat history API · Exposes contract-specific conversation history.
backend/routes/chat.js - Uploaded PDFs · AI engine must access backend-supplied file paths.
AI engine
- FastAPI service · Coordinates upload, ask, and analyze-risks endpoints.
ai-engine/main.py - PDF indexing pipeline · Extracts PDF text, segments it, and creates embeddings.
ai-engine/pdf_loader.py - ChromaDB · Persists contract chunks, embeddings, and metadata.
ai-engine/vector_db - Retrieval · Selects relevant contract chunks from ChromaDB.
ai-engine/retriever.py - LLM reasoning · Builds grounded answers and risk findings with rule fallback.
ai-engine/reasoning.py
Other components
- MongoDB · Stores users, contract metadata, and chats.
- Embedding model
- LLM endpoint · GitHub-hosted or OpenAI-compatible model endpoint.
Connections 22
- Browser session → React SPA: loads UI
- React SPA → API client: uses
- API client → Express API: authenticated HTTP
- Express API → Auth boundary: routes through
- Express API → Contract ingestion: upload requests
- Express API → QA and risk proxy: question and risk requests
- Express API → Chat history API: history requests
- Contract ingestion → MongoDB: contract metadata
- Chat history API → MongoDB: chat records
- QA and risk proxy → MongoDB: stores interactions
- Contract ingestion → Uploaded PDFs: writes PDF
- Contract ingestion → FastAPI service: local HTTP /upload
- QA and risk proxy → FastAPI service: local HTTP /ask, /analyze-risks
- FastAPI service → Uploaded PDFs: reads supplied file path
- FastAPI service → PDF indexing pipeline: ingests PDF
- PDF indexing pipeline → Embedding model: embeds chunks
- PDF indexing pipeline → ChromaDB: persists vectors
- FastAPI service → Retrieval: answers questions
- Retrieval → ChromaDB: semantic search
- Retrieval → LLM reasoning: relevant chunks
- FastAPI service → LLM reasoning: risk analysis
- LLM reasoning → LLM endpoint: LLM requests