业务背景:跨境母婴电商选品需要同时考虑:① 当前趋势(TikTok热词)② 安全认证(FDA/CE)③ 价格竞争力 ④ 用户历史偏好
MAS Collaborative Recommendation — 多智能体协同个性化推荐:LLM Agent 异构协作框架
Skill-MAS-Collaborative-Recommendation
老客连带购买率提升 20-35%,客单价提升,品类交叉销售做起来
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
1. 解决的问题
业务背景:跨境母婴电商选品需要同时考虑:① 当前趋势(TikTok热词)② 安全认证(FDA/CE)③ 价格竞争力 ④ 用户历史偏好
2. 核心算法逻辑
核心思想
传统推荐系统是单一模型的端到端优化,难以整合多维用户意图(价格敏感、品牌偏好、安全认证关注)。MAS Collaborative Recommendation 将推荐任务分解为多个专业化 LLM Agent 的协作问题:
- 角色专业化:每个 Agent 负责特定维度的推荐逻辑(价格分析 Agent、品质评估 Agent、趋势预测 Agent、个性化匹配 Agent)
- 协同一致性:多 Agent 的推荐意见通过 Orchestrator 协调,避免"推荐冲突"(A Agent 推高端品,B Agent 推低价品,用户困惑)
- 动态路由:根据用户查询意图,Orchestrator 决定激活哪些 Agent、以何种权重聚合其输出
- 记忆共享:Agent 间共享用户的长期偏好记忆,避免每个 Agent 独立推断导致重复探索
与传统推荐的核心区别:传统 MF/深度学习推荐是"参数内隐含知识",MAS 推荐是"显式推理链+可解释协作",特别适合需要解释"为什么推荐这个"的母婴场景。
数学模型
Agent 专业化输出:
设共有 $K$ 个专业化 Agent,每个 Agent $k$ 对商品 $i$ 输出一个推荐分:
r_{ui}^{(k)} = \text{Agent}_k(q_u, \mathbf{h}_u, \mathbf{x}_i)其中 $q_u$ 是用户查询,$\mathbf{h}_u$ 是用户历史记忆,$\mathbf{x}_i$ 是商品特征。
动态 Agent 权重分配:
Orchestrator 根据查询意图向量 $\mathbf{z}_u$ 计算各 Agent 的激活权重:
\mathbf{w} = \text{softmax}(\mathbf{W}_{orch} \mathbf{z}_u + \mathbf{b}_{orch})加权聚合最终推荐分:
\hat{r}_{ui} = \sum_{k=1}^{K} w_k \cdot r_{ui}^{(k)} + \lambda \cdot \hat{r}_{ui}^{MF}其中 $\hat{r}_{ui}^{MF}$ 是矩阵分解的基础相关分,$\lambda$ 是 MF 锚定系数,防止 LLM Agent 完全偏离统计规律。
Agent 间协商机制(辩论轮次):
当 Agent 输出方差过大时($\text{Var}(\{r_{ui}^{(k)}\}) > \sigma_{thresh}$),触发协商:
r_{ui}^{(k)} \leftarrow r_{ui}^{(k)} + \eta \cdot \sum_{j \neq k} w_j(r_{ui}^{(j)} - r_{ui}^{(k)})迭代至收敛,降低推荐冲突。
与现有方法对比
| 方法 | 可解释性 | 多维意图 | 跨域知识 | 冷启动 | 代表工作 |
|---|---|---|---|---|---|
| 单 LLM 推荐 | 部分(CoT) | 有限 | ✗ | 弱 | LLM-CF |
| 纯 MF/Deep 推荐 | ✗ | ✗ | ✗ | 弱 | BPR, BERT4Rec |
| RAG 推荐 | 部分 | 有限 | ✅ | 中 | RecMind |
| MAS 协同推荐(本方法) | ✅(推理链可视) | ✅(专业化分工) | ✅(Agent 专域) | ✅(角色可弥补) | arXiv 2409.xxxxx |
关键优势:母婴场景中,用户常问"这款奶粉适合3-6个月宝宝吗?有无激素超标记录?"——单一模型无法同时处理月龄适配(推荐领域)+ 质量安全(专业领域)+ 价格比较(广告领域)。MAS 可将这三个问题分别交给三个专业 Agent。
参考论文
| 论文 | arXiv | 年份 | 关键贡献 |
|---|---|---|---|
| MACRec: Multi-Agent Collaboration for Recommendation | [2408.16714](https://arxiv.org/abs/2408.16714) | 2024-08 | 专业化 Agent 协作框架 + 动态角色分配 |
| AgentCF: Collaborative Filtering via LLM Agents | [2310.09233](https://arxiv.org/abs/2310.09233) | 2024-01 | Agent 模拟用户行为 + 双边协商 |
| RecMAS: Multi-Agent System for E-Commerce Recommendation | [2501.12547](https://arxiv.org/abs/2501.12547) | 2025-01 | 电商多 Agent 推荐实践 + Orchestrator 路由设计 |
3. 业务应用场景
业务背景:跨境母婴电商选品需要同时考虑:① 当前趋势(TikTok热词)② 安全认证(FDA/CE)③ 价格竞争力 ④ 用户历史偏好。单一模型无法均衡处理这四个维度。
MAS Collaborative Recommendation 应用:
业务背景:母婴跨境供应商评估新 SKU 时,需要同时考虑:市场需求预测、竞品分析、合规审查、利润测算。AIM 系统当前用人工判断,效率低、一致性差。
4. 输入数据要求
未提取到输入条目;请核对原始 Skill 卡片与代码。
5. 输出结果
未提取到输出条目;请核对原始 Skill 卡片与代码。
6. 业务价值与 ROI
未自动抽取;请查看原始 Skill 卡片。
7. 代码模板
代码块数量:5
"""
MAS Collaborative Recommendation: 多智能体协同个性化推荐
整合 MF 基础分 + 专业化 LLM Agent + Orchestrator 动态路由 + 协商机制
完全使用 mock 数据,无需真实 LLM API
"""
import numpy as np
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List, Optional, Tuple
from abc import ABC, abstractmethod
# ── 数据结构 ──────────────────────────────────────────────────────────────────
@dataclass
class UserQuery:
"""用户查询(含意图向量)"""
user_id: str
query_text: str
age_group: str = "0-12m" # 宝宝月龄段
price_budget: float = 50.0 # 价格上限
quality_concern: float = 0.7 # 质量关注度 [0,1]
price_sensitivity: float = 0.5 # 价格敏感度 [0,1]
trend_sensitivity: float = 0.3 # 趋势敏感度 [0,1]
@property
def intent_vector(self) -> np.ndarray:
"""3维意图向量:[质量, 价格, 趋势]"""
return np.array([
self.quality_concern,
self.price_sensitivity,
self.trend_sensitivity,
])
@dataclass
class Product:
"""商品信息(用于 Agent 评分)"""
product_id: str
name: str
price: float
category: str
safety_cert: List[str] = field(default_factory=list) # ["FDA", "BPA-free", "CE"]
age_range: Tuple[int, int] = (0, 36) # 适用月龄范围
rating: float = 4.0
bsr_rank: int = 1000 # Amazon Best Seller Rank
is_trending: bool = False
@dataclass
class AgentOutput:
"""单个 Agent 的推荐输出"""
agent_name: str
scores: Dict[str, float] # {product_id: score}
reasoning: Dict[str, str] # {product_id: 推荐理由}
confidence: float = 1.0 # Agent 对自身输出的置信度
# ── 专业化 Agent 基类 ─────────────────────────────────────────────────────────
class SpecializedRecommendationAgent(ABC):
"""专业化推荐 Agent 基类"""
def __init__(self, name: str):
self.name = name
@abstractmethod
def evaluate(self, query: UserQuery, products: List[Product]) -> AgentOutput:
"""评估商品列表,返回推荐分和理由"""
...
def normalize_scores(self, raw_scores: Dict[str, float]) -> Dict[str, float]:
"""Min-Max 归一化到 [0, 1]"""
if not raw_scores:
return {}
vals = list(raw_scores.values())
min_v, max_v = min(vals), max(vals)
if max_v == min_v:
return {k: 0.5 for k in raw_scores}
return {k: (v - min_v) / (max_v - min_v) for k, v in raw_scores.items()}
class SafetyQualityAgent(SpecializedRecommendationAgent):
"""安全与质量专家 Agent"""
CERT_SCORES = {"FDA": 0.4, "BPA-free": 0.3, "CE": 0.2, "ASTM": 0.1}
def __init__(self):
super().__init__("safety_quality")
def evaluate(self, query: UserQuery, products: List[Product]) -> AgentOutput:
raw_scores = {}
reasoning = {}
for p in products:
# 认证得分
cert_score = sum(
self.CERT_SCORES.get(cert, 0.05)
for cert in p.safety_cert
)
# 评分得分(4.5+为优)
rating_score = max(0.0, (p.rating - 3.0) / 2.0)
# 综合
score = min(1.0, cert_score + 0.3 * rating_score)
raw_scores[p.product_id] = score
cert_str = "+".join(p.safety_cert) if p.safety_cert else "无认证"
reasoning[p.product_id] = (
f"认证: {cert_str}; 评分: {p.rating}/5.0; 安全综合分: {score:.2f}"
)
return AgentOutput(
agent_name=self.name,
scores=raw_scores,
reasoning=reasoning,
confidence=0.9,
)
class PriceValueAgent(SpecializedRecommendationAgent):
"""价格与性价比专家 Agent"""
def __init__(self):
super().__init__("price_value")
def evaluate(self, query: UserQuery, products: List[Product]) -> AgentOutput:
raw_scores = {}
reasoning = {}
# 计算预算内商品的性价比(更低价格 = 更高分,但超预算直接扣分)
for p in products:
if p.price > query.price_budget:
score = max(0.0, 0.3 - (p.price - query.price_budget) / query.price_budget)
note = f"超出预算${p.price - query.price_budget:.1f}"
else:
# 预算使用率:70-85%为最优
budget_use = p.price / query.price_budget
if 0.5 <= budget_use <= 0.85:
score = 0.8 + 0.2 * (1 - abs(budget_use - 0.7) / 0.2)
else:
score = 0.5 + 0.3 * budget_use
note = f"预算利用率{budget_use:.0%}"
raw_scores[p.product_id] = min(1.0, score)
reasoning[p.product_id] = f"售价${p.price:.2f},{note};性价比评分: {raw_scores[p.product_id]:.2f}"
return AgentOutput(
agent_name=self.name,
scores=raw_scores,
reasoning=reasoning,
confidence=0.95,
)
class AgeRelevanceAgent(SpecializedRecommendationAgent):
"""月龄适配专家 Agent"""
AGE_GROUP_MAP = {
"0-3m": (0, 3),
"0-6m": (0, 6),
"3-6m": (3, 6),
"6-12m": (6, 12),
"0-12m": (0, 12),
"12-24m": (12, 24),
"1-3y": (12, 36),
}
def __init__(self):
super().__init__("age_relevance")
def evaluate(self, query: UserQuery, products: List[Product]) -> AgentOutput:
query_range = self.AGE_GROUP_MAP.get(query.age_group, (0, 36))
raw_scores = {}
reasoning = {}
for p in products:
p_min, p_max = p.age_range
q_min, q_max = query_range
# 计算月龄覆盖重叠度
overlap_start = max(p_min, q_min)
overlap_end = min(p_max, q_max)
overlap_months = max(0, overlap_end - overlap_start)
query_span = q_max - q_min
if query_span == 0:
coverage = 1.0 if p_min <= q_min <= p_max else 0.0
else:
coverage = overlap_months / query_span
# 完全覆盖 + 有成长空间的商品额外加分
growth_bonus = 0.1 if p_max > q_max else 0.0
score = min(1.0, coverage + growth_bonus)
raw_scores[p.product_id] = score
reasoning[p.product_id] = (
f"商品适用{p_min}-{p_max}月;查询月龄{query.age_group};"
f"覆盖度{coverage:.0%}; 适配分: {score:.2f}"
)
return AgentOutput(
agent_name=self.name,
scores=raw_scores,
reasoning=reasoning,
confidence=0.85,
)
class TrendRelevanceAgent(SpecializedRecommendationAgent):
"""趋势与热度专家 Agent"""
def __init__(self):
super().__init__("trend_relevance")
def evaluate(self, query: UserQuery, products: List[Product]) -> AgentOutput:
raw_scores = {}
reasoning = {}
for p in products:
# BSR 排名得分(越小越好,取倒数归一化)
bsr_score = 1.0 / (1.0 + np.log1p(p.bsr_rank / 100))
# 趋势加成
trend_bonus = 0.2 if p.is_trending else 0.0
score = min(1.0, bsr_score + trend_bonus)
raw_scores[p.product_id] = score
trend_str = "🔥趋势商品" if p.is_trending else "常规商品"
reasoning[p.product_id] = (
f"BSR排名#{p.bsr_rank};{trend_str};热度分: {score:.2f}"
)
return AgentOutput(
agent_name=self.name,
scores=raw_scores,
reasoning=reasoning,
confidence=0.7,
)
# ── Orchestrator 动态路由 ──────────────────────────────────────────────────────
class MASRecommendationOrchestrator:
"""
多智能体推荐 Orchestrator
- 根据用户意图向量动态分配 Agent 权重
- 检测 Agent 输出冲突并触发协商
- 整合 MF 基础分防止漂移
"""
def __init__(
self,
agents: List[SpecializedRecommendationAgent],
mf_lambda: float = 0.2, # MF 基础分权重
conflict_threshold: float = 0.3, # 方差阈值,超出触发协商
negotiation_rounds: int = 2, # 协商迭代轮次
negotiation_lr: float = 0.15, # 协商学习率 η
):
self.agents = {a.name: a for a in agents}
self.mf_lambda = mf_lambda
self.conflict_threshold = conflict_threshold
self.negotiation_rounds = negotiation_rounds
self.negotiation_lr = negotiation_lr
def compute_agent_weights(
self, query: UserQuery
) -> Dict[str, float]:
"""
根据查询意图向量动态分配 Agent 权重
intent_vector: [quality, price, trend]
对应 Agent: [safety, price, trend, age]
"""
intent = query.intent_vector
# 预定义意图-Agent 映射矩阵(可替换为学习得到的W_orch)
intent_agent_mapping = {
"safety_quality": np.array([0.8, 0.1, 0.1]), # 质量导向
"price_value": np.array([0.1, 0.8, 0.1]), # 价格导向
"age_relevance": np.array([0.5, 0.3, 0.2]), # 通用(月龄始终重要)
"trend_relevance": np.array([0.1, 0.2, 0.7]), # 趋势导向
}
raw_weights = {}
for agent_name, mapping in intent_agent_mapping.items():
if agent_name in self.agents:
raw_weights[agent_name] = float(np.dot(intent, mapping))
# Softmax 归一化
vals = np.array(list(raw_weights.values()))
exp_vals = np.exp(vals - vals.max())
softmax_vals = exp_vals / exp_vals.sum()
return {name: float(w) for name, w in zip(raw_weights.keys(), softmax_vals)}
def negotiate(
self,
all_scores: Dict[str, Dict[str, float]],
weights: Dict[str, float],
product_ids: List[str],
) -> Dict[str, Dict[str, float]]:
"""
Agent 间协商:减少推荐冲突
对方差超过阈值的商品,让各 Agent 向加权均值靠拢
"""
updated_scores = {name: dict(scores) for name, scores in all_scores.items()}
for _ in range(self.negotiation_rounds):
for pid in product_ids:
agent_scores = {
name: updated_scores[name].get(pid, 0.5)
for name in updated_scores
}
variance = np.var(list(agent_scores.values()))
if variance > self.conflict_threshold:
# 加权均值
weighted_mean = sum(
agent_scores[name] * weights.get(name, 0)
for name in agent_scores
)
# 各 Agent 向均值靠拢
for name in updated_scores:
old_score = updated_scores[name].get(pid, 0.5)
other_influence = sum(
weights.get(other_name, 0) * agent_scores[other_name]
for other_name in agent_scores
if other_name != name
)
new_score = (
old_score
+ self.negotiation_lr * (other_influence - old_score)
)
updated_scores[name][pid] = float(np.clip(new_score, 0, 1))
return updated_scores
def recommend(
self,
query: UserQuery,
products: List[Product],
mf_base_scores: Optional[Dict[str, float]] = None,
top_k: int = 5,
) -> List[Tuple[str, float, str]]:
"""
多 Agent 协同推荐主流程
Args:
query: 用户查询
products: 候选商品列表
mf_base_scores: MF 模型给出的基础相关分(可选)
top_k: 返回Top-K推荐
Returns:
List of (product_id, final_score, explanation)
"""
product_ids = [p.product_id for p in products]
# 1. 各 Agent 并行评估
all_outputs: Dict[str, AgentOutput] = {}
for name, agent in self.agents.items():
all_outputs[name] = agent.evaluate(query, products)
all_scores = {name: out.scores for name, out in all_outputs.items()}
# 2. 动态权重分配
weights = self.compute_agent_weights(query)
# 3. 冲突检测与协商
negotiated_scores = self.negotiate(all_scores, weights, product_ids)
# 4. 加权聚合
final_scores = {}
for pid in product_ids:
agent_score = sum(
negotiated_scores[name].get(pid, 0.5) * weights.get(name, 0)
for name in negotiated_scores
)
# 加入 MF 基础分(防漂移锚定)
mf_score = (mf_base_scores or {}).get(pid, 0.5)
final_scores[pid] = (
(1 - self.mf_lambda) * agent_score + self.mf_lambda * mf_score
)
# 5. 生成解释
explanations = {}
for pid in product_ids:
top_agent = max(weights, key=lambda n: weights[n])
top_reasoning = all_outputs[top_agent].reasoning.get(pid, "")
agent_weight_str = "; ".join(
f"{n}({w:.0%})" for n, w in sorted(weights.items(), key=lambda x: -x[1])[:2]
)
explanations[pid] = f"[主要依据: {agent_weight_str}] {top_reasoning}"
# 6. 排序并返回
sorted_products = sorted(final_scores.items(), key=lambda x: -x[1])
return [
(pid, score, explanations[pid])
for pid, score in sorted_products[:top_k]
]
# ── 使用示例 ─────────────────────────────────────────────────────────────────
def demo_baby_mas_recommendation():
"""
母婴电商 MAS 协同推荐 Demo
场景:用户搜索6个月宝宝奶嘴,关注安全与价格
"""
# Mock 商品数据(5个候选奶嘴商品)
products = [
Product("P001", "Philips Avent 天然奶嘴", price=14.99,
category="奶嘴", safety_cert=["FDA", "BPA-free"],
age_range=(0, 18), rating=4.6, bsr_rank=120, is_trending=True),
Product("P002", "MAM 超软奶嘴", price=11.99,
category="奶嘴", safety_cert=["FDA", "BPA-free", "CE"],
age_range=(6, 18), rating=4.4, bsr_rank=285, is_trending=False),
Product("P003", "Dr. Brown 防胀气奶嘴", price=18.99,
category="奶嘴", safety_cert=["FDA"],
age_range=(0, 12), rating=4.7, bsr_rank=68, is_trending=True),
Product("P004", "NUK 正畸奶嘴", price=8.99,
category="奶嘴", safety_cert=["CE"],
age_range=(6, 24), rating=4.1, bsr_rank=512, is_trending=False),
Product("P005", "无品牌通用奶嘴", price=5.99,
category="奶嘴", safety_cert=[],
age_range=(0, 36), rating=3.8, bsr_rank=1200, is_trending=False),
]
# 用户查询
query = UserQuery(
user_id="U_42",
query_text="6个月宝宝奶嘴,预算$15,要安全",
age_group="6-12m",
price_budget=15.0,
quality_concern=0.85,
price_sensitivity=0.65,
trend_sensitivity=0.25,
)
# 初始化 Agents + Orchestrator
agents = [
SafetyQualityAgent(),
PriceValueAgent(),
AgeRelevanceAgent(),
TrendRelevanceAgent(),
]
orchestrator = MASRecommendationOrchestrator(
agents=agents,
mf_lambda=0.2,
conflict_threshold=0.25,
negotiation_rounds=2,
)
# Mock MF 基础分(模拟矩阵分解协同过滤输出)
rng = np.random.default_rng(42)
mf_scores = {p.product_id: float(rng.uniform(0.3, 0.9)) for p in products}
print("=" * 65)
print("MAS 协同推荐 Demo — 6个月宝宝奶嘴推荐")
print("=" * 65)
# 显示意图向量
intent = query.intent_vector
print(f"\n[用户意图向量] 质量={intent[0]:.2f}, 价格={intent[1]:.2f}, 趋势={intent[2]:.2f}")
# 显示 Agent 权重
weights = orchestrator.compute_agent_weights(query)
print("\n[动态 Agent 权重分配]")
for name, w in sorted(weights.items(), key=lambda x: -x[1]):
print(f" {name}: {w:.0%}")
# 执行推荐
results = orchestrator.recommend(
query=query,
products=products,
mf_base_scores=mf_scores,
top_k=5,
)
print("\n[推荐结果 Top5]")
for rank, (pid, score, explanation) in enumerate(results, 1):
p = next(p for p in products if p.product_id == pid)
print(f"\n {rank}. {p.name} (${p.price:.2f})")
print(f" 综合分: {score:.4f}")
print(f" 解释: {explanation[:80]}...")
return {
"top1_product": results[0][0],
"top1_score": results[0][1],
"agent_weights": weights,
}
def test_conflict_negotiation():
"""测试协商机制是否确实降低了 Agent 分歧"""
products = [
Product("PA", "测试商品A", price=20.0, category="test",
safety_cert=["FDA"], age_range=(0, 12), rating=4.5, bsr_rank=100),
]
query = UserQuery("test_user", "test", price_budget=30.0)
agents = [SafetyQualityAgent(), PriceValueAgent(), AgeRelevanceAgent()]
orchestrator = MASRecommendationOrchestrator(
agents=agents, negotiation_rounds=3, negotiation_lr=0.3
)
# 获取各 Agent 原始分
all_outputs = {a.name: a.evaluate(query, products) for a in agents}
all_scores = {name: out.scores for name, out in all_outputs.items()}
weights = orchestrator.compute_agent_weights(query)
# 协商前方差
pre_scores = [all_scores[n].get("PA", 0.5) for n in all_scores]
pre_var = float(np.var(pre_scores))
# 协商后
negotiated = orchestrator.negotiate(all_scores, weights, ["PA"])
post_scores = [negotiated[n].get("PA", 0.5) for n in negotiated]
post_var = float(np.var(post_scores))
print(f"\n[协商测试]")
print(f" 协商前各Agent分数: {[f'{s:.3f}' for s in pre_scores]}, 方差={pre_var:.4f}")
print(f" 协商后各Agent分数: {[f'{s:.3f}' for s in post_scores]}, 方差={post_var:.4f}")
assert post_var <= pre_var + 0.001, f"协商应降低方差,实际 {pre_var:.4f} → {post_var:.4f}"
print(f" ✅ 测试通过:协商机制有效降低分歧(方差 {pre_var:.4f} → {post_var:.4f})")
return True
if __name__ == "__main__":
np.random.seed(42)
result = demo_baby_mas_recommendation()
print(f"\n=== 结果摘要 ===")
print(f" 最优推荐: {result['top1_product']}, 综合分={result['top1_score']:.4f}")
test_conflict_negotiation()
print("[✓] MAS Collaborative Recommendation 测试通过")8. 论文来源
论文链接用于核验方法来源,不自动证明本页商业结果。