feat: 增强 Agent 系统和完善项目结构
主要改进: - Agent 增强: 订单查询、售后支持、客服路由等功能优化 - 新增语言检测和 Token 管理模块 - 改进 Chatwoot webhook 处理和用户标识 - MCP 服务器增强: 订单 MCP 和 Strapi MCP 功能扩展 - 新增商城客户端、知识库、缓存和同步模块 - 添加多语言提示词系统 (YAML) - 完善项目结构: 整理文档、脚本和测试文件 - 新增调试和测试工具脚本 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,109 +6,20 @@ from typing import Any
|
||||
|
||||
from core.state import AgentState, ConversationState, add_tool_call, set_response, update_context
|
||||
from core.llm import get_llm_client, Message
|
||||
from prompts import get_prompt
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
AFTERSALE_AGENT_PROMPT = """你是一个专业的 B2B 售后服务助手。
|
||||
你的职责是帮助用户处理售后问题,包括:
|
||||
- 退货申请
|
||||
- 换货申请
|
||||
- 投诉处理
|
||||
- 工单创建
|
||||
- 售后进度查询
|
||||
|
||||
## 可用工具
|
||||
|
||||
1. **apply_return** - 退货申请
|
||||
- order_id: 订单号
|
||||
- items: 退货商品列表 [{item_id, quantity, reason}]
|
||||
- description: 问题描述
|
||||
- images: 图片URL列表(可选)
|
||||
|
||||
2. **apply_exchange** - 换货申请
|
||||
- order_id: 订单号
|
||||
- items: 换货商品列表 [{item_id, reason}]
|
||||
- description: 问题描述
|
||||
|
||||
3. **create_complaint** - 创建投诉
|
||||
- type: 投诉类型(product_quality/service/logistics/other)
|
||||
- title: 投诉标题
|
||||
- description: 详细描述
|
||||
- related_order_id: 关联订单号(可选)
|
||||
- attachments: 附件URL列表(可选)
|
||||
|
||||
4. **create_ticket** - 创建工单
|
||||
- category: 工单类别
|
||||
- priority: 优先级(low/medium/high/urgent)
|
||||
- title: 工单标题
|
||||
- description: 详细描述
|
||||
|
||||
5. **query_aftersale_status** - 查询售后状态
|
||||
- aftersale_id: 售后单号(可选,不填查询全部)
|
||||
|
||||
## 工具调用格式
|
||||
|
||||
当需要使用工具时,请返回 JSON 格式:
|
||||
```json
|
||||
{
|
||||
"action": "call_tool",
|
||||
"tool_name": "工具名称",
|
||||
"arguments": {
|
||||
"参数名": "参数值"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
当需要向用户询问更多信息时:
|
||||
```json
|
||||
{
|
||||
"action": "ask_info",
|
||||
"question": "需要询问的问题",
|
||||
"required_fields": ["需要收集的字段列表"]
|
||||
}
|
||||
```
|
||||
|
||||
当可以直接回答时:
|
||||
```json
|
||||
{
|
||||
"action": "respond",
|
||||
"response": "回复内容"
|
||||
}
|
||||
```
|
||||
|
||||
## 售后流程引导
|
||||
|
||||
退货流程:
|
||||
1. 确认订单号和退货商品
|
||||
2. 了解退货原因
|
||||
3. 收集问题描述和图片(质量问题时)
|
||||
4. 提交退货申请
|
||||
5. 告知用户后续流程
|
||||
|
||||
换货流程:
|
||||
1. 确认订单号和换货商品
|
||||
2. 了解换货原因
|
||||
3. 确认是否有库存
|
||||
4. 提交换货申请
|
||||
|
||||
## 注意事项
|
||||
- 售后申请需要完整信息才能提交
|
||||
- 对用户的问题要表示理解和歉意
|
||||
- 复杂投诉建议转人工处理
|
||||
- 金额较大的退款需要特别确认
|
||||
"""
|
||||
|
||||
|
||||
async def aftersale_agent(state: AgentState) -> AgentState:
|
||||
"""Aftersale agent node
|
||||
|
||||
|
||||
Handles returns, exchanges, complaints and aftersale queries.
|
||||
|
||||
|
||||
Args:
|
||||
state: Current agent state
|
||||
|
||||
|
||||
Returns:
|
||||
Updated state with tool calls or response
|
||||
"""
|
||||
@@ -117,34 +28,70 @@ async def aftersale_agent(state: AgentState) -> AgentState:
|
||||
conversation_id=state["conversation_id"],
|
||||
sub_intent=state.get("sub_intent")
|
||||
)
|
||||
|
||||
|
||||
state["current_agent"] = "aftersale"
|
||||
state["agent_history"].append("aftersale")
|
||||
state["state"] = ConversationState.PROCESSING.value
|
||||
|
||||
|
||||
# Check if we have tool results to process
|
||||
if state["tool_results"]:
|
||||
return await _generate_aftersale_response(state)
|
||||
|
||||
# Get detected language
|
||||
locale = state.get("detected_language", "en")
|
||||
|
||||
# Auto-query FAQ for return-related questions
|
||||
message_lower = state["current_message"].lower()
|
||||
faq_keywords = ["return", "refund", "defective", "exchange", "complaint", "damaged", "wrong", "missing"]
|
||||
|
||||
# 如果消息包含退货相关关键词,且没有工具调用记录,自动查询 FAQ
|
||||
if any(keyword in message_lower for keyword in faq_keywords):
|
||||
# 检查是否已经查询过 FAQ
|
||||
tool_calls = state.get("tool_calls", [])
|
||||
has_faq_query = any(tc.get("tool_name") in ["query_faq", "search_knowledge_base"] for tc in tool_calls)
|
||||
|
||||
if not has_faq_query:
|
||||
logger.info(
|
||||
"Auto-querying FAQ for return-related question",
|
||||
conversation_id=state["conversation_id"]
|
||||
)
|
||||
|
||||
# 自动添加 FAQ 工具调用
|
||||
state = add_tool_call(
|
||||
state,
|
||||
tool_name="query_faq",
|
||||
arguments={
|
||||
"category": "return",
|
||||
"locale": locale,
|
||||
"limit": 5
|
||||
},
|
||||
server="strapi"
|
||||
)
|
||||
state["state"] = ConversationState.TOOL_CALLING.value
|
||||
return state
|
||||
|
||||
# Build messages for LLM
|
||||
# Load prompt in detected language
|
||||
system_prompt = get_prompt("aftersale", locale)
|
||||
|
||||
messages = [
|
||||
Message(role="system", content=AFTERSALE_AGENT_PROMPT),
|
||||
Message(role="system", content=system_prompt),
|
||||
]
|
||||
|
||||
|
||||
# Add conversation history
|
||||
for msg in state["messages"][-8:]: # More history for aftersale context
|
||||
messages.append(Message(role=msg["role"], content=msg["content"]))
|
||||
|
||||
|
||||
# Build context info
|
||||
context_info = f"用户ID: {state['user_id']}\n账户ID: {state['account_id']}\n"
|
||||
|
||||
context_info = f"User ID: {state['user_id']}\nAccount ID: {state['account_id']}\n"
|
||||
|
||||
if state["entities"]:
|
||||
context_info += f"已提取的信息: {json.dumps(state['entities'], ensure_ascii=False)}\n"
|
||||
|
||||
context_info += f"Extracted entities: {json.dumps(state['entities'], ensure_ascii=False)}\n"
|
||||
|
||||
if state["context"]:
|
||||
context_info += f"会话上下文: {json.dumps(state['context'], ensure_ascii=False)}\n"
|
||||
|
||||
user_content = f"{context_info}\n用户消息: {state['current_message']}"
|
||||
context_info += f"Conversation context: {json.dumps(state['context'], ensure_ascii=False)}\n"
|
||||
|
||||
user_content = f"{context_info}\nUser message: {state['current_message']}"
|
||||
messages.append(Message(role="user", content=user_content))
|
||||
|
||||
try:
|
||||
@@ -206,46 +153,46 @@ async def aftersale_agent(state: AgentState) -> AgentState:
|
||||
|
||||
async def _generate_aftersale_response(state: AgentState) -> AgentState:
|
||||
"""Generate response based on aftersale tool results"""
|
||||
|
||||
|
||||
tool_context = []
|
||||
for result in state["tool_results"]:
|
||||
if result["success"]:
|
||||
data = result["data"]
|
||||
tool_context.append(f"工具 {result['tool_name']} 返回:\n{json.dumps(data, ensure_ascii=False, indent=2)}")
|
||||
|
||||
tool_context.append(f"Tool {result['tool_name']} returned:\n{json.dumps(data, ensure_ascii=False, indent=2)}")
|
||||
|
||||
# Extract aftersale_id for context
|
||||
if isinstance(data, dict) and data.get("aftersale_id"):
|
||||
state = update_context(state, {"aftersale_id": data["aftersale_id"]})
|
||||
else:
|
||||
tool_context.append(f"工具 {result['tool_name']} 执行失败: {result['error']}")
|
||||
|
||||
prompt = f"""基于以下售后系统返回的信息,生成对用户的回复。
|
||||
tool_context.append(f"Tool {result['tool_name']} failed: {result['error']}")
|
||||
|
||||
用户问题: {state["current_message"]}
|
||||
prompt = f"""Based on the following aftersale system information, generate a response to the user.
|
||||
|
||||
系统返回信息:
|
||||
User question: {state["current_message"]}
|
||||
|
||||
System returned information:
|
||||
{chr(10).join(tool_context)}
|
||||
|
||||
请生成一个体贴、专业的回复:
|
||||
- 如果是申请提交成功,告知用户售后单号和后续流程
|
||||
- 如果是状态查询,清晰说明当前进度
|
||||
- 如果申请失败,说明原因并提供解决方案
|
||||
- 对用户的问题表示理解
|
||||
Please generate a compassionate and professional response:
|
||||
- If application submitted successfully, inform user of aftersale ID and next steps
|
||||
- If status query, clearly explain current progress
|
||||
- If application failed, explain reason and provide solution
|
||||
- Show understanding for user's issue
|
||||
|
||||
Return only the response content, do not return JSON."""
|
||||
|
||||
只返回回复内容,不要返回 JSON。"""
|
||||
|
||||
messages = [
|
||||
Message(role="system", content="你是一个专业的售后客服助手,请根据系统返回的信息回答用户的售后问题。"),
|
||||
Message(role="system", content="You are a professional aftersale service assistant, please answer user's aftersale questions based on system returned information."),
|
||||
Message(role="user", content=prompt)
|
||||
]
|
||||
|
||||
|
||||
try:
|
||||
llm = get_llm_client()
|
||||
response = await llm.chat(messages, temperature=0.7)
|
||||
state = set_response(state, response.content)
|
||||
return state
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Aftersale response generation failed", error=str(e))
|
||||
state = set_response(state, "抱歉,处理售后请求时遇到问题。请稍后重试或联系人工客服。")
|
||||
state = set_response(state, "Sorry, there was a problem processing your aftersale request. Please try again later or contact customer support.")
|
||||
return state
|
||||
|
||||
@@ -6,76 +6,20 @@ from typing import Any
|
||||
|
||||
from core.state import AgentState, ConversationState, add_tool_call, set_response
|
||||
from core.llm import get_llm_client, Message
|
||||
from prompts import get_prompt
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
CUSTOMER_SERVICE_PROMPT = """你是一个专业的 B2B 购物网站客服助手。
|
||||
你的职责是回答用户的一般性问题,包括:
|
||||
- 常见问题解答 (FAQ)
|
||||
- 公司信息查询
|
||||
- 政策咨询(退换货政策、隐私政策等)
|
||||
- 产品使用指南
|
||||
- 其他一般性咨询
|
||||
|
||||
## 可用工具
|
||||
|
||||
你可以使用以下工具获取信息:
|
||||
1. **query_faq** - 搜索 FAQ 常见问题
|
||||
- query: 搜索关键词
|
||||
- category: 分类(可选)
|
||||
|
||||
2. **get_company_info** - 获取公司信息
|
||||
- section: 信息类别(about_us, contact, etc.)
|
||||
|
||||
3. **get_policy** - 获取政策文档
|
||||
- policy_type: 政策类型(return_policy, privacy_policy, etc.)
|
||||
|
||||
## 工具调用格式
|
||||
|
||||
当需要使用工具时,请返回 JSON 格式:
|
||||
```json
|
||||
{
|
||||
"action": "call_tool",
|
||||
"tool_name": "工具名称",
|
||||
"arguments": {
|
||||
"参数名": "参数值"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
当可以直接回答时,请返回:
|
||||
```json
|
||||
{
|
||||
"action": "respond",
|
||||
"response": "回复内容"
|
||||
}
|
||||
```
|
||||
|
||||
当需要转人工时,请返回:
|
||||
```json
|
||||
{
|
||||
"action": "handoff",
|
||||
"reason": "转人工原因"
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
- 保持专业、友好的语气
|
||||
- 如果不确定答案,建议用户联系人工客服
|
||||
- 不要编造信息,只使用工具返回的数据
|
||||
"""
|
||||
|
||||
|
||||
async def customer_service_agent(state: AgentState) -> AgentState:
|
||||
"""Customer service agent node
|
||||
|
||||
|
||||
Handles FAQ, company info, and general inquiries using Strapi MCP tools.
|
||||
|
||||
|
||||
Args:
|
||||
state: Current agent state
|
||||
|
||||
|
||||
Returns:
|
||||
Updated state with tool calls or response
|
||||
"""
|
||||
@@ -83,18 +27,87 @@ async def customer_service_agent(state: AgentState) -> AgentState:
|
||||
"Customer service agent processing",
|
||||
conversation_id=state["conversation_id"]
|
||||
)
|
||||
|
||||
|
||||
state["current_agent"] = "customer_service"
|
||||
state["agent_history"].append("customer_service")
|
||||
state["state"] = ConversationState.PROCESSING.value
|
||||
|
||||
|
||||
# Check if we have tool results to process
|
||||
if state["tool_results"]:
|
||||
return await _generate_response_from_results(state)
|
||||
|
||||
# Get detected language
|
||||
locale = state.get("detected_language", "en")
|
||||
|
||||
# Auto-detect category and query FAQ
|
||||
message_lower = state["current_message"].lower()
|
||||
|
||||
# 定义分类关键词
|
||||
category_keywords = {
|
||||
"register": ["register", "sign up", "account", "login", "password", "forgot"],
|
||||
"order": ["order", "place order", "cancel order", "modify order", "change order"],
|
||||
"payment": ["pay", "payment", "checkout", "voucher", "discount", "promo"],
|
||||
"shipment": ["ship", "shipping", "delivery", "courier", "transit", "logistics", "tracking"],
|
||||
"return": ["return", "refund", "exchange", "defective", "damaged"],
|
||||
}
|
||||
|
||||
# 检测分类
|
||||
detected_category = None
|
||||
for category, keywords in category_keywords.items():
|
||||
if any(keyword in message_lower for keyword in keywords):
|
||||
detected_category = category
|
||||
break
|
||||
|
||||
# 检查是否已经查询过 FAQ
|
||||
tool_calls = state.get("tool_calls", [])
|
||||
has_faq_query = any(tc.get("tool_name") in ["query_faq", "search_knowledge_base"] for tc in tool_calls)
|
||||
|
||||
# 如果检测到分类且未查询过 FAQ,自动查询
|
||||
if detected_category and not has_faq_query:
|
||||
logger.info(
|
||||
f"Auto-querying FAQ for category: {detected_category}",
|
||||
conversation_id=state["conversation_id"]
|
||||
)
|
||||
|
||||
# 自动添加 FAQ 工具调用
|
||||
state = add_tool_call(
|
||||
state,
|
||||
tool_name="query_faq",
|
||||
arguments={
|
||||
"category": detected_category,
|
||||
"locale": locale,
|
||||
"limit": 5
|
||||
},
|
||||
server="strapi"
|
||||
)
|
||||
state["state"] = ConversationState.TOOL_CALLING.value
|
||||
return state
|
||||
|
||||
# 如果询问营业时间或联系方式,自动查询公司信息
|
||||
if any(keyword in message_lower for keyword in ["opening hour", "contact", "address", "phone", "email"]) and not has_faq_query:
|
||||
logger.info(
|
||||
"Auto-querying company info",
|
||||
conversation_id=state["conversation_id"]
|
||||
)
|
||||
|
||||
state = add_tool_call(
|
||||
state,
|
||||
tool_name="get_company_info",
|
||||
arguments={
|
||||
"section": "contact",
|
||||
"locale": locale
|
||||
},
|
||||
server="strapi"
|
||||
)
|
||||
state["state"] = ConversationState.TOOL_CALLING.value
|
||||
return state
|
||||
|
||||
# Build messages for LLM
|
||||
# Load prompt in detected language
|
||||
system_prompt = get_prompt("customer_service", locale)
|
||||
|
||||
messages = [
|
||||
Message(role="system", content=CUSTOMER_SERVICE_PROMPT),
|
||||
Message(role="system", content=system_prompt),
|
||||
]
|
||||
|
||||
# Add conversation history
|
||||
@@ -151,37 +164,37 @@ async def customer_service_agent(state: AgentState) -> AgentState:
|
||||
|
||||
async def _generate_response_from_results(state: AgentState) -> AgentState:
|
||||
"""Generate response based on tool results"""
|
||||
|
||||
|
||||
# Build context from tool results
|
||||
tool_context = []
|
||||
for result in state["tool_results"]:
|
||||
if result["success"]:
|
||||
tool_context.append(f"工具 {result['tool_name']} 返回:\n{json.dumps(result['data'], ensure_ascii=False, indent=2)}")
|
||||
tool_context.append(f"Tool {result['tool_name']} returned:\n{json.dumps(result['data'], ensure_ascii=False, indent=2)}")
|
||||
else:
|
||||
tool_context.append(f"工具 {result['tool_name']} 执行失败: {result['error']}")
|
||||
|
||||
prompt = f"""基于以下工具返回的信息,生成对用户的回复。
|
||||
tool_context.append(f"Tool {result['tool_name']} failed: {result['error']}")
|
||||
|
||||
用户问题: {state["current_message"]}
|
||||
prompt = f"""Based on the following tool returned information, generate a response to the user.
|
||||
|
||||
工具返回信息:
|
||||
User question: {state["current_message"]}
|
||||
|
||||
Tool returned information:
|
||||
{chr(10).join(tool_context)}
|
||||
|
||||
请生成一个友好、专业的回复。如果工具没有返回有用信息,请诚实告知用户并建议其他方式获取帮助。
|
||||
只返回回复内容,不要返回 JSON。"""
|
||||
|
||||
Please generate a friendly and professional response. If the tool did not return useful information, honestly inform the user and suggest other ways to get help.
|
||||
Return only the response content, do not return JSON."""
|
||||
|
||||
messages = [
|
||||
Message(role="system", content="你是一个专业的 B2B 客服助手,请根据工具返回的信息回答用户问题。"),
|
||||
Message(role="system", content="You are a professional B2B customer service assistant, please answer user questions based on tool returned information."),
|
||||
Message(role="user", content=prompt)
|
||||
]
|
||||
|
||||
|
||||
try:
|
||||
llm = get_llm_client()
|
||||
response = await llm.chat(messages, temperature=0.7)
|
||||
state = set_response(state, response.content)
|
||||
return state
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Response generation failed", error=str(e))
|
||||
state = set_response(state, "抱歉,处理您的请求时遇到问题。请稍后重试或联系人工客服。")
|
||||
state = set_response(state, "Sorry, there was a problem processing your request. Please try again later or contact customer support.")
|
||||
return state
|
||||
|
||||
@@ -21,62 +21,100 @@ ORDER_AGENT_PROMPT = """你是一个专业的 B2B 订单服务助手。
|
||||
|
||||
## 可用工具
|
||||
|
||||
1. **query_order** - 查询订单
|
||||
1. **get_mall_order** - 从商城 API 查询订单(推荐使用)
|
||||
- order_id: 订单号(必需)
|
||||
- 说明:此工具会自动使用用户的身份 token 查询商城订单详情
|
||||
|
||||
2. **query_order** - 查询历史订单
|
||||
- user_id: 用户 ID(自动注入)
|
||||
- account_id: 账户 ID(自动注入)
|
||||
- order_id: 订单号(可选,不填则查询最近订单)
|
||||
- date_start: 开始日期(可选)
|
||||
- date_end: 结束日期(可选)
|
||||
- status: 订单状态(可选)
|
||||
|
||||
2. **track_logistics** - 物流跟踪
|
||||
3. **track_logistics** - 物流跟踪
|
||||
- order_id: 订单号
|
||||
- tracking_number: 物流单号(可选)
|
||||
|
||||
3. **modify_order** - 修改订单
|
||||
4. **modify_order** - 修改订单
|
||||
- order_id: 订单号
|
||||
- user_id: 用户 ID(自动注入)
|
||||
- modifications: 修改内容(address/items/quantity 等)
|
||||
|
||||
4. **cancel_order** - 取消订单
|
||||
5. **cancel_order** - 取消订单
|
||||
- order_id: 订单号
|
||||
- user_id: 用户 ID(自动注入)
|
||||
- reason: 取消原因
|
||||
|
||||
5. **get_invoice** - 获取发票
|
||||
6. **get_invoice** - 获取发票
|
||||
- order_id: 订单号
|
||||
- invoice_type: 发票类型(normal/vat)
|
||||
|
||||
## 工具调用格式
|
||||
## 回复格式要求
|
||||
|
||||
当需要使用工具时,请返回 JSON 格式:
|
||||
**重要**:你必须始终返回完整的 JSON 对象,不要包含任何其他文本或解释。
|
||||
|
||||
### 格式 1:调用工具
|
||||
当需要使用工具查询信息时,返回:
|
||||
```json
|
||||
{
|
||||
"action": "call_tool",
|
||||
"tool_name": "工具名称",
|
||||
"tool_name": "get_mall_order",
|
||||
"arguments": {
|
||||
"参数名": "参数值"
|
||||
"order_id": "202071324"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
当需要向用户询问更多信息时:
|
||||
### 格式 2:询问信息
|
||||
当需要向用户询问更多信息时,返回:
|
||||
```json
|
||||
{
|
||||
"action": "ask_info",
|
||||
"question": "需要询问的问题"
|
||||
"question": "请提供您的订单号"
|
||||
}
|
||||
```
|
||||
|
||||
当可以直接回答时:
|
||||
### 格式 3:直接回复
|
||||
当可以直接回答时,返回:
|
||||
```json
|
||||
{
|
||||
"action": "respond",
|
||||
"response": "回复内容"
|
||||
"response": "您的订单已发货,预计3天内到达"
|
||||
}
|
||||
```
|
||||
|
||||
## 重要提示
|
||||
- 订单修改和取消是敏感操作,需要确认订单号
|
||||
- 如果用户没有提供订单号,先查询他的最近订单
|
||||
- 物流查询需要订单号或物流单号
|
||||
- 对于批量操作或大金额订单,建议转人工处理
|
||||
## 示例对话
|
||||
|
||||
用户: "查询订单 202071324"
|
||||
回复:
|
||||
```json
|
||||
{
|
||||
"action": "call_tool",
|
||||
"tool_name": "get_mall_order",
|
||||
"arguments": {
|
||||
"order_id": "202071324"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
用户: "我的订单发货了吗?"
|
||||
回复:
|
||||
```json
|
||||
{
|
||||
"action": "ask_info",
|
||||
"question": "请提供您的订单号,以便查询订单状态"
|
||||
}
|
||||
```
|
||||
|
||||
## 重要约束
|
||||
- **必须返回完整的 JSON 对象**,不要只返回部分内容
|
||||
- **不要添加任何 markdown 代码块标记**(如 \`\`\`json)
|
||||
- **不要添加任何解释性文字**,只返回 JSON
|
||||
- user_id 和 account_id 会自动注入到 arguments 中,无需手动添加
|
||||
- 如果用户提供了订单号,优先使用 get_mall_order 工具
|
||||
- 对于敏感操作(取消、修改),确保有明确的订单号
|
||||
"""
|
||||
|
||||
|
||||
@@ -131,27 +169,133 @@ async def order_agent(state: AgentState) -> AgentState:
|
||||
try:
|
||||
llm = get_llm_client()
|
||||
response = await llm.chat(messages, temperature=0.5)
|
||||
|
||||
|
||||
# Parse response
|
||||
content = response.content.strip()
|
||||
if content.startswith("```"):
|
||||
content = content.split("```")[1]
|
||||
if content.startswith("json"):
|
||||
content = content[4:]
|
||||
|
||||
result = json.loads(content)
|
||||
logger.info(
|
||||
"LLM response received",
|
||||
conversation_id=state["conversation_id"],
|
||||
response_length=len(content),
|
||||
response_preview=content[:300]
|
||||
)
|
||||
|
||||
# 检查是否是简化的工具调用格式:工具名称\n{参数}
|
||||
# 例如:get_mall_order\n{"order_id": "202071324"}
|
||||
if "\n" in content and "{" in content:
|
||||
lines = content.split("\n")
|
||||
if len(lines) >= 2:
|
||||
tool_name_line = lines[0].strip()
|
||||
json_line = "\n".join(lines[1:]).strip()
|
||||
|
||||
# 如果第一行看起来像工具名称(不包含 {),且第二行是 JSON
|
||||
if "{" not in tool_name_line and "{" in json_line:
|
||||
logger.info(
|
||||
"Detected simplified tool call format",
|
||||
tool_name=tool_name_line,
|
||||
json_preview=json_line[:200]
|
||||
)
|
||||
|
||||
try:
|
||||
arguments = json.loads(json_line)
|
||||
# 直接构建工具调用
|
||||
arguments["user_id"] = state["user_id"]
|
||||
arguments["account_id"] = state["account_id"]
|
||||
|
||||
# Inject user_token if available
|
||||
if state.get("user_token"):
|
||||
arguments["user_token"] = state["user_token"]
|
||||
logger.info("Injected user_token into tool call")
|
||||
|
||||
# Use entity if available
|
||||
if "order_id" not in arguments and state["entities"].get("order_id"):
|
||||
arguments["order_id"] = state["entities"]["order_id"]
|
||||
|
||||
state = add_tool_call(
|
||||
state,
|
||||
tool_name=tool_name_line,
|
||||
arguments=arguments,
|
||||
server="order"
|
||||
)
|
||||
state["state"] = ConversationState.TOOL_CALLING.value
|
||||
|
||||
logger.info(
|
||||
"Tool call added from simplified format",
|
||||
tool_name=tool_name_line,
|
||||
arguments_keys=list(arguments.keys())
|
||||
)
|
||||
|
||||
return state
|
||||
except json.JSONDecodeError as e:
|
||||
logger.warning(
|
||||
"Failed to parse simplified format",
|
||||
error=str(e),
|
||||
json_line=json_line[:200]
|
||||
)
|
||||
|
||||
# 清理内容,去除可能的 markdown 代码块标记
|
||||
# 例如:```json\n{...}\n``` 或 ```\n{...}\n```
|
||||
if "```" in content:
|
||||
# 找到第一个 ``` 后的内容
|
||||
parts = content.split("```")
|
||||
if len(parts) >= 2:
|
||||
content = parts[1].strip()
|
||||
# 去掉可能的 "json" 标记
|
||||
if content.startswith("json"):
|
||||
content = content[4:].strip()
|
||||
# 去掉结尾的 ``` 标记
|
||||
if content.endswith("```"):
|
||||
content = content[:-3].strip()
|
||||
|
||||
# 尝试提取 JSON 对象(处理周围可能有文本的情况)
|
||||
json_start = content.find("{")
|
||||
json_end = content.rfind("}")
|
||||
if json_start != -1 and json_end != -1 and json_end > json_start:
|
||||
content = content[json_start:json_end + 1]
|
||||
|
||||
logger.info(
|
||||
"Cleaned content for JSON parsing",
|
||||
conversation_id=state["conversation_id"],
|
||||
content_length=len(content),
|
||||
content_preview=content[:500]
|
||||
)
|
||||
|
||||
try:
|
||||
result = json.loads(content)
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(
|
||||
"Failed to parse LLM response as JSON",
|
||||
conversation_id=state["conversation_id"],
|
||||
error=str(e),
|
||||
content_preview=content[:500]
|
||||
)
|
||||
# 如果解析失败,尝试将原始内容作为直接回复
|
||||
state = set_response(state, response.content)
|
||||
return state
|
||||
|
||||
action = result.get("action")
|
||||
|
||||
|
||||
logger.info(
|
||||
"LLM action parsed",
|
||||
conversation_id=state["conversation_id"],
|
||||
action=action,
|
||||
tool_name=result.get("tool_name")
|
||||
)
|
||||
|
||||
if action == "call_tool":
|
||||
# Inject user context into arguments
|
||||
arguments = result.get("arguments", {})
|
||||
arguments["user_id"] = state["user_id"]
|
||||
arguments["account_id"] = state["account_id"]
|
||||
|
||||
|
||||
# Inject user_token if available (for Mall API calls)
|
||||
if state.get("user_token"):
|
||||
arguments["user_token"] = state["user_token"]
|
||||
logger.debug("Injected user_token into tool call")
|
||||
|
||||
# Use entity if available
|
||||
if "order_id" not in arguments and state["entities"].get("order_id"):
|
||||
arguments["order_id"] = state["entities"]["order_id"]
|
||||
|
||||
|
||||
state = add_tool_call(
|
||||
state,
|
||||
tool_name=result["tool_name"],
|
||||
@@ -159,6 +303,12 @@ async def order_agent(state: AgentState) -> AgentState:
|
||||
server="order"
|
||||
)
|
||||
state["state"] = ConversationState.TOOL_CALLING.value
|
||||
|
||||
logger.info(
|
||||
"Tool call added",
|
||||
tool_name=result["tool_name"],
|
||||
arguments_keys=list(arguments.keys())
|
||||
)
|
||||
|
||||
elif action == "ask_info":
|
||||
state = set_response(state, result["question"])
|
||||
@@ -171,13 +321,9 @@ async def order_agent(state: AgentState) -> AgentState:
|
||||
elif action == "handoff":
|
||||
state["requires_human"] = True
|
||||
state["handoff_reason"] = result.get("reason", "Complex order operation")
|
||||
|
||||
|
||||
return state
|
||||
|
||||
except json.JSONDecodeError:
|
||||
state = set_response(state, response.content)
|
||||
return state
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Order agent failed", error=str(e))
|
||||
state["error"] = str(e)
|
||||
|
||||
@@ -4,92 +4,24 @@ Router Agent - Intent recognition and routing
|
||||
import json
|
||||
from typing import Any, Optional
|
||||
|
||||
from core.state import AgentState, Intent, ConversationState, set_intent, add_entity
|
||||
from core.state import AgentState, Intent, ConversationState, set_intent, add_entity, set_language
|
||||
from core.llm import get_llm_client, Message
|
||||
from core.language_detector import get_cached_or_detect
|
||||
from prompts import get_prompt
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# Intent classification prompt
|
||||
CLASSIFICATION_PROMPT = """你是一个 B2B 购物网站的智能助手路由器。
|
||||
你的任务是分析用户消息,识别用户意图并提取关键实体。
|
||||
|
||||
## 可用意图分类
|
||||
|
||||
1. **customer_service** - 通用咨询
|
||||
- FAQ 问答
|
||||
- 产品使用问题
|
||||
- 公司信息查询
|
||||
- 政策咨询(退换货政策、隐私政策等)
|
||||
|
||||
2. **order** - 订单相关
|
||||
- 订单查询("我的订单在哪"、"查一下订单")
|
||||
- 物流跟踪("快递到哪了"、"什么时候到货")
|
||||
- 订单修改("改一下收货地址"、"修改订单数量")
|
||||
- 订单取消("取消订单"、"不想要了")
|
||||
- 发票查询("开发票"、"要发票")
|
||||
|
||||
3. **aftersale** - 售后服务
|
||||
- 退货申请("退货"、"不满意想退")
|
||||
- 换货申请("换货"、"换一个")
|
||||
- 投诉("投诉"、"服务态度差")
|
||||
- 工单/问题反馈
|
||||
|
||||
4. **product** - 商品相关
|
||||
- 商品搜索("有没有xx"、"找一下xx")
|
||||
- 商品推荐("推荐"、"有什么好的")
|
||||
- 询价("多少钱"、"批发价"、"大量购买价格")
|
||||
- 库存查询("有货吗"、"还有多少")
|
||||
|
||||
5. **human_handoff** - 需要转人工
|
||||
- 用户明确要求转人工
|
||||
- 复杂问题 AI 无法处理
|
||||
- 敏感问题需要人工处理
|
||||
|
||||
## 实体提取
|
||||
|
||||
请从消息中提取以下实体(如果存在):
|
||||
- order_id: 订单号(如 ORD123456)
|
||||
- product_id: 商品ID
|
||||
- product_name: 商品名称
|
||||
- quantity: 数量
|
||||
- date_reference: 时间引用(今天、昨天、上周、具体日期等)
|
||||
- tracking_number: 物流单号
|
||||
- phone: 电话号码
|
||||
- address: 地址信息
|
||||
|
||||
## 输出格式
|
||||
|
||||
请以 JSON 格式返回,包含以下字段:
|
||||
```json
|
||||
{
|
||||
"intent": "意图分类",
|
||||
"confidence": 0.95,
|
||||
"sub_intent": "子意图(可选)",
|
||||
"entities": {
|
||||
"entity_type": "entity_value"
|
||||
},
|
||||
"reasoning": "简短的推理说明"
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
- 如果意图不明确,置信度应该较低
|
||||
- 如果无法确定意图,返回 "unknown"
|
||||
- 实体提取要准确,没有的字段不要填写
|
||||
"""
|
||||
|
||||
|
||||
async def classify_intent(state: AgentState) -> AgentState:
|
||||
"""Classify user intent and extract entities
|
||||
|
||||
|
||||
This is the first node in the workflow that analyzes the user's message
|
||||
and determines which agent should handle it.
|
||||
|
||||
|
||||
Args:
|
||||
state: Current agent state
|
||||
|
||||
|
||||
Returns:
|
||||
Updated state with intent and entities
|
||||
"""
|
||||
@@ -98,24 +30,38 @@ async def classify_intent(state: AgentState) -> AgentState:
|
||||
conversation_id=state["conversation_id"],
|
||||
message=state["current_message"][:100]
|
||||
)
|
||||
|
||||
|
||||
state["state"] = ConversationState.CLASSIFYING.value
|
||||
state["step_count"] += 1
|
||||
|
||||
|
||||
# Detect language
|
||||
detected_locale = get_cached_or_detect(state, state["current_message"])
|
||||
confidence = 0.85 # Default confidence for language detection
|
||||
state = set_language(state, detected_locale, confidence)
|
||||
|
||||
logger.info(
|
||||
"Language detected",
|
||||
locale=detected_locale,
|
||||
confidence=confidence
|
||||
)
|
||||
|
||||
# Build context from conversation history
|
||||
context_summary = ""
|
||||
if state["context"]:
|
||||
context_parts = []
|
||||
if state["context"].get("order_id"):
|
||||
context_parts.append(f"当前讨论的订单: {state['context']['order_id']}")
|
||||
context_parts.append(f"Current order: {state['context']['order_id']}")
|
||||
if state["context"].get("product_id"):
|
||||
context_parts.append(f"当前讨论的商品: {state['context']['product_id']}")
|
||||
context_parts.append(f"Current product: {state['context']['product_id']}")
|
||||
if context_parts:
|
||||
context_summary = "\n".join(context_parts)
|
||||
|
||||
|
||||
# Load prompt in detected language
|
||||
classification_prompt = get_prompt("router", detected_locale)
|
||||
|
||||
# Build messages for LLM
|
||||
messages = [
|
||||
Message(role="system", content=CLASSIFICATION_PROMPT),
|
||||
Message(role="system", content=classification_prompt),
|
||||
]
|
||||
|
||||
# Add recent conversation history for context
|
||||
@@ -123,9 +69,9 @@ async def classify_intent(state: AgentState) -> AgentState:
|
||||
messages.append(Message(role=msg["role"], content=msg["content"]))
|
||||
|
||||
# Add current message with context
|
||||
user_content = f"用户消息: {state['current_message']}"
|
||||
user_content = f"User message: {state['current_message']}"
|
||||
if context_summary:
|
||||
user_content += f"\n\n当前上下文:\n{context_summary}"
|
||||
user_content += f"\n\nCurrent context:\n{context_summary}"
|
||||
|
||||
messages.append(Message(role="user", content=user_content))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user