第 03 课 记忆系统:用 OpenClaw 构建客户画像引擎

第 03 课 记忆系统:用 OpenClaw 构建客户画像引擎


本课目标

读完这一课,你将完成:

  • 理解"记录"与"记忆"的本质区别
  • 部署 OpenClaw 并配置 PostgreSQL + ChromaDB 双数据库
  • 构建跨境电商客户画像系统
  • 实现 RAG 知识库(产品知识、FAQ、政策法规)
  • 用自然语言查询业务数据(Text-to-SQL)

3.1 为什么电商需要"记忆"而不只是"记录"?

记录 vs 记忆

记录(传统 ERP) 记忆(OpenClaw)
存什么 订单号、金额、日期 客户偏好、投诉原因、购买动机
怎么查 精确查询(订单号=xxx) 语义查询(“哪些客户抱怨过包装”)
用途 对账、报表 个性化营销、预判客户需求
举例 客户A买了3单 客户A偏好USB-C、在意静音、上次投诉过物流慢

一句话:记录告诉你"发生了什么",记忆告诉你"为什么发生"和"接下来会怎样"。


3.2 OpenClaw 架构

客户交互
├── Amazon 客户邮件
├── Shopify 在线聊天
├── Telegram 直接对话
└── Review / Feedback
        ↓
OpenClaw(意图识别 + 信息提取)
        ↓
    ┌───┴───────┐
    ↓           ↓
ChromaDB      PostgreSQL
(向量库)      (结构库)
    ↓           ↓
语义检索      精确查询
"类似投诉"    "月度销量"
    ↓           ↓
    └───┬───────┘
        ↓
  AI 生成响应
(基于完整上下文)

为什么用双数据库?

数据库 存什么 怎么查 电商用例
PostgreSQL 结构化数据(订单、库存、财务) SQL 精确查询 “上个月日本站销售额”
ChromaDB 非结构化数据(邮件内容、Review、知识库) 向量语义检索 “有没有客户反映过电池不耐用”

3.3 部署 OpenClaw

步骤一:安装依赖

pip install openclaw chromadb psycopg2-binary

步骤二:初始化数据库

# init_db.py
from openclaw import OpenClaw
from openclaw.memory import DualMemoryStore

# 配置双数据库
memory = DualMemoryStore(
    postgres_url="postgresql://user:pass@localhost:5432/ecommerce",
    chroma_path="./chroma_data",
    chroma_collection="customer_memory"
)

# 初始化 OpenClaw
claw = OpenClaw(
    memory_store=memory,
    llm_model="claude-opus-4-7",
    llm_api_key="sk-ant-xxxxx"
)

claw.init()
print("OpenClaw 初始化完成!")

步骤三:定义客户画像结构

# customer_profile.py
CUSTOMER_SCHEMA = {
    "customer_id": "string",          # AMZ-US-C1234
    "source": "string",               # amazon_us / shopify / tiktok
    "name": "string",
    "language": "string",             # en / ja / de
    "total_orders": "integer",
    "lifetime_value": "float",
    "preferences": ["string"],         # ["USB-C", "silent", "compact"]
    "pain_points": ["string"],         # ["shipping slow", "packaging poor"]
    "last_interaction": "datetime",
    "predicted_next_purchase": "string",
    "risk_level": "string",           # low / medium / high
    "notes": "text"                   # AI 自动总结
}

3.4 客户记忆实战:从邮件到画像

场景:收到一封客户邮件

From: john@example.com
Subject: Fan stopped working after 2 weeks

Hi, I bought the BREEZO Portable Fan V3 and it worked great for the
first two weeks. But now the motor makes a buzzing sound and the
battery only lasts 30 minutes instead of 4 hours. 

This is really disappointing because I chose this over the competitor
specifically for the quiet operation and long battery life.

Can I get a replacement?

OpenClaw 自动处理流程

# 1. 意图识别
intent = claw.classify_intent(email_content)
# → {"intent": "warranty_claim", "urgency": "high", "sentiment": "negative"}

# 2. 信息提取
extracted = claw.extract_info(email_content)
# → {
#     "product": "BREEZO Portable Fan V3",
#     "issue": ["motor buzzing", "battery degraded"],
#     "purchase_reason": ["quiet operation", "long battery"],
#     "request": "replacement"
# }

