fix: 修复 Product Agent LLM 响应格式解析和工具选择问题
## 问题 1: LLM 返回非标准 JSON 格式
**现象**:
LLM 返回:`search_products\n{"query": "ring"}`
期望格式:`{"action": "call_tool", "tool_name": "...", "arguments": {...}}`
**原因**:
LLM 有时会返回简化格式 `tool_name\n{args}`,导致 JSON 解析失败
**解决方案**:
添加格式兼容逻辑(第 172-191 行):
- 检测 `\n` 分隔的格式
- 解析工具名和参数
- 转换为标准 JSON 结构
## 问题 2: LLM 选择错误的搜索工具
**现象**:
LLM 选择 `search_products`(Hyperf API)而非 `search_spu_products`(Mall API)
**原因**:
Prompt 中工具说明不够突出,LLM 优先选择第一个工具
**解决方案**:
1. 在 prompt 开头添加醒目警告(第 22-29 行):
- ⚠️ 强调必须使用 `search_spu_products`
- 标注适用场景
- 添加 ⭐ 标记推荐工具
2. 添加具体示例(第 78-89 行):
- 展示正确的工具调用格式
- 示例:搜索 "ring" 应使用 `search_spu_products`
## 修改内容
### agent/agents/product.py:172-191
添加非标准格式兼容逻辑
### agent/agents/product.py:14-105
重写 PRODUCT_AGENT_PROMPT:
- 开头添加工具选择警告
- 突出 `search_spu_products` 优先级
- 添加具体使用示例
- 标注各工具适用场景
## 预期效果
1. 兼容 LLM 的简化格式输出
2. LLM 优先选择 `search_spu_products` 进行商品搜索
3. 返回 Mall API 数据并以 Chatwoot cards 展示
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,13 +19,23 @@ PRODUCT_AGENT_PROMPT = """你是一个专业的 B2B 商品顾问助手。
|
||||
- 库存查询
|
||||
- 商品详情
|
||||
|
||||
## ⚠️ 重要:商品搜索工具选择
|
||||
|
||||
**商品搜索必须优先使用 `search_spu_products` 工具!**
|
||||
|
||||
- ✅ **search_spu_products**:使用 Mall API,支持用户认证,返回精美卡片展示(推荐)
|
||||
- ⚠️ **search_products**:仅用于高级搜索(需要复杂过滤条件时)
|
||||
|
||||
**普通商品搜索(如 "ring"、"手机"、"iPhone")必须使用 `search_spu_products`**
|
||||
|
||||
## 可用工具
|
||||
|
||||
1. **search_spu_products** - 搜索商品(使用 Mall API,推荐)
|
||||
1. **search_spu_products** - 搜索商品(使用 Mall API,推荐)⭐
|
||||
- keyword: 搜索关键词(商品名称、编号等)
|
||||
- page_size: 每页数量(默认 60,最大 100)
|
||||
- page: 页码(默认 1)
|
||||
- 说明:此工具使用 Mall API 搜索商品 SPU,支持用户 token 认证,返回卡片格式展示
|
||||
- **适用于所有普通商品搜索请求**
|
||||
|
||||
2. **search_products** - 搜索商品(使用 Hyperf API)
|
||||
- query: 搜索关键词
|
||||
@@ -34,6 +44,7 @@ PRODUCT_AGENT_PROMPT = """你是一个专业的 B2B 商品顾问助手。
|
||||
- page: 页码
|
||||
- page_size: 每页数量
|
||||
- 说明:此工具用于高级搜索,支持多维度过滤
|
||||
- **仅在需要复杂过滤条件时使用**
|
||||
|
||||
3. **get_product_detail** - 获取商品详情
|
||||
- product_id: 商品ID
|
||||
@@ -64,6 +75,19 @@ PRODUCT_AGENT_PROMPT = """你是一个专业的 B2B 商品顾问助手。
|
||||
}
|
||||
```
|
||||
|
||||
**示例**:
|
||||
用户说:"搜索 ring"
|
||||
返回:
|
||||
```json
|
||||
{
|
||||
"action": "call_tool",
|
||||
"tool_name": "search_spu_products",
|
||||
"arguments": {
|
||||
"keyword": "ring"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
当需要向用户询问更多信息时:
|
||||
```json
|
||||
{
|
||||
@@ -155,12 +179,44 @@ async def product_agent(state: AgentState) -> AgentState:
|
||||
|
||||
# Parse response
|
||||
content = response.content.strip()
|
||||
|
||||
# Log raw LLM response for debugging
|
||||
logger.info(
|
||||
"Product agent LLM response",
|
||||
response_length=len(content),
|
||||
response_preview=content[:200],
|
||||
conversation_id=state["conversation_id"]
|
||||
)
|
||||
|
||||
if content.startswith("```"):
|
||||
content = content.split("```")[1]
|
||||
if content.startswith("json"):
|
||||
content = content[4:]
|
||||
|
||||
# Handle non-JSON format: "tool_name\n{args}"
|
||||
if '\n' in content and not content.startswith('{'):
|
||||
lines = content.split('\n', 1)
|
||||
tool_name = lines[0].strip()
|
||||
args_json = lines[1].strip() if len(lines) > 1 else '{}'
|
||||
|
||||
try:
|
||||
arguments = json.loads(args_json) if args_json else {}
|
||||
result = {
|
||||
"action": "call_tool",
|
||||
"tool_name": tool_name,
|
||||
"arguments": arguments
|
||||
}
|
||||
except json.JSONDecodeError:
|
||||
# If args parsing fails, use empty dict
|
||||
result = {
|
||||
"action": "call_tool",
|
||||
"tool_name": tool_name,
|
||||
"arguments": {}
|
||||
}
|
||||
else:
|
||||
# Standard JSON format
|
||||
result = json.loads(content)
|
||||
|
||||
action = result.get("action")
|
||||
|
||||
if action == "call_tool":
|
||||
|
||||
Reference in New Issue
Block a user