用户上月购买了 Stage 1 奶粉,系统推断宝宝约 2-3 月龄
User Profile Long Memory — 跨会话用户画像:育儿阶段感知与偏好记忆
Skill-User-Profile-Long-Memory
50 万
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
1. 解决的问题
用户上月购买了 Stage 1 奶粉,系统推断宝宝约 2-3 月龄
2. 核心算法逻辑
核心思想
传统电商推荐的痛点:每次会话独立,用户偏好从零识别——上月的购买行为、表达过的品牌偏好、当下育儿阶段的隐含需求全部丢失。用户画像长期记忆(User Profile Long Memory)通过跨会话持久化画像,将用户的历史交互沉淀为可更新、可检索的结构化特征,驱动无缝个性化体验。
四层画像架构
| 层级 | 内容 | 示例 |
|---|---|---|
| L1 人口统计 | 静态属性,长期稳定 | 宝宝出生日期、家庭构成、地区 |
| L2 行为特征 | 中期偏好,月级更新 | 偏好有机认证、高频购买段 |
| L3 偏好标签 | 动态标签,周级更新 | Stage 1→2 升阶期、价格敏感 |
| L4 动态状态 | 实时上下文,会话级 | 当前正在比较配方奶品牌 |
育儿阶段感知机制
从购买历史推断宝宝月龄是核心差异化能力。算法依据品类购买时序回推月龄:
\text{baby\_age\_months} = \text{ref\_month} + \Delta t_{\text{purchase}}- Stage 1 奶粉(0-6月龄)购买 → 宝宝约 0-3 月
- Stage 2 奶粉(6-12月龄)购买 → 宝宝约 5-9 月
- 辅食泥类购买 → 宝宝约 4-8 月
- 学步车/站立辅助品 → 宝宝约 9-15 月
多次购买置信度叠加:$\text{confidence} = 1 - \prod_i(1 - c_i)$
特征衰减机制
画像特征并非永久有效,需时间衰减 + 行为更新双重管理:
v_{\text{current}} = v_0 \times e^{-\lambda \cdot \Delta t} + \text{behavior\_update}其中 $\lambda$ 为衰减率,因特征类型不同而差异显著:
- 育儿阶段(
decay_rate=0.01):极慢衰减,宝宝月龄是客观事实 - 品牌偏好(
decay_rate=0.05):缓慢衰减,偏好有一定稳定性 - 价格敏感度(
decay_rate=0.1):中速衰减,受促销活动影响
隐私保护存储(标识化 ≠ 匿名化)
实测锚点:画像序列化用 hashlib.sha256(user_id).hexdigest()[:16] 作伪标识,user_001 → 稳定得到 ab2201ddf6cec4bd(跨进程、跨机器逐字节一致);而只要拿到 3 个候选 ID 的字典,就能把这个摘要反查回原 ID——③ 里这条断言是故意要求反查成功的,反查成功才证明「可还原」这件事成立。
- 画像存储与用户 ID 关联,不存储原始对话内容
- 支持用户主动重置画像(GDPR/CCPA 合规)
- 敏感属性(如健康信息)单独加密存储
- 落盘的
user_id_hash只是「标识化」(pseudonymisation),不是「匿名化」:SHA-256 是稳定且公开的,用户 ID 空间又小又可枚举(user_001、手机号、邮箱),任何拿到摘要的人跑一遍字典就能还原原 ID。真正达到匿名化需要服务端密钥的 HMAC(加盐),且密钥不落同一份存储;只在库内做关联时,应把它当作假名并按个人信息管控 - 反例(本卡修掉的写法):
hash(user_id) % 109—— Python 内建hash()对 str 按进程加盐**,同一 ID 在两个进程里得到两个值,既不能跨会话关联(功能坏),也不是任何脱敏(10^9 桶照样能字典还原)
3. 业务应用场景
业务问题:用户上月购买了 Stage 1 奶粉,系统推断宝宝约 2-3 月龄。本月用户再次进入 App,系统需要主动预判宝宝已接近 4-5 月,即将进入 Stage 2 窗口,无需用户重复说明偏好即可精准推荐。
数据要求: - 历史购买记录:品类(Stage 1/2/3 奶粉、辅食泥、学步玩具)+ 购买时间 - 浏览行为:浏览了哪些品类页 + 停留时长 - 画像特征存储:JSON 结构,持久化到用户数据库
| 时间节点 | 画像状态 | 推荐行为 | |---------|---------|---------| | 购买 Stage 1(Month 0) | `baby_stage=NEWBORN, brand=Aptamil, organic=True` | 写入画像 | | 用户回访(Month 3) | 推断宝宝≈4月,检测到临近升阶窗口 | 推荐 Stage 2 同品牌有机配方 + 辅食入门套装 | | 用户回访(Month 6) | 推断宝宝≈8月,进入辅食深度期 | 推荐有机米粉/果泥 + Stage 3 过渡产品 |
4. 输入数据要求
- 历史购买记录:品类(Stage 1/2/3 奶粉、辅食泥、学步玩具)+ 购买时间
- 浏览行为:浏览了哪些品类页 + 停留时长
- 画像特征存储:JSON 结构,持久化到用户数据库
- 用户画像:BRAND_PREFERENCE(品牌层级)+ PRICE_SENSITIVITY(价格敏感度)+ QUALITY_WEIGHT(品质权重)
- 选品评分矩阵:默认权重 + 用户偏好调节因子
5. 输出结果
未提取到输出条目;请核对原始 Skill 卡片与代码。
6. 业务价值与 ROI
- 育儿阶段感知是母婴场景特有的强信号,其他通用推荐系统缺失此维度
- 实现成本低(无 LLM 调用、无向量库),运行成本接近零
- 与 Shopping Companion Agent 结合后形成完整的"偏好识别→记忆→复现"闭环
- 仅基于购买行为推断,浏览行为未纳入时推断精度受限
- 多宝宝家庭(二胎)可能导致阶段推断混乱(需要会话分离机制)
7. 代码模板
代码块数量:1 · 路径:paper2skills-code/user_analytics/user_profile_long_memory
"""
User Profile Long Memory — 跨会话用户画像积累
来源: Personalized Memory Architecture + LLM Personal Assistant 2025-2026
场景: 育儿阶段感知 + 品牌偏好 + 价格敏感度的持久化画像管理
"""
import math
import json
import hashlib
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from typing import Optional
# 评测参考时刻:本卡夹具的购买时间固定在 2026-03-01 … 2026-05-20,
# 因此推断也必须以同一个"当前时刻"为基准。用 datetime.now() 会让"宝宝月龄"
# 随真实日历一直增长(今天跑出 11 月,明年就冲破 assert 的上限 15)。
EVAL_NOW = datetime(2026, 6, 1, tzinfo=timezone.utc)
# ─────────────────────────────────────────────
# 枚举定义
# ─────────────────────────────────────────────
class UserProfileDimension(Enum):
"""用户画像维度枚举"""
PARENTING_STAGE = "parenting_stage" # 育儿阶段
BRAND_PREFERENCE = "brand_preference" # 品牌偏好
PRICE_SENSITIVITY = "price_sensitivity" # 价格敏感度
ORGANIC_PREFERENCE = "organic_preference" # 有机偏好
CATEGORY_AFFINITY = "category_affinity" # 品类亲和度
QUALITY_WEIGHT = "quality_weight" # 品质权重
BABY_AGE_MONTHS = "baby_age_months" # 宝宝月龄(推断值)
class ParentingStage(Enum):
"""育儿阶段枚举"""
NEWBORN = "newborn" # 新生儿(0-3月)
INFANT = "infant" # 婴儿期(4-6月)
CRAWLER = "crawler" # 爬行期(7-9月)
TODDLER_EARLY = "toddler_early" # 学步初期(10-12月)
TODDLER_MID = "toddler_mid" # 学步中期(13-18月)
TODDLER_LATE = "toddler_late" # 学步晚期(19-24月)
PRESCHOOL = "preschool" # 学前期(25+月)
UNKNOWN = "unknown" # 未知阶段
# ─────────────────────────────────────────────
# 数据类定义
# ─────────────────────────────────────────────
@dataclass
class ProfileAttribute:
"""单一画像属性,含置信度与衰减机制"""
value: object
confidence: float # 置信度 [0,1]
last_updated: str # ISO 8601 时间戳
decay_rate: float = 0.05 # 月衰减率(λ)
source: str = "purchase_history" # 来源标注
evidence_count: int = 1 # 支撑证据条数
def effective_confidence(self, now: Optional[datetime] = None) -> float:
"""计算时间衰减后的有效置信度"""
if now is None:
now = datetime.now(timezone.utc)
last = datetime.fromisoformat(self.last_updated)
# 转换为月数
delta_months = (now - last).days / 30.0
decayed = self.confidence * math.exp(-self.decay_rate * delta_months)
return round(max(0.0, min(1.0, decayed)), 4)
def to_dict(self) -> dict:
return {
"value": self.value,
"confidence": self.confidence,
"last_updated": self.last_updated,
"decay_rate": self.decay_rate,
"source": self.source,
"evidence_count": self.evidence_count,
}
@classmethod
def from_dict(cls, d: dict) -> "ProfileAttribute":
return cls(
value=d["value"],
confidence=d["confidence"],
last_updated=d["last_updated"],
decay_rate=d.get("decay_rate", 0.05),
source=d.get("source", "unknown"),
evidence_count=d.get("evidence_count", 1),
)
@dataclass
class PurchaseEvent:
"""购买事件,用于育儿阶段推断"""
product_category: str # 如 "formula_stage1", "baby_puree"
purchase_date: str # ISO 8601
brand: Optional[str] = None
price: Optional[float] = None
quantity: int = 1
# ─────────────────────────────────────────────
# 育儿阶段推断器
# ─────────────────────────────────────────────
class ParentingStageInferrer:
"""从购买历史推断育儿阶段与宝宝月龄"""
# 品类 → (最低月龄, 最高月龄, 推断月龄中值)
CATEGORY_AGE_MAP: dict = {
"formula_stage1": (0, 6, 2),
"formula_stage2": (6, 12, 8),
"formula_stage3": (12, 36, 18),
"baby_puree": (4, 8, 6),
"baby_cereal": (4, 12, 7),
"finger_food": (8, 18, 12),
"sippy_cup": (6, 18, 10),
"walker": (9, 15, 12),
"push_toy": (10, 24, 15),
"toddler_snack": (12, 36, 18),
}
@classmethod
def infer_from_purchase(cls, event: PurchaseEvent) -> tuple[int, float]:
"""从单次购买推断宝宝月龄,返回 (月龄, 置信度)"""
cat = event.product_category
if cat not in cls.CATEGORY_AGE_MAP:
return 0, 0.0
_, _, midpoint = cls.CATEGORY_AGE_MAP[cat]
# 置信度取 0.5(单次证据)
return midpoint, 0.5
@classmethod
def infer_from_history(cls, purchases: list[PurchaseEvent]) -> tuple[int, float]:
"""从多次购买历史综合推断月龄(按时间最新的购买权重最高)"""
if not purchases:
return 0, 0.0
# 按购买时间排序(最新的最重要)
sorted_events = sorted(purchases, key=lambda e: e.purchase_date, reverse=True)
weighted_age_sum = 0.0
total_weight = 0.0
confidence_complement = 1.0 # 用于置信度叠加
for idx, event in enumerate(sorted_events):
age, conf = cls.infer_from_purchase(event)
if conf == 0:
continue
# 时间权重递减(最新购买权重1.0,次新0.7,再次0.5...)
time_weight = max(0.1, 1.0 - idx * 0.15)
# 考虑时间流逝:购买后宝宝月龄会增长
purchase_dt = datetime.fromisoformat(event.purchase_date)
months_since = (EVAL_NOW - purchase_dt).days / 30.0
current_age = age + months_since
weighted_age_sum += current_age * time_weight * conf
total_weight += time_weight * conf
confidence_complement *= (1 - conf * time_weight)
if total_weight == 0:
return 0, 0.0
final_age = int(weighted_age_sum / total_weight)
final_confidence = round(min(0.95, 1 - confidence_complement), 3)
return final_age, final_confidence
@classmethod
def age_to_stage(cls, age_months: int) -> ParentingStage:
"""月龄映射到育儿阶段"""
if age_months <= 3:
return ParentingStage.NEWBORN
elif age_months <= 6:
return ParentingStage.INFANT
elif age_months <= 9:
return ParentingStage.CRAWLER
elif age_months <= 12:
return ParentingStage.TODDLER_EARLY
elif age_months <= 18:
return ParentingStage.TODDLER_MID
elif age_months <= 24:
return ParentingStage.TODDLER_LATE
else:
return ParentingStage.PRESCHOOL
# ─────────────────────────────────────────────
# 用户画像存储
# ─────────────────────────────────────────────
class UserProfileStore:
"""跨会话用户画像存储:支持更新/检索/衰减/隐私保护"""
def __init__(self, user_id: str):
self.user_id = user_id
self._attributes: dict[str, ProfileAttribute] = {}
self._inferrer = ParentingStageInferrer()
def _now_iso(self) -> str:
return datetime.now(timezone.utc).isoformat()
# ── 更新 ──────────────────────────────────
def update(
self,
dimension: UserProfileDimension,
value: object,
confidence: float,
decay_rate: float = 0.05,
source: str = "purchase_history",
) -> None:
"""更新或创建画像属性(置信度叠加)"""
key = dimension.value
if key in self._attributes:
existing = self._attributes[key]
# 置信度叠加:新证据提升置信度
combined_conf = min(0.97, 1 - (1 - existing.confidence) * (1 - confidence))
self._attributes[key] = ProfileAttribute(
value=value,
confidence=round(combined_conf, 4),
last_updated=self._now_iso(),
decay_rate=decay_rate,
source=source,
evidence_count=existing.evidence_count + 1,
)
else:
self._attributes[key] = ProfileAttribute(
value=value,
confidence=confidence,
last_updated=self._now_iso(),
decay_rate=decay_rate,
source=source,
evidence_count=1,
)
def ingest_purchase(self, event: PurchaseEvent) -> None:
"""从购买事件更新画像(品牌偏好 + 价格区间 + 品类亲和度)"""
if event.brand:
self.update(
UserProfileDimension.BRAND_PREFERENCE,
event.brand,
confidence=0.6,
decay_rate=0.05,
source="purchase",
)
if event.price:
self.update(
UserProfileDimension.PRICE_SENSITIVITY,
round(event.price, 2),
confidence=0.5,
decay_rate=0.1,
source="purchase",
)
cat_key = event.product_category
self.update(
UserProfileDimension.CATEGORY_AFFINITY,
cat_key,
confidence=0.55,
decay_rate=0.08,
source="purchase",
)
def ingest_purchase_history(self, purchases: list[PurchaseEvent]) -> None:
"""批量处理购买历史,更新育儿阶段画像"""
for event in purchases:
self.ingest_purchase(event)
# 综合推断宝宝月龄
age, conf = ParentingStageInferrer.infer_from_history(purchases)
if conf > 0.1:
stage = ParentingStageInferrer.age_to_stage(age)
self.update(
UserProfileDimension.BABY_AGE_MONTHS,
age,
confidence=conf,
decay_rate=0.01, # 月龄极慢衰减
source="inferred_from_purchases",
)
self.update(
UserProfileDimension.PARENTING_STAGE,
stage.value,
confidence=conf,
decay_rate=0.01,
source="inferred_from_purchases",
)
# ── 检索 ──────────────────────────────────
def get(
self,
dimension: UserProfileDimension,
min_confidence: float = 0.2,
) -> Optional[ProfileAttribute]:
"""检索画像属性,低于置信度阈值返回 None"""
attr = self._attributes.get(dimension.value)
if attr is None:
return None
if attr.effective_confidence() < min_confidence:
return None
return attr
def get_all_active(self, min_confidence: float = 0.2) -> dict:
"""返回所有有效置信度超过阈值的画像维度"""
result = {}
now = datetime.now(timezone.utc)
for key, attr in self._attributes.items():
eff_conf = attr.effective_confidence(now)
if eff_conf >= min_confidence:
result[key] = {
"value": attr.value,
"effective_confidence": eff_conf,
"evidence_count": attr.evidence_count,
"source": attr.source,
}
return result
# ── 隐私保护 ──────────────────────────────
def reset(self) -> None:
"""用户主动重置画像(GDPR/CCPA 合规)"""
self._attributes.clear()
def export_anonymized(self) -> dict:
"""导出脱敏画像(不含用户 ID 原始信息)"""
return {
"dimensions": {k: v.to_dict() for k, v in self._attributes.items()},
"exported_at": self._now_iso(),
}
# ── 序列化 ────────────────────────────────
def pseudonym(self) -> str:
"""用户 ID 的**伪标识**(pseudonym),不是匿名化。
刻意不用内建 hash():它对 str 按进程加盐(PYTHONHASHSEED),
同一个 user_id 在两个进程里得到两个数——跨会话根本关联不起来,
而它也不是任何意义上的脱敏(10^9 个桶照样能字典还原)。
SHA-256 截断只保证**稳定可关联**;要真做到不可还原,
必须换成服务端密钥的 HMAC,且密钥不落进同一份存储。
"""
return hashlib.sha256(self.user_id.encode("utf-8")).hexdigest()[:16]
def to_json(self) -> str:
data = {
"user_id_pseudonym": self.pseudonym(), # 标识化:可关联,但可被字典还原
"attributes": {k: v.to_dict() for k, v in self._attributes.items()},
}
return json.dumps(data, ensure_ascii=False, indent=2)
@classmethod
def from_json(cls, user_id: str, json_str: str) -> "UserProfileStore":
store = cls(user_id)
data = json.loads(json_str)
for key, attr_dict in data.get("attributes", {}).items():
store._attributes[key] = ProfileAttribute.from_dict(attr_dict)
return store
# ─────────────────────────────────────────────
# 测试验证
# ─────────────────────────────────────────────
def test_user_profile_accumulation() -> None:
"""5次购买历史,验证画像积累和育儿阶段推断"""
print("=" * 55)
print("测试:用户画像积累 + 育儿阶段推断")
print("=" * 55)
store = UserProfileStore(user_id="user_001")
# 模拟 5 次购买历史(宝宝约 3 月龄时开始购买)
purchases = [
PurchaseEvent("formula_stage1", "2026-03-01T10:00:00+00:00", brand="Aptamil", price=72.0),
PurchaseEvent("formula_stage1", "2026-03-28T14:00:00+00:00", brand="Aptamil", price=72.0),
PurchaseEvent("baby_cereal", "2026-04-15T09:00:00+00:00", brand="Gerber", price=18.5),
PurchaseEvent("baby_puree", "2026-05-01T11:00:00+00:00", brand="Ella's", price=24.0),
PurchaseEvent("formula_stage2", "2026-05-20T16:00:00+00:00", brand="Aptamil", price=78.0),
]
store.ingest_purchase_history(purchases)
print("\n【活跃画像维度】")
active = store.get_all_active(min_confidence=0.15)
for dim, info in active.items():
print(f" {dim:30s} → value={info['value']!s:20s} conf={info['effective_confidence']:.3f} "
f"(n={info['evidence_count']})")
# 验证育儿阶段
stage_attr = store.get(UserProfileDimension.PARENTING_STAGE)
age_attr = store.get(UserProfileDimension.BABY_AGE_MONTHS)
brand_attr = store.get(UserProfileDimension.BRAND_PREFERENCE)
print("\n【关键断言】")
assert stage_attr is not None, "❌ 育儿阶段未推断"
assert age_attr is not None, "❌ 宝宝月龄未推断"
assert brand_attr is not None, "❌ 品牌偏好未记录"
assert brand_attr.value == "Aptamil", f"❌ 品牌应为 Aptamil,实际={brand_attr.value}"
inferred_age = age_attr.value
# 购买跨度 2026-03-01 → 2026-05-20,以 EVAL_NOW=2026-06-01 为当前时刻:
# 宝宝约从 3 月龄开始购买 Stage 2,推断落在 7 月(爬行期),不再是 3-15 的宽区间
assert inferred_age == 7, f"❌ 推断月龄应为 7(EVAL_NOW 锚定后是确定值),实际={inferred_age}"
assert stage_attr.value == ParentingStage.CRAWLER.value, f"❌ 阶段异常: {stage_attr.value}"
print(f" ✅ 育儿阶段推断: {stage_attr.value} (conf={stage_attr.effective_confidence():.3f})")
print(f" ✅ 宝宝月龄推断: {inferred_age} 月 (conf={age_attr.effective_confidence():.3f})")
print(f" ✅ 品牌偏好记录: {brand_attr.value} (conf={brand_attr.effective_confidence():.3f})")
print(f" ✅ 价格区间记录: {store.get(UserProfileDimension.PRICE_SENSITIVITY).value}")
# 测试序列化往返
json_str = store.to_json()
restored = UserProfileStore.from_json("user_001", json_str)
assert restored.get(UserProfileDimension.PARENTING_STAGE) is not None
print(" ✅ JSON 序列化/反序列化 往返正常")
# ── 标识化 ≠ 匿名化(这条断言要求"能被还原"才算通过)──
pseudo = json.loads(json_str)["user_id_pseudonym"]
print(f"\n【隐私边界】落盘的伪标识(非原始 ID): {pseudo}")
candidate_ids = ["user_001", "user_002", "user_003"]
recovered = {
uid: hashlib.sha256(uid.encode("utf-8")).hexdigest()[:16]
for uid in candidate_ids
}
hit = [uid for uid, h in recovered.items() if h == pseudo]
assert hit == ["user_001"], f"字典攻击应还原出 user_001,实际={hit}"
print(f" ⚠ 仅用 {len(candidate_ids)} 个候选 ID 的字典即可反查命中 → {hit[0]}")
print(" ⚠ 结论:SHA-256 摘要=标识化(pseudonymisation),不是匿名化;"
"需密钥 HMAC 或不可逆聚合才谈得上匿名")
print("\n✅ 所有断言通过!")
if __name__ == "__main__":
test_user_profile_accumulation()
print("[✓] User Profile Long Memory 测试通过")8. 论文来源
论文链接用于核验方法来源,不自动证明本页商业结果。
未自动抽取;请查看原始 Skill 卡片。