本项目实现了一个完整的智能客服系统,基于LangChain和LangGraph构建,支持多轮对话、工具调用、热更新等高级功能。系统采用多Agent协作架构,能够处理订单查询、退款申请、发票开具等复杂业务流程。
- 框架: LangChain, LangGraph
- 后端: FastAPI
- 数据库: PostgreSQL + Redis
- 部署: Docker, Docker Compose
- 监控: Prometheus, Grafana
- 测试: pytest, aiohttp
intelligent-customer-service/
├── src/ # 源代码
│ ├── agents/ # Agent实现
│ │ ├── base_agent.py # Agent基类
│ │ ├── customer_service_agent.py
│ │ ├── order_agent.py
│ │ ├── refund_agent.py
│ │ └── invoice_agent.py
│ ├── chains/ # LangChain链
│ │ ├── time_reasoning.py # 时间推理链
│ │ ├── intent_recognition.py # 意图识别链
│ │ └── response_generation.py # 响应生成链
│ ├── tools/ # 工具函数
│ │ ├── base_tool.py # 工具基类
│ │ ├── order_tools.py # 订单工具
│ │ ├── refund_tools.py # 退款工具
│ │ └── invoice_tools.py # 发票工具
│ ├── workflows/ # LangGraph工作流
│ │ └── customer_service_workflow.py
│ ├── models/ # 数据模型
│ │ ├── conversation.py # 对话模型
│ │ ├── agent.py # Agent模型
│ │ └── tools.py # 工具模型
│ ├── utils/ # 工具函数
│ │ ├── agent_manager.py # Agent管理器
│ │ ├── hot_reload.py # 热重载管理
│ │ ├── health_check.py # 健康检查
│ │ └── conversation_storage.py # 对话存储
│ └── main.py # 主应用程序
├── tests/ # 测试文件
│ ├── test_customer_service.py # 客服系统测试
│ └── test_hot_reload.py # 热重载测试
├── scripts/ # 脚本文件
│ ├── start.sh # 启动脚本
│ ├── deploy.sh # 部署脚本
│ ├── test.sh # 测试脚本
│ └── run_tests.py # 自动化测试
├── docs/ # 文档
│ ├── API.md # API文档
│ └── DEPLOYMENT.md # 部署指南
├── monitoring/ # 监控配置
│ └── prometheus.yml # Prometheus配置
├── docker-compose.yml # Docker Compose配置
├── Dockerfile # Docker镜像配置
├── requirements.txt # Python依赖
├── env.example # 环境变量示例
└── PROJECT_SUMMARY.md # 项目总结
- ✅ 时间推理:理解"昨天"、"上周"等相对时间
- ✅ 上下文管理:维护对话状态
- ✅ 意图识别:准确识别用户意图
- ✅ 订单查询流程
- ✅ 退款申请流程
- ✅ 发票开具流程
- ✅ 工具自动调用
- ✅ 模型热更新
- ✅ 插件热重载
- ✅ 健康检查接口
- ✅ 自动化测试
# 克隆项目
git clone <repository-url>
cd intelligent-customer-service
# 配置环境变量
cp env.example .env
# 编辑 .env 文件,设置 OPENAI_API_KEY# 使用脚本启动(推荐)
chmod +x scripts/start.sh
./scripts/start.sh
# 或使用 Docker Compose
docker-compose up -d# 运行自动化测试
python3 scripts/run_tests.py
# 或使用测试脚本
chmod +x scripts/test.sh
./scripts/test.sh- API服务: http://localhost:8000
- API文档: http://localhost:8000/docs
- 健康检查: http://localhost:8000/health
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3000 (admin/admin)
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{"user_id": "user_001", "message": "你好,我想了解一下你们的服务"}'curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{"user_id": "user_001", "message": "我想查询订单12345的状态"}'curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{"user_id": "user_001", "message": "我昨天下的订单什么时候发货?"}'curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{"user_id": "user_001", "message": "我想对订单12345申请退款,原因是质量问题"}'curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{"user_id": "user_001", "message": "我想为订单12345开具发票,抬头是北京科技有限公司"}'- 在
src/agents/目录下创建新的Agent类 - 继承
BaseAgent并实现必要方法 - 在
AgentManager中注册新Agent
- 在
src/tools/目录下创建工具函数 - 使用
@tool装饰器注册 - 在Agent中引用工具
- 在
src/workflows/目录下创建LangGraph工作流 - 定义节点和边的关系
- 在Agent中集成工作流
# 运行所有测试
pytest tests/
# 运行特定测试
pytest tests/test_customer_service.py
# 运行自动化测试
python3 scripts/run_tests.py# 本地开发
pip install -r requirements.txt
python -m uvicorn src.main:app --reload# 使用部署脚本
chmod +x scripts/deploy.sh
./scripts/deploy.sh production
# 或使用 Docker Compose
docker-compose up -d- 健康检查:
curl http://localhost:8000/health - 系统指标:
curl http://localhost:8000/metrics - 查看日志:
docker-compose logs -f - 重启服务:
docker-compose restart
- ✅ 时间推理:理解相对时间表达
- ✅ 意图识别:准确识别用户意图
- ✅ 上下文管理:维护对话状态
- ✅ 多轮对话:支持复杂对话流程
- ✅ 订单查询:查询订单状态和物流
- ✅ 退款处理:处理退款申请
- ✅ 发票开具:生成和下载发票
- ✅ 自动调用:智能工具选择和执行
- ✅ 代码监控:实时监控代码变更
- ✅ 自动重载:无需重启的热更新
- ✅ 影响分析:智能分析变更影响
- ✅ 回滚机制:支持快速回滚
- ✅ 容器化部署:Docker + Docker Compose
- ✅ 监控告警:Prometheus + Grafana
- ✅ 健康检查:完整的健康检查体系
- ✅ 日志管理:结构化日志和监控
MIT License