Skip to main content

基于云服务的用户记忆系统

Project description

mem1 - 用户记忆系统

让 AI 真正"记住"用户:三层记忆架构 + 图片记忆 + 话题隔离 + 业务场景解耦。

核心特性

  • 三层记忆架构:短期会话 → 用户画像 → 长期记录
  • 话题隔离:同一用户可有多个话题,对话按话题隔离,画像跨话题共享
  • 图片记忆:支持存储和语义搜索用户发送的图片
  • 业务解耦:通过 ProfileTemplate 适配不同场景
  • 画像自动更新:基于对话轮数/时间自动触发 LLM 更新用户画像

安装

pip install mem1

快速开始

from mem1 import Mem1Memory, Mem1Config

# 从环境变量加载配置
config = Mem1Config.from_env()

# 创建记忆实例(绑定用户和话题)
memory = Mem1Memory(config, user_id="user001", topic_id="project_a")

# 添加对话
memory.add_conversation(
    messages=[
        {"role": "user", "content": "你好,我是张明"},
        {"role": "assistant", "content": "你好张明!"}
    ]
)

# 获取上下文(含用户画像 + 最近对话)
ctx = memory.get_context()
print(ctx['import_content'])   # 用户画像
print(ctx['normal_content'])   # 最近对话记录
print(ctx['current_time'])     # 当前时间

环境变量配置

# LLM 配置
MEM1_LLM_API_KEY=your-api-key
MEM1_LLM_BASE_URL=https://api.deepseek.com
MEM1_LLM_MODEL=deepseek-chat

# ES 配置
MEM1_ES_HOSTS=http://localhost:9200
MEM1_ES_INDEX=conversation_history

# 记忆配置
MEM1_MEMORY_DIR=./memories
MEM1_AUTO_UPDATE_PROFILE=true
MEM1_MAX_PROFILE_CHARS=3000
MEM1_UPDATE_INTERVAL_ROUNDS=5
MEM1_UPDATE_INTERVAL_MINUTES=3
MEM1_SAVE_ASSISTANT_MESSAGES=true
MEM1_MAX_ASSISTANT_CHARS=500
MEM1_CONTEXT_DAYS_LIMIT=31

核心接口

memory = Mem1Memory(config, user_id="user001", topic_id="project_a")

# 添加对话
memory.add_conversation(messages=[...], images=[...], metadata={...})

# 获取上下文(画像 + 最近 N 天对话)
ctx = memory.get_context(days_limit=31)

# 查询对话
convs = memory.get_conversations(days_limit=7)
all_convs = memory.get_all_conversations(days_limit=7)

# 图片搜索
results = memory.search_images(query="麻花")

# 话题管理
topics = memory.list_topics()
memory.delete_topic()
memory.delete_user()

ES 索引

索引 用途
conversation_history 对话记录
mem1_user_state 用户状态
mem1_user_profile 用户画像

LLM 提示词建议

使用 get_context() 获取上下文后,建议在 system prompt 中加入以下规则,避免 LLM 编造信息:

## 重要规则
1. 回答必须基于上述对话记录中的实际内容,严禁编造任何信息
2. 涉及数字(金额、数量、百分比、日期等)时,必须从对话记录中原样提取,不得估算或编造
3. 需要汇总累加时,必须列出计算过程(如:23+31+18+25=97)
4. 涉及人名、公司名、账号名等实体时,必须使用对话中的原始名称
5. 如果对话记录中没有相关信息,请明确说"对话记录中未提及",不要猜测

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mem1-0.0.5.tar.gz (2.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mem1-0.0.5-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file mem1-0.0.5.tar.gz.

File metadata

  • Download URL: mem1-0.0.5.tar.gz
  • Upload date:
  • Size: 2.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for mem1-0.0.5.tar.gz
Algorithm Hash digest
SHA256 c5e833a9d044a6538b9a6bdbacf420e7855df85aaa44abd27cd17af49198dafd
MD5 b10666d8f79469ec873522615d8801cd
BLAKE2b-256 e2d30e00488001aaa9b5fc5a248a9697c8e01f0f133776ab6f4fba8c1739d84b

See more details on using hashes here.

File details

Details for the file mem1-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: mem1-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for mem1-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cfffc0be791369d2468bbdee136a652d475bff5502f55a9314cab42c00a807e2
MD5 00393f3e6eca294be7b2f38b7781d29c
BLAKE2b-256 695b8be91295595bca6b52d73a7ddf3e1dfeac3736f0a6bffcca5d1193776249

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page