User Profile Injection
User Profile Injection 是将用户画像(年龄、偏好、历史行为)注入到 LLM 上下文的技术,让 Agent 能够提供个性化的服务。
#type / concept
#status / evergreen
#tech / ai
[!info] related notes
- 所属 MOC: Context Engineering MOC
- 相关: Long-term Memory, Prompt Assembly
User Profile Injection
一句话定义
User Profile Injection 是将用户画像注入到 LLM 上下文的技术。用户说”帮我推荐训练计划”,如果 Agent 知道他”35 岁、有腰椎间盘突出、每周跑步 3 次”,就能给出更精准的建议。
核心原理
注入方式
def build_user_profile_context(profile: dict) -> str:
"""构建用户画像上下文"""
parts = []
if profile.get("age"):
parts.append(f"年龄: {profile['age']}")
if profile.get("gender"):
parts.append(f"性别: {profile['gender']}")
if profile.get("exercise_habit"):
parts.append(f"运动习惯: {profile['exercise_habit']}")
if profile.get("known_limitations"):
parts.append(f"已知限制: {', '.join(profile['known_limitations'])}")
return "用户信息:\n" + "\n".join(parts)
注入位置
System Prompt
+
用户画像: ← 注入这里
年龄: 35
运动习惯: 每周跑步3次
已知限制: 腰椎间盘突出
+
对话历史
+
当前输入
常见坑
- 注入太多: 用户的所有信息都注入,浪费 token
- 不更新: 用户偏好变了但画像没更新
- 隐私问题: 不应该注入敏感信息到 LLM
- 注入格式不好: LLM 难以理解结构