Project Overview & Problem Statement
Challenge: Traditional booking systems require extensive development for business logic, validation rules, and edge cases. They're rigid, difficult to modify, and can't adapt to natural language requests or complex business rules without significant coding effort.
Solution: This project implements an agentic AI workflow where the LLM (Google Gemini 2.5 Flash) generates and executes Python code dynamically. Instead of hard-coded business logic, the AI agent interprets user requests, writes the necessary code, and executes it to perform bookings, cancellations, and queries - all while enforcing complex pricing rules and conflict detection.
Key Benefits
- 24/7 Autonomous Operation: AI agent handles all booking requests without human intervention
- Natural Language Interface: Users can make requests in plain English or Malay
- Dynamic Pricing: Four-tier time-based pricing (daytime/nighttime, weekday/weekend)
- Intelligent Conflict Detection: Automatically prevents double bookings and validates Malaysian IC numbers
- Code-as-Action Pattern: LLM generates executable Python code for each request, enabling complex business logic
Technical Architecture & Implementation
Backend Architecture
Python 3.8+
FastAPI Framework
Google Gemini 2.5 Flash
TinyDB (JSON Database)
Async/Await
AI & Agentic Technologies
M5 Agentic Workflow
Code-as-Action Pattern
Dynamic Code Generation
Natural Language Processing
Context-Aware AI
Deployment & Infrastructure
Google Cloud Run
Docker Containers
CI/CD Pipelines
Auto Scaling
Persistent Storage
System Architecture
Agentic AI Workflow:
- FastAPI backend receives natural language booking requests
- Gemini 2.5 Flash analyzes intent and generates Python code
- Generated code interacts with TinyDB for CRUD operations
- Business logic (pricing, conflicts, validation) embedded in generated code
- Results returned as natural language responses
API Endpoints & Usage Examples
Core API Endpoints
POST /book
- Books a badminton court with automatic conflict detection
- Calculates pricing based on time and day
- Validates Malaysian IC numbers
POST /cancel
- Cancels existing booking by IC number and court
- Refund processing and availability update
GET /availability
- Checks court availability for specific date/time
- Returns available courts and pricing
GET /bookings
- Lists all bookings with filters (date, court, IC)
- Supports pagination and sorting
Example Natural Language Requests
"Book court 5 for tomorrow at 7 PM for 2 hours. IC: 850715-08-1234"
"Cancel my booking for court 3 on 2024-12-25"
"What courts are available this Saturday evening?"
"Show me all bookings for IC number 920315-10-5678"
"I want to book court 1 from 8am to 10am on Monday"
Code-as-Action Example
When a user says "Book court 5 tomorrow at 7 PM", the LLM generates code like:
from datetime import datetime, timedelta
from tinydb import TinyDB, Query
# Calculate booking details
booking_date = (datetime.now() + timedelta(days=1)).date()
start_time = datetime.combine(booking_date, datetime.strptime("19:00", "%H:%M").time())
end_time = start_time + timedelta(hours=2)
# Determine pricing
is_weekend = booking_date.weekday() >= 5
is_nighttime = start_time.hour >= 18 or start_time.hour < 6
rate = 120 if (is_weekend and is_nighttime) else 100 if is_nighttime else 90 if is_weekend else 80
# Check conflicts and create booking
# ... (conflict detection code)
# ... (insert into database)
Development Setup & Installation Guide
Prerequisites
- Python 3.8+ with pip package manager
- Google Gemini API Key (Gemini 2.5 Flash)
- Virtual Environment for dependency isolation
- Development Tools: VS Code with Python extensions
Quick Start Installation
# Clone the repository
git clone https://github.com/lyven81/ai-project.git
cd ai-project/projects/badminton-booking-agent
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
cp .env.example .env
# Add your Gemini API key to .env
# Run the application
uvicorn main:app --reload --port 8000
Environment Configuration
# Required API Configuration
GEMINI_API_KEY=your_gemini_api_key_here
# Application Settings
PORT=8000
DATABASE_PATH=./data/bookings.json
MAX_COURTS=12
DEBUG_MODE=false
Development Workflow
- Local Development: FastAPI auto-reload for rapid iteration
- Testing: Comprehensive test suite with mock bookings
- Code Quality: Black formatting and pylint validation
- Documentation: Auto-generated API docs at /docs
Deployment Options & Production Configuration
Google Cloud Run Deployment (Recommended)
# Build and deploy using Cloud Build
gcloud builds submit --config cloudbuild.yaml
# Direct deployment
gcloud run deploy badminton-booking-agent \
--image gcr.io/PROJECT-ID/badminton-booking-agent \
--platform managed \
--region us-west1 \
--set-env-vars GEMINI_API_KEY=your_api_key \
--allow-unauthenticated
Alternative Deployment Methods
- Docker Containers: Containerized deployment for any cloud provider
- AWS Lambda: Serverless deployment with API Gateway
- Heroku: Simple deployment with Procfile configuration
- Railway/Render: One-click deployment platforms
Production Optimizations
- Performance: Async processing for concurrent requests
- Security: Code sandbox execution, input validation, rate limiting
- Monitoring: Application logs and performance metrics
- Scalability: Stateless design enables horizontal scaling
- Persistence: TinyDB with file-based storage, supports backup/restore