Metadata-Version: 2.4
Name: mem1
Version: 0.0.5
Summary: 基于云服务的用户记忆系统
Project-URL: Homepage, https://github.com/sougannkyou/mem1
Project-URL: Repository, https://github.com/sougannkyou/mem1
Author: Song
License: MIT
Keywords: langchain,llm,memory,user-profile
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.12
Requires-Dist: elasticsearch>=8.0.0
Requires-Dist: openai>=1.0.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: python-dotenv>=1.0.0
Provides-Extra: dev
Requires-Dist: ipython>=8.0.0; extra == 'dev'
Description-Content-Type: text/markdown

# mem1 - 用户记忆系统

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

## 核心特性

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

## 安装

```bash
pip install mem1
```

## 快速开始

```python
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'])     # 当前时间
```

## 环境变量配置

```bash
# 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
```

## 核心接口

```python
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
