How kea-renov8/befly-ai works
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)