A Flask-based REST API for a CAG (Context-Aware Generation) chatbot system.
- RESTful API endpoints for chat interactions
- Chat history management
- User session handling
- Configurable CAG integration
- CORS support for frontend integration
- Comprehensive error handling and logging
.
├── app.py # Main Flask application
├── config.py # Configuration management
├── cag_service.py # CAG chatbot service layer
├── requirements.txt # Python dependencies
└── README.md # This file
pip install -r requirements.txtCreate a .env file in the root directory with the following variables:
# Flask Configuration
SECRET_KEY=your-secret-key-here
DEBUG=True
PORT=5000
# CAG Chatbot Configuration
CAG_API_KEY=your-cag-api-key
CAG_MODEL_NAME=your-model-name
CAG_API_URL=https://api.cag.example.com
# Logging Configuration
LOG_LEVEL=INFOpython app.pygunicorn -w 4 -b 0.0.0.0:5000 app:appThe application will be available at http://localhost:5000
- GET
/ - Returns application status
- POST
/api/chat - Body:
{ "message": "Hello, how are you?", "user_id": "user123" } - Response:
{ "response": "Hello! I'm doing well, thank you for asking.", "user_id": "user123", "timestamp": "2024-01-01T12:00:00" }
- GET
/api/chat/history?user_id=user123 - Response:
{ "chat_history": [ { "user_id": "user123", "message": "Hello", "timestamp": "2024-01-01T12:00:00", "type": "user" }, { "user_id": "bot", "message": "Hello! How can I help you?", "timestamp": "2024-01-01T12:00:01", "type": "bot" } ], "total_messages": 2 }
- POST
/api/chat/clear - Body:
{ "user_id": "user123" } - Response:
{ "message": "Chat history cleared for user user123", "remaining_messages": 0 }
The application includes a placeholder CAG service in cag_service.py. To integrate with your actual CAG system:
- Update the
CAGService.generate_response()method incag_service.py - Configure your CAG API credentials in the environment variables
- Implement the actual API calls to your CAG system
def generate_response(self, message: str, user_id: str, context: Optional[Dict[str, Any]] = None) -> str:
payload = {
'message': message,
'user_id': user_id,
'model': self.model_name,
'context': context or {}
}
headers = {
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json'
}
response = requests.post(
f"{self.api_url}/generate",
json=payload,
headers=headers,
timeout=30
)
response.raise_for_status()
return response.json()['response']- Add your route in
app.py - Implement proper error handling
- Add logging for debugging
- Update this README with endpoint documentation
You can test the API using curl or any HTTP client:
# Health check
curl http://localhost:5000/
# Send a message
curl -X POST http://localhost:5000/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "Hello", "user_id": "test_user"}'
# Get chat history
curl http://localhost:5000/api/chat/history?user_id=test_userCreate a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]- Set
DEBUG=False - Use a strong
SECRET_KEY - Configure your CAG API credentials
- Set appropriate
LOG_LEVEL
The application includes comprehensive error handling:
- 400: Bad Request (missing required fields)
- 404: Not Found (invalid endpoints)
- 500: Internal Server Error (server-side issues)
All errors return JSON responses with descriptive messages.
The application uses Python's logging module with configurable log levels. Logs include:
- Incoming requests
- CAG API interactions
- Error conditions
- Application startup/shutdown
- CORS is enabled for frontend integration
- Input validation on all endpoints
- Environment variable configuration for sensitive data
- Error messages don't expose internal system details
- Follow the existing code structure
- Add proper error handling and logging
- Update documentation for new features
- Test your changes thoroughly