# himanshumohanty-git24/khatagpt architecture

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

- Interactive diagram: https://gitdiagram.com/himanshumohanty-git24/khatagpt
- Repository: https://github.com/himanshumohanty-git24/khatagpt

## Overview

##### Project Type and Purpose
- KhataGPT is a full-stack application that uses FastAPI on the backend with a MongoDB database and integrates Google Gemini AI for intelligent document processing and chat interactions.
- The frontend is built using React, utilizing Material-UI and other libraries to provide a responsive, component-based user interface for document viewing, upload, and chat functionalities.

##### Analyzing the File Structure
- The top-level directories “backend” and “frontend” immediately indicate a separation of concerns between server-side functionality and client-side interactions.
- The backend has clear subdivisions:
- “app/” houses the main application logic.
- “config.py” handles configuration.
- “database.py” connects to MongoDB.
- “routes/” includes API endpoints for chat and document operations.
- “models/” encapsulates data models (e.g., chat and document schemas).
- “services/” contains business logic such as chat, document processing, Gemini integration, etc.
- “utils/” provides additional helpers like database utilities and image or JSON processing.
- The frontend structure includes:
- “src/” where main code lives, with dedicated folders for components (organized into sub-folders like chat, documents and landing), pages, layouts, hooks, context, and services.
- Public assets and configuration files that support the React application.

##### Insights from the README
- The README emphasizes the key features (document intelligence, interactive AI chat, and smart management) and tech stack.
- The system integrates external services such as Google Gemini AI along with MongoDB for data storage.
- There’s clear documentation around API endpoints which helps in understanding how the backend exposes functionality to the frontend.

a. Main Components:
- Frontend: Represent the React single-page application with components (landing pages, chat interface, document management UI).
- Backend: Illustrate the FastAPI-based server split into:
- API Routes (chat and document endpoints)
- Service Layer (chat_service, document_service, gemini_service, document_processor, etc.)
- Models and Database Integration via MongoDB.
- Database: MongoDB storing documents, chats, metadata, etc.
- External Services: Google Gemini AI integration for advanced analysis and possibly third-party libraries such as PIL/Pillow for image processing.

b. Relationships and Interactions:
- That both the document processing and chat modules may invoke Google Gemini AI for analysis and generate responses which are then passed back through the API.

c. Architectural Patterns & Technologies:
- Separation of concerns (frontend vs. backend).
- Clearly demarcated layers in the backend (routing, business logic/services, and data access).
- Use of MVC or similar patterns, where the “models” complement the business logic and API routes.
- Mention key technologies: FastAPI, MongoDB, React, Material-UI, Axios, and Google Gemini AI.

- From user interfaces (frontend components) to API endpoints.
- From API endpoints to the service layers.
- From the service layers to the database and Gemini AI.

##### Instructing the Principal Software Engineer
- That the most essential architectural elements – from API routing through service logic to database interactions and external calls – are captured in as much detail as possible.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend"
        feApp["React Application"]:::frontend
        chatUI["Chat UI Components"]:::frontend
        docUI["Document Management UI"]:::frontend
        landingUI["Landing & Informational Pages"]:::frontend
        feApiService["API Service Integration"]:::frontend
    end

    %% Backend Subgraph
    subgraph "Backend"
        beConfig["API Configuration"]:::backend
        beMain["Main Application"]:::backend
        apiRoutes["API Routing"]:::backend
        subgraph "Services"
            chatService["Chat Processing"]:::service
            docProcessor["Document Processor"]:::service
            docService["Document Service"]:::service
            geminiService["Google Gemini AI Integration"]:::service
            utils["Utility Functions"]:::service
        end
        dataModels["Data Models"]:::backend
        dbConn["Database Connectivity"]:::backend
    end

    %% Database Subgraph
    subgraph "Database"
        mongoDB["MongoDB"]:::database
    end

    %% External Services Subgraph
    subgraph "External Services"
        geminiAI["Google Gemini AI"]:::external
    end

    %% Connections - Frontend to Backend
    feApiService -->|"REST_API_Call"| apiRoutes
    %% Internal Backend Connections
    beConfig -->|"init"| beMain
    beMain -->|"loads"| apiRoutes
    apiRoutes -->|"chat_request"| chatService
    apiRoutes -->|"doc_request"| docProcessor
    apiRoutes -->|"doc_request"| docService
    chatService -->|"db_query"| dbConn
    docProcessor -->|"db_query"| dbConn
    docService -->|"db_query"| dbConn
    chatService -->|"uses_model"| dataModels
    docProcessor -->|"uses_model"| dataModels
    docService -->|"uses_model"| dataModels
    chatService -->|"AI_call"| geminiService
    docProcessor -->|"AI_call"| geminiService
    geminiService -->|"process"| geminiAI

    %% Click Events for Frontend Components
    click feApp "https://github.com/himanshumohanty-git24/khatagpt/tree/main/frontend/src"
    click chatUI "https://github.com/himanshumohanty-git24/khatagpt/tree/main/frontend/src/components/chat"
    click docUI "https://github.com/himanshumohanty-git24/khatagpt/tree/main/frontend/src/components/documents"
    click landingUI "https://github.com/himanshumohanty-git24/khatagpt/tree/main/frontend/src/components/landing"
    click feApiService "https://github.com/himanshumohanty-git24/khatagpt/tree/main/frontend/src/services"

    %% Click Events for Backend Components
    click beMain "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/main.py"
    click beConfig "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/config.py"
    click dbConn "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/database.py"
    click dataModels "https://github.com/himanshumohanty-git24/khatagpt/tree/main/backend/app/models"
    click apiRoutes "https://github.com/himanshumohanty-git24/khatagpt/tree/main/backend/app/routes"
    click chatService "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/services/chat_service.py"
    click docProcessor "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/services/document_processor.py"
    click docService "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/services/document_service.py"
    click geminiService "https://github.com/himanshumohanty-git24/khatagpt/blob/main/backend/app/services/gemini_service.py"
    click utils "https://github.com/himanshumohanty-git24/khatagpt/tree/main/backend/app/utils"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#0066cc,stroke-width:2px;
    classDef backend fill:#ffe6cc,stroke:#cc6600,stroke-width:2px;
    classDef service fill:#e8daef,stroke:#8e44ad,stroke-width:2px;
    classDef database fill:#d5f5e3,stroke:#239b56,stroke-width:2px;
    classDef external fill:#f9e79f,stroke:#f1c40f,stroke-width:2px;
```
