Spaces:
Runtime error
Runtime error
File size: 8,439 Bytes
330b6e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | # Deployment Guide
This guide covers deployment options for the Multi-Language Chat Agent application.
## Table of Contents
- [Prerequisites](#prerequisites)
- [Environment Setup](#environment-setup)
- [Docker Deployment](#docker-deployment)
- [Manual Deployment](#manual-deployment)
- [Production Considerations](#production-considerations)
- [Monitoring and Health Checks](#monitoring-and-health-checks)
- [Troubleshooting](#troubleshooting)
## Prerequisites
### System Requirements
- **Python**: 3.8 or higher
- **PostgreSQL**: 12 or higher
- **Redis**: 6 or higher (optional but recommended)
- **Docker**: 20.10+ and Docker Compose 2.0+ (for containerized deployment)
### External Services
- **Groq API Key**: Required for LLM functionality
- **Database**: PostgreSQL instance (local or cloud)
- **Cache**: Redis instance (optional)
## Environment Setup
### 1. Clone and Prepare
```bash
git clone <repository-url>
cd chat-agent
```
### 2. Environment Configuration
Choose your deployment environment and set up configuration:
```bash
# Development
python scripts/setup_environment.py --environment development
# Production
python scripts/setup_environment.py --environment production
# Testing
python scripts/setup_environment.py --environment testing
```
### 3. Configure Environment Variables
Edit your `.env` file with actual values:
```bash
# Required: Groq API Key
GROQ_API_KEY=your_actual_groq_api_key
# Required: Database Connection
DATABASE_URL=postgresql://user:password@host:port/database
# Optional: Redis Connection
REDIS_URL=redis://host:port/db
# Required: Security
SECRET_KEY=your_secure_secret_key
```
## Docker Deployment
### Development with Docker
```bash
# Start development environment
docker-compose -f docker-compose.dev.yml up -d
# View logs
docker-compose -f docker-compose.dev.yml logs -f chat-agent-dev
# Stop services
docker-compose -f docker-compose.dev.yml down
```
### Production with Docker
```bash
# Build and start production services
docker-compose up -d
# With nginx reverse proxy
docker-compose --profile production up -d
# View logs
docker-compose logs -f chat-agent
# Stop services
docker-compose down
```
### Docker Environment Variables
Create a `.env` file for Docker Compose:
```bash
# .env file for Docker Compose
GROQ_API_KEY=your_groq_api_key
SECRET_KEY=your_secret_key
POSTGRES_DB=chat_agent_db
POSTGRES_USER=chatuser
POSTGRES_PASSWORD=secure_password
```
## Manual Deployment
### 1. Install Dependencies
```bash
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
```
### 2. Database Setup
```bash
# Initialize database
python scripts/init_db.py init --config production
# Or run migrations manually
python migrations/migrate.py migrate --config production
```
### 3. Start Application
```bash
# Production server (using Gunicorn)
pip install gunicorn
gunicorn --bind 0.0.0.0:5000 --workers 4 --worker-class eventlet app:app
# Development server
python app.py
```
### 4. Process Management (Production)
Use a process manager like systemd, supervisor, or PM2:
#### Systemd Service Example
Create `/etc/systemd/system/chat-agent.service`:
```ini
[Unit]
Description=Chat Agent Application
After=network.target
[Service]
Type=simple
User=chatuser
WorkingDirectory=/opt/chat-agent
Environment=PATH=/opt/chat-agent/venv/bin
ExecStart=/opt/chat-agent/venv/bin/gunicorn --bind 0.0.0.0:5000 --workers 4 --worker-class eventlet app:app
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
```
Enable and start:
```bash
sudo systemctl enable chat-agent
sudo systemctl start chat-agent
sudo systemctl status chat-agent
```
## Production Considerations
### 1. Security
- **HTTPS**: Use SSL/TLS certificates (Let's Encrypt recommended)
- **Firewall**: Configure firewall rules
- **API Keys**: Store securely, rotate regularly
- **Database**: Use connection pooling and SSL
### 2. Performance
- **Reverse Proxy**: Use Nginx or Apache
- **Load Balancing**: Multiple application instances
- **Caching**: Redis for session and response caching
- **Database**: Connection pooling, read replicas
### 3. Monitoring
- **Health Checks**: Built-in endpoints at `/health/*`
- **Logging**: Centralized logging (ELK stack, Fluentd)
- **Metrics**: Application and system metrics
- **Alerts**: Set up alerts for critical issues
### 4. Backup and Recovery
- **Database Backups**: Regular automated backups
- **Configuration**: Version control for configurations
- **Disaster Recovery**: Documented recovery procedures
## Monitoring and Health Checks
### Health Check Endpoints
The application provides several health check endpoints:
- `GET /health/` - Basic health check
- `GET /health/detailed` - Detailed component status
- `GET /health/ready` - Readiness probe (Kubernetes)
- `GET /health/live` - Liveness probe (Kubernetes)
- `GET /health/metrics` - Basic metrics
### Example Health Check Response
```json
{
"status": "healthy",
"timestamp": "2024-01-15T10:30:00Z",
"service": "chat-agent",
"version": "1.0.0",
"components": {
"database": {
"status": "healthy",
"connection": "ok",
"response_time_ms": 5.2
},
"redis": {
"status": "healthy",
"connection": "ok",
"response_time_ms": 1.8
},
"groq_api": {
"status": "configured",
"api_key_present": true
}
}
}
```
### Load Balancer Configuration
For load balancers, use the basic health check:
```nginx
upstream chat_agent {
server app1:5000;
server app2:5000;
}
server {
location / {
proxy_pass http://chat_agent;
}
location /health {
proxy_pass http://chat_agent;
access_log off;
}
}
```
## Troubleshooting
### Common Issues
#### 1. Database Connection Failed
```bash
# Check database connectivity
python -c "import psycopg2; psycopg2.connect('your_database_url')"
# Check database status
python scripts/init_db.py status --config production
```
#### 2. Redis Connection Failed
```bash
# Test Redis connection
redis-cli -u redis://your_redis_url ping
# Check Redis status in app
curl http://localhost:5000/health/detailed
```
#### 3. Groq API Issues
```bash
# Test API key
curl -H "Authorization: Bearer your_api_key" https://api.groq.com/openai/v1/models
# Check configuration
python -c "from config import config; print(config['production'].GROQ_API_KEY)"
```
#### 4. WebSocket Connection Issues
- Check firewall rules for WebSocket traffic
- Verify proxy configuration for WebSocket upgrades
- Check browser console for connection errors
### Log Analysis
```bash
# View application logs
tail -f logs/chat_agent.log
# Docker logs
docker-compose logs -f chat-agent
# System logs (systemd)
journalctl -u chat-agent -f
```
### Performance Issues
```bash
# Check system resources
htop
df -h
free -m
# Database performance
psql -c "SELECT * FROM pg_stat_activity;"
# Redis performance
redis-cli info stats
```
## Scaling
### Horizontal Scaling
1. **Load Balancer**: Distribute traffic across multiple instances
2. **Session Storage**: Use Redis for shared session storage
3. **Database**: Use connection pooling and read replicas
4. **File Storage**: Use shared storage for logs and uploads
### Vertical Scaling
1. **CPU**: Increase worker processes
2. **Memory**: Optimize caching and database connections
3. **Storage**: Use SSD for database and logs
### Container Orchestration
For Kubernetes deployment, see `k8s/` directory for manifests:
```bash
kubectl apply -f k8s/
kubectl get pods -l app=chat-agent
kubectl logs -f deployment/chat-agent
```
## Security Checklist
- [ ] HTTPS enabled with valid certificates
- [ ] API keys stored securely (not in code)
- [ ] Database connections encrypted
- [ ] Firewall configured
- [ ] Regular security updates
- [ ] Input validation and sanitization
- [ ] Rate limiting configured
- [ ] Monitoring and alerting set up
- [ ] Backup and recovery tested
- [ ] Access logs enabled |