# eightrocks/chatbot architecture

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

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

## Overview

##### Project type and purpose
- This is a lightweight full-stack chatbot application.
- Frontend: Single static page (`index.html`) served to the user.
- Backend: FastAPI app in `chatbot.py` exposing HTTP endpoints.
- External systems: Supabase (PostgreSQL-based) for retrieving Reddit data, and OpenAI API for generating responses.

##### File structure
- Top level:
- `chatbot.py` – FastAPI app, route definitions, client initializations.
- `index.html` – simple user interface for chat.
- `requirements.txt` – Python dependencies (FastAPI, uvicorn, supabase, openai, python-dotenv).
- `.gitignore`, `.cursor/` – ancillary.

##### Key components
a. Client (Browser)
– Loads `index.html`.
– Sends user messages via HTTP (fetch/XHR) to the backend.
b. FastAPI Server (`chatbot.py` + uvicorn)
– HTTP REST endpoint(s) (e.g., POST /chat).
– Reads environment variables (`.env`) for credentials.
– Orchestrates calls to Supabase and OpenAI.
c. Supabase
– Houses Reddit posts or other data.
– FastAPI uses the Supabase client library to query.
d. OpenAI API
– Receives prompts (combining user input + context) from FastAPI.
– Returns generated text.
e. (Optional) Environment/config layer
– `.env` for SUPABASE_URL/KEY and OPENAI_API_KEY.

##### Relationships and interactions (data flow)
1. User opens `index.html` in browser.
2. User submits a message → Browser sends HTTP POST to FastAPI (`/chat`).
3. FastAPI endpoint handler:
a. Reads message.
b. Queries Supabase for relevant Reddit context.
c. Constructs prompt and calls OpenAI API.
d. Receives AI response.
e. Returns JSON response to browser.
4. Browser displays the chatbot’s reply.

##### Architectural patterns and design principles
- Separation of concerns: UI, API layer, data store, AI service.
- Single process monolith (no microservices), but clear logical layers.
- Environment-based configuration for secrets.

##### Final advice
- Don’t over-fragment: only separate components that have distinct responsibilities.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Tier
    subgraph "Client Tier"
        direction TB
        Browser["Browser UI\nindex.html"]:::frontend
    end

    %% Server Tier
    subgraph "Server Tier"
        direction TB
        API["FastAPI Server\n(chatbot.py)"]:::backend
        Config[".env\nConfiguration"]:::config
        Req["requirements.txt\nDependencies"]:::config
        Ig[".gitignore"]:::config
        Readme["README.md"]:::config
    end

    %% External Services
    subgraph "External Services"
        direction TB
        Supabase["Supabase (PostgreSQL)"]:::service
        OpenAI["OpenAI API"]:::service
    end

    %% Data Flow
    Browser -->|"HTTP POST /chat (REST)"| API
    API -->|"REST (Supabase client)"| Supabase
    Supabase -.->|"JSON Data"| API
    API -->|"HTTPS"| OpenAI
    OpenAI -.->|"Completion Response"| API
    API -.->|"JSON Response"| Browser

    %% Click Events
    click Browser "https://github.com/eightrocks/chatbot/blob/main/index.html"
    click API "https://github.com/eightrocks/chatbot/blob/main/chatbot.py"
    click Req "https://github.com/eightrocks/chatbot/blob/main/requirements.txt"
    click Config "https://github.com/eightrocks/chatbot/blob/main/.env"
    click Ig "https://github.com/eightrocks/chatbot/blob/main/.gitignore"
    click Readme "https://github.com/eightrocks/chatbot/blob/main/README.md"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#006064,color:#006064
    classDef backend fill:#E3F2FD,stroke:#0D47A1,color:#0D47A1
    classDef service fill:#E8F5E9,stroke:#1B5E20,color:#1B5E20
    classDef config fill:#F5F5F5,stroke:#9E9E9E,color:#424242
```
