# MES AI服务部署指南 ## 快速部署步骤 ### 1. 环境准备 ```bash # 克隆项目 git clone cd imes # 检查Python版本 python --version # 需要 >= 3.11 # 安装uv包管理器 pip install uv ``` ### 2. 依赖安装 ```bash # 同步依赖 uv sync # 或者使用pip安装 pip install -r requirements.txt ``` ### 3. 环境配置 ```bash # 创建环境配置文件 cat > .env << EOF MES_AI_OPENAI_API_KEY=your_openai_api_key_here MES_AI_DEBUG=true MES_AI_PORT=8000 MES_AI_LOG_LEVEL=INFO EOF ``` ### 4. 启动服务 ```bash # 使用启动脚本(推荐) python scripts/start.py # 或直接启动 python main.py ``` ### 5. 验证部署 ```bash # 健康检查 curl http://localhost:8000/api/v1/health # 测试聊天接口 curl -X POST "http://localhost:8000/api/v1/chat" \ -H "Content-Type: application/json" \ -d '{"message": "你好,我想了解系统功能"}' ``` ## Docker部署 ### Dockerfile ```dockerfile FROM python:3.11-slim WORKDIR /app # 安装系统依赖 RUN apt-get update && apt-get install -y \ build-essential \ && rm -rf /var/lib/apt/lists/* # 复制项目文件 COPY . . # 安装Python依赖 RUN pip install uv RUN uv sync # 创建必要目录 RUN mkdir -p knowledge_base logs # 暴露端口 EXPOSE 8000 # 启动命令 CMD ["python", "main.py"] ``` ### Docker Compose ```yaml version: '3.8' services: mes-ai: build: . ports: - "8000:8000" environment: - MES_AI_OPENAI_API_KEY=${OPENAI_API_KEY} - MES_AI_DEBUG=false - MES_AI_LOG_LEVEL=INFO volumes: - ./knowledge_base:/app/knowledge_base - ./logs:/app/logs restart: unless-stopped # 可选:添加Redis缓存 redis: image: redis:7-alpine ports: - "6379:6379" restart: unless-stopped ``` ## 生产环境部署 ### 1. 环境变量配置 ```bash # 生产环境配置 export MES_AI_OPENAI_API_KEY="your_production_api_key" export MES_AI_DEBUG=false export MES_AI_WORKERS=4 export MES_AI_LOG_LEVEL=WARNING export MES_AI_LOG_FILE="/var/log/mes-ai/app.log" export MES_AI_SECRET_KEY="your-secure-secret-key" export MES_AI_CORS_ORIGINS='["https://your-domain.com"]' ``` ### 2. 使用Gunicorn部署 ```bash # 安装Gunicorn pip install gunicorn # 启动服务 gunicorn src.app:app \ --host 0.0.0.0 \ --port 8000 \ --workers 4 \ --worker-class uvicorn.workers.UvicornWorker \ --access-logfile /var/log/mes-ai/access.log \ --error-logfile /var/log/mes-ai/error.log ``` ### 3. Nginx反向代理 ```nginx server { listen 80; server_name your-domain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ``` ### 4. 系统服务配置 ```ini # /etc/systemd/system/mes-ai.service [Unit] Description=MES AI Service After=network.target [Service] Type=exec User=mes-ai Group=mes-ai WorkingDirectory=/opt/mes-ai Environment=PATH=/opt/mes-ai/venv/bin ExecStart=/opt/mes-ai/venv/bin/python main.py Restart=always RestartSec=10 [Install] WantedBy=multi-user.target ``` ```bash # 启用并启动服务 sudo systemctl enable mes-ai.service sudo systemctl start mes-ai.service sudo systemctl status mes-ai.service ``` ## 监控和维护 ### 1. 日志监控 ```bash # 查看实时日志 tail -f logs/imes.log # 查看错误日志 grep "ERROR" logs/imes.log # 日志轮转配置 # 在/etc/logrotate.d/mes-ai中配置 ``` ### 2. 性能监控 ```bash # 检查服务状态 curl http://localhost:8000/api/v1/health # 监控系统资源 htop free -h df -h ``` ### 3. 备份策略 ```bash # 备份知识库 tar -czf knowledge_base_backup_$(date +%Y%m%d).tar.gz knowledge_base/ # 备份配置文件 cp .env .env.backup.$(date +%Y%m%d) ``` ## 故障排除 ### 常见问题 1. **服务无法启动** ```bash # 检查端口占用 lsof -i :8000 # 检查Python版本 python --version # 检查依赖安装 uv sync --check ``` 2. **OpenAI API调用失败** ```bash # 测试API连接 curl -H "Authorization: Bearer $MES_AI_OPENAI_API_KEY" \ https://api.openai.com/v1/models ``` 3. **内存使用过高** ```bash # 调整worker数量 export MES_AI_WORKERS=2 # 监控内存使用 ps aux | grep python ``` ## 安全建议 1. **API密钥安全** - 使用环境变量存储敏感信息 - 定期轮换API密钥 - 限制API密钥权限 2. **网络安全** - 使用HTTPS - 配置合适的CORS策略 - 实施API限流 3. **访问控制** - 实施身份验证 - 添加API访问日志 - 监控异常访问 ## 性能优化 1. **缓存策略** - 使用Redis缓存常用查询 - 缓存向量数据库查询结果 - 实施会话缓存 2. **资源优化** - 调整worker数量 - 优化数据库查询 - 使用连接池 3. **监控指标** - 响应时间 - 错误率 - 资源使用率 - API调用频率