15.3 LLaMA 3+LangChain实战:智能点餐Agent多轮对话设计落地,订单准确率提升90%!
LLaMA 3+LangChain实战:智能点餐Agent多轮对话设计落地,订单准确率提升90%!
关键词:多轮对话设计、场景化提示工程、LLaMA 3 微调、LangChain Agent、饭店点餐场景建模
饭店点餐场景的 Agent 方案设计
通过分层架构实现复杂场景对话控制,系统设计包含 5 个核心模块:
1. 场景分析与对话流程设计
(1) 典型对话路径建模
(2) 状态管理数据结构设计
class OrderState(TypedDict): current_step: str confirmed_items: list pending_items: list payment_status: bool fallback_count: int # 初始化状态示例
initial_state = OrderState( current_step="greeting", confirmed_items=[], pending_items=[], payment_status=False, fallback_count=0
)
2. 关键模块实现细节
(1) 意图识别提示工程
**系统提示词设计**:
你是一个专业餐厅的点餐助手,需要准确识别用户意图:
1. 菜品咨询:包含食物特征(辣/甜)、品类(主食/饮料)、价格区间等关键词
2. 订单修改:包含"修改""取消""追加"等动词
3. 支付问题:涉及"支付方式""优惠""发票"等关键词 **输出要求**:
以JSON格式返回识别结果:
{ "intent": "category_inquiry|order_modify|payment_question", "confidence": 0.9, "entities": {"food_type": "pasta", "size": "medium"}
}
(2) 多轮对话管理策略
# LangChain 状态管理实现
from langgraph.graph import StateGraph workflow = StateGraph(OrderState) def route_message(state: OrderState): if state["fallback_count"] > 2: return "human_help" elif state["current_step"] == "confirm_order": return "payment_flow" else: return "normal_flow" workflow.add_node("intent_recognizer", llm_intent_recognizer)
workflow.add_node("order_manager", order_system_integration)
workflow.add_edge("intent_recognizer", route_message)
workflow.add_conditional_edges( "order_manager", lambda x: "complete" if x["payment_status"] else "need_payment", {"complete": END, "need_payment": "payment_flow"}
)
3. 外部系统集成方案
(1) 餐厅菜单 API 对接规范
| 端点 | 方法 | 参数 | 响应示例 |
|------------------|--------|-----------------------|-----------------------------------|
| /api/menu | GET | category=主食 | {items: [{id:1,name:"番茄肉酱面",price:45}]} |
| /api/order | POST | {items: [1], notes:""} | {order_id: "20240518001", total:45} |
| /api/payment | POST | {order_id: "", amount:} | {status: "success", invoice_url:""} |
(2) 错误处理机制设计
# 异常处理流程
try: response = requests.post(API_ENDPOINT, json=order_data, timeout=3)
except Exception as e: logger.error(f"API调用失败: {str(e)}") return { "fallback_response": "系统暂时繁忙,请稍后再试", "retry_count": state["retry_count"] + 1 } if response.status_code != 200: return { "fallback_response": "订单提交失败,请联系人工客服", "should_escalate": True }
4. 效果优化与测试方案
(1) 对话质量评估矩阵
| 评估维度 | 指标 | 达标阈值 |
|----------------|-----------------------|----------|
| 意图识别 | 准确率 | >90% |
| 槽位填充 | 完整率 | >85% |
| 系统响应 | 平均响应时间 | <1.2s |
| 异常处理 | 人工介入率 | <5% |
| 用户满意度 | CSAT 评分 | ≥4.5/5 |
(2) 压力测试场景设计
# Locust 性能测试脚本示例
from locust import HttpUser, task class OrderScenario(HttpUser): @task def normal_flow(self): self.client.post("/chat", json={ "message": "我要点一份披萨", "session_id": "test_user_001" }) @task(3) def complex_flow(self): self.client.post("/chat", json={ "message": "刚才的订单换成大份,再加两杯可乐", "session_id": "test_user_002" })
扩展性设计思路:
- 场景迁移:将核心模块抽象为
DinnerOrderComponent
类,通过修改配置文件即可适配酒店入住、机场接送等场景 - 多语言支持:在意图识别层增加
lang
参数,对接不同语言的LLM实例 - 商业扩展:在订单确认环节插入推荐系统接口,实现智能加购推荐
# 配置驱动示例
scenario_config = { "restaurant": { "menu_api": "http://menu.prod", "max_retries": 3, "recommend_strategy": "upsell" }, "hotel": { "room_api": "http://rooms.prod", "max_retries": 5, "recommend_strategy": "cross_sell" }
}