Commit Graph

5 Commits

Author SHA1 Message Date
wangliang
1aeb17fcce refactor: 移除 search_products 工具,统一使用 search_spu_products
## 修改内容

### 1. 简化 Product Agent prompt
- 移除 `search_products` 工具说明
- 移除工具选择警告和说明
- 只保留 `search_spu_products` 作为唯一商品搜索工具
- 调整工具序号 1-5

### 2. 添加工具名自动映射
**位置**:第 195-201 行(非 JSON 格式),第 221-227 行(JSON 格式)

**功能**:
- 自动将 `search_products` 转换为 `search_spu_products`
- 防止 LLM 缓存或习惯导致的旧工具调用
- 添加日志记录映射操作

**示例**:
```python
# LLM 返回
{"tool_name": "search_products", "arguments": {"query": "ring"}}

# 自动转换为
{"tool_name": "search_spu_products", "arguments": {"keyword": "ring"}}
```

### 3. 添加参数自动映射
**位置**:第 240-246 行

**功能**:
- 自动将 `query` 参数转换为 `keyword` 参数
- 兼容 LLM 使用旧参数名的情况

**示例**:
```python
# LLM 返回
{"arguments": {"query": "ring"}}

# 自动转换为
{"arguments": {"keyword": "ring"}}
```

## 优势

1. **简化逻辑**:LLM 只有一个搜索工具可选,不会选错
2. **向后兼容**:即使 LLM 调用旧工具,也能自动转换
3. **参数兼容**:支持旧参数名 `query`,自动转为 `keyword`
4. **可观测性**:所有映射操作都有日志记录

## 预期效果
- LLM 调用 `search_spu_products`(Mall API)
- 返回商品卡片到 Chatwoot
- 即使调用旧工具也能正常工作

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 18:19:12 +08:00
wangliang
e58c3f0caf 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>
2026-01-26 18:17:37 +08:00
wangliang
15a4bdeb75 fix: 为 search_spu_products 工具注入 user_token 参数
## 问题
即使更新了 Product Agent prompt,LLM 仍然调用 search_products 而非 search_spu_products

## 根本原因
search_spu_products 工具需要 user_token 参数(Mall API 认证必需),
但 product_agent 函数中没有注入此参数,导致工具调用失败或被忽略

## 修改内容

### agent/agents/product.py:169-173
在工具调用前注入 user_token、user_id、account_id 参数:

```python
# Inject context for SPU product search (Mall API)
if result["tool_name"] == "search_spu_products":
    arguments["user_token"] = state.get("user_token")
    arguments["user_id"] = state["user_id"]
    arguments["account_id"] = state["account_id"]
```

## 参数来源
- user_token: 从 Chatwoot webhook 提取(contact.custom_attributes.jwt_token)
- user_id: 从 AgentState 获取
- account_id: 从 AgentState 获取

## 预期效果
LLM 现在可以成功调用 search_spu_products 工具,
返回 Mall API 商品数据并以 Chatwoot cards 格式展示

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 18:10:36 +08:00
wangliang
fa2c8f8102 fix: 更新 Product Agent prompt 添加 search_spu_products 工具说明
## 问题
搜索商品时返回错误的工具调用 search_products 而非 search_spu_products

## 根本原因
Product Agent 的 PRODUCT_AGENT_PROMPT 中没有列出 search_spu_products 工具,
导致 LLM 不知道可以使用 Mall API 的 SPU 搜索工具

## 修改内容

### agent/agents/product.py
- 将 search_spu_products 设为第一个工具(推荐使用)
- 说明此工具使用 Mall API 搜索商品 SPU,支持用户 token 认证,返回卡片格式展示
- 原有的 search_products 标记为高级搜索工具(使用 Hyperf API)
- 调整工具序号 1-6

### docs/PRODUCT_SEARCH_SERVICE.md
- 添加 Product Agent Prompt 更新说明章节
- 调整章节序号

## 预期效果
LLM 现在应该优先使用 search_spu_products 工具进行商品搜索,
返回 Mall API 的商品数据并以 Chatwoot cards 格式展示

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 17:50:29 +08:00
wl
3ad6eee0d9 feat: 初始化 B2B AI Shopping Assistant 项目
- 配置 Docker Compose 多服务编排
- 实现 Chatwoot + Agent 集成
- 配置 Strapi MCP 知识库
- 支持 7 种语言的 FAQ 系统
- 实现 LangGraph AI 工作流

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-14 19:25:22 +08:00