A modular, configuration-driven chatbot backend built with Flask and Google Gemini API. Designed for hackathon adaptability with pluggable prompts and data contexts.
- Modular Architecture: Separated concerns (config, service, brain, API)
- Dynamic Prompt Injection: Change system prompts at runtime without redeployment
- Conversation Memory: Maintains session-based conversation history
- Flexible Configuration: YAML + Environment variables
- RESTful API: Clean JSON endpoints for integration
- Error Handling: Robust handling of API errors and rate limits
chatbot/
βββ app/
β βββ __init__.py # Package initializer
β βββ config.py # Configuration management
β βββ service.py # Gemini API wrapper
β βββ brain.py # Conversation & context logic
β βββ main.py # Flask application
βββ config.yaml # Default prompts & settings
βββ .env.example # Environment variables template
βββ requirements.txt # Python dependencies
βββ run.py # Application entry point
βββ README.md # This file
- Python 3.10+
- Google Gemini API Key (Get one here)
# Clone or navigate to project directory
cd chatbot
# Install dependencies
pip install -r requirements.txt
# Copy environment template
copy .env.example .env
# Edit .env and add your API key
notepad .envEdit .env file:
GEMINI_API_KEY=your_actual_api_key_here
FLASK_PORT=5000Edit config.yaml to customize default prompts:
system_prompt: |
You are a helpful AI assistant...Note: All code is currently commented out. Uncomment the code in all files to run.
# Run the chatbot server
python run.pyServer will start at http://localhost:5000
GET /healthPOST /chat
Content-Type: application/json
{
"message": "Hello, how are you?",
"session_id": "user123",
"include_history": true
}Response:
{
"success": true,
"response": "I'm doing well, thank you!",
"session_id": "user123",
"model": "gemini-pro",
"usage": {
"prompt_tokens": 45,
"completion_tokens": 12
}
}POST /config
Content-Type: application/json
{
"system_prompt": "You are a pirate assistant. Speak like a pirate!",
"temperature": 0.9
}DELETE /session/user123GET /sessions- In
brain.py, modifybuild_prompt():
# Add context injection
if session.context_data:
prompt_parts.append(f"CONTEXT DATA:\n{session.context_data}")- Set context via API:
session.set_context('user_profile', {'name': 'John', 'preferences': 'tech'})# During hackathon, update prompt based on problem statement
curl -X POST http://localhost:5000/config \
-H "Content-Type: application/json" \
-d '{"system_prompt": "You are a legal advisor assistant..."}'- Add data loader in
brain.py:
def load_data_context(self, data_file: str):
with open(data_file) as f:
data = json.load(f)
return data- Use in prompt building
# Test with curl
curl -X POST http://localhost:5000/chat \
-H "Content-Type: application/json" \
-d '{"message": "What can you do?"}'The system handles:
- Invalid API keys
- Rate limiting (automatic retry logic can be added)
- Malformed requests
- Network errors
| Variable | Description | Default |
|---|---|---|
GEMINI_API_KEY |
Google Gemini API key | Required |
FLASK_PORT |
Server port | 5000 |
GEMINI_MODEL |
Model name | gemini-pro |
TEMPERATURE |
Model temperature | 0.7 |
system_prompt: Default AI persona and instructionsmodel: Model parameters (temperature, tokens, etc.)conversation: History settings
- Setup (5 min): Install, configure API key
- Test (5 min): Verify basic chat works
- Problem Statement Released:
- Update
system_promptvia/configendpoint - Add domain data to context
- Test with sample queries
- Update
- Demo: Show API calls and responses
For production/demo:
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app.main:appThis is a hackathon template. Feel free to modify any module to fit your use case.
MIT License - Use freely for hackathons and projects.
Built with β€οΈ for hackathon speed and flexibility