# laybaatariq/langchain-medicalchatbot architecture

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

- Interactive diagram: https://gitdiagram.com/laybaatariq/langchain-medicalchatbot
- Repository: https://github.com/laybaatariq/langchain-medicalchatbot

## Overview

This is a medical-information chatbot with a React chat interface and a FastAPI backend. A user submits a message; the backend loads session history, routes and triages the question, then returns an emergency response, conversational answer, document-grounded answer, or general answer. The interface renders the reply and any sources, while chat history is stored in MongoDB and medical-document retrieval uses Qdrant. The README provides no capability detail; additional branches are based on the source index and tree, so some ingestion and frontend-to-backend links remain unverified.

Main entry points: [`backend/app/chains/medical_qa_chain.py`](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/medical_qa_chain.py) (Answer workflow), [`backend/app/rag/vector_store.py`](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/vector_store.py) (Vector search), [`frontend/src/App.jsx`](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/App.jsx) (Chat app), [`backend/app/api/routes/chat.py`](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/routes/chat.py) (Chat endpoint)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Chat interface"]
  node_app["Chat app<br/>[App.jsx]"]
  node_chat_state["Chat state<br/>[useChat.js]"]
  node_api_client["API client<br/>[api.js]"]
  node_composer["Message composer<br/>[Composer.jsx]"]
  node_empty_state["Empty state<br/>[EmptyState.jsx]"]
  node_message_view["Message view<br/>[Message.jsx]"]
  node_sources_view["Source display<br/>[Sources.jsx]"]
  node_icons["Chat icons<br/>[Icons.jsx]"]
end

subgraph group_api["API and sessions"]
  node_server["FastAPI app<br/>[main.py]"]
  node_chat_route["Chat endpoint<br/>[chat.py]"]
  node_health_route["Health endpoint<br/>[health.py]"]
  node_schemas["API schemas<br/>[schemas.py]"]
  node_mongo[("Chat history<br/>[mongo.py]")]
end

subgraph group_answer["Answer orchestration"]
  node_qa["Answer workflow"]
  node_router["Intent router<br/>[router.py]"]
  node_prompts["Answer prompts<br/>[system_prompts.py]"]
end

subgraph group_safety["Safety and knowledge"]
  node_safety_rules["Safety checks<br/>[emergency.py]"]
  node_keyword_filters["Keyword filters<br/>[keyword_filters.py]"]
  node_triage["Triage classifier"]
  node_knowledge["Rules and doctors<br/>[knowledge.py]"]
  node_rule_data["Clinical rule data"]
end

subgraph group_retrieval["Document retrieval"]
  node_vector_store[("Vector search<br/>[vector_store.py]")]
  node_ingest_docs["Document ingestion<br/>[ingest.py]"]
  node_ingest_medline["MedlinePlus ingestion"]
  node_medical_docs["Medical source files"]
  node_medline_data["MedlinePlus source"]
end

node_user(("User"))
node_settings["App settings<br/>[config.py]"]
node_qdrant[("Qdrant Cloud")]
node_mongo_service[("MongoDB Atlas")]

node_user -->|"enters question"| node_composer
node_app -->|"uses chat state"| node_chat_state
node_app -->|"passes send handler"| node_composer
node_app -->|"renders"| node_empty_state
node_app -->|"renders replies"| node_message_view
node_app -->|"renders"| node_icons
node_chat_state -->|"calls"| node_api_client
node_message_view -->|"renders sources"| node_sources_view
node_message_view -->|"renders"| node_icons
node_composer -->|"renders"| node_icons
node_empty_state -->|"renders"| node_icons
node_api_client -.->|"sends chat request"| node_chat_route
node_server -->|"registers route"| node_chat_route
node_server -->|"registers route"| node_health_route
node_server -->|"sets indexes"| node_mongo
node_server -->|"checks collection"| node_vector_store
node_chat_route -->|"uses models"| node_schemas
node_health_route -->|"uses models"| node_schemas
node_chat_route -->|"requests answer"| node_qa
node_chat_route -->|"reads and saves history"| node_mongo
node_mongo -.->|"stores chat records"| node_mongo_service
node_qa -->|"routes message"| node_router
node_qa -->|"uses prompts"| node_prompts
node_qa -->|"checks urgency"| node_safety_rules
node_qa -->|"checks keywords"| node_keyword_filters
node_router -->|"guesses language"| node_safety_rules
node_safety_rules -->|"loads red flags"| node_knowledge
node_knowledge -->|"loads rule files"| node_rule_data
node_qa -->|"searches passages"| node_vector_store
node_vector_store -.->|"queries vectors"| node_qdrant
node_ingest_docs -->|"upserts chunks"| node_vector_store
node_ingest_medline -->|"upserts chunks"| node_vector_store
node_medical_docs -.->|"source input"| node_ingest_docs
node_medline_data -.->|"source input"| node_ingest_medline
node_settings -->|"configures connection"| node_mongo
node_settings -->|"configures connection"| node_vector_store

