Skip to content

About

智能客服系统架构与多Agent协作

Resources

Stars

34 stars

Watchers

0 watching

Forks

Repository files navigation

智能客服系统架构与多Agent协作

项目概述

本项目实现了一个完整的智能客服系统,基于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       # 项目总结

功能特性

阶段一:基础对话系统

  • ✅ 时间推理:理解"昨天"、"上周"等相对时间
  • ✅ 上下文管理:维护对话状态
  • ✅ 意图识别:准确识别用户意图

阶段二:多轮对话与工具调用

  • ✅ 订单查询流程
  • ✅ 退款申请流程
  • ✅ 发票开具流程
  • ✅ 工具自动调用

阶段三:热更新与生产部署

  • ✅ 模型热更新
  • ✅ 插件热重载
  • ✅ 健康检查接口
  • ✅ 自动化测试

快速开始

1. 环境准备

# 克隆项目
git clone <repository-url>
cd intelligent-customer-service

# 配置环境变量
cp env.example .env
# 编辑 .env 文件,设置 OPENAI_API_KEY

2. 启动服务

# 使用脚本启动(推荐)
chmod +x scripts/start.sh
./scripts/start.sh

# 或使用 Docker Compose
docker-compose up -d

3. 运行测试

# 运行自动化测试
python3 scripts/run_tests.py

# 或使用测试脚本
chmod +x scripts/test.sh
./scripts/test.sh

4. 访问服务

使用示例

基础对话

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开具发票,抬头是北京科技有限公司"}'

开发指南

添加新的Agent

  1. 在 src/agents/ 目录下创建新的Agent类
  2. 继承 BaseAgent 并实现必要方法
  3. 在 AgentManager 中注册新Agent

添加新的工具

  1. 在 src/tools/ 目录下创建工具函数
  2. 使用 @tool 装饰器注册
  3. 在Agent中引用工具

添加新的工作流

  1. 在 src/workflows/ 目录下创建LangGraph工作流
  2. 定义节点和边的关系
  3. 在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

About

智能客服系统架构与多Agent协作

Resources

Stars

34 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages