# kea-renov8/befly-ai architecture

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

- Interactive diagram: https://gitdiagram.com/kea-renov8/befly-ai
- Repository: https://github.com/kea-renov8/befly-ai

## Overview

##### Project type and purpose
- This is a backend microservice/API for an AI-driven chat platform (“Befly-AI”).
- It exposes HTTP endpoints (in app/api/routes/chat.py) to accept chat requests, invokes AI prompts (in app/prompt), persists chat data in MongoDB (app/database), and returns responses.

##### File structure
- Top-level files: DockerFile, requirements.txt, GitHub workflows → containerized, CI/CD.
- app/main.py: application bootstrap (probably Flask or FastAPI).
- app/api/routes: HTTP route definitions (API layer).
- app/core: configuration, exception handling, shared utilities.
- app/service: business logic (chat_service).
- app/repository: data‐access abstraction (chat_repository).
- app/database: MongoDB connection wrappers (CustomMongoChat, MongoDB).
- app/models: Pydantic/ORM models for chat/messages.
- app/prompt: AI‐prompt generation modules (counselorAI, evaluatorAI).
- app/utils: session management or other helpers.

##### README
- Minimal content (“Befly-AI”) implies focus on the service itself; no client or front-end is included.

a. Main components
- API Layer (app/api/routes)
- Core/Config Layer (app/core)
- Service Layer (app/service)
- Repository/Data-Access Layer (app/repository)
- Database (MongoDB, wrappers in app/database)
- Prompt Engine (app/prompt modules calling external AI APIs)
- External AI Services (e.g., OpenAI, custom LLM endpoints)
- Docker Container & CI/CD (DockerFile, GitHub workflows)
b. Relationships & interactions
1. Client → HTTP request → API Layer
2. API Layer delegates to Service Layer
3. Service Layer:
– Calls Prompt Engine to build/submit AI prompts
– Calls Repository Layer to read/write chat history
4. Repository Layer → MongoDB (app/database wrappers)
5. Prompt Engine → External AI Service → returns response
6. Service Layer composes final response → API Layer → Client
7. Docker/CI pipeline builds and deploys the service container
c. Architectural patterns & principles
- Layered architecture (API → Service → Repository → Database)
- Dependency inversion: high-level modules (service) depend on abstractions (repository interfaces).
- Single responsibility: separate prompt creation, business logic, data access.
- Configuration management centralized in app/core/config.py.
d. Technologies & frameworks
- Python web framework (FastAPI or Flask)
- Pydantic models (app/models)
- MongoDB, custom wrappers in app/database
- AI prompt modules interfacing with external LLM services
- Docker for containerization, GitHub Actions for CI/CD

