设置和运行说明-实验7
快速参考
开始一切
docker run -d -p 6379:6379 --name devops-redis redis:7-alpine
python mcp_server.py # Terminal 1
python agent_api.py # Terminal 2测试一切
curl http://localhost:8000/health
curl -X POST "http://localhost:8000/rag/ingest?directory=data/docs"
python test_mcp_tools.py
k6 run k6/load_test.js停止一切
# Press Ctrl+C in both terminals
docker stop devops-redis先决条件
pip install fastapi uvicorn chromadb pydantic groq python-dotenv langgraph langchain-core redis requests附加要求:
- Docker(适用于Redis)
- k6(用于负载测试)
______________________________________________________________________
步骤0:启动Redis
docker run -d -p 6379:6379 --name devops-redis redis:7-alpine验证Redis:
docker exec -it devops-redis redis-cli ping
# Expected: PONG______________________________________________________________________
步骤1:配置环境
创建 .env 文件:
GROQ_API_KEY=gsk_your_key_here
REDIS_URL=redis://localhost:6379
BASIC_RPM=10
PRO_RPM=60
VIP_RPM=300______________________________________________________________________
步骤2:启动MCP服务器(终端1)
python mcp_server.py预期产量:
✓ Database initialized
✓ Sample server data seeded
✓ MCP Server started______________________________________________________________________
步骤3:启动代理API(终端2)
python agent_api.py预期产量:
✓ Connected to Redis at redis://localhost:6379
✓ Groq LLM configured
✓ LangGraph workflow initialized
✓ MCP client connected to server
INFO: Uvicorn running on http://0.0.0.0:8000______________________________________________________________________
第4步:摄取文件
curl -X POST "http://localhost:8000/rag/ingest?directory=data/docs"预期产量:
{
"status": "success",
"documents_ingested": 45,
"message": "Ingested 45 chunks from data/docs"
}______________________________________________________________________
步骤5:测试系统
健康检查
curl http://localhost:8000/health预期: 所有值 true
测试查询(RAG)
curl -X POST "http://localhost:8000/agent/query" `
-H "Content-Type: application/json" `
-H "X-Client-ID: basic-test" `
-d '{\"message\": \"What is the deployment procedure?\"}'预期: 带有引用和速率限制标头的HTTP 200
测试速率限制
# Send 15 requests (over 10 RPM limit)
for ($i=1; $i -le 15; $i++) {
Write-Host "Request $i"
curl -X POST "http://localhost:8000/agent/query" `
-H "Content-Type: application/json" `
-H "X-Client-ID: basic-ratelimit-test" `
-d '{\"message\": \"Test\"}'
Start-Sleep -Milliseconds 500
}预期: 前~10个获取HTTP 200,其余获取HTTP 429
测试MCP工具
# Create ticket
curl -X POST "http://localhost:8000/agent/query" `
-H "X-Client-ID: vip-test" `
-H "Content-Type: application/json" `
-d '{\"message\": \"Create a critical ticket for database outage\"}'
# Get ticket
curl -X POST "http://localhost:8000/agent/query" `
-H "X-Client-ID: vip-test" `
-H "Content-Type: application/json" `
-d '{\"message\": \"Get ticket 1\"}'
# Append note
curl -X POST "http://localhost:8000/agent/query" `
-H "X-Client-ID: vip-test" `
-H "Content-Type: application/json" `
-d '{\"message\": \"Append note to deploy-2024-01: Success\"}'______________________________________________________________________
步骤6:运行k6负载测试
安装k6
choco install k6运行测试
# Basic tier test
k6 run k6/simple_test_basic.js
# Comprehensive test
k6 run k6/load_test.js
# Mixed clients test
k6 run k6/simple_test_mixed.js预期: 超过限制时,请参阅429条回复
______________________________________________________________________
步骤7:运行自动测试
测试MCP工具
python test_mcp_tools.py测试RAG精度
python eval_rag.py______________________________________________________________________
建筑
Client (k6/curl) → Rate Limiter (Redis) → LangGraph Orchestrator
↓
┌───────────┴────────────┐
↓ ↓
RAG System MCP Tools
↓ ↓
ChromaDB SQLite DB具有速率限制的请求流
1. Client → API: Request with X-Client-ID header
2. Rate Limiter → Redis: Check bucket state (Leaky Bucket)
3. If allowed → Orchestrator: Process request
4. Orchestrator → RAG/MCP: Based on intent
5. Response → Client: With rate limit headers______________________________________________________________________
技术栈
核心组件
- API:带限速中间件的Python FastAPI
- 速率限制器:Redis+漏桶算法
- 矢量数据库:ChromaDB(嵌入式)
- LLM:Groq(免费套餐)
- MCP服务器:Python(stdio传输)
- 编排器:LangGraph(基于图形的工作流)
测试
- 负载测试图形 k6
- 单元测试:Python单元测试
- 集成测试:自定义测试套件
______________________________________________________________________
项目结构
lab7-devops-agent/
├── agent_api.py # FastAPI server with rate limiting
├── rate_limiter.py # Redis + Leaky Bucket implementation
├── devops_orchestrator.py # LangGraph orchestrator
├── rag_system.py # RAG with ChromaDB
├── mcp_server.py # MCP server (3 tools)
├── mcp_client.py # MCP client
├── test_mcp_tools.py # Automated tool tests
├── eval_rag.py # RAG evaluation script
├── docker-compose.yml # Redis + API services
├── Dockerfile # API container image
├── requirements.txt # Python dependencies
├── .env # Environment variables
├── k6/
│ ├── load_test.js # Comprehensive k6 test
│ ├── simple_test_basic.js # Basic tier test
│ ├── simple_test_pro.js # Pro tier test
│ └── simple_test_mixed.js # Mixed clients test
├── data/
│ └── docs/ # DevOps documentation (10+ files)
│ ├── deployment_procedure.md
│ ├── incident_management.md
│ ├── server_maintenance.md
│ ├── monitoring.md
│ ├── backup_policy.md
│ └── ... (5+ more)
└── tests/
├── test_queries.json # RAG test queries
└── eval_results.json # Evaluation results______________________________________________________________________
费率限制级别
| 层 | 客户端ID模式 | RPM限制 | 用例 |
|---|---|---|---|
| 基础 | basic-* | 10 | 免费等级,测试 |
| 专业版 | pro-* | 60 | 付费订阅 |
| 贵宾 | vip-* | 300 | 企业,负载测试 |
客户端ID示例:
basic-user-123pro-company-456vip-enterprise-789
______________________________________________________________________
MCP工具(3个工具)
- 创建_票 -创建事件/问题工单
- 输入:摘要、详细信息、优先级 - 输出:ticket_id,状态
- 获取门票 -按ID检索门票详细信息
- 输入:ticket_id - 输出:票详细信息或未找到
- 附录_注释 -为实体添加注释
- 输入:entity_id,备注 - 输出:note_id,确认
______________________________________________________________________
Docker编写替代方案
使用Docker Compose而不是手动步骤:
# Start all services
docker-compose up -d
# Check status
docker-compose ps
# View logs
docker-compose logs -f agent-api
# Ingest documents
curl -X POST "http://localhost:8000/rag/ingest?directory=data/docs"
# Stop services
docker-compose down______________________________________________________________________
故障排除
Redis未连接
docker ps | findstr redis
docker start devops-redis速率限制不起作用
curl http://localhost:8000/health
# Check "redis_ready": trueMCP服务器没有响应
# Restart MCP server (Terminal 1)
# Press Ctrl+C, then:
python mcp_server.py重置所有速率限制
docker exec -it devops-redis redis-cli FLUSHALL端口8000已在使用中
netstat -ano | findstr :8000
taskkill /PID
/F______________________________________________________________________
成功检查表
- ✅ Redis正在运行:
docker ps | findstr redis - ✅ 健康检查:全部
true在/health - ✅ 摄入的文件:40+块
- ✅ RAG工作:查询返回引用
- ✅ MCP工具工作:所有3个工具都经过测试
- ✅ 速率限制:基本层在10个请求后获得429
- ✅ k6测试:负载测试显示预期的速率限制
- ✅ 不同级别:专业/VIP有更高的限制
______________________________________________________________________
