# tensormax/medai architecture

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

- Interactive diagram: https://gitdiagram.com/tensormax/medai
- Repository: https://github.com/tensormax/medai

## Overview

MedAI is a Django clinical dashboard for doctors. Its main workflow runs from patient and visit management through document upload/indexing, patient-scoped retrieval, and AI-assisted chat or reports; separate paths produce deterministic clinical PDFs and manage follow-ups. The graph includes documented product features whose views or generators were not sampled. Edges cite explicit README descriptions or source evidence; unsampled implementation details are not inferred beyond those descriptions.

Main entry points: [`apps/ai/facade.py`](https://github.com/tensormax/medai/blob/main/apps/ai/facade.py) (AI orchestrator), [`apps/documents/services.py`](https://github.com/tensormax/medai/blob/main/apps/documents/services.py) (Upload processing), [`apps/visits/models.py`](https://github.com/tensormax/medai/blob/main/apps/visits/models.py) (Visit workspace), [`apps/visits/services.py`](https://github.com/tensormax/medai/blob/main/apps/visits/services.py) (Visit chat service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_care["Clinical workflow"]
  node_accounts[("Doctor accounts<br/>[models.py]")]
  node_patients[("Patient records<br/>[models.py]")]
  node_dashboard["Tasks dashboard<br/>[views.py]"]
  node_tasks[("Tasks and follow-ups<br/>[models.py]")]
  node_visits[("Visit workspace<br/>[models.py]")]
  node_chatservice["Visit chat service<br/>[services.py]"]
end

subgraph group_documents["Document pipeline"]
  node_upload["Upload processing<br/>[services.py]"]
  node_chunking["PDF extraction and chunking<br/>[chunking.py]"]
  node_deidentify["PHI de-identification<br/>[deidentify.py]"]
  node_chunkstore[("Document chunks<br/>[models.py]")]
end

subgraph group_ai["AI and retrieval"]
  node_aiorchestrator["AI orchestrator<br/>[facade.py]"]
  node_retrieval["RAG retrieval and reranking<br/>[retrieval.py]"]
  node_embeddings["Local embeddings<br/>[embeddings.py]"]
  node_vectorstore["Vector search<br/>[vectorstore.py]"]
  node_prompts["Prompt templates<br/>[prompts.py]"]
end

subgraph group_outputs["Clinical outputs"]
  node_clinicalpdf["Clinical PDF builders<br/>[clinical_docs.py]"]
  node_reportgen["AI report generation"]
  node_analysis[("Document analyses and reports<br/>[models.py]")]
  node_pdfoutput["Report PDF rendering<br/>[pdf.py]"]
end

node_doctor(("Doctor"))
node_llm{{"OpenRouter LLM"}}

node_doctor -->|"registers"| node_accounts
node_doctor -->|"manages"| node_patients
node_doctor -->|"uses"| node_dashboard
node_dashboard -->|"manages"| node_tasks
node_doctor -->|"starts"| node_visits
node_visits -->|"posts messages"| node_chatservice
node_chatservice -->|"saves messages"| node_visits
node_chatservice -->|"requests reply"| node_aiorchestrator
node_aiorchestrator -->|"retrieves context"| node_retrieval
node_aiorchestrator -->|"builds prompts"| node_prompts
node_aiorchestrator -->|"requests completion"| node_llm
node_retrieval -->|"embeds queries"| node_embeddings
node_retrieval -->|"queries vectors"| node_vectorstore
node_vectorstore -->|"searches chunks"| node_chunkstore
node_doctor -->|"uploads documents"| node_upload
node_upload -->|"extracts and chunks"| node_chunking
node_upload -->|"de-identifies text"| node_deidentify
node_upload -->|"stores chunks"| node_chunkstore
node_upload -->|"creates vectors"| node_embeddings
node_doctor -->|"requests document"| node_clinicalpdf
node_doctor -->|"requests report"| node_aiorchestrator
node_aiorchestrator -->|"generates report"| node_reportgen
node_reportgen -.->|"requests completion"| node_llm
node_upload -->|"requests analysis"| node_aiorchestrator
node_aiorchestrator -->|"returns analysis data"| node_analysis
node_doctor -->|"downloads report PDF"| node_pdfoutput
node_visits -->|"creates follow-up"| node_tasks

click node_accounts "https://github.com/tensormax/medai/blob/main/apps/accounts/models.py"
click node_patients "https://github.com/tensormax/medai/blob/main/apps/patients/models.py"
click node_dashboard "https://github.com/tensormax/medai/blob/main/apps/tasks/views.py"
click node_tasks "https://github.com/tensormax/medai/blob/main/apps/tasks/models.py"
click node_visits "https://github.com/tensormax/medai/blob/main/apps/visits/models.py"
click node_chatservice "https://github.com/tensormax/medai/blob/main/apps/visits/services.py"
click node_upload "https://github.com/tensormax/medai/blob/main/apps/documents/services.py"
click node_chunking "https://github.com/tensormax/medai/blob/main/apps/ai/chunking.py"
click node_deidentify "https://github.com/tensormax/medai/blob/main/apps/ai/deidentify.py"
click node_chunkstore "https://github.com/tensormax/medai/blob/main/apps/documents/models.py"
click node_clinicalpdf "https://github.com/tensormax/medai/blob/main/apps/documents/clinical_docs.py"
click node_aiorchestrator "https://github.com/tensormax/medai/blob/main/apps/ai/facade.py"
click node_retrieval "https://github.com/tensormax/medai/blob/main/apps/ai/retrieval.py"
click node_embeddings "https://github.com/tensormax/medai/blob/main/apps/ai/embeddings.py"
click node_vectorstore "https://github.com/tensormax/medai/blob/main/apps/ai/vectorstore.py"
click node_prompts "https://github.com/tensormax/medai/blob/main/apps/ai/prompts.py"
click node_reportgen "https://github.com/tensormax/medai/blob/main/apps/ai/report_generation.py"
click node_analysis "https://github.com/tensormax/medai/blob/main/apps/documents/models.py"
click node_pdfoutput "https://github.com/tensormax/medai/blob/main/apps/documents/pdf.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_accounts,node_patients,node_dashboard,node_tasks,node_visits,node_chatservice toneBlue
class node_upload,node_chunking,node_deidentify,node_chunkstore toneAmber
class node_aiorchestrator,node_retrieval,node_embeddings,node_vectorstore,node_prompts,node_llm toneMint
class node_clinicalpdf,node_reportgen,node_analysis,node_pdfoutput toneRose
class node_doctor toneIndigo
```

## Components

### Clinical workflow

- [Doctor accounts](https://github.com/tensormax/medai/blob/main/apps/accounts/models.py): `apps/accounts/models.py`
- [Patient records](https://github.com/tensormax/medai/blob/main/apps/patients/models.py): `apps/patients/models.py`
- [Tasks dashboard](https://github.com/tensormax/medai/blob/main/apps/tasks/views.py): `apps/tasks/views.py`
- [Tasks and follow-ups](https://github.com/tensormax/medai/blob/main/apps/tasks/models.py): `apps/tasks/models.py`
- [Visit workspace](https://github.com/tensormax/medai/blob/main/apps/visits/models.py): `apps/visits/models.py`
- [Visit chat service](https://github.com/tensormax/medai/blob/main/apps/visits/services.py): `apps/visits/services.py`

### Document pipeline

- [Upload processing](https://github.com/tensormax/medai/blob/main/apps/documents/services.py): `apps/documents/services.py`
- [PDF extraction and chunking](https://github.com/tensormax/medai/blob/main/apps/ai/chunking.py): `apps/ai/chunking.py`
- [PHI de-identification](https://github.com/tensormax/medai/blob/main/apps/ai/deidentify.py): `apps/ai/deidentify.py`
- [Document chunks](https://github.com/tensormax/medai/blob/main/apps/documents/models.py): `apps/documents/models.py`

### AI and retrieval

- [AI orchestrator](https://github.com/tensormax/medai/blob/main/apps/ai/facade.py): `apps/ai/facade.py`
- [RAG retrieval and reranking](https://github.com/tensormax/medai/blob/main/apps/ai/retrieval.py): `apps/ai/retrieval.py`
- [Local embeddings](https://github.com/tensormax/medai/blob/main/apps/ai/embeddings.py): `apps/ai/embeddings.py`
- [Vector search](https://github.com/tensormax/medai/blob/main/apps/ai/vectorstore.py): `apps/ai/vectorstore.py`
- [Prompt templates](https://github.com/tensormax/medai/blob/main/apps/ai/prompts.py): `apps/ai/prompts.py`

### Clinical outputs

- [Clinical PDF builders](https://github.com/tensormax/medai/blob/main/apps/documents/clinical_docs.py): `apps/documents/clinical_docs.py`
- [AI report generation](https://github.com/tensormax/medai/blob/main/apps/ai/report_generation.py): `apps/ai/report_generation.py`
- [Document analyses and reports](https://github.com/tensormax/medai/blob/main/apps/documents/models.py): `apps/documents/models.py`
- [Report PDF rendering](https://github.com/tensormax/medai/blob/main/apps/documents/pdf.py): `apps/documents/pdf.py`

### Other components

- **Doctor**
- **OpenRouter LLM**

## Connections

- Doctor → Doctor accounts: registers
- Doctor → Patient records: manages
- Doctor → Tasks dashboard: uses
- Tasks dashboard → Tasks and follow-ups: manages
- Doctor → Visit workspace: starts
- Visit workspace → Visit chat service: posts messages (evidence: `apps/visits/services.py`)
- Visit chat service → Visit workspace: saves messages (evidence: `apps/visits/services.py`)
- Visit chat service → AI orchestrator: requests reply (evidence: `apps/visits/services.py`)
- AI orchestrator → RAG retrieval and reranking: retrieves context (evidence: `apps/ai/facade.py`)
- AI orchestrator → Prompt templates: builds prompts (evidence: `apps/ai/facade.py`)
- AI orchestrator → OpenRouter LLM: requests completion (evidence: `apps/ai/facade.py`)
- RAG retrieval and reranking → Local embeddings: embeds queries (evidence: `apps/ai/retrieval.py`)
- RAG retrieval and reranking → Vector search: queries vectors (evidence: `apps/ai/retrieval.py`)
- Vector search → Document chunks: searches chunks (evidence: `apps/ai/vectorstore.py`)
- Doctor → Upload processing: uploads documents
- Upload processing → PDF extraction and chunking: extracts and chunks (evidence: `apps/documents/services.py`)
- Upload processing → PHI de-identification: de-identifies text (evidence: `apps/documents/services.py`)
- Upload processing → Document chunks: stores chunks (evidence: `apps/documents/services.py`)
- Upload processing → Local embeddings: creates vectors (evidence: `apps/documents/services.py`)
- Doctor → Clinical PDF builders: requests document
- Doctor → AI orchestrator: requests report
- AI orchestrator → AI report generation: generates report (evidence: `apps/ai/facade.py`)
- AI report generation → OpenRouter LLM: requests completion
- Upload processing → AI orchestrator: requests analysis (evidence: `apps/documents/services.py`)
- AI orchestrator → Document analyses and reports: returns analysis data (evidence: `apps/ai/facade.py`)
- Doctor → Report PDF rendering: downloads report PDF
- Visit workspace → Tasks and follow-ups: creates follow-up (evidence: `apps/visits/services.py`)
