# kartikkalia-enkash/enkash-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/kartikkalia-enkash/enkash-chatbot
- Repository: https://github.com/kartikkalia-enkash/enkash-chatbot

## Overview

##### Project Overview (“What exactly this project does”)
- This is a backend service—an intelligent chatbot API for Enkash Payment Gateway documentation.
- Key functions:
- Document Management: upload, list, delete API docs.
- Vectorization: split documents into chunks, embed using Google Generative AI Embeddings, store vectors in Pinecone.
- RAG Chat: on chat requests, retrieve relevant chunks (LangChain RAG), send prompt + context to Google Gemini LLM, return response.
- Persistence: stores document metadata and chat history in SQLite.
- Exposes RESTful endpoints (FastAPI) for document and chat operations.

##### Main Components
- Client: any HTTP client or future frontend invoking `/api/v1/documents/*` and `/api/v1/chat`.
- FastAPI Backend (main.py + app/):
- API Layer (app/api/routes): documents.py, chat.py
- Core (app/core): config.py (env), database.py (SQLite setup)
- Models & Schemas (app/models, app/schemas): Pydantic schemas and ORM models
- Services (app/services):
– document_service.py (file ingestion, text splitting)
– pinecone_service.py (vector store CRUD)
– rag_service.py (LangChain orchestration)
- External Services:
- Pinecone Vector Database (serverless index)
- Google Gemini LLM (gemini-pro model)
- Persistence:
- SQLite via aiosqlite for chat history & document metadata

##### Relationships & Data Flows
- Client → HTTP Request → FastAPI Router
- Documents API → DocumentService → PineconeService → Pinecone
↘ Database (metadata)
- Chat API → RagService → PineconeService (retrieve relevant chunks)
↘ Google Gemini LLM → response
↘ Database (chat history)
- Config & Database modules injected into services at startup

##### Architectural Patterns & Principles
- Layered Architecture:
- Presentation/API layer → Business logic/services → Data layer (DB & external)
- Dependency Injection: FastAPI’s `Depends` to wire config, DB session, services
- RAG Pipeline (LangChain): decouples retrieval (vector DB) and generation (LLM)
- Async I/O: FastAPI + aiosqlite + async Pinecone/HTTP calls

- Client, FastAPI App, Services, Databases, External APIs
- “HTTP POST /api/v1/documents/upload”
- “Pinecone upsert/query”
- “gRPC/REST to Google Gemini”
- “SQL INSERT/SELECT”

## Diagram (Mermaid)

```mermaid
flowchart TB
    Client["Client/Frontend"]:::external

    subgraph "FastAPI Backend"
        FastAPI["main.py"]:::internal

        subgraph "API Layer"
            Documents["Documents Routes"]:::internal
            Chat["Chat Routes"]:::internal
        end

        subgraph "Service Layer"
            DocService["DocumentService"]:::internal
            PineService["PineconeService"]:::internal
            RAGService["RAGService"]:::internal
        end

        subgraph "Core"
            Config["Config"]:::internal
            DBModule["Database Module"]:::internal
        end

        subgraph "Models & Schemas"
            documentModel["Document Metadata Model"]:::internal
            chatModel["Chat History Model"]:::internal
            documentSchema["Document I/O Schema"]:::internal
            chatSchema["Chat I/O Schema"]:::internal
        end
    end

    SQLite[(SQLite DB)]:::db
    Pinecone[(Pinecone Vector Store)]:::db
    Gemini{{Google Gemini LLM}}:::external

    %% Connections
    Client -->|"HTTP POST /api/v1/documents/*"| Documents
    Client -->|"HTTP POST /api/v1/chat"| Chat

    Documents -->|"invoke"| DocService
    DocService -->|"split & embed"| PineService
    PineService -->|"Pinecone upsert"| Pinecone
    DocService -->|"SQL INSERT metadata"| DBModule
    DBModule -->|"aiosqlite"| SQLite

    Chat -->|"invoke"| RAGService
    RAGService -->|"Pinecone query"| PineService
    PineService -->|"vector retrieval"| Pinecone
    RAGService -->|"gRPC/REST to Gemini"| Gemini
    RAGService -->|"SQL INSERT chat"| DBModule
    DBModule -->|"aiosqlite"| SQLite

    %% Click Events
    click FastAPI "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/main.py"
    click Documents "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/api/routes/documents.py"
    click Chat "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/api/routes/chat.py"
    click Config "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/core/config.py"
    click DBModule "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/core/database.py"
    click documentModel "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/models/document.py"
    click chatModel "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/models/chat.py"
    click documentSchema "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/schemas/document.py"
    click chatSchema "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/schemas/chat.py"
    click DocService "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/services/document_service.py"
    click PineService "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/services/pinecone_service.py"
    click RAGService "https://github.com/kartikkalia-enkash/enkash-chatbot/blob/claude/api-doc-chatbot-01VGr3MfkQkHm9HsgtJvkaK3/app/services/rag_service.py"

    %% Styles
    classDef internal fill:#cce5ff,stroke:#004085
    classDef db fill:#d4edda,stroke:#155724
    classDef external fill:#fff3cd,stroke:#856404
```
