# mayankjhaprojects/risk-analyzer architecture

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

- Interactive diagram: https://gitdiagram.com/mayankjhaprojects/risk-analyzer
- Repository: https://github.com/mayankjhaprojects/risk-analyzer

## Overview

#### 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: [`ai-engine/main.py`](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/main.py) (FastAPI service), [`backend/index.js`](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/index.js) (Express API), [`backend/routes/contract.js`](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/contract.js) (Contract ingestion), [`backend/routes/qa.js`](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/qa.js) (QA and risk proxy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React client"]
  node_browser(("Browser session<br/>user client"))
  node_react_app["React SPA<br/>Vite React<br/>[App.jsx]"]
  node_api_client["API client<br/>JWT HTTP client<br/>[api.js]"]
end

subgraph group_api["Express API"]
  node_express["Express API<br/>Node.js service<br/>[index.js]"]
  node_auth["Auth boundary<br/>JWT routes and middleware<br/>[auth.js]"]
  node_contracts["Contract ingestion<br/>upload route<br/>[contract.js]"]
  node_qa["QA and risk proxy<br/>AI route<br/>[qa.js]"]
  node_chat_history["Chat history API<br/>chat route<br/>[chat.js]"]
  node_uploads["Uploaded PDFs<br/>shared runtime filesystem"]
end

subgraph group_engine["AI engine"]
  node_fastapi["FastAPI service<br/>Python service<br/>[main.py]"]
  node_pdf_pipeline["PDF indexing pipeline<br/>extract, chunk, embed<br/>[pdf_loader.py]"]
  node_chroma[("ChromaDB<br/>persistent vector store")]
  node_retrieval["Retrieval<br/>[retriever.py]"]
  node_reasoning["LLM reasoning<br/>answer and risk analysis<br/>[reasoning.py]"]
end

node_mongodb[("MongoDB<br/>system-of-record database")]
node_embedding_model{{"Embedding model<br/>sentence-transformers"}}
node_llm{{"LLM endpoint<br/>external compatible API"}}

node_browser -->|"loads UI"| node_react_app
node_react_app -->|"uses"| node_api_client
node_api_client -->|"authenticated HTTP"| node_express
node_express -->|"routes through"| node_auth
node_express -->|"upload requests"| node_contracts
node_express -->|"question and risk requests"| node_qa
node_express -->|"history requests"| node_chat_history
node_contracts -->|"contract metadata"| node_mongodb
node_chat_history -->|"chat records"| node_mongodb
node_qa -->|"stores interactions"| node_mongodb
node_contracts -->|"writes PDF"| node_uploads
node_contracts -->|"local HTTP /upload"| node_fastapi
node_qa -->|"local HTTP /ask, /analyze-risks"| node_fastapi
node_fastapi -->|"reads supplied file path"| node_uploads
node_fastapi -->|"ingests PDF"| node_pdf_pipeline
node_pdf_pipeline -->|"embeds chunks"| node_embedding_model
node_pdf_pipeline -->|"persists vectors"| node_chroma
node_fastapi -->|"answers questions"| node_retrieval
node_retrieval -->|"semantic search"| node_chroma
node_retrieval -->|"relevant chunks"| node_reasoning
node_fastapi -->|"risk analysis"| node_reasoning
node_reasoning -->|"LLM requests"| node_llm

click node_react_app "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/frontend/src/App.jsx"
click node_api_client "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/frontend/src/api.js"
click node_express "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/index.js"
click node_auth "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/auth.js"
click node_contracts "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/contract.js"
click node_qa "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/qa.js"
click node_chat_history "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/chat.js"
click node_fastapi "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/main.py"
click node_pdf_pipeline "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/pdf_loader.py"
click node_chroma "https://github.com/mayankjhaprojects/risk-analyzer/tree/main/ai-engine/vector_db"
click node_retrieval "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/retriever.py"
click node_reasoning "https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/reasoning.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_browser,node_react_app,node_api_client toneBlue
class node_express,node_auth,node_contracts,node_qa,node_chat_history,node_uploads toneAmber
class node_fastapi,node_pdf_pipeline,node_chroma,node_retrieval,node_reasoning toneMint
class node_mongodb,node_embedding_model,node_llm toneNeutral
```

## Components

### React client

- **Browser session**: user client · Stores JWT in local storage.
- [React SPA](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/frontend/src/App.jsx): Vite React · Login, dashboard, contract chat, and risk views. · `frontend/src/App.jsx`
- [API client](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/frontend/src/api.js): JWT HTTP client · Attaches the browser JWT to API requests. · `frontend/src/api.js`

### Express API

- [Express API](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/index.js): Node.js service · System-of-record-facing API and MongoDB bootstrap. · `backend/index.js`
- [Auth boundary](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/auth.js): JWT routes and middleware · Authenticates users and scopes protected contract access. · `backend/routes/auth.js`
- [Contract ingestion](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/contract.js): upload route · Persists metadata, writes PDFs, and invokes engine ingestion. · `backend/routes/contract.js`
- [QA and risk proxy](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/qa.js): AI route · Forwards questions and risk requests to the AI engine. · `backend/routes/qa.js`
- [Chat history API](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/backend/routes/chat.js): chat route · Exposes contract-specific conversation history. · `backend/routes/chat.js`
- **Uploaded PDFs**: shared runtime filesystem · AI engine must access backend-supplied file paths.

### AI engine

- [FastAPI service](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/main.py): Python service · Coordinates upload, ask, and analyze-risks endpoints. · `ai-engine/main.py`
- [PDF indexing pipeline](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/pdf_loader.py): extract, chunk, embed · Extracts PDF text, segments it, and creates embeddings. · `ai-engine/pdf_loader.py`
- [ChromaDB](https://github.com/mayankjhaprojects/risk-analyzer/tree/main/ai-engine/vector_db): persistent vector store · Persists contract chunks, embeddings, and metadata. · `ai-engine/vector_db`
- [Retrieval](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/retriever.py): semantic chunk retrieval · Selects relevant contract chunks from ChromaDB. · `ai-engine/retriever.py`
- [LLM reasoning](https://github.com/mayankjhaprojects/risk-analyzer/blob/main/ai-engine/reasoning.py): answer and risk analysis · Builds grounded answers and risk findings with rule fallback. · `ai-engine/reasoning.py`

### Other components

- **MongoDB**: system-of-record database · Stores users, contract metadata, and chats.
- **Embedding model**: sentence-transformers
- **LLM endpoint**: external compatible API · GitHub-hosted or OpenAI-compatible model endpoint.

## Connections

- 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
