# pushkrjain/ai_marketing_api architecture

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

- Interactive diagram: https://gitdiagram.com/pushkrjain/ai_marketing_api
- Repository: https://github.com/pushkrjain/ai_marketing_api

## Overview

##### Project Type and Purpose
- This is a containerized, back-end API service (FastAPI) implementing a modular multi-agent architecture for generating personalized marketing content in real time.
- Key goals: expose HTTP endpoints, orchestrate Segmentation/Generation/Optimization agents, persist feedback, log activities, and emit metrics for Prometheus/Grafana.

##### File Structure
- Top-level files & directories:
– app.py: FastAPI application entrypoint
– agent/: segmentation.py, generation.py, optimization.py, orchestrator.py
– auth/: JWT authentication logic
– db/: feedback persistence
– hf_models/: local Hugging Face model storage
– monitoring/: Prometheus instrumentation code
– mylogging/: structured error & research logging
– prometheus.yml & prometheus-2.53.0…/: standalone Prometheus distribution
– Dockerfile & docker-compose.yml: container build & orchestration
– tests/: pytest integration & unit tests
- Patterns: modular agent packages, clear separation of concerns (auth, logging, monitoring, persistence).

##### README Insights
- Architecture sketch included: User Profile → Segmentation → Generation → (Feedback & Stats) → Optimization → Re-generation.
- Technical stack: FastAPI, Hugging Face Transformers, Chroma/FAISS, Prometheus, Grafana, Docker Compose.
- Monitoring: /metrics endpoint instrumented via prometheus-fastapi-instrumentator.
- Logging: Python logging to console & files (error.log, research.log).

##### Main Components
a. Client Layer
– “API Consumer” (curl, HTTPie, SDK)
b. API Gateway / Orchestrator
– FastAPI app (app.py)
– Auth module (auth/auth.py)
– Orchestration logic (agent/orchestrator.py)
c. Agent Layer
– Segmentation Agent (agent/segmentation.py) → Chroma/FAISS vector store
– Generation Agent (agent/generation.py) → local HF model (hf_models/)
– Optimization Agent (agent/optimization.py) → feedback from DB
d. Persistence & Storage
– Feedback DB (db/feedback.py; could be SQLite or JSON store)
– Vector store (external Chroma/FAISS service)
– Model store (local HF directories)
e. Cross-cutting Concerns
– Logging subsystem (mylogging/error_logger.py, research_logger.py)
– Monitoring subsystem (monitoring/metrics.py) → /metrics endpoint → Prometheus
f. Infrastructure Orchestration
– Docker Compose
· fastapi-app container
· prometheus container (prometheus.yml)
· optional grafana container
· other dependencies (vector store, DB)

##### Architectural Patterns & Design Principles
- Modular multi-agent pipeline—each agent is a reusable Python module.
- Separation of concerns—distinct layers for API, agents, persistence, logging, and monitoring.
- Containerization—isolated services orchestrated via Docker Compose.
- Observability—structured logs + metrics exposed for Prometheus/Grafana.
- Extensibility—new agents or model versions can be plugged in by adding modules under agent/ or hf_models/.

##### Final Checklist
[ ] All major components (API, Auth, Agents, DB, Stores, Observability) are represented.
[ ] Data flows are directional and labeled.
[ ] External dependencies and containers are drawn distinctly.
[ ] Architectural patterns (multi-agent, feedback loop) are clearly visible.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "docker-compose"
        direction TB
        subgraph "fastapi-app container"
            direction TB
            subgraph "API Layer"
                direction TB
                API["FastAPI App"]:::core
                Auth["Auth Module"]:::core
            end
            subgraph "Agent Layer"
                direction TB
                Orch["Orchestrator Service"]:::core
                Seg["Segmentation Agent"]:::core
                Gen["Generation Agent"]:::core
                Opt["Optimization Agent"]:::core
            end
            subgraph "Persistence & Monitoring"
                direction TB
                Metrics["/metrics Endpoint"]:::infra
                ErrorLog["Error Logger"]:::infra
                ResearchLog["Research Logger"]:::infra
            end
        end
        Prom["Prometheus"]:::infra
        Graf["Grafana"]:::infra
    end

    Client["API Consumer"]:::external
    DB["Feedback DB"]:::storage
    Vector["Vector Store (Chroma/FAISS)"]:::storage
    HFStore["HF Model Store"]:::storage
    DockerfileNode["Dockerfile"]:::infra
    Compose["docker-compose.yml"]:::infra
    PromConf["prometheus.yml"]:::infra
    PromDist["prometheus-2.53.0.linux-amd64/"]:::infra
    Tests["tests/"]:::infra

    Client -->|POST /generate-content| API
    API -->|authenticate| Auth
    Auth -->|invoke| Orch
    Orch -->|customer_profile| Seg
    Seg -->|vector_query| Vector
    Vector -->|segments| Seg
    Seg -->|segments| Orch
    Orch -->|invoke| Gen
    Gen -->|model_inference| HFStore
    HFStore -->|content| Gen
    Gen -->|generated_content| Orch
    Orch -->|log_metrics| Metrics
    Orch -->|log_error| ErrorLog
    Orch -->|log_research| ResearchLog
    Client -->|POST /feedback| API
    API -->|store_feedback| DB
    Orch -->|fetch_feedback| DB
    DB -->|feedback_data| Opt
    Opt -->|optimized_prompt| Orch
    Prom -->|scrape /metrics| Metrics
    Graf -->|visualize| Prom

    click API "https://github.com/pushkrjain/ai_marketing_api/blob/main/app.py"
    click Auth "https://github.com/pushkrjain/ai_marketing_api/blob/main/auth/auth.py"
    click Orch "https://github.com/pushkrjain/ai_marketing_api/blob/main/agent/orchestrator.py"
    click Seg "https://github.com/pushkrjain/ai_marketing_api/blob/main/agent/segmentation.py"
    click Gen "https://github.com/pushkrjain/ai_marketing_api/blob/main/agent/generation.py"
    click Opt "https://github.com/pushkrjain/ai_marketing_api/blob/main/agent/optimization.py"
    click DB "https://github.com/pushkrjain/ai_marketing_api/blob/main/db/feedback.py"
    click HFStore "https://github.com/pushkrjain/ai_marketing_api/tree/main/hf_models/phi3/"
    click Metrics "https://github.com/pushkrjain/ai_marketing_api/blob/main/monitoring/metrics.py"
    click ErrorLog "https://github.com/pushkrjain/ai_marketing_api/blob/main/mylogging/error_logger.py"
    click ResearchLog "https://github.com/pushkrjain/ai_marketing_api/blob/main/mylogging/research_logger.py"
    click DockerfileNode "https://github.com/pushkrjain/ai_marketing_api/tree/main/Dockerfile"
    click Compose "https://github.com/pushkrjain/ai_marketing_api/blob/main/docker-compose.yml"
    click PromConf "https://github.com/pushkrjain/ai_marketing_api/blob/main/prometheus.yml"
    click PromDist "https://github.com/pushkrjain/ai_marketing_api/tree/main/prometheus-2.53.0.linux-amd64/"
    click Tests "https://github.com/pushkrjain/ai_marketing_api/tree/main/tests/"

    classDef core fill:#AED6F1,stroke:#1F618D,color:#1B2631
    classDef storage fill:#A9DFBF,stroke:#196F3D,color:#1B2631,shape:cylinder
    classDef infra fill:#F5B041,stroke:#B9770E,color:#1B2631
    classDef external fill:#D5D8DC,stroke:#566573,color:#1B2631
```
