UserProfiler 让 AI 在聊天过程中自动学习用户信息——不需要用户主动填写,不需要问卷,只需要正常对话。
| 类别 | 示例提取 |
|------|---------|
| 身份 | "我叫小明" → name: 小明 |
| 偏好 | "我喜欢Python" → prefer: Python |
| 地点 | "我在上海" → location: 上海 |
| 职业 | "我是程序员" → job: 程序员 |
| 负向反馈 | "别说那么正式" → dislike: 正式口吻 |
| 习惯 | "我每天晚上11点睡" → habit: 晚睡 |
from user_profiler import UserProfiler
profiler = UserProfiler()
# 1. 从对话中学习
profiler.learn("小明", "我叫小明,是个Python程序员,住在北京")
profiler.learn("小明", "我喜欢简洁的回复,别废话太多")
# 2. 获取用户画像
profile = profiler.get_profile("小明")
print(profile)
# {
# "name": "小明",
# "job": "Python程序员",
# "location": "北京",
# "prefer": ["简洁回复"],
# "dislike": ["废话"]
# }
# 3. 生成个性化系统提示词
system_prompt = profiler.generate_system_prompt("小明")
# "用户叫小明,程序员,在北京。偏好:简洁回复。请注意:别废话太多。"
# 4. 查询某个字段
name = profiler.get("小明", "name") # → "小明"
# 5. 手动更新
profiler.update("小明", "prefer", "用中文回复")
# 6. 导出画像
profiler.export("小明", "profile_小明.json")
用户说话
↓ 关键词匹配 + 句式模板
提取实体(姓名/地点/职业/偏好)
↓ 置信度评估
写入用户画像(JSON持久化)
↓ 下次对话
自动注入到 AI 上下文
jieba 提升中文分词效果
共 1 个版本