How lect1val/ai-chatbot-backend works
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)