LangGraph构建智能客服工作流,实现精准的意图识别和业务路由

LangGraph构建智能客服工作流,实现精准的意图识别和业务路由 LangGraph原理什么是LangGraphLangGraph是一个用于构建复杂工作流和多Agent系统的框架它将工作流表示为图结构。工作流架构开始 → 意图识别 → [路由]↓┌────────┴────────┬────────────┐↓ ↓ ↓订单查询 产品咨询 投诉处理↓ ↓ ↓└────────┬────────┴────────────┘↓回复生成 → 结束状态管理工作流状态包含以下关键数据| 字段 | 类型 | 说明 ||------|------|------|| user_query | str | 用户查询内容 || intent | str | 意图类型 || order_id | str | 订单ID || order_info | dict | 订单信息 || product_info | dict | 产品信息 || messages | list | 对话消息 || response | str | 最终回复 |意图类型| 意图 | 说明 | 示例 ||------|------|------|| order_query | 订单查询 | 我的订单发货了吗 || product_consultation | 产品咨询 | 智能手表多少钱 || complaint | 投诉建议 | 我对商品不满意 || general | 一般性问题 | 你们几点上班 |---项目文件结构├── state.py 状态定义TypedDict├── customer_service.py 智能客服工作流LangGraph├── product_knowledge.py 产品知识库LlamaIndex├── config.yaml 配置文件├── run.sh 启动脚本(Linux)├── run.bat 启动脚本(Windows)├── requirements.txt Python依赖├── data/│ ├── orders.json 模拟订单数据│ └── products/ 产品文档目录│ ├── 智能手表.md│ ├── 蓝牙耳机.md│ ├── 平板电脑.md│ ├── 无线鼠标.md│ └── 智能音箱.md└── readme.md 任务说明大模型全栈工程师微调 RAG 部署的完整视频地址https://edu.csdn.net/course/detail/41122操作步骤步骤1安装依赖bash创建虚拟环境python -m venv venvvenv\Scripts\activate Windowssource venv/bin/activate Linux/Mac安装依赖pip install -r requirements.txt步骤2安装Ollamabash下载并安装OllamaWindows: https://ollama.com/download/windowsmacOS: https://ollama.com/download/macLinux: curl -fsSL https://ollama.com/install.sh | sh拉取Llama3模型ollama pull llama3步骤3测试产品知识库bash运行产品知识库测试python product_knowledge.py输出示例初始化产品知识库索引...已创建 5 个产品文档产品知识库索引创建完成共 5 个文档测试产品查询智能手表的价格是标准版1299元豪华版1599元。步骤4启动智能客服工作流bash交互式模式python customer_service.py --interactive单次执行python customer_service.py --query 我的订单 ORD202401010001 发货了吗输出示例 智能客服工作流输入 exit 或 quit 退出输入 help 查看示例问题------------------------------------------------------------请输入您的问题: 我的订单 ORD202401010001 发货了吗用户查询: 我的订单 ORD202401010001 发货了吗意图识别: 我的订单 ORD202401010001 发货了吗识别意图: order_query (订单查询)提取订单ID: ORD202401010001订单查询: ORD202401010001查询到订单: 已发货生成回复: 意图order_query最终回复: 订单查询结果订单号: ORD202401010001状态: 已发货购买商品:- 智能手表 × 1 1299元- 充电器 × 1 99元订单总额: 1398元下单时间: 2024-01-01 10:30:00物流信息:快递单号: SF1234567890收货地址: 北京市朝阳区xxx街道xxx小区如有其他问题请随时联系我们 订单查询结果...步骤5一键运行bashLinux/Macbash run.shWindowsrun.bat---工作流节点说明节点1意图识别 (classify_intent)**功能**使用LLM分析用户查询分类为四种意图之一**输入**user_query**输出**intent, order_id如果是订单查询**实现**pythondef _classify_intent(self, state):prompt f请分析用户查询并分类为order_query, product_consultation, complaint, generalintent self.llm.predict(prompt)return {**state, intent: intent, order_id: order_id}节点2订单查询 (query_order)**功能**根据订单ID查询订单信息**输入**order_id**输出**order_info**实现**pythondef _query_order(self, state):order_id state[order_id]order_info find_order(order_id)return {**state, order_info: order_info}节点3产品咨询 (query_product)**功能**使用LlamaIndex查询产品知识库**输入**user_query**输出**product_info**实现**pythondef _query_product(self, state):result self.product_kb.query(state[user_query])return {**state, product_info: {content: result}}节点4投诉处理 (handle_complaint)**功能**记录投诉信息**输入**user_query**输出**product_info投诉信息节点5回复生成 (generate_response)**功能**根据意图和查询结果生成最终回复**输入**intent, order_info, product_info**输出**response---路由机制路由函数pythondef _route(self, state):intent state[intent]routing_map {order_query: query_order,product_consultation: query_product,complaint: handle_complaint,general: generate_response,}return routing_map.get(intent, generate_response)条件边配置pythonworkflow.add_conditional_edges(classify_intent,self._route,{query_order: query_order,query_product: query_product,handle_complaint: handle_complaint,generate_response: generate_response,})---测试查询示例订单查询- 我的订单 ORD202401010001 发货了吗- 查询订单 ORD202401030003 的状态- 订单 ORD202401050005 为什么退款了产品咨询- 智能手表多少钱- 蓝牙耳机支持降噪吗- 平板电脑有哪些配置投诉建议- 我对商品不满意- 快递太慢了一般性问题- 你们几点上班- 可以开发票吗---配置参数说明| 参数 | 说明 | 默认值 ||------|------|--------|| llm.model | LLM模型名称 | llama3 || data.orders_path | 订单数据路径 | ./data/orders.json || data.products_dir | 产品文档目录 | ./data/products |---常见问题**Q1: LangGraph安装失败**bashpip install langgraph0.0.40**Q2: LlamaIndex安装失败**bashpip install llama-index llama-index-core**Q3: Ollama未安装**bash安装OllamaWindows: https://ollama.com/download/windowsmacOS: https://ollama.com/download/macLinux: curl -fsSL https://ollama.com/install.sh | sh**Q4: 意图识别不准确**- 检查LLM服务是否运行- 优化意图分类提示词- 添加更多训练数据**Q5: 订单查询失败**- 检查订单数据文件路径- 确保订单号格式正确ORD开头- 检查JSON文件格式参考命令速查表bash测试产品知识库python product_knowledge.py启动工作流交互式python customer_service.py --interactive启动工作流单次python customer_service.py --query 我的订单发货了吗一键运行bash run.sh Linuxrun.bat Windows---注意事项1. **Ollama依赖**意图识别和回复生成需要Ollama运行Llama3模型2. **LangGraph**必须安装langgraph包3. **LlamaIndex**产品知识库使用LlamaIndex构建4. **数据文件**订单数据和产品文档需要预先创建5. **路由机制**根据意图自动路由到不同处理节点---核心代码片段状态定义pythonfrom typing import TypedDict, Optional, Listclass CustomerServiceState(TypedDict):user_query: strintent: Optional[str]order_id: Optional[str]order_info: Optional[dict]product_info: Optional[dict]messages: List[str]response: Optional[str]工作流构建pythonfrom langgraph.graph import StateGraph, ENDworkflow StateGraph(CustomerServiceState)添加节点workflow.add_node(classify_intent, classify_intent)workflow.add_node(query_order, query_order)workflow.add_node(query_product, query_product)workflow.add_node(generate_response, generate_response)设置入口workflow.set_entry_point(classify_intent)添加条件边workflow.add_conditional_edges(classify_intent, route, {query_order: query_order,query_product: query_product,generate_response: generate_response,})添加边workflow.add_edge(query_order, generate_response)workflow.add_edge(query_product, generate_response)workflow.add_edge(generate_response, END)编译graph workflow.compile()运行工作流pythonstate {user_query: 我的订单 ORD202401010001 发货了吗,intent: None,order_id: None,order_info: None,product_info: None,messages: [],response: None,}result graph.invoke(state)print(result[response])