# lect1val/ai-chatbot-backend architecture

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

- Interactive diagram: https://gitdiagram.com/lect1val/ai-chatbot-backend
- Repository: https://github.com/lect1val/ai-chatbot-backend

## Overview

##### Project Type and Purpose
- This repo actually contains two separate but conceptually identical microservices, each exposing a simple REST API that accepts a user message + session ID, forwards it to Dialogflow, enriches or processes the reply via OpenAI, then returns the combined response.
- One implementation is written in Go using the Gin framework (`go_chatbot/`), the other is written in Python using Django (`python_chatbot/chatbot_project/`). Both are stateless HTTP backends, with the Python version optionally persisting session/context data in a database (SQLite by default).

##### File‐Structure Analysis
- go_chatbot/
- `main.go` – Bootstraps a Gin router, registers HTTP handlers under `/dialogflow/session/`, loads ENV config for Dialogflow/OpenAI credentials.
- `go.mod`/`go.sum` – Dependency declarations.
- python_chatbot/chatbot_project/
- `manage.py`, `settings.py`, `urls.py`, `wsgi.py`, `asgi.py` – Standard Django project scaffold.
- `dialogflow_integration/` app – Contains `views.py` (HTTP endpoint), `models.py` (optional session persistence), `urls.py` (route registration), `admin.py`, `migrations/`.
- `requirements.txt` – Third-party libs for Django, google‐cloud‐dialogflow, openai, etc.

##### Key Architectural Patterns
- Layered/Onion:
- Controllers/Views (Gin handlers or Django views)
- Service/Integration layer (adapters for Dialogflow & OpenAI)
- (Optional) Persistence layer (models + Django ORM in Python version)
- External-API Integration: both services rely on two cloud APIs (Dialogflow & OpenAI).
- 12-Factor Config: credentials and project IDs are injected via environment variables (`.env`).

##### Main Components
a. Client (curl, Web UI, mobile app)
b. API Gateway / Web Server
– Go Service (Gin)
– Python Service (Django)
c. Business Logic / Integration Layer
– Dialogflow Adapter
– OpenAI Adapter
d. Database (only for Python version; e.g., SQLite/PostgreSQL)
e. External Services (Google Dialogflow Cloud, OpenAI API)
f. Configuration / Secret Store (ENV/.env or Vault)

##### Component Interactions & Data Flow
1. Client → HTTP POST /dialogflow/session/ (JSON: `{ text, session_id }`)
2. Web Server loads credentials from ENV
3. Integration layer:
a. Call Dialogflow Sessions API → parse intent/fulfillment
b. Optionally store/retrieve session context in DB (Python only)
c. Forward dialog or enriched prompt to OpenAI → get completion
4. Aggregate and return JSON response to client

##### Capturing Essential Details
- Which libraries/frameworks are used (Gin vs Django ORM).
- Use of service-account JSON for Dialogflow and the OpenAI API key.
Layer 1: Client Layer
Layer 2: HTTP/API Layer (controllers)
Layer 3: Service/Integration Layer (adapters)
Layer 4: Data Layer (DB) and External Layer (Cloud APIs)

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client\n(curl/Web UI)"]:::external
    Config["Config/env Variables"]:::internal
    Dialogflow["Google Dialogflow Cloud"]:::external
    OpenAI["OpenAI API"]:::external
    DB["SQLite/PostgreSQL"]:::database

    subgraph "go_chatbot Service"
        GIN["Gin HTTP Handler\n(main.go)\nEndpoint: /dialogflow/session/"]:::internal
        GO_MOD["go.mod"]:::internal
        GO_SUM["go.sum"]:::internal
        subgraph "Integration Layer"
            DFG["Dialogflow Adapter"]:::internal
            OAG["OpenAI Adapter"]:::internal
        end
        GIN --> DFG
        GIN --> OAG
        click GIN "https://github.com/lect1val/ai-chatbot-backend/blob/main/go_chatbot/main.go"
        click GO_MOD "https://github.com/lect1val/ai-chatbot-backend/blob/main/go_chatbot/go.mod"
        click GO_SUM "https://github.com/lect1val/ai-chatbot-backend/blob/main/go_chatbot/go.sum"
    end

    subgraph "python_chatbot Service"
        MGPY["manage.py"]:::internal
        SETT["settings.py"]:::internal
        WSGI["wsgi.py"]:::internal
        ASGI["asgi.py"]:::internal
        REQ["requirements.txt"]:::internal
        subgraph "App Configuration"
            MGPY --> SETT
            click MGPY "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/manage.py"
            click SETT "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/chatbot_project/settings.py"
            click WSGI "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/chatbot_project/wsgi.py"
            click ASGI "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/chatbot_project/asgi.py"
            click REQ "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/requirements.txt"
        end
        subgraph "Integration Layer"
            PY_URLS["urls.py"]:::internal
            PY_VIEW["views.py\n(Endpoint: /dialogflow/session/)"]:::internal
            APPS["apps.py"]:::internal
            PY_DF["Dialogflow Adapter"]:::internal
            PY_OA["OpenAI Adapter"]:::internal
            click PY_URLS "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/dialogflow_integration/urls.py"
            click PY_VIEW "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/dialogflow_integration/views.py"
            click APPS "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/dialogflow_integration/apps.py"
        end
        subgraph "Persistence Layer"
            MODELS["models.py"]:::internal
            MIG["migrations/"]:::internal
            ADMIN["admin.py"]:::internal
            click MODELS "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/dialogflow_integration/models.py"
            click MIG "https://github.com/lect1val/ai-chatbot-backend/tree/main/python_chatbot/chatbot_project/dialogflow_integration/migrations/"
            click ADMIN "https://github.com/lect1val/ai-chatbot-backend/blob/main/python_chatbot/chatbot_project/dialogflow_integration/admin.py"
        end
        PY_URLS --> PY_VIEW
        PY_VIEW --> PY_DF
        PY_VIEW --> PY_OA
        PY_VIEW --> MODELS
        MODELS --> MIG
        PY_VIEW --> ADMIN
    end

    Client --> GIN
    Client --> PY_VIEW
    Config --> GIN
    Config --> PY_VIEW
    DFG --> Dialogflow
    PY_DF --> Dialogflow
    OAG --> OpenAI
    PY_OA --> OpenAI
    PY_VIEW --> DB

    classDef internal fill:#ADD8E6,stroke:#0000FF
    classDef external fill:#FFA500,stroke:#CC5500
    classDef database fill:#90EE90,stroke:#008000
```