click node_app "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/App.jsx"
click node_chat_state "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/hooks/useChat.js"
click node_api_client "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/api.js"
click node_composer "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Composer.jsx"
click node_empty_state "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/EmptyState.jsx"
click node_message_view "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Message.jsx"
click node_sources_view "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Sources.jsx"
click node_icons "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Icons.jsx"
click node_server "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/main.py"
click node_chat_route "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/routes/chat.py"
click node_health_route "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/routes/health.py"
click node_schemas "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/schemas.py"
click node_mongo "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/db/mongo.py"
click node_qa "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/medical_qa_chain.py"
click node_router "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/router.py"
click node_prompts "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/system_prompts.py"
click node_safety_rules "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/safety/emergency.py"
click node_keyword_filters "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/safety/keyword_filters.py"
click node_triage "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/safety/triage_classifier.py"
click node_knowledge "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/core/knowledge.py"
click node_rule_data "https://github.com/laybaatariq/langchain-medicalchatbot/tree/main/backend/app/data/rules"
click node_vector_store "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/vector_store.py"
click node_ingest_docs "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/ingest.py"
click node_ingest_medline "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/ingest_medlineplus.py"
click node_medical_docs "https://github.com/laybaatariq/langchain-medicalchatbot/tree/main/backend/app/data/medical_docs"
click node_medline_data "https://github.com/laybaatariq/langchain-medicalchatbot/tree/main/backend/app/data/medlineplus"
click node_settings "https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/core/config.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_app,node_chat_state,node_api_client,node_composer,node_empty_state,node_message_view,node_sources_view,node_icons,node_user toneBlue
class node_server,node_chat_route,node_health_route,node_schemas,node_mongo,node_qdrant,node_mongo_service toneAmber
class node_qa,node_router,node_prompts toneMint
class node_safety_rules,node_keyword_filters,node_triage,node_knowledge,node_rule_data toneRose
class node_vector_store,node_ingest_docs,node_ingest_medline,node_medical_docs,node_medline_data toneIndigo
class node_settings toneTeal
```

## Components

### Chat interface

- [Chat app](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/App.jsx): `frontend/src/App.jsx`
- [Chat state](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/hooks/useChat.js): `frontend/src/hooks/useChat.js`
- [API client](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/api.js): `frontend/src/api.js`
- [Message composer](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Composer.jsx): `frontend/src/components/Composer.jsx`
- [Empty state](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/EmptyState.jsx): `frontend/src/components/EmptyState.jsx`
- [Message view](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Message.jsx): `frontend/src/components/Message.jsx`
- [Source display](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Sources.jsx): `frontend/src/components/Sources.jsx`
- [Chat icons](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/frontend/src/components/Icons.jsx): `frontend/src/components/Icons.jsx`

### API and sessions

- [FastAPI app](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/main.py): `backend/app/main.py`
- [Chat endpoint](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/routes/chat.py): `backend/app/api/routes/chat.py`
- [Health endpoint](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/routes/health.py): `backend/app/api/routes/health.py`
- [API schemas](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/api/schemas.py): `backend/app/api/schemas.py`
- [Chat history](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/db/mongo.py): `backend/app/db/mongo.py`

### Answer orchestration

- [Answer workflow](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/medical_qa_chain.py): `backend/app/chains/medical_qa_chain.py`
- [Intent router](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/router.py): `backend/app/chains/router.py`
- [Answer prompts](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/chains/system_prompts.py): `backend/app/chains/system_prompts.py`

### Safety and knowledge

- [Safety checks](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/safety/emergency.py): `backend/app/safety/emergency.py`
- [Keyword filters](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/safety/keyword_filters.py): `backend/app/safety/keyword_filters.py`
- [Triage classifier](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/safety/triage_classifier.py): `backend/app/safety/triage_classifier.py`
- [Rules and doctors](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/core/knowledge.py): `backend/app/core/knowledge.py`
- [Clinical rule data](https://github.com/laybaatariq/langchain-medicalchatbot/tree/main/backend/app/data/rules): `backend/app/data/rules`

### Document retrieval

- [Vector search](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/vector_store.py): `backend/app/rag/vector_store.py`
- [Document ingestion](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/ingest.py): `backend/app/rag/ingest.py`
- [MedlinePlus ingestion](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/rag/ingest_medlineplus.py): `backend/app/rag/ingest_medlineplus.py`
- [Medical source files](https://github.com/laybaatariq/langchain-medicalchatbot/tree/main/backend/app/data/medical_docs): `backend/app/data/medical_docs`
- [MedlinePlus source](https://github.com/laybaatariq/langchain-medicalchatbot/tree/main/backend/app/data/medlineplus): `backend/app/data/medlineplus`

### Other components

- **User**
- [App settings](https://github.com/laybaatariq/langchain-medicalchatbot/blob/main/backend/app/core/config.py): `backend/app/core/config.py`
- **Qdrant Cloud**
- **MongoDB Atlas**

## Connections

- User → Message composer: enters question
- Chat app → Chat state: uses chat state (evidence: `frontend/src/App.jsx`)
- Chat app → Message composer: passes send handler (evidence: `frontend/src/App.jsx`)
- Chat app → Empty state: renders (evidence: `frontend/src/App.jsx`)
- Chat app → Message view: renders replies (evidence: `frontend/src/App.jsx`)
- Chat app → Chat icons: renders (evidence: `frontend/src/App.jsx`)
- Chat state → API client: calls (evidence: `frontend/src/hooks/useChat.js`)
- Message view → Source display: renders sources (evidence: `frontend/src/components/Message.jsx`)
- Message view → Chat icons: renders (evidence: `frontend/src/components/Message.jsx`)
- Message composer → Chat icons: renders (evidence: `frontend/src/components/Composer.jsx`)
- Empty state → Chat icons: renders (evidence: `frontend/src/components/EmptyState.jsx`)
- API client → Chat endpoint: sends chat request
- FastAPI app → Chat endpoint: registers route (evidence: `backend/app/main.py`)
- FastAPI app → Health endpoint: registers route (evidence: `backend/app/main.py`)
- FastAPI app → Chat history: sets indexes (evidence: `backend/app/main.py`)
- FastAPI app → Vector search: checks collection (evidence: `backend/app/main.py`)
- Chat endpoint → API schemas: uses models (evidence: `backend/app/api/routes/chat.py`)
- Health endpoint → API schemas: uses models (evidence: `backend/app/api/routes/health.py`)
- Chat endpoint → Answer workflow: requests answer (evidence: `backend/app/api/routes/chat.py`)
- Chat endpoint → Chat history: reads and saves history (evidence: `backend/app/api/routes/chat.py`)
- Chat history → MongoDB Atlas: stores chat records
- Answer workflow → Intent router: routes message (evidence: `backend/app/chains/medical_qa_chain.py`)
- Answer workflow → Answer prompts: uses prompts (evidence: `backend/app/chains/medical_qa_chain.py`)
- Answer workflow → Safety checks: checks urgency (evidence: `backend/app/chains/medical_qa_chain.py`)
- Answer workflow → Keyword filters: checks keywords (evidence: `backend/app/chains/medical_qa_chain.py`)
- Intent router → Safety checks: guesses language (evidence: `backend/app/chains/router.py`)
- Safety checks → Rules and doctors: loads red flags (evidence: `backend/app/safety/emergency.py`)
- Rules and doctors → Clinical rule data: loads rule files (evidence: `backend/app/core/knowledge.py`)
- Answer workflow → Vector search: searches passages (evidence: `backend/app/chains/medical_qa_chain.py`)
- Vector search → Qdrant Cloud: queries vectors
- Document ingestion → Vector search: upserts chunks (evidence: `backend/app/rag/ingest.py`)
- MedlinePlus ingestion → Vector search: upserts chunks (evidence: `backend/app/rag/ingest_medlineplus.py`)
- Medical source files → Document ingestion: source input
- MedlinePlus source → MedlinePlus ingestion: source input
- App settings → Chat history: configures connection (evidence: `backend/app/db/mongo.py`)
- App settings → Vector search: configures connection (evidence: `backend/app/rag/vector_store.py`)
