# sairohith28/medsuggestions architecture

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

- Interactive diagram: https://gitdiagram.com/sairohith28/medsuggestions
- Repository: https://github.com/sairohith28/medsuggestions

## Overview

##### Project type and purpose
- This is a backend-only Medical AI Assistant microservice written in Python (Flask).
- It exposes a single HTTP API (`/api/suggest`) that ingests a patientId and doctorId and returns structured medical suggestions.
- It combines data from MongoDB, a semantic-similarity module, an external ICD-11 code API, and a Language Learning Model (LLM) API.

##### File structure
- Top level contains:
- med5.py (the main Flask application)
- A series of Python modules/scripts: extract.py, suggestions.py, past_history.py, agentic.py, etc., each encapsulating a slice of business logic (data extraction, prompt construction, few-shot example formatting, LLM invocation, etc.)
- No frontend directory—this is purely backend.
- Configuration is externalized via environment variables (`.env` → MONGO_URI, LLM_API_URL, etc.).

##### README for additional insights
- Core dependencies: Flask, PyMongo (MongoDB driver), a semantic model library (e.g., HuggingFace embeddings), and an LLM API client.
- Workflow:
a) Fetch patient and doctor data from MongoDB
b) Compute semantic similarity to find analogous past visits and preferences
c) Format a prompt (few-shot examples)
d) Call the LLM API
e) Fetch ICD-11 codes from external API
f) Assemble and return the JSON response

##### Summary
- Break the system into the smallest meaningful components (API layer, data layer, logic modules, external services).
- How data flows between them in sequence.
- Technologies and call out configuration.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["Client"]:::external

    %% API Layer
    API["Flask API (med5.py)"]:::internal

    %% Business Logic Subgraph
    subgraph "Business Logic"
        direction TB
        subgraph "Data Extraction & Aggregation"
            direction TB
            Extract["extract.py"]:::internal
            PastHistory["past_history.py"]:::internal
        end
        SemanticEngine["Semantic Similarity Engine (agentic.py)"]:::internal
        PromptBuilder["Prompt Builder & LLM Invoker (suggestions.py)"]:::internal
        Aggregator["Response Assembler (final.py)"]:::internal
    end

    %% Database
    MongoDB["MongoDB Cluster"]:::database

    %% External Services
    LLMAPI["LLM API"]:::external
    ICD11API["ICD-11 Code API"]:::external

    %% Utilities
    subgraph "Utility Scripts"
        direction TB
        LiveVersion["live_version.py"]:::internal
        FinalNoSearch["final_version_without_searching.py"]:::internal
    end

    %% Data Flow
    Client -->|"HTTP POST /api/suggest"| API
    API -->|"fetch patient & doctor data"| Extract
    API -->|"fetch past visits"| PastHistory
    Extract -->|Query| MongoDB
    PastHistory -->|Query| MongoDB
    API -->|"compute embeddings & similarity"| SemanticEngine
    SemanticEngine -->|nearest examples| API
    API -->|"build prompt"| PromptBuilder
    PromptBuilder -->|LLM request| LLMAPI
    LLMAPI -->|suggestions response| PromptBuilder
    API -->|"fetch diagnosis codes"| ICD11API
    ICD11API -->|codes response| API
    API -->|"assemble JSON"| Aggregator
    Aggregator -->|JSON response| Client

    %% Click Events
    click API "https://github.com/sairohith28/medsuggestions/blob/main/med5.py"
    click Extract "https://github.com/sairohith28/medsuggestions/blob/main/extract.py"
    click PastHistory "https://github.com/sairohith28/medsuggestions/blob/main/past_history.py"
    click SemanticEngine "https://github.com/sairohith28/medsuggestions/blob/main/agentic.py"
    click PromptBuilder "https://github.com/sairohith28/medsuggestions/blob/main/suggestions.py"
    click Aggregator "https://github.com/sairohith28/medsuggestions/blob/main/final.py"
    click LiveVersion "https://github.com/sairohith28/medsuggestions/blob/main/live_version.py"
    click FinalNoSearch "https://github.com/sairohith28/medsuggestions/blob/main/final_version_without_searching.py"

    %% Styles
    classDef internal fill:#add8e6,stroke:#333,stroke-width:1px
    classDef external fill:#ffa500,stroke:#333,stroke-width:1px
    classDef database fill:#90ee90,stroke:#333,stroke-width:1px
```
