# kiendoo4/doctorqa architecture

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

- Interactive diagram: https://gitdiagram.com/kiendoo4/doctorqa
- Repository: https://github.com/kiendoo4/doctorqa

## Overview

##### Project Type & Purpose
- DoctorQA is an AI-powered medical Q&A web application targeted at Vietnamese users. It integrates advanced AI techniques, including Chain-of-Thought (CoT) reasoning and Retrieval-Augmented Generation (RAG) through the LangChain framework and an LLM (Gemini-1.5). It also uses a Qdrant vector database for medical document embeddings and PostgreSQL for user and conversation management.

##### Analyzing the File Structure
- Key Backend Files: Files like main.py, database.py, CoT.py, and CoT_ver2.py indicate server-side logic, AI reasoning, and database interactions.
- Frontend Components: The static and templates directories house user interface assets (CSS, JavaScript, HTML) for the chat UI, registration, and the home/index page.
- Data Processing: The Jupyter Notebook (crawled-data-for-doctorqa.ipynb) indicates a separate data ingestion and processing pipeline used to crawl, clean, and vectorize over 30,000 medical documents.
- Additional Tools: test_model.py suggests some form of testing or validation, likely for the model integrations.

##### Main Components to Capture
a. Frontend UI:
– Web pages rendered from the /templates directory (index.html, chatUI.html, registration.html)
b. Backend API & Web Server:
– Flask-based server (main.py) that handles HTTP requests.
– Specific endpoints for login, registration (e.g., static/registration.py may be part of the workflow), and chat interactions.
c. AI Components:
– Integration with the LLM (Gemini-1.5) for generating context-aware answers.
– Chain-of-Thought logic modules (CoT.py and CoT_ver2.py) to enable enhanced reasoning in responses.
– LangChain as the orchestrator coordinating between LLM and data retrieval.
d. Data Storage:
– Qdrant Vector Database: Used to store and query vectorized medical documents. This serves the retrieval segment for RAG.
– PostgreSQL: Structured storage for user management (sessions, login data) and conversation logging.
e. Data Ingestion Pipeline:
– Data crawling and processing (via crawled-data-for-doctorqa.ipynb) that feeds data into Qdrant.
f. External & Auxiliary Components:
– Any error or fallback pages (e.g., a "failed.html" under the next_page directory) for handling unsuccessful operations.

##### Relationships and Data Flow
- Frontend UI communicates with the backend API via HTTP calls (AJAX or form submissions), which is served by Flask.
- The backend invokes the LangChain framework for coordinating with the Gemini-1.5 LLM and the CoT modules, ensuring that the questions get processed with enhanced reasoning.
- For medical knowledge retrieval, the backend queries the Qdrant vector database with the processed query to fetch relevant documents.
- User-related and logging operations are handled by PostgreSQL.
- The Data Ingestion component (from the Jupyter Notebook) periodically updates or feeds new data into Qdrant.

##### Architectural Patterns & Design Principles
- Layered architecture separating frontend, backend business logic, data management, and AI orchestration.
- Service orchestration using LangChain that encapsulates interactions between the LLM, chain-of-thought processing, and the data layer.
- Emphasis on modularity where each component (UI, API, AI modules, databases) is logically isolated, making it easier to update or scale parts independently.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend UI"
        Templates["Templates"]:::frontend
        StaticAssets["Static Assets"]:::frontend
    end

    subgraph "Backend API / Flask Server"
        FlaskServer["Flask Server"]:::backend
        MainServer["Main Server File (main.py)"]:::backend
        DatabaseInter["Database Interactions (database.py)"]:::backend
    end

    subgraph "AI Engine"
        LangChainLLM["LangChain & LLM Integration"]:::ai
        CoT1["Chain-of-Thought (CoT.py)"]:::ai
        CoT2["Chain-of-Thought (CoT_ver2.py)"]:::ai
    end

    subgraph "Databases"
        Qdrant["Qdrant (Vector DB)"]:::database
        PostgreSQL["PostgreSQL (User & Logging DB)"]:::database
    end

    subgraph "Data Ingestion Pipeline"
        DataCrawl["Data Crawling and Processing"]:::ingestion
    end

    subgraph "External & Auxiliary"
        ErrorPage["Error/Fallback Page"]:::auxiliary
    end

    subgraph "Testing Tools"
        TestTool["Testing Tools (test_model.py)"]:::test
    end

    %% Data Flow Connections
    Templates -->|"HTTP"| FlaskServer
    StaticAssets -->|"HTTP"| FlaskServer

    FlaskServer -->|"AI Processing"| LangChainLLM
    FlaskServer -->|"Uses"| CoT1
    FlaskServer -->|"Uses"| CoT2

    FlaskServer -->|"Query"| Qdrant
    FlaskServer -->|"Log and Manage"| PostgreSQL

    DataCrawl -->|"Feeds Data"| Qdrant

    FlaskServer -->|"Fallback on Error"| ErrorPage

    TestTool -->|"Tests"| FlaskServer

    %% Styles
    classDef frontend fill:#f9d6a5,stroke:#333,stroke-width:2px;
    classDef backend fill:#a5d8f9,stroke:#333,stroke-width:2px;
    classDef ai fill:#caffbf,stroke:#333,stroke-width:2px;
    classDef database fill:#ffd6a5,stroke:#333,stroke-width:2px;
    classDef ingestion fill:#d3a5f9,stroke:#333,stroke-width:2px;
    classDef auxiliary fill:#f2a5ff,stroke:#333,stroke-width:2px;
    classDef test fill:#a5f9d8,stroke:#333,stroke-width:2px;

    %% Click Events
    click Templates "https://github.com/kiendoo4/doctorqa/tree/main//templates"
    click StaticAssets "https://github.com/kiendoo4/doctorqa/tree/main//static"
    click MainServer "https://github.com/kiendoo4/doctorqa/blob/main//main.py"
    click DatabaseInter "https://github.com/kiendoo4/doctorqa/blob/main//database.py"
    click CoT1 "https://github.com/kiendoo4/doctorqa/blob/main//CoT.py"
    click CoT2 "https://github.com/kiendoo4/doctorqa/blob/main//CoT_ver2.py"
    click DataCrawl "https://github.com/kiendoo4/doctorqa/blob/main//crawled-data-for-doctorqa.ipynb"
    click ErrorPage "https://github.com/kiendoo4/doctorqa/blob/main//next_page/failed.html"
    click TestTool "https://github.com/kiendoo4/doctorqa/blob/main//test_model.py"
```
