This project implements a backend service for an AI-powered chatbot system. It allows users to authenticate, create chat sessions, interact with an AI assistant, and store conversation history.
The backend is built using FastAPI and MongoDB, following a modular and scalable architecture.
- User authentication using JWT
- Chat session management
- AI-powered chat responses
- Persistent message storage
- Modular and scalable backend design
- Interactive API documentation with Swagger
- Backend Framework: FastAPI
- Database: MongoDB
- Async Driver: Motor
- Authentication: JWT (JSON Web Tokens)
- Password Hashing: Passlib (bcrypt)
- Validation: Pydantic
- AI Integration: Google Gemini API (optional)
- Documentation: Swagger / OpenAPI
Client (Swagger / Frontend)
│
▼
API Routes (FastAPI)
│
▼
Service Layer (Business Logic)
│
▼
Database Layer (MongoDB)
- API Layer: Handles HTTP requests and responses
- Service Layer: Contains business logic
- Database Layer: Manages data persistence
- Schemas: Validate request and response data
- Core: Handles configuration and security
- Middleware: Logging and error handling
app/
├── main.py
├── core/
│ ├── config.py
│ ├── security.py
│
├── api/
│ └── routes/
│ ├── auth.py
│ ├── chat.py
│ └── sessions.py
│
├── models/
│ ├── user.py
│ ├── session.py
│ └── message.py
│
├── schemas/
│ ├── user.py
│ └── chat.py
│
├── services/
│ ├── auth_service.py
│ ├── chat_service.py
│ └── llm_service.py
│
├── db/
│ └── mongodb.py
│
├── middleware/
│ ├── logging.py
│ └── error_handler.py
Initializes FastAPI application, registers routes, and loads middleware.
Manages environment variables such as database connection, JWT secrets, and API keys.
Handles JWT authentication and extracts user information from tokens.
Provides authentication endpoints:
- POST /auth/signup
- POST /auth/login
Handles chat session creation:
- POST /sessions
Handles chatbot interaction:
- POST /chat/{session_id}
- GET /sessions/{session_id}
Defines data structures for users, sessions, and messages.
Defines request and response validation using Pydantic.
Contains business logic:
- auth_service.py → authentication logic
- chat_service.py → chat handling
- llm_service.py → AI response generation
Initializes MongoDB connection and collections.
Handles logging and error management.
Swagger UI is available at:
http://127.0.0.1:8000/docs
It provides:
- Interactive API testing
- Request/response schemas
- Authorization support
POST /auth/signup
Request:
{
"email": "user@example.com",
"password": "password123"
}
POST /auth/login
Response:
{
"access_token": "JWT_TOKEN"
}
POST /sessions
Response:
{
"session_id": "session_id"
}
POST /chat/{session_id}
Request:
{
"message": "Hello AI"
}
Response:
{
"reply": "AI response"
}
GET /sessions/{session_id}
Response:
{
"session_id": "...",
"messages": [...]
}
{
"_id": ObjectId,
"email": "string",
"password_hash": "string"
}
{
"_id": ObjectId,
"user_id": "string",
"created_at": "timestamp"
}
{
"_id": ObjectId,
"session_id": "string",
"role": "user | assistant",
"content": "string",
"timestamp": "timestamp"
}
git clone <repository-url>
cd ai-chatbot-backend
python -m venv venv
Activate (Windows):
venv\Scripts\activate
pip install -r requirements.txt
Create a .env file:
MONGO_URL=mongodb://localhost:27017
DATABASE_NAME=ai_chatbot_db
JWT_SECRET=your_secure_secret
JWT_ALGORITHM=HS256
LLM_PROVIDER=mock
GEMINI_API_KEY=
uvicorn app.main:app --reload
Visit:
http://127.0.0.1:8000/docs
- Each user can create multiple chat sessions
- Each session represents one conversation
- Messages are stored separately and linked via session_id
- JWT is used for authentication
- Passwords are hashed using bcrypt
- MongoDB is used for flexible schema handling
- AI provider can be switched between mock and real
Example:
User: What is Python?
AI: Python is a programming language.
User: Who created it?
With context:
AI: Python was created by Guido van Rossum.
- Streaming responses
- Rate limiting
- Chat summarization
- Multi-user chat support
- AI moderation system
This project demonstrates a scalable and modular backend system for AI-powered chat applications. It follows best practices in API design, authentication, and database handling, making it suitable for real-world applications and further extension.