##### Diagramming recommendations
- Use rectangles for each major layer/component.
- Client → API → Service → Repository → MongoDB
- Service → Prompt Engine → External AI → Prompt Engine → Service
- Service → API → Client
- Blue for internal code layers (API, Service, Repository)
- Green for data stores (MongoDB)
- Orange for external services (AI providers)
- Gray for infrastructure (Docker, CI/CD)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Actors
    Client["Client"]:::external

    %% Bootstrap & Core
    subgraph "Bootstrap & Core"
        Main["app/main.py"]:::internal
        Config["config.py"]:::internal
        Exceptions["exceptions.py"]:::internal
        Handlers["handlers.py"]:::internal
    end

    %% API Layer
    subgraph "API Layer"
        API["chat.py"]:::internal
    end

    %% Service Layer
    subgraph "Service Layer"
        Service["chat_service.py"]:::internal
    end

    %% Repository Layer
    subgraph "Repository Layer"
        Repo["chat_repository.py"]:::internal
    end

    %% Database Layer
    subgraph "Database Layer"
        CustomMongo["CustomMongoChat.py"]:::internal
        MongoWrapper["MongoDB.py"]:::internal
        MongoDB["MongoDB"]:::data
    end

    %% Data Models
    subgraph "Data Models"
        BaseModel["base.py"]:::internal
        ChatModel["chat.py"]:::internal
    end

    %% Prompt Engine
    subgraph "Prompt Engine"
        Counselor["counselorAI.py"]:::internal
        Evaluator["evaulatorAI.py"]:::internal
    end

    %% Utilities
    subgraph "Utilities"
        Session["session.py"]:::internal
    end

    %% External Services
    ExternalAI["External AI Services"]:::external

    %% Infrastructure
    subgraph "Infrastructure"
        Docker["DockerFile"]:::infra
        Workflow1["complete-jira-issue.yml"]:::infra
        Workflow2["create-jira-issue.yml"]:::infra
    end

    %% Connections
    Client -->|"HTTP Request"| API
    API -->|"delegates to"| Service
    Service -->|"calls"| Repo
    Service -->|"uses"| Counselor
    Service -->|"uses"| Evaluator
    Repo -->|"reads/writes"| CustomMongo
    Repo -->|"reads/writes"| MongoWrapper
    CustomMongo -->|"connects to"| MongoDB
    MongoWrapper -->|"connects to"| MongoDB
    Counselor -->|"prompts"| ExternalAI
    Evaluator -->|"prompts"| ExternalAI
    ExternalAI -->|"response"| Counselor
    ExternalAI -->|"response"| Evaluator
    Service -->|"response"| API
    API -->|"HTTP Response"| Client

    Main -->|"bootstraps"| API
    Main -->|"loads"| Config
    Main -->|"hooks"| Exceptions
    Main -->|"hooks"| Handlers

    Config --> API
    Config --> Service
    Exceptions --> API
    Exceptions --> Service
    Handlers --> API
    Handlers --> Service
    Session --> API
    Session --> Service

    %% Click Events
    click Main "https://github.com/kea-renov8/befly-ai/blob/main/app/main.py"
    click Config "https://github.com/kea-renov8/befly-ai/blob/main/app/core/config.py"
    click Exceptions "https://github.com/kea-renov8/befly-ai/blob/main/app/core/exceptions.py"
    click Handlers "https://github.com/kea-renov8/befly-ai/blob/main/app/core/handlers.py"
    click API "https://github.com/kea-renov8/befly-ai/blob/main/app/api/routes/chat.py"
    click Service "https://github.com/kea-renov8/befly-ai/blob/main/app/service/chat_service.py"
    click Repo "https://github.com/kea-renov8/befly-ai/blob/main/app/repository/chat_repository.py"
    click CustomMongo "https://github.com/kea-renov8/befly-ai/blob/main/app/database/CustomMongoChat.py"
    click MongoWrapper "https://github.com/kea-renov8/befly-ai/blob/main/app/database/MongoDB.py"
    click BaseModel "https://github.com/kea-renov8/befly-ai/blob/main/app/models/base.py"
    click ChatModel "https://github.com/kea-renov8/befly-ai/blob/main/app/models/chat.py"
    click Counselor "https://github.com/kea-renov8/befly-ai/blob/main/app/prompt/counselorAI.py"
    click Evaluator "https://github.com/kea-renov8/befly-ai/blob/main/app/prompt/evaulatorAI.py"
    click Session "https://github.com/kea-renov8/befly-ai/blob/main/app/utils/session.py"
    click Docker "https://github.com/kea-renov8/befly-ai/tree/main/DockerFile"
    click Workflow1 "https://github.com/kea-renov8/befly-ai/blob/main/.github/workflows/complete-jira-issue.yml"
    click Workflow2 "https://github.com/kea-renov8/befly-ai/blob/main/.github/workflows/create-jira-issue.yml"

    %% Styles
    classDef internal fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef data fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef external fill:#ffe0b3,stroke:#333,stroke-width:1px
    classDef infra fill:#e0e0e0,stroke:#333,stroke-width:1px
```
