A Gemini-powered chatroom backend with user authentication, chat management, AI integration, and subscription support.
- Directory Structure
- Setup & Run
- Architecture Overview
- Queue System (Celery)
- Gemini API Integration
- Assumptions & Design Decisions
- API Usage & Postman Testing
- Access & Deployment
GemChatRoom/
├── alembic/ # Database migrations
│ ├── env.py
│ ├── README
│ ├── script.py.mako
│ └── versions/
│ └── fb3d0fc6ae00_init_schema.py
├── alembic.ini # Alembic config
├── app/ # Main application code
│ ├── api/ # API routers
│ │ ├── api.py
│ │ └── v1/
│ │ ├── auth.py
│ │ ├── chatroom.py
│ │ ├── health.py
│ │ ├── message.py
│ │ ├── subscription.py
│ │ ├── test.py
│ │ └── user.py
│ ├── core/ # Config, DB, Celery
│ │ ├── celery_app.py
│ │ ├── config.py
│ │ └── db.py
│ ├── models/ # SQLAlchemy models
│ │ ├── __init__.py
│ │ ├── chatroom.py
│ │ ├── message.py
│ │ └── user.py
│ ├── schemas/ # Pydantic schemas
│ │ ├── auth.py
│ │ ├── chatroom.py
│ │ ├── common.py
│ │ ├── message.py
│ │ ├── subscription.py
│ │ └── user.py
│ ├── services/ # Business logic
│ │ ├── auth.py
│ │ ├── chatroom.py
│ │ ├── gemini.py
│ │ ├── hash.py
│ │ ├── jwt.py
│ │ ├── message.py
│ │ ├── otp.py
│ │ └── subscription.py
│ ├── tasks/ # Celery tasks
│ │ ├── __init__.py
│ │ └── gemini_tasks.py
│ ├── utils/ # Utility functions
│ │ └── dependencies.py
│ └── main.py # FastAPI entrypoint
├── Procfile # For deployment (e.g., Heroku)
├── README.md
├── requirements.txt
├── script.sh
└── venv/ # Python virtual environment
Alembic/env.py Customization:
- Ensure your
alembic/env.pyincludes the following lines to load the database URL and models from your FastAPI app:from app.core.config import settings from app.core.db import Base from app.models import * config.set_main_option("sqlalchemy.url", settings.DATABASE_URL) target_metadata = Base.metadata- This ensures Alembic uses the correct database and models for migrations.
Alembic Configuration Note:
- Only
alembic/env.pywas changed to load the database URL from your.env(viaapp/core/config.py).- You do not need to manually edit
alembic.inifor the database URL; it is ignored in favor of the dynamic value.- Ensure your
.envis correctly configured before running migrations.
git clone git@github.com:SoniArpit/gemini-clone.git
cd GemChatRoom
python -m venv venv
source venv/bin/activate
pip install -r requirements.txtCreate a .env file in the root with the following (see app/core/config.py):
DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_db
DB_USER=your_user
DB_PASSWORD=your_password
REDIS_URL=redis://localhost:6379/0
CACHE_TTL_SECONDS=300
OTP_EXPIRATION_MINUTES=5
JWT_SECRET=your_jwt_secret
JWT_ALGORITHM=HS256
JWT_EXPIRATION_HOURS=24
CELERY_BROKER_URL=redis://localhost:6379/1
CELERY_RESULT_BACKEND=redis://localhost:6379/2
GOOGLE_API_KEY=your_google_api_key
STRIPE_PUBLISHABLE_KEY=your_stripe_publishable_key
STRIPE_SECRET_KEY=your_stripe_secret_key
STRIPE_PRO_PRICE_ID=your_stripe_price_id
STRIPE_WEBHOOK_SECRET=your_stripe_webhook_secret
STRIPE_SUCCESS_URL=http://localhost:8000/success
STRIPE_CANCEL_URL=http://localhost:8000/cancel
alembic revision --autogenerate -m "init schema"alembic upgrade head- API Server:
uvicorn app.main:app --reload
- Celery Worker:
celery -A app.core.celery_app.celery_app worker --loglevel=info -Q gemini
- Redis Server:
redis-server
- FastAPI: Main web framework for API endpoints.
- PostgreSQL: Stores users, chatrooms, messages, subscriptions.
- Redis: Used for OTP, caching, and as Celery broker/result backend.
- Celery: Handles background tasks (e.g., AI message generation).
- Stripe: Manages Pro subscriptions and webhooks.
- Google Gemini API: Generates AI chat responses.
app/api/v1/: API endpoints (auth, chatroom, message, subscription, user)app/models/: SQLAlchemy modelsapp/services/: Business logic (auth, chat, gemini, etc.)app/tasks/: Celery tasksapp/core/: Config, DB, Celery setup
- Setup: See
app/core/celery_app.py. - Broker/Backend: Redis (configurable via env vars).
- Task Routing: All Gemini-related tasks routed to the
geminiqueue. - Example Task:
generate_reply_taskinapp/tasks/gemini_tasks.pycalls Gemini API and saves the response.
- Usage:
- When a user sends a message, a Celery task is triggered to generate the AI reply asynchronously.
- Library: Uses
google-generativeaiPython SDK. - API Key: Set via
GOOGLE_API_KEYenv variable. - Model:
gemini-1.5-flash(seeapp/services/gemini.py). - Error Handling: Handles API errors, rate limits, safety blocks, and connection issues gracefully.
- Flow:
- User sends a message (API endpoint).
- Celery task (
generate_reply_task) callscall_gemini_api. - Gemini response is saved and returned to the user.
- Authentication: OTP-based signup via mobile number. JWT for session management.
- User Tiers:
basicandpro(Pro via Stripe subscription). - AI Safety: Handles Gemini safety blocks and errors.
- Async Processing: All AI responses are generated asynchronously via Celery.
- Payments: Stripe Checkout for Pro subscriptions; webhooks for event handling.
- Extensibility: Modular service and API structure for easy feature addition.
Postman Collection:
- You can find a ready-to-use public Postman collection for this API here: Gemini Clone API Postman Collection
Swagger/OpenAPI Documentation:
- View the live API docs here: https://gemini-clone-yiem.onrender.com/docs
POST /api/v1/auth/signup— Register with mobilePOST /api/v1/auth/send-otp— Request OTPPOST /api/v1/auth/verify-otp— Verify OTP, receive JWTPOST /api/v1/auth/forgot-password— Request OTP for password resetPOST /api/v1/auth/change-password— Change password (JWT required)
POST /api/v1/chatroom/— Create chatroom (JWT required)GET /api/v1/chatroom/— List user chatrooms (JWT required)GET /api/v1/chatroom/{chatroom_id}— Get chatroom details (JWT required)
POST /api/v1/message/chatroom/{chatroom_id}/message— Send message to chatroom (JWT required)- Triggers Gemini AI reply via Celery
POST /api/v1/subscription/subscribe/pro— Start Pro subscription (Stripe Checkout, JWT required)POST /api/v1/subscription/webhook/stripe— Stripe webhook endpoint (no auth)GET /api/v1/subscription/status— Get current subscription status (JWT required)
GET /api/v1/user/me— Get current user info (JWT required)
GET /api/v1/health— Health check
- Obtain JWT via
/auth/verify-otp. - Set
Authorization: Bearer <token>header for protected endpoints.
POST /api/v1/auth/signupwith{ "mobile": "1234567890" }POST /api/v1/auth/send-otpwith{ "mobile": "1234567890" }POST /api/v1/auth/verify-otpwith{ "mobile": "1234567890", "otp": "<received_otp>" }→ Getaccess_token- Use
access_tokenas Bearer token for chatroom/message endpoints