# 3. 更新客户画像
claw.update_customer_profile("AMZ-US-john123", {
    "pain_points": ["motor quality", "battery durability"],
    "preferences": ["quiet operation", "long battery life"],
    "risk_level": "high",  # 可能差评
    "notes": "有退换货倾向,但原因合理。建议优先处理。"
})

# 4. 检索相关记忆
similar_cases = claw.memory.search(
    "motor buzzing sound battery degraded portable fan",
    top_k=5
)
# → 找到3个类似案例,其中2个最终给了差评

# 5. 生成回复建议
response = claw.generate_response(
    template="warranty_claim",
    context={
        "customer_profile": profile,
        "similar_cases": similar_cases,
        "product_warranty": "1 year"
    }
)

生成的回复(发送到 Telegram 等待你审批)

📩 新客户邮件 — 需要审批

客户:John (AMZ-US-john123)
意图:保修索赔(紧急)
风险:⚠️ 高 — 类似案例67%给了差评

建议回复:
---
Dear John,

Thank you for reaching out. I'm sorry to hear about the issues with 
your BREEZO Portable Fan V3.

The buzzing motor and reduced battery life you described are covered 
under our 1-year warranty. I've already initiated a replacement 
shipment — you should receive a new unit within 3-5 business days.

You do NOT need to return the defective unit.

As a token of appreciation for your patience, I've included a 15% 
discount code for your next purchase: BREEZO15

Best regards,
[Your Store Name]
---

📊 上下文:过去30天有3个类似投诉
  → 建议通知供应商检查电机批次

[✅ 发送] [✏️ 修改] [❌ 取消]

3.5 RAG 知识库:让 Agent 随时查阅产品资料

导入产品知识

# 导入产品手册、FAQ、退换货政策
claw.knowledge.add_documents([
    {"title": "BREEZO V3 Product Manual", "path": "docs/breezo_v3_manual.pdf"},
    {"title": "Return & Refund Policy", "path": "docs/return_policy.md"},
    {"title": "Amazon Category Requirements", "path": "docs/amazon_requirements.md"},
    {"title": "FAQ - All Products", "path": "docs/faq.md"},
])

使用场景

当 Agent 需要回复客户关于产品规格的问题时:

# Agent 自动从知识库检索
answer = claw.knowledge.query("BREEZO V3 battery capacity and charging time")
# → "BREEZO V3 has a 4000mAh battery, charges in 2.5 hours via USB-C,
#    provides up to 8 hours on low speed, 4 hours on high speed."

3.6 Text-to-SQL:用自然语言查询销售数据

# 你在 Telegram 中发送:
"上个月美国站销量最好的5个产品是什么?"

# OpenClaw Text-to-SQL 自动转换:
# SELECT product_name, SUM(quantity) as total_sold, SUM(revenue) as total_revenue
# FROM orders
# WHERE marketplace = 'amazon_us'
#   AND order_date >= '2026-03-01' AND order_date < '2026-04-01'
# GROUP BY product_name
# ORDER BY total_sold DESC
# LIMIT 5

# 返回:
"""
📊 2026年3月 美国站 Top 5

| 排名 | 产品 | 销量 | 收入 |
|------|------|------|------|
| 1 | BREEZO Fan V3 | 1,234 | $37,016 |
| 2 | LUMIO Light Pro | 892 | $22,299 |
| 3 | AQUA Bottle S2 | 756 | $11,340 |
| 4 | BREEZO Fan Mini | 634 | $12,680 |
| 5 | FLEXO Stand X | 521 | $13,025 |
"""

本课小结

  • 记忆 ≠ 记录:AI Agent 的记忆系统提取语义信息,支持模糊检索和推理
  • 双数据库架构:PostgreSQL 管结构化数据,ChromaDB 管语义信息
  • 客户画像自动更新:每次客户互动后自动提取和更新
  • RAG 知识库:产品手册、政策文档随时可查
  • Text-to-SQL:用自然语言查业务数据

下一课预告:第04课 — 用 MCP 协议连接 Amazon SP-API、物流查询等外部系统。