# deepakindresh/mhai-backend architecture

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

- Interactive diagram: https://gitdiagram.com/deepakindresh/mhai-backend
- Repository: https://github.com/deepakindresh/mhai-backend

## Overview

MHAI-Backend is a FastAPI mental-health chat backend. The documented entry point is `deepSeek.py`; `/chat` gathers profile, assessment, and semantically relevant conversation context, constructs a prompt, generates a response, and stores the exchange. The files also expose message-editing and alternate chat behavior, while `main.py` contains a parallel chat implementation. Supabase provides profile, assessment, and message data; the Unsloth-loaded model generates replies and embeddings. The excerpts show the retrieval and persistence details, but omit parts of both chat handlers, so some documented integration links are cited to the README.

Main entry points: [`deepSeek.py`](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py) (Chat orchestration), [`main.py`](https://github.com/deepakindresh/mhai-backend/blob/main/main.py) (Alternate Chat API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API Layer"]
  node_deepseek_api["Chat API<br/>[deepSeek.py]"]
  node_main_api["Alternate Chat API<br/>[main.py]"]
  node_chat_flow["Chat orchestration<br/>[deepSeek.py]"]
  node_message_ops["Message operations<br/>[deepSeek.py]"]
end

subgraph group_context["Conversation Context"]
  node_profile_lookup["Profile lookup<br/>[deepSeek.py]"]
  node_assessment_lookup["Assessment lookup<br/>[deepSeek.py]"]
  node_context_retrieval["Relevant history<br/>[deepSeek.py]"]
  node_severity_summary["Severity summary<br/>[deepSeek.py]"]
end

subgraph group_inference["Model Inference"]
  node_embedding["Embedding generation<br/>[deepSeek.py]"]
  node_language_model["Unsloth model<br/>[deepSeek.py]"]
end

subgraph group_persistence["Data Services"]
  node_supabase[("Supabase")]
  node_message_store[("Chat messages")]
  node_profile_store[("User profiles")]
  node_assessment_store[("Assessments")]
end

node_client(("Application client"))

node_client -->|"sends requests"| node_deepseek_api
node_deepseek_api -->|"routes chat"| node_chat_flow
node_deepseek_api -->|"routes edits"| node_message_ops
node_chat_flow -->|"fetches profile"| node_profile_lookup
node_chat_flow -->|"fetches assessment"| node_assessment_lookup
node_chat_flow -->|"retrieves history"| node_context_retrieval
node_chat_flow -->|"formats severity"| node_severity_summary
node_profile_lookup -->|"queries profiles"| node_supabase
node_assessment_lookup -->|"queries assessments"| node_supabase
node_context_retrieval -->|"embeds message"| node_embedding
node_context_retrieval -->|"matches messages"| node_supabase
node_embedding -->|"embeds text"| node_language_model
node_chat_flow -->|"generates reply"| node_language_model
node_chat_flow -->|"embeds exchange"| node_embedding
node_chat_flow -->|"stores exchange"| node_supabase
node_message_ops -->|"embeds edited text"| node_embedding
node_message_ops -->|"updates message"| node_supabase
node_supabase -->|"reads"| node_profile_store
node_supabase -->|"reads"| node_assessment_store
node_supabase -->|"reads and writes"| node_message_store
node_client -.->|"sends requests"| node_main_api
node_main_api -.->|"parallel chat flow"| node_chat_flow

click node_deepseek_api "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_main_api "https://github.com/deepakindresh/mhai-backend/blob/main/main.py"
click node_chat_flow "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_message_ops "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_profile_lookup "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_assessment_lookup "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_context_retrieval "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_severity_summary "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_embedding "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py"
click node_language_model "https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.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_deepseek_api,node_main_api,node_chat_flow,node_message_ops,node_client toneBlue
class node_profile_lookup,node_assessment_lookup,node_context_retrieval,node_severity_summary toneAmber
class node_embedding,node_language_model toneMint
class node_supabase,node_message_store,node_profile_store,node_assessment_store toneRose
```

## Components

### API Layer

- [Chat API](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`
- [Alternate Chat API](https://github.com/deepakindresh/mhai-backend/blob/main/main.py): `main.py`
- [Chat orchestration](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`
- [Message operations](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`

### Conversation Context

- [Profile lookup](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`
- [Assessment lookup](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`
- [Relevant history](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`
- [Severity summary](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`

### Model Inference

- [Embedding generation](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`
- [Unsloth model](https://github.com/deepakindresh/mhai-backend/blob/main/deepSeek.py): `deepSeek.py`

### Data Services

- **Supabase**
- **Chat messages**
- **User profiles**
- **Assessments**

### Other components

- **Application client**

## Connections

- Application client → Chat API: sends requests (evidence: `README.md`)
- Chat API → Chat orchestration: routes chat (evidence: `deepSeek.py`)
- Chat API → Message operations: routes edits (evidence: `deepSeek.py`)
- Chat orchestration → Profile lookup: fetches profile (evidence: `deepSeek.py`)
- Chat orchestration → Assessment lookup: fetches assessment (evidence: `deepSeek.py`)
- Chat orchestration → Relevant history: retrieves history (evidence: `deepSeek.py`)
- Chat orchestration → Severity summary: formats severity (evidence: `deepSeek.py`)
- Profile lookup → Supabase: queries profiles (evidence: `deepSeek.py`)
- Assessment lookup → Supabase: queries assessments (evidence: `deepSeek.py`)
- Relevant history → Embedding generation: embeds message (evidence: `deepSeek.py`)
- Relevant history → Supabase: matches messages (evidence: `deepSeek.py`)
- Embedding generation → Unsloth model: embeds text (evidence: `deepSeek.py`)
- Chat orchestration → Unsloth model: generates reply (evidence: `README.md`)
- Chat orchestration → Embedding generation: embeds exchange (evidence: `deepSeek.py`)
- Chat orchestration → Supabase: stores exchange (evidence: `deepSeek.py`)
- Message operations → Embedding generation: embeds edited text (evidence: `deepSeek.py`)
- Message operations → Supabase: updates message (evidence: `deepSeek.py`)
- Supabase → User profiles: reads
- Supabase → Assessments: reads
- Supabase → Chat messages: reads and writes
- Application client → Alternate Chat API: sends requests (evidence: `README.md`)
- Alternate Chat API → Chat orchestration: parallel chat flow
