12.5 实战:多功能客服 Agent
客服实战用固定数据演示 FAQ、订单查询、商品推荐和投诉草稿。工具函数封装业务逻辑,create_agent 根据会话选择工具。示例只生成投诉草稿。真实工单写入时,应用应验证身份、授权和业务审批。
在仓库根目录设置 OPENAI_API_KEY,然后运行脚本。可通过 CUSTOMER_SERVICE_MODEL 指定支持 Chat Completions 工具调用的模型。
python -m pip install "langchain>=1,<2" "langchain-openai>=1,<2"
python examples/chapter12/customer_service.py
"""Customer service with fixture data and a local in-memory checkpointer."""
import json
import os
from langchain.agents import create_agent
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import InMemorySaver
@tool
def search_faq(query: str) -> str:
"""Search the FAQ knowledge base. Suitable for answering questions about product usage, policies, and processes."""
faq_data = {
"refund": "Refund policy: no-questions-asked refunds within 7 days; original packaging required. Contact customer service to apply.",
"shipping": "Standard shipping: 1–3 business days; delayed during holidays. Express shipping available.",
"warranty": "Products purchased through official channels come with a 1-year official warranty. Screen damage is not covered.",
"discount": "New users get 20% off their first order. Members can use points to offset payment.",
"payment": "Accepts credit cards, PayPal, and bank transfers. Cash on delivery not supported.",
}
for keyword, answer in faq_data.items():
if keyword in query.lower():
return answer
return "No relevant FAQ found. We recommend contacting live customer support for more help."
@tool
def check_order(order_id: str) -> str:
"""
Query order status and shipping information.
Input an order number (e.g., ORD-12345678) to get order details.
"""
orders = {
"ORD-12345678": {
"status": "Shipped",
"items": "Python Programming Book × 1",
"amount": 29.99,
"shipping": "FedEx: FX1234567890, expected delivery tomorrow"
},
"ORD-87654321": {
"status": "Pending Shipment",
"items": "AI Development Course × 1",
"amount": 99.00,
"shipping": "Expected to ship tomorrow"
}
}
order = orders.get(order_id)
if order:
return json.dumps(order, ensure_ascii=False)
return f"Order {order_id} not found. Please verify the order number."
@tool
def draft_complaint(
order_id: str,
complaint_type: str,
description: str
) -> str:
"""
Draft an after-sales complaint for review.
complaint_type: refund request / quality issue / shipping issue / other
"""
return json.dumps(
{"order_id": order_id, "type": complaint_type, "description": description},
ensure_ascii=False,
)
@tool
def recommend_products(user_need: str) -> str:
"""Recommend suitable products based on user needs."""
catalog = [
{"name": "Python: From Beginner to Expert", "price": 29.99, "tag": "programming python beginner"},
{"name": "AI Agent Hands-On Course", "price": 99.00, "tag": "ai machine-learning agent"},
{"name": "LangChain Practical Tutorial", "price": 69.00, "tag": "langchain python ai"},
{"name": "Data Analysis Complete Guide", "price": 49.99, "tag": "data-analysis pandas excel"},
]
# Simple keyword matching
results = []
for item in catalog:
if any(keyword in user_need.lower() for keyword in item["tag"].split()):
results.append(f"• {item['name']} - ${item['price']}")
if results:
return "Based on your needs, here are our recommendations:\n" + "\n".join(results)
return "No exact matches found. Please describe your needs in more detail."
def main() -> None:
agent = create_agent(
ChatOpenAI(model=os.getenv("CUSTOMER_SERVICE_MODEL", "gpt-4.1-mini")),
tools=[search_faq, check_order, draft_complaint, recommend_products],
system_prompt=(
"You are a customer service assistant for a fictional store. "
"Use the tools for factual answers, and describe draft complaints as drafts."
),
checkpointer=InMemorySaver(),
)
config = {"configurable": {"thread_id": "customer-demo"}, "recursion_limit": 20}
print("Customer service demo. Type 'quit' to exit.")
while True:
try:
message = input("Customer: ").strip()
except (EOFError, KeyboardInterrupt):
break
if message.lower() == "quit":
break
if message:
state = agent.invoke({"messages": [{"role": "user", "content": message}]}, config)
print("Assistant:", state["messages"][-1].content)
if __name__ == "__main__":
main()
InMemorySaver 搭配固定的 thread_id 保留该 CLI 会话的上下文,退出后状态消失。先问 Where is ORD-12345678?,再问 What did I order?。生产环境需要持久化 checkpointer,并在服务端验证调用者与会话的归属关系。参见第 13 章。