mem1 0.0.5__py3-none-any.whl → 0.0.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mem1/__init__.py +6 -0
- mem1/config.py +33 -0
- mem1/llm.py +98 -2
- mem1/memory_es.py +225 -50
- mem1/prompts.py +55 -6
- mem1-0.0.7.dist-info/METADATA +249 -0
- mem1-0.0.7.dist-info/RECORD +11 -0
- mem1-0.0.5.dist-info/METADATA +0 -139
- mem1-0.0.5.dist-info/RECORD +0 -11
- {mem1-0.0.5.dist-info → mem1-0.0.7.dist-info}/WHEEL +0 -0
mem1/__init__.py
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Mem1 - 基于 Elasticsearch 的用户记忆系统
|
|
3
3
|
"""
|
|
4
|
+
import logging
|
|
4
5
|
|
|
5
6
|
__version__ = "0.0.5"
|
|
6
7
|
|
|
8
|
+
# 屏蔽第三方库的详细日志(必须在导入前设置)
|
|
9
|
+
logging.getLogger("elastic_transport").setLevel(logging.WARNING)
|
|
10
|
+
logging.getLogger("elastic_transport.transport").setLevel(logging.WARNING)
|
|
11
|
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
12
|
+
|
|
7
13
|
from mem1.memory_es import Mem1Memory
|
|
8
14
|
from mem1.config import Mem1Config, LLMConfig
|
|
9
15
|
|
mem1/config.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""配置管理"""
|
|
2
2
|
import os
|
|
3
|
+
from typing import Optional
|
|
3
4
|
from pydantic import BaseModel
|
|
4
5
|
|
|
5
6
|
|
|
@@ -11,6 +12,24 @@ class LLMConfig(BaseModel):
|
|
|
11
12
|
base_url: str
|
|
12
13
|
|
|
13
14
|
|
|
15
|
+
class VLConfig(BaseModel):
|
|
16
|
+
"""视觉语言模型配置(可选,配置了 provider 即启用)
|
|
17
|
+
|
|
18
|
+
支持的 provider:
|
|
19
|
+
- qwen: 使用 dashscope SDK 调用 Qwen-VL
|
|
20
|
+
- doubao: 使用 OpenAI 兼容接口调用豆包视觉模型
|
|
21
|
+
"""
|
|
22
|
+
provider: str = "" # qwen / doubao
|
|
23
|
+
model: str = ""
|
|
24
|
+
api_key: str = ""
|
|
25
|
+
base_url: str = "" # doubao 需要
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def enabled(self) -> bool:
|
|
29
|
+
"""配置了 provider 就启用"""
|
|
30
|
+
return bool(self.provider)
|
|
31
|
+
|
|
32
|
+
|
|
14
33
|
class MemoryConfig(BaseModel):
|
|
15
34
|
"""记忆系统配置"""
|
|
16
35
|
memory_dir: str
|
|
@@ -37,6 +56,7 @@ class ImagesConfig(BaseModel):
|
|
|
37
56
|
class Mem1Config(BaseModel):
|
|
38
57
|
"""Mem1 总配置"""
|
|
39
58
|
llm: LLMConfig
|
|
59
|
+
vl: VLConfig
|
|
40
60
|
memory: MemoryConfig
|
|
41
61
|
es: ESConfig
|
|
42
62
|
images: ImagesConfig
|
|
@@ -56,6 +76,10 @@ class Mem1Config(BaseModel):
|
|
|
56
76
|
- MEM1_MAX_PROFILE_CHARS: 画像最大字符数
|
|
57
77
|
- MEM1_UPDATE_INTERVAL_ROUNDS: 画像更新间隔轮数
|
|
58
78
|
- MEM1_UPDATE_INTERVAL_MINUTES: 画像更新间隔分钟数
|
|
79
|
+
|
|
80
|
+
可选的环境变量(VL 模型,使用 dashscope SDK):
|
|
81
|
+
- MEM1_VL_MODEL: VL 模型名(如 qwen-vl-max),配置即启用
|
|
82
|
+
- MEM1_VL_API_KEY: dashscope API 密钥
|
|
59
83
|
"""
|
|
60
84
|
# 必需配置检查
|
|
61
85
|
required_vars = {
|
|
@@ -84,6 +108,14 @@ class Mem1Config(BaseModel):
|
|
|
84
108
|
memory_dir = required_vars["MEM1_MEMORY_DIR"]
|
|
85
109
|
images_dir = f"{memory_dir}/images"
|
|
86
110
|
|
|
111
|
+
# VL 模型配置(可选,配置了 provider 即启用)
|
|
112
|
+
vl_config = VLConfig(
|
|
113
|
+
provider=os.getenv("MEM1_VL_PROVIDER", ""),
|
|
114
|
+
model=os.getenv("MEM1_VL_MODEL", ""),
|
|
115
|
+
api_key=os.getenv("MEM1_VL_API_KEY", ""),
|
|
116
|
+
base_url=os.getenv("MEM1_VL_BASE_URL", "")
|
|
117
|
+
)
|
|
118
|
+
|
|
87
119
|
return cls(
|
|
88
120
|
llm=LLMConfig(
|
|
89
121
|
provider="openai",
|
|
@@ -91,6 +123,7 @@ class Mem1Config(BaseModel):
|
|
|
91
123
|
api_key=required_vars["MEM1_LLM_API_KEY"],
|
|
92
124
|
base_url=required_vars["MEM1_LLM_BASE_URL"]
|
|
93
125
|
),
|
|
126
|
+
vl=vl_config,
|
|
94
127
|
memory=MemoryConfig(
|
|
95
128
|
memory_dir=memory_dir,
|
|
96
129
|
auto_update_profile=required_vars["MEM1_AUTO_UPDATE_PROFILE"].lower() == "true",
|
mem1/llm.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""LLM 客户端"""
|
|
2
|
-
from typing import List, Dict
|
|
2
|
+
from typing import List, Dict, Optional
|
|
3
3
|
from openai import OpenAI
|
|
4
|
-
from mem1.config import LLMConfig
|
|
4
|
+
from mem1.config import LLMConfig, VLConfig
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
class LLMClient:
|
|
@@ -41,3 +41,99 @@ class LLMClient:
|
|
|
41
41
|
response = self.client.chat.completions.create(**kwargs)
|
|
42
42
|
|
|
43
43
|
return response.choices[0].message.content
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class VLClient:
|
|
47
|
+
"""视觉语言模型客户端(支持 qwen/doubao)"""
|
|
48
|
+
|
|
49
|
+
def __init__(self, config: VLConfig):
|
|
50
|
+
self.config = config
|
|
51
|
+
self.provider = config.provider.lower()
|
|
52
|
+
|
|
53
|
+
if self.provider == "qwen":
|
|
54
|
+
import dashscope
|
|
55
|
+
dashscope.api_key = config.api_key
|
|
56
|
+
elif self.provider == "doubao":
|
|
57
|
+
self.client = OpenAI(
|
|
58
|
+
api_key=config.api_key,
|
|
59
|
+
base_url=config.base_url
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def understand_image(
|
|
63
|
+
self,
|
|
64
|
+
image_path: str,
|
|
65
|
+
user_description: str = ""
|
|
66
|
+
) -> str:
|
|
67
|
+
"""理解图片内容(OCR + 图片理解)
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
image_path: 图片本地路径
|
|
71
|
+
user_description: 用户对图片的描述(可选)
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
图片理解结果(包含 OCR 文字和内容描述)
|
|
75
|
+
"""
|
|
76
|
+
prompt = "请分析这张图片,完成以下任务:\n1. OCR识别:提取图片中的所有文字\n2. 内容理解:描述图片的主要内容和关键信息\n\n请用简洁的中文回答,格式如下:\n【文字内容】...\n【图片描述】..."
|
|
77
|
+
|
|
78
|
+
if user_description:
|
|
79
|
+
prompt += f"\n\n用户补充说明:{user_description}"
|
|
80
|
+
|
|
81
|
+
if self.provider == "qwen":
|
|
82
|
+
return self._call_qwen(image_path, prompt)
|
|
83
|
+
elif self.provider == "doubao":
|
|
84
|
+
return self._call_doubao(image_path, prompt)
|
|
85
|
+
else:
|
|
86
|
+
raise ValueError(f"不支持的 VL provider: {self.provider}")
|
|
87
|
+
|
|
88
|
+
def _call_qwen(self, image_path: str, prompt: str) -> str:
|
|
89
|
+
"""调用 Qwen-VL(dashscope SDK)"""
|
|
90
|
+
import dashscope
|
|
91
|
+
|
|
92
|
+
messages = [
|
|
93
|
+
{
|
|
94
|
+
"role": "user",
|
|
95
|
+
"content": [
|
|
96
|
+
{"image": image_path},
|
|
97
|
+
{"text": prompt}
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
response = dashscope.MultiModalConversation.call(
|
|
103
|
+
model=self.config.model,
|
|
104
|
+
messages=messages
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
return response.output.choices[0].message.content[0]["text"]
|
|
108
|
+
|
|
109
|
+
def _call_doubao(self, image_path: str, prompt: str) -> str:
|
|
110
|
+
"""调用豆包视觉模型(OpenAI 兼容接口)"""
|
|
111
|
+
import base64
|
|
112
|
+
|
|
113
|
+
# 读取图片并转为 base64
|
|
114
|
+
with open(image_path, "rb") as f:
|
|
115
|
+
image_data = base64.b64encode(f.read()).decode("utf-8")
|
|
116
|
+
|
|
117
|
+
# 获取图片格式
|
|
118
|
+
ext = image_path.lower().split(".")[-1]
|
|
119
|
+
mime_type = {"png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg", "gif": "image/gif", "webp": "image/webp"}.get(ext, "image/png")
|
|
120
|
+
|
|
121
|
+
messages = [
|
|
122
|
+
{
|
|
123
|
+
"role": "user",
|
|
124
|
+
"content": [
|
|
125
|
+
{
|
|
126
|
+
"type": "image_url",
|
|
127
|
+
"image_url": {"url": f"data:{mime_type};base64,{image_data}"}
|
|
128
|
+
},
|
|
129
|
+
{"type": "text", "text": prompt}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
|
|
134
|
+
response = self.client.chat.completions.create(
|
|
135
|
+
model=self.config.model,
|
|
136
|
+
messages=messages
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
return response.choices[0].message.content
|
mem1/memory_es.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"""基于 Elasticsearch 的记忆管理系统"""
|
|
2
|
-
import
|
|
2
|
+
import re
|
|
3
3
|
import shutil
|
|
4
4
|
import base64
|
|
5
5
|
import logging
|
|
@@ -8,8 +8,8 @@ from typing import List, Dict, Any, Optional
|
|
|
8
8
|
from pathlib import Path
|
|
9
9
|
from elasticsearch import Elasticsearch
|
|
10
10
|
from mem1.config import Mem1Config
|
|
11
|
-
from mem1.llm import LLMClient
|
|
12
|
-
from mem1.prompts import ProfileTemplate, RECALL_DECISION_PROMPT, IMAGE_SEARCH_PROMPT, ASSISTANT_SUMMARY_PROMPT
|
|
11
|
+
from mem1.llm import LLMClient, VLClient
|
|
12
|
+
from mem1.prompts import ProfileTemplate, RECALL_DECISION_PROMPT, IMAGE_SEARCH_PROMPT, ASSISTANT_SUMMARY_PROMPT, CONTEXT_SUFFICIENT_PROMPT
|
|
13
13
|
|
|
14
14
|
logger = logging.getLogger(__name__)
|
|
15
15
|
|
|
@@ -23,10 +23,10 @@ class Mem1Memory:
|
|
|
23
23
|
"""基于 Elasticsearch 的用户记忆系统
|
|
24
24
|
|
|
25
25
|
数据存储(全部在 ES):
|
|
26
|
-
- ES 索引
|
|
26
|
+
- ES 索引 {index_name}: 历史对话记录 + 图片索引(按 user_id + topic_id 隔离)
|
|
27
27
|
- ES 索引 mem1_user_state: 用户更新状态(轮数、上次更新时间)
|
|
28
28
|
- ES 索引 mem1_user_profile: 用户画像(按 user_id 共享,跨话题)
|
|
29
|
-
-
|
|
29
|
+
- 本地文件: 图片文件存储
|
|
30
30
|
"""
|
|
31
31
|
|
|
32
32
|
def __init__(
|
|
@@ -63,6 +63,9 @@ class Mem1Memory:
|
|
|
63
63
|
# LLM 客户端
|
|
64
64
|
self.llm = LLMClient(config.llm)
|
|
65
65
|
|
|
66
|
+
# VL 客户端(可选)
|
|
67
|
+
self.vl = VLClient(config.vl) if config.vl.enabled else None
|
|
68
|
+
|
|
66
69
|
# 业务场景模板
|
|
67
70
|
self.profile_template = profile_template or ProfileTemplate()
|
|
68
71
|
|
|
@@ -74,7 +77,7 @@ class Mem1Memory:
|
|
|
74
77
|
self.save_assistant_messages = config.memory.save_assistant_messages
|
|
75
78
|
self.max_assistant_chars = config.memory.max_assistant_chars
|
|
76
79
|
|
|
77
|
-
#
|
|
80
|
+
# 确保索引存在
|
|
78
81
|
self._ensure_state_index()
|
|
79
82
|
|
|
80
83
|
def _get_user_images_dir(self, user_id: str) -> Path:
|
|
@@ -83,21 +86,36 @@ class Mem1Memory:
|
|
|
83
86
|
images_dir.mkdir(parents=True, exist_ok=True)
|
|
84
87
|
return images_dir
|
|
85
88
|
|
|
86
|
-
def _get_images_index_path(self, user_id: str) -> Path:
|
|
87
|
-
"""获取图片索引文件路径"""
|
|
88
|
-
return self.images_dir / user_id / "_images.json"
|
|
89
|
-
|
|
90
89
|
def _load_images_index(self, user_id: str) -> List[Dict[str, str]]:
|
|
91
|
-
"""
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
"""从对话记录中提取用户所有图片"""
|
|
91
|
+
try:
|
|
92
|
+
response = self.es.search(
|
|
93
|
+
index=self.index_name,
|
|
94
|
+
query={
|
|
95
|
+
"bool": {
|
|
96
|
+
"must": [
|
|
97
|
+
{"term": {"user_id": user_id}},
|
|
98
|
+
{"exists": {"field": "images"}}
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
size=1000,
|
|
103
|
+
sort=[{"timestamp": {"order": "asc"}}]
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
images = []
|
|
107
|
+
for hit in response["hits"]["hits"]:
|
|
108
|
+
conv_images = hit["_source"].get("images", [])
|
|
109
|
+
images.extend(conv_images)
|
|
110
|
+
return images
|
|
111
|
+
except Exception:
|
|
112
|
+
return []
|
|
96
113
|
|
|
97
|
-
def
|
|
98
|
-
"""
|
|
99
|
-
|
|
100
|
-
|
|
114
|
+
def _save_image_to_conversation(self, conversation_entry: Dict, image_doc: Dict[str, str]) -> None:
|
|
115
|
+
"""将图片信息添加到对话记录"""
|
|
116
|
+
if "images" not in conversation_entry:
|
|
117
|
+
conversation_entry["images"] = []
|
|
118
|
+
conversation_entry["images"].append(image_doc)
|
|
101
119
|
|
|
102
120
|
def _get_profile(self, user_id: str) -> Optional[str]:
|
|
103
121
|
"""从 ES 获取用户画像"""
|
|
@@ -264,11 +282,19 @@ class Mem1Memory:
|
|
|
264
282
|
user_id = self.user_id
|
|
265
283
|
topic_id = self.topic_id
|
|
266
284
|
|
|
285
|
+
# 构建对话记录
|
|
286
|
+
conversation_entry = {
|
|
287
|
+
"user_id": user_id,
|
|
288
|
+
"topic_id": topic_id,
|
|
289
|
+
"timestamp": ts,
|
|
290
|
+
"messages": [],
|
|
291
|
+
"metadata": metadata or {}
|
|
292
|
+
}
|
|
293
|
+
|
|
267
294
|
# 处理图片
|
|
268
295
|
image_refs = []
|
|
269
296
|
if images:
|
|
270
297
|
user_images_dir = self._get_user_images_dir(user_id)
|
|
271
|
-
images_index = self._load_images_index(user_id)
|
|
272
298
|
timestamp_str = datetime.now().strftime('%Y%m%d_%H%M%S')
|
|
273
299
|
|
|
274
300
|
for img in images:
|
|
@@ -281,46 +307,49 @@ class Mem1Memory:
|
|
|
281
307
|
elif 'path' in img:
|
|
282
308
|
shutil.copy(img['path'], img_path)
|
|
283
309
|
|
|
284
|
-
|
|
285
|
-
|
|
310
|
+
image_refs.append(filename)
|
|
311
|
+
|
|
312
|
+
# 生成图片描述(用户描述 + VL 理解)
|
|
313
|
+
user_desc = ""
|
|
314
|
+
for msg in messages:
|
|
315
|
+
if msg["role"] == "user":
|
|
316
|
+
user_desc = msg["content"]
|
|
317
|
+
break
|
|
286
318
|
|
|
287
|
-
|
|
288
|
-
if
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
319
|
+
# 如果启用了 VL 模型,调用视觉理解
|
|
320
|
+
if self.vl:
|
|
321
|
+
try:
|
|
322
|
+
vl_result = self.vl.understand_image(str(img_path), user_desc)
|
|
323
|
+
if user_desc:
|
|
324
|
+
description = f"【用户描述】{user_desc}\n\n{vl_result}"
|
|
325
|
+
else:
|
|
326
|
+
description = vl_result
|
|
327
|
+
logger.info(f"🖼️ VL 图片理解完成: {filename}")
|
|
328
|
+
except Exception as e:
|
|
329
|
+
logger.warning(f"⚠️ VL 图片理解失败: {e}, 使用用户描述")
|
|
330
|
+
description = user_desc or img['filename']
|
|
331
|
+
else:
|
|
332
|
+
description = user_desc or img['filename']
|
|
293
333
|
|
|
294
|
-
|
|
334
|
+
# 图片信息存入对话记录
|
|
335
|
+
self._save_image_to_conversation(conversation_entry, {
|
|
295
336
|
"filename": filename,
|
|
296
|
-
"path": rel_path,
|
|
297
337
|
"description": description,
|
|
298
338
|
"timestamp": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
|
299
339
|
"original_name": img['filename']
|
|
300
340
|
})
|
|
301
|
-
|
|
302
|
-
self._save_images_index(user_id, images_index)
|
|
303
|
-
|
|
304
|
-
# 构建对话记录(包含 topic_id)
|
|
305
|
-
conversation_entry = {
|
|
306
|
-
"user_id": user_id,
|
|
307
|
-
"topic_id": topic_id,
|
|
308
|
-
"timestamp": ts,
|
|
309
|
-
"messages": [],
|
|
310
|
-
"metadata": metadata or {}
|
|
311
|
-
}
|
|
312
341
|
|
|
342
|
+
# 处理消息
|
|
313
343
|
first_user_msg = True
|
|
314
344
|
for msg in messages:
|
|
315
345
|
if msg["role"] == "user":
|
|
316
346
|
msg_obj = {"role": "user", "content": msg["content"]}
|
|
317
347
|
if first_user_msg and image_refs:
|
|
318
|
-
msg_obj["
|
|
348
|
+
msg_obj["image_refs"] = image_refs
|
|
319
349
|
first_user_msg = False
|
|
320
350
|
conversation_entry["messages"].append(msg_obj)
|
|
321
351
|
elif self.save_assistant_messages and msg["role"] == "assistant":
|
|
322
352
|
content = msg["content"]
|
|
323
|
-
# 超长回复触发摘要
|
|
324
353
|
if len(content) > self.max_assistant_chars:
|
|
325
354
|
content = self._summarize_assistant_response(content)
|
|
326
355
|
conversation_entry["messages"].append({
|
|
@@ -515,6 +544,148 @@ class Mem1Memory:
|
|
|
515
544
|
"profile_last_updated": profile_last_updated
|
|
516
545
|
}
|
|
517
546
|
|
|
547
|
+
def get_context_progressive(
|
|
548
|
+
self,
|
|
549
|
+
query: str,
|
|
550
|
+
max_days: int = 31,
|
|
551
|
+
step: int = 7
|
|
552
|
+
) -> Dict[str, Any]:
|
|
553
|
+
"""渐进式检索:每次多查一周,直到 LLM 认为信息足够
|
|
554
|
+
|
|
555
|
+
Args:
|
|
556
|
+
query: 用户问题
|
|
557
|
+
max_days: 最大检索天数,默认31天
|
|
558
|
+
step: 每步增加的天数,默认7天
|
|
559
|
+
"""
|
|
560
|
+
user_id = self.user_id
|
|
561
|
+
profile_content = self._init_profile(user_id)
|
|
562
|
+
|
|
563
|
+
now = datetime.now()
|
|
564
|
+
weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
|
|
565
|
+
current_time = f"{now.strftime('%Y-%m-%d %H:%M')} {weekdays[now.weekday()]}"
|
|
566
|
+
|
|
567
|
+
# 从 ES 获取画像更新时间
|
|
568
|
+
profile_last_updated = "未更新"
|
|
569
|
+
try:
|
|
570
|
+
response = self.es.get(index=USER_PROFILE_INDEX, id=user_id)
|
|
571
|
+
profile_last_updated = response["_source"].get("updated_at", "未更新")
|
|
572
|
+
except Exception:
|
|
573
|
+
pass
|
|
574
|
+
|
|
575
|
+
# 渐进式检索
|
|
576
|
+
all_conversations = []
|
|
577
|
+
searched_days = 0
|
|
578
|
+
|
|
579
|
+
for end_day in range(step, max_days + step, step):
|
|
580
|
+
end_day = min(end_day, max_days)
|
|
581
|
+
|
|
582
|
+
# 检索这个时间段的对话
|
|
583
|
+
new_conversations = self._get_conversations_range(searched_days, end_day)
|
|
584
|
+
all_conversations.extend(new_conversations)
|
|
585
|
+
searched_days = end_day
|
|
586
|
+
|
|
587
|
+
if not all_conversations:
|
|
588
|
+
logger.info(f"📖 渐进检索: 0-{end_day}天 无对话,继续...")
|
|
589
|
+
continue
|
|
590
|
+
|
|
591
|
+
# LLM 判断信息是否足够
|
|
592
|
+
normal_content = self._format_conversations_for_llm(all_conversations)
|
|
593
|
+
if self._is_context_sufficient(query, profile_content, normal_content, end_day):
|
|
594
|
+
logger.info(f"✓ 渐进检索完成: 0-{end_day}天,{len(all_conversations)}条对话")
|
|
595
|
+
break
|
|
596
|
+
|
|
597
|
+
logger.info(f"📖 渐进检索: 0-{end_day}天 信息不足,继续...")
|
|
598
|
+
|
|
599
|
+
normal_content = self._format_conversations_for_llm(all_conversations) if all_conversations else ""
|
|
600
|
+
|
|
601
|
+
return {
|
|
602
|
+
"current_time": current_time,
|
|
603
|
+
"user_id": user_id,
|
|
604
|
+
"topic_id": self.topic_id,
|
|
605
|
+
"import_content": profile_content,
|
|
606
|
+
"normal_content": normal_content,
|
|
607
|
+
"conversations_count": len(all_conversations),
|
|
608
|
+
"profile_last_updated": profile_last_updated,
|
|
609
|
+
"searched_days": searched_days
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
def _get_conversations_range(
|
|
613
|
+
self,
|
|
614
|
+
start_days_ago: int,
|
|
615
|
+
end_days_ago: int
|
|
616
|
+
) -> List[Dict[str, Any]]:
|
|
617
|
+
"""获取指定天数范围内的对话(start_days_ago 到 end_days_ago 天前)"""
|
|
618
|
+
user_id = self.user_id
|
|
619
|
+
topic_id = self.topic_id
|
|
620
|
+
|
|
621
|
+
now = datetime.now()
|
|
622
|
+
start_date = (now - timedelta(days=end_days_ago)).strftime('%Y-%m-%d %H:%M:%S')
|
|
623
|
+
end_date = (now - timedelta(days=start_days_ago)).strftime('%Y-%m-%d %H:%M:%S')
|
|
624
|
+
|
|
625
|
+
query = {
|
|
626
|
+
"bool": {
|
|
627
|
+
"must": [
|
|
628
|
+
{"term": {"user_id": user_id}},
|
|
629
|
+
{"term": {"topic_id": topic_id}},
|
|
630
|
+
{"range": {"timestamp": {"gte": start_date, "lt": end_date}}}
|
|
631
|
+
]
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
response = self.es.search(
|
|
636
|
+
index=self.index_name,
|
|
637
|
+
query=query,
|
|
638
|
+
size=1000,
|
|
639
|
+
sort=[{"timestamp": {"order": "asc"}}]
|
|
640
|
+
)
|
|
641
|
+
|
|
642
|
+
return [hit["_source"] for hit in response["hits"]["hits"]]
|
|
643
|
+
|
|
644
|
+
def _is_context_sufficient(
|
|
645
|
+
self,
|
|
646
|
+
query: str,
|
|
647
|
+
profile: str,
|
|
648
|
+
conversations: str,
|
|
649
|
+
days: int
|
|
650
|
+
) -> bool:
|
|
651
|
+
"""LLM 判断当前上下文是否足够回答问题"""
|
|
652
|
+
prompt = CONTEXT_SUFFICIENT_PROMPT.format(
|
|
653
|
+
query=query,
|
|
654
|
+
profile=profile,
|
|
655
|
+
conversations=conversations or "(无对话记录)",
|
|
656
|
+
days=days
|
|
657
|
+
)
|
|
658
|
+
|
|
659
|
+
messages = [
|
|
660
|
+
{"role": "system", "content": prompt},
|
|
661
|
+
{"role": "user", "content": query}
|
|
662
|
+
]
|
|
663
|
+
|
|
664
|
+
response = self.llm.generate(messages, response_format="text")
|
|
665
|
+
is_sufficient = "true" in response.strip().lower()
|
|
666
|
+
|
|
667
|
+
return is_sufficient
|
|
668
|
+
|
|
669
|
+
def search_conversations(
|
|
670
|
+
self,
|
|
671
|
+
start_days: int,
|
|
672
|
+
end_days: int
|
|
673
|
+
) -> List[Dict[str, Any]]:
|
|
674
|
+
"""按时间范围检索对话(供外部 LLM 作为 tool 调用)
|
|
675
|
+
|
|
676
|
+
Args:
|
|
677
|
+
start_days: 起始天数(距今多少天,较近)
|
|
678
|
+
end_days: 结束天数(距今多少天,较远)
|
|
679
|
+
|
|
680
|
+
Returns:
|
|
681
|
+
对话记录列表
|
|
682
|
+
|
|
683
|
+
示例:
|
|
684
|
+
search_conversations(0, 7) # 最近7天
|
|
685
|
+
search_conversations(170, 180) # 约半年前的10天
|
|
686
|
+
"""
|
|
687
|
+
return self._get_conversations_range(start_days, end_days)
|
|
688
|
+
|
|
518
689
|
def _compress_profile(self, user_id: str, profile_content: str) -> str:
|
|
519
690
|
"""压缩用户画像"""
|
|
520
691
|
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M')
|
|
@@ -575,7 +746,7 @@ class Mem1Memory:
|
|
|
575
746
|
return []
|
|
576
747
|
|
|
577
748
|
images_desc = "\n".join([
|
|
578
|
-
f"[{i}] 文件名: {img['original_name']}, 时间: {img['timestamp']}, 描述: {img['description']
|
|
749
|
+
f"[{i}] 文件名: {img['original_name']}, 时间: {img['timestamp']}, 描述: {img['description']}"
|
|
579
750
|
for i, img in enumerate(images_index)
|
|
580
751
|
])
|
|
581
752
|
|
|
@@ -589,12 +760,16 @@ class Mem1Memory:
|
|
|
589
760
|
response = self.llm.generate(messages, response_format="text")
|
|
590
761
|
|
|
591
762
|
results = []
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
763
|
+
# 提取所有数字(支持多种格式:纯数字、[0]、0. 等)
|
|
764
|
+
import re
|
|
765
|
+
numbers = re.findall(r'\b(\d+)\b', response)
|
|
766
|
+
for num_str in numbers:
|
|
767
|
+
idx = int(num_str)
|
|
768
|
+
if 0 <= idx < len(images_index):
|
|
769
|
+
img = images_index[idx].copy()
|
|
770
|
+
img['abs_path'] = str((self._get_user_images_dir(user_id) / img['filename']).resolve())
|
|
771
|
+
if img not in results: # 去重
|
|
772
|
+
results.append(img)
|
|
598
773
|
|
|
599
774
|
logger.info(f"🖼️ 图片搜索: query='{query}', 找到 {len(results)} 张")
|
|
600
775
|
return results
|
mem1/prompts.py
CHANGED
|
@@ -32,6 +32,14 @@ class ProfileTemplate:
|
|
|
32
32
|
- 关注重点:
|
|
33
33
|
- 特殊要求:
|
|
34
34
|
|
|
35
|
+
## 周期性任务
|
|
36
|
+
(固定日程和重复性工作,格式:[周期] 任务内容)
|
|
37
|
+
- 例:[每周一] 提交周报、[每周五] 制定下周计划、[每月底] 月度汇总
|
|
38
|
+
|
|
39
|
+
## 关键数据
|
|
40
|
+
(重要的数字、金额、数量,用加粗标记)
|
|
41
|
+
- 例:处置 **97起** 案件、涉及金额 **230万元**、完成 **365个** 检查点
|
|
42
|
+
|
|
35
43
|
## 任务时间线
|
|
36
44
|
(用户提到的计划、截止日期、里程碑,格式:[YYYY-MM-DD] 事项)
|
|
37
45
|
|
|
@@ -111,15 +119,26 @@ DEFAULT_PROFILE_UPDATE_PROMPT = """你是用户画像分析专家。从对话记
|
|
|
111
119
|
4. 只记录用户明确表达的信息,不要推测
|
|
112
120
|
5. 如果某个章节没有信息,保留标题但内容留空
|
|
113
121
|
|
|
122
|
+
## 周期性任务提取
|
|
123
|
+
6. 识别固定日程:用户提到"每周一"、"每周五"、"每天"、"每月"等周期性任务
|
|
124
|
+
7. 记录到「周期性任务」章节,格式:[周期] 任务内容
|
|
125
|
+
8. 示例:[每周一] 提交周报、[每周五] 制定下周计划
|
|
126
|
+
|
|
127
|
+
## 关键数字保留【重要】
|
|
128
|
+
9. 所有具体数字必须原样保留并用 **加粗** 标记,禁止概括为"多个"、"若干"、"大量"
|
|
129
|
+
10. 记录到「关键数据」章节,格式:事项描述 **数字**
|
|
130
|
+
11. 正确示例:处置 **97起** 案件、涉及金额 **230万元** ✓
|
|
131
|
+
12. 错误示例:处置多起案件、涉及大额资金 ✗
|
|
132
|
+
|
|
114
133
|
## 时间敏感信息提取
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
134
|
+
13. 任务时间线:提取用户提到的日期、截止时间、计划安排、里程碑,记录到「任务时间线」
|
|
135
|
+
14. 待办事项:识别用户说"下次"、"回头"、"先这样"、"稍后"、"改天"等挂起信号,将未完成的请求记录到「待办事项」
|
|
136
|
+
15. 已完成事项:如果对话中确认某个待办已完成,将其从「待办事项」移除或标记为 [x]
|
|
118
137
|
|
|
119
138
|
## 矛盾检测
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
139
|
+
16. 发现用户前后说法矛盾时(如偏好、身份、需求不一致),不要直接覆盖旧信息
|
|
140
|
+
17. 将矛盾记录到「待澄清事项」,格式:⚠️ 用户曾说"..."(日期),但又说"..."(日期)
|
|
141
|
+
18. 只有用户明确澄清后,才能更新对应信息并移除待澄清标记
|
|
123
142
|
|
|
124
143
|
---
|
|
125
144
|
*最后更新: {timestamp}*
|
|
@@ -188,6 +207,28 @@ ASSISTANT_SUMMARY_PROMPT = """你是对话摘要专家。将助手的长回复
|
|
|
188
207
|
直接输出摘要内容,开头标注 [摘要]"""
|
|
189
208
|
|
|
190
209
|
|
|
210
|
+
# ============ 渐进式检索判断提示词 ============
|
|
211
|
+
|
|
212
|
+
CONTEXT_SUFFICIENT_PROMPT = """判断当前信息是否足够回答用户问题。
|
|
213
|
+
|
|
214
|
+
## 用户问题
|
|
215
|
+
{query}
|
|
216
|
+
|
|
217
|
+
## 用户画像
|
|
218
|
+
{profile}
|
|
219
|
+
|
|
220
|
+
## 已检索的对话记录(最近 {days} 天)
|
|
221
|
+
{conversations}
|
|
222
|
+
|
|
223
|
+
## 判断标准
|
|
224
|
+
- 如果画像或对话中包含回答问题所需的信息,输出 `true`
|
|
225
|
+
- 如果问题涉及的时间、事件、数据在已有信息中找不到,输出 `false`
|
|
226
|
+
- 如果是通用问题(不依赖历史记录),输出 `true`
|
|
227
|
+
|
|
228
|
+
## 输出
|
|
229
|
+
只输出:`true`(信息足够)或 `false`(需要检索更早的记录)"""
|
|
230
|
+
|
|
231
|
+
|
|
191
232
|
# ============ 图片搜索提示词(通用) ============
|
|
192
233
|
|
|
193
234
|
IMAGE_SEARCH_PROMPT = """根据用户查询,从图片列表中找出匹配的图片。
|
|
@@ -220,6 +261,14 @@ YUQING_PROFILE_TEMPLATE = ProfileTemplate(
|
|
|
220
261
|
- 关注重点:(时效性/准确性/全面性等)
|
|
221
262
|
- 图表偏好:
|
|
222
263
|
|
|
264
|
+
## 周期性任务
|
|
265
|
+
(固定日程和重复性工作,格式:[周期] 任务内容)
|
|
266
|
+
- 例:[每周一] 提交周报、[每周五] 制定下周计划、[每月底] 月度汇总
|
|
267
|
+
|
|
268
|
+
## 关键数据
|
|
269
|
+
(重要的数字、金额、数量,用加粗标记)
|
|
270
|
+
- 例:处置 **97起** 案件、涉及金额 **230万元**
|
|
271
|
+
|
|
223
272
|
## 任务时间线
|
|
224
273
|
(报告截止日期、汇报计划、定期任务,格式:[YYYY-MM-DD] 事项)
|
|
225
274
|
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mem1
|
|
3
|
+
Version: 0.0.7
|
|
4
|
+
Summary: 基于云服务的用户记忆系统
|
|
5
|
+
Project-URL: Homepage, https://github.com/sougannkyou/mem1
|
|
6
|
+
Project-URL: Repository, https://github.com/sougannkyou/mem1
|
|
7
|
+
Author: Song
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: langchain,llm,memory,user-profile
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Requires-Dist: dashscope>=1.14.0
|
|
18
|
+
Requires-Dist: elasticsearch>=8.0.0
|
|
19
|
+
Requires-Dist: openai>=1.0.0
|
|
20
|
+
Requires-Dist: pydantic>=2.0.0
|
|
21
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: ipython>=8.0.0; extra == 'dev'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# mem1 - 用户记忆系统
|
|
27
|
+
|
|
28
|
+
让 AI 真正"记住"用户:三层记忆架构 + 图片记忆 + 话题隔离 + 业务场景解耦。
|
|
29
|
+
|
|
30
|
+
## 核心特性
|
|
31
|
+
|
|
32
|
+
- **三层记忆架构**:短期会话 → 用户画像 → 长期记录
|
|
33
|
+
- **话题隔离**:同一用户可有多个话题,对话按话题隔离,画像跨话题共享
|
|
34
|
+
- **图片记忆**:存储图片时自动调用 VL 模型生成描述(OCR + 内容理解),搜索时基于文字描述召回
|
|
35
|
+
- **业务解耦**:通过 ProfileTemplate 适配不同场景
|
|
36
|
+
- **画像自动更新**:基于对话轮数/时间自动触发 LLM 更新用户画像
|
|
37
|
+
|
|
38
|
+
## 安装
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install mem1
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 快速开始
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from mem1 import Mem1Memory, Mem1Config
|
|
48
|
+
|
|
49
|
+
# 从环境变量加载配置
|
|
50
|
+
config = Mem1Config.from_env()
|
|
51
|
+
|
|
52
|
+
# 创建记忆实例(绑定用户和话题)
|
|
53
|
+
memory = Mem1Memory(config, user_id="user001", topic_id="project_a")
|
|
54
|
+
|
|
55
|
+
# 添加对话
|
|
56
|
+
memory.add_conversation(
|
|
57
|
+
messages=[
|
|
58
|
+
{"role": "user", "content": "你好,我是张明"},
|
|
59
|
+
{"role": "assistant", "content": "你好张明!"}
|
|
60
|
+
]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# 获取上下文(含用户画像 + 最近对话)
|
|
64
|
+
ctx = memory.get_context()
|
|
65
|
+
print(ctx['import_content']) # 用户画像
|
|
66
|
+
print(ctx['normal_content']) # 最近对话记录
|
|
67
|
+
print(ctx['current_time']) # 当前时间
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 环境变量配置
|
|
71
|
+
|
|
72
|
+
复制 `.env.example` 为 `.env` 并填写配置:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
cp .env.example .env
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## 图片记忆
|
|
79
|
+
|
|
80
|
+
存储图片时自动调用 VL 模型(如 Qwen-VL)生成描述:
|
|
81
|
+
- 【用户描述】用户发送图片时的文字说明
|
|
82
|
+
- 【文字内容】OCR 识别图片中的文字
|
|
83
|
+
- 【图片描述】VL 模型对图片内容的理解
|
|
84
|
+
|
|
85
|
+
搜索时基于描述文本进行关键词匹配,返回图片路径。
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
# 添加带图片的对话
|
|
89
|
+
memory.add_conversation(
|
|
90
|
+
messages=[{"role": "user", "content": "这是今天的报表"}],
|
|
91
|
+
images=[{"path": "./report.png", "filename": "report.png"}]
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
# 搜索图片(基于 VL 生成的描述)
|
|
95
|
+
results = memory.search_images(query="报表")
|
|
96
|
+
# 返回: [{"filename": "...", "description": "...", "abs_path": "..."}]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## LangChain 集成
|
|
100
|
+
|
|
101
|
+
完整示例(记忆存储 + 召回):
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from langchain_openai import ChatOpenAI
|
|
105
|
+
from langchain_core.messages import SystemMessage, HumanMessage
|
|
106
|
+
from mem1 import Mem1Memory, Mem1Config
|
|
107
|
+
|
|
108
|
+
config = Mem1Config.from_env()
|
|
109
|
+
memory = Mem1Memory(config, user_id="user001", topic_id="project_a")
|
|
110
|
+
llm = ChatOpenAI(model=config.llm.model, api_key=config.llm.api_key, base_url=config.llm.base_url)
|
|
111
|
+
|
|
112
|
+
# ========== 第一次对话:存储记忆 ==========
|
|
113
|
+
memory.add_conversation(messages=[
|
|
114
|
+
{"role": "user", "content": "我是李明,市网信办的,每周一要交周报"},
|
|
115
|
+
{"role": "assistant", "content": "李明您好!已记录:周一交周报。"}
|
|
116
|
+
])
|
|
117
|
+
memory.add_conversation(messages=[
|
|
118
|
+
{"role": "user", "content": "本月处理了97起舆情,重大舆情11起"},
|
|
119
|
+
{"role": "assistant", "content": "已记录本月数据。"}
|
|
120
|
+
])
|
|
121
|
+
|
|
122
|
+
# ========== 第二次对话:召回记忆 ==========
|
|
123
|
+
user_question = "帮我写个本月舆情简报"
|
|
124
|
+
|
|
125
|
+
# 1. 获取记忆上下文
|
|
126
|
+
ctx = memory.get_context(query=user_question, days_limit=7)
|
|
127
|
+
|
|
128
|
+
# 2. 构建 system prompt(注入画像 + 历史对话)
|
|
129
|
+
system_prompt = f"""你是舆情助手。
|
|
130
|
+
|
|
131
|
+
## 用户画像
|
|
132
|
+
{ctx['import_content']}
|
|
133
|
+
|
|
134
|
+
## 最近对话记录
|
|
135
|
+
{ctx['normal_content']}
|
|
136
|
+
|
|
137
|
+
## 当前时间
|
|
138
|
+
{ctx['current_time']}
|
|
139
|
+
|
|
140
|
+
## 重要规则
|
|
141
|
+
- 回答必须基于对话记录,不要编造
|
|
142
|
+
- 数字必须从记录中原样提取
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
# 3. 调用 LLM(记忆已注入)
|
|
146
|
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_question)]
|
|
147
|
+
response = llm.invoke(messages)
|
|
148
|
+
print(response.content) # AI 会基于记忆中的 97起、11起 来回答
|
|
149
|
+
|
|
150
|
+
# 4. 保存本次对话
|
|
151
|
+
memory.add_conversation(messages=[
|
|
152
|
+
{"role": "user", "content": user_question},
|
|
153
|
+
{"role": "assistant", "content": response.content}
|
|
154
|
+
])
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## 核心接口
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
memory = Mem1Memory(config, user_id="user001", topic_id="project_a")
|
|
161
|
+
|
|
162
|
+
# 添加对话
|
|
163
|
+
memory.add_conversation(messages=[...], images=[...], metadata={...})
|
|
164
|
+
|
|
165
|
+
# 获取上下文(画像 + 最近 N 天对话)
|
|
166
|
+
ctx = memory.get_context(days_limit=31)
|
|
167
|
+
|
|
168
|
+
# 渐进式检索(先查近期,不够再扩展)
|
|
169
|
+
ctx = memory.get_context_progressive(query="帮我写周报", max_days=31, step=7)
|
|
170
|
+
|
|
171
|
+
# 按时间范围检索(供外部 LLM 作为 tool 调用)
|
|
172
|
+
convs = memory.search_conversations(start_days=170, end_days=180) # 查半年前
|
|
173
|
+
|
|
174
|
+
# 查询对话
|
|
175
|
+
convs = memory.get_conversations(days_limit=7)
|
|
176
|
+
all_convs = memory.get_all_conversations(days_limit=7)
|
|
177
|
+
|
|
178
|
+
# 图片搜索
|
|
179
|
+
results = memory.search_images(query="麻花")
|
|
180
|
+
|
|
181
|
+
# 话题管理
|
|
182
|
+
topics = memory.list_topics()
|
|
183
|
+
memory.delete_topic()
|
|
184
|
+
memory.delete_user()
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## 远期记忆检索
|
|
188
|
+
|
|
189
|
+
mem1 定位是**记忆存储层**,不内置时间意图解析。当用户问"半年前的XX事"时,建议:
|
|
190
|
+
|
|
191
|
+
1. **外部 LLM 判断时间范围**:通过 function calling 让 LLM 提取时间意图
|
|
192
|
+
2. **调用 `search_conversations(start_days, end_days)`**:定向检索指定时间段
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
# 示例:作为 LangChain Tool 暴露给 LLM
|
|
196
|
+
from langchain.tools import tool
|
|
197
|
+
|
|
198
|
+
@tool
|
|
199
|
+
def search_memory(start_days: int, end_days: int) -> str:
|
|
200
|
+
"""搜索用户历史对话。start_days 和 end_days 表示距今多少天。"""
|
|
201
|
+
convs = memory.search_conversations(start_days=start_days, end_days=end_days)
|
|
202
|
+
return memory._format_conversations_for_llm(convs)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
这样设计的原因:
|
|
206
|
+
- 外部 LLM 有完整对话上下文,判断时间范围更准确
|
|
207
|
+
- 避免 mem1 内部嵌套 LLM 调用,架构更清晰
|
|
208
|
+
- 符合 Agent / function calling 的设计模式
|
|
209
|
+
|
|
210
|
+
## ES 索引
|
|
211
|
+
|
|
212
|
+
| 索引 | 用途 |
|
|
213
|
+
|------|------|
|
|
214
|
+
| `conversation_history` | 对话记录(含图片索引) |
|
|
215
|
+
| `mem1_user_state` | 用户状态 |
|
|
216
|
+
| `mem1_user_profile` | 用户画像 |
|
|
217
|
+
|
|
218
|
+
## LLM 提示词建议
|
|
219
|
+
|
|
220
|
+
使用 `get_context()` 获取上下文后,建议在 system prompt 中加入以下规则,避免 LLM 编造信息:
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
## 重要规则
|
|
224
|
+
1. 回答必须基于上述对话记录中的实际内容,严禁编造任何信息
|
|
225
|
+
2. 涉及数字(金额、数量、百分比、日期等)时,必须从对话记录中原样提取,不得估算或编造
|
|
226
|
+
3. 需要汇总累加时,必须列出计算过程(如:23+31+18+25=97)
|
|
227
|
+
4. 涉及人名、公司名、账号名等实体时,必须使用对话中的原始名称
|
|
228
|
+
5. 如果对话记录中没有相关信息,请明确说"对话记录中未提及",不要猜测
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## 设计决策:为什么不用 Context Caching
|
|
232
|
+
|
|
233
|
+
豆包等大模型提供了 Context Caching 功能(缓存命中可省 86% token 费),但 mem1 选择不使用:
|
|
234
|
+
|
|
235
|
+
| 对比 | Context Caching | mem1 架构 |
|
|
236
|
+
|------|-----------------|-----------|
|
|
237
|
+
| 原理 | 缓存整个对话历史,按 session 复用 | 画像压缩 + 按需检索 |
|
|
238
|
+
| 适用场景 | 单 session 内反复分析同一长文档 | 跨 session 持久化记忆 |
|
|
239
|
+
| 多模态 | Responses API 支持图片/视频缓存 | 图片转描述文本存储 |
|
|
240
|
+
| 过期 | 72h 自动过期需重建 | ES 永久存储 |
|
|
241
|
+
| 灵活性 | 固定缓存内容 | 动态组装 prompt |
|
|
242
|
+
|
|
243
|
+
mem1 的记忆是动态组装的(画像 + 检索到的相关对话),每次 prompt 内容不同,Context Caching 的"相同前缀复用"优势无法发挥。
|
|
244
|
+
|
|
245
|
+
如果担心 token 消耗,建议调小 `MEM1_CONTEXT_DAYS_LIMIT`(如 3-7 天),让远期记忆靠画像覆盖。
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
mem1/__init__.py,sha256=tNsBrO4d7fujDIPpvl6pweVcg5kHr_EYRgslR8nWWEI,494
|
|
2
|
+
mem1/config.py,sha256=9U-dJD6JsQ2CyDy-rwLqE3_kWwG3sPo7jTbkV_Tl4SE,5422
|
|
3
|
+
mem1/langchain_middleware.py,sha256=h2mG7K2Tq1N7IovXMvCyvOhsAwTWOR1NAqivF4db2AE,6648
|
|
4
|
+
mem1/llm.py,sha256=S23OA2OpZVb6A36iQ2YY_7Q7rRtnqC7xUbVW-bZSjsI,4419
|
|
5
|
+
mem1/memory_es.py,sha256=keLiTq2yw2QC_tmIvCYSdOVnSJNpq981ko3GrQpsyjE,34301
|
|
6
|
+
mem1/memory_md.py,sha256=uu_TvdBoUpAncT1eissOSe1Y3vCy3iWMcuvCy3vCjEA,26258
|
|
7
|
+
mem1/memory_tools.py,sha256=b1YBiRNet0gXnW-KGIZ2KQclluB9Q6dli_DbWLS571k,3646
|
|
8
|
+
mem1/prompts.py,sha256=5HUG-yvTD7iBUzzXwO-WnRomDLkz0UJWox3z3zcT0kI,10599
|
|
9
|
+
mem1-0.0.7.dist-info/METADATA,sha256=VnJlMl-3KoG9zw59qA7xDKKoxo9Bzy1EeChu9EpjGhE,8300
|
|
10
|
+
mem1-0.0.7.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
11
|
+
mem1-0.0.7.dist-info/RECORD,,
|
mem1-0.0.5.dist-info/METADATA
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: mem1
|
|
3
|
-
Version: 0.0.5
|
|
4
|
-
Summary: 基于云服务的用户记忆系统
|
|
5
|
-
Project-URL: Homepage, https://github.com/sougannkyou/mem1
|
|
6
|
-
Project-URL: Repository, https://github.com/sougannkyou/mem1
|
|
7
|
-
Author: Song
|
|
8
|
-
License: MIT
|
|
9
|
-
Keywords: langchain,llm,memory,user-profile
|
|
10
|
-
Classifier: Development Status :: 3 - Alpha
|
|
11
|
-
Classifier: Intended Audience :: Developers
|
|
12
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
-
Classifier: Programming Language :: Python :: 3
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
-
Requires-Python: >=3.12
|
|
17
|
-
Requires-Dist: elasticsearch>=8.0.0
|
|
18
|
-
Requires-Dist: openai>=1.0.0
|
|
19
|
-
Requires-Dist: pydantic>=2.0.0
|
|
20
|
-
Requires-Dist: python-dotenv>=1.0.0
|
|
21
|
-
Provides-Extra: dev
|
|
22
|
-
Requires-Dist: ipython>=8.0.0; extra == 'dev'
|
|
23
|
-
Description-Content-Type: text/markdown
|
|
24
|
-
|
|
25
|
-
# mem1 - 用户记忆系统
|
|
26
|
-
|
|
27
|
-
让 AI 真正"记住"用户:三层记忆架构 + 图片记忆 + 话题隔离 + 业务场景解耦。
|
|
28
|
-
|
|
29
|
-
## 核心特性
|
|
30
|
-
|
|
31
|
-
- **三层记忆架构**:短期会话 → 用户画像 → 长期记录
|
|
32
|
-
- **话题隔离**:同一用户可有多个话题,对话按话题隔离,画像跨话题共享
|
|
33
|
-
- **图片记忆**:支持存储和语义搜索用户发送的图片
|
|
34
|
-
- **业务解耦**:通过 ProfileTemplate 适配不同场景
|
|
35
|
-
- **画像自动更新**:基于对话轮数/时间自动触发 LLM 更新用户画像
|
|
36
|
-
|
|
37
|
-
## 安装
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
pip install mem1
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## 快速开始
|
|
44
|
-
|
|
45
|
-
```python
|
|
46
|
-
from mem1 import Mem1Memory, Mem1Config
|
|
47
|
-
|
|
48
|
-
# 从环境变量加载配置
|
|
49
|
-
config = Mem1Config.from_env()
|
|
50
|
-
|
|
51
|
-
# 创建记忆实例(绑定用户和话题)
|
|
52
|
-
memory = Mem1Memory(config, user_id="user001", topic_id="project_a")
|
|
53
|
-
|
|
54
|
-
# 添加对话
|
|
55
|
-
memory.add_conversation(
|
|
56
|
-
messages=[
|
|
57
|
-
{"role": "user", "content": "你好,我是张明"},
|
|
58
|
-
{"role": "assistant", "content": "你好张明!"}
|
|
59
|
-
]
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
# 获取上下文(含用户画像 + 最近对话)
|
|
63
|
-
ctx = memory.get_context()
|
|
64
|
-
print(ctx['import_content']) # 用户画像
|
|
65
|
-
print(ctx['normal_content']) # 最近对话记录
|
|
66
|
-
print(ctx['current_time']) # 当前时间
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## 环境变量配置
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
# LLM 配置
|
|
73
|
-
MEM1_LLM_API_KEY=your-api-key
|
|
74
|
-
MEM1_LLM_BASE_URL=https://api.deepseek.com
|
|
75
|
-
MEM1_LLM_MODEL=deepseek-chat
|
|
76
|
-
|
|
77
|
-
# ES 配置
|
|
78
|
-
MEM1_ES_HOSTS=http://localhost:9200
|
|
79
|
-
MEM1_ES_INDEX=conversation_history
|
|
80
|
-
|
|
81
|
-
# 记忆配置
|
|
82
|
-
MEM1_MEMORY_DIR=./memories
|
|
83
|
-
MEM1_AUTO_UPDATE_PROFILE=true
|
|
84
|
-
MEM1_MAX_PROFILE_CHARS=3000
|
|
85
|
-
MEM1_UPDATE_INTERVAL_ROUNDS=5
|
|
86
|
-
MEM1_UPDATE_INTERVAL_MINUTES=3
|
|
87
|
-
MEM1_SAVE_ASSISTANT_MESSAGES=true
|
|
88
|
-
MEM1_MAX_ASSISTANT_CHARS=500
|
|
89
|
-
MEM1_CONTEXT_DAYS_LIMIT=31
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## 核心接口
|
|
93
|
-
|
|
94
|
-
```python
|
|
95
|
-
memory = Mem1Memory(config, user_id="user001", topic_id="project_a")
|
|
96
|
-
|
|
97
|
-
# 添加对话
|
|
98
|
-
memory.add_conversation(messages=[...], images=[...], metadata={...})
|
|
99
|
-
|
|
100
|
-
# 获取上下文(画像 + 最近 N 天对话)
|
|
101
|
-
ctx = memory.get_context(days_limit=31)
|
|
102
|
-
|
|
103
|
-
# 查询对话
|
|
104
|
-
convs = memory.get_conversations(days_limit=7)
|
|
105
|
-
all_convs = memory.get_all_conversations(days_limit=7)
|
|
106
|
-
|
|
107
|
-
# 图片搜索
|
|
108
|
-
results = memory.search_images(query="麻花")
|
|
109
|
-
|
|
110
|
-
# 话题管理
|
|
111
|
-
topics = memory.list_topics()
|
|
112
|
-
memory.delete_topic()
|
|
113
|
-
memory.delete_user()
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## ES 索引
|
|
117
|
-
|
|
118
|
-
| 索引 | 用途 |
|
|
119
|
-
|------|------|
|
|
120
|
-
| `conversation_history` | 对话记录 |
|
|
121
|
-
| `mem1_user_state` | 用户状态 |
|
|
122
|
-
| `mem1_user_profile` | 用户画像 |
|
|
123
|
-
|
|
124
|
-
## LLM 提示词建议
|
|
125
|
-
|
|
126
|
-
使用 `get_context()` 获取上下文后,建议在 system prompt 中加入以下规则,避免 LLM 编造信息:
|
|
127
|
-
|
|
128
|
-
```
|
|
129
|
-
## 重要规则
|
|
130
|
-
1. 回答必须基于上述对话记录中的实际内容,严禁编造任何信息
|
|
131
|
-
2. 涉及数字(金额、数量、百分比、日期等)时,必须从对话记录中原样提取,不得估算或编造
|
|
132
|
-
3. 需要汇总累加时,必须列出计算过程(如:23+31+18+25=97)
|
|
133
|
-
4. 涉及人名、公司名、账号名等实体时,必须使用对话中的原始名称
|
|
134
|
-
5. 如果对话记录中没有相关信息,请明确说"对话记录中未提及",不要猜测
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
## License
|
|
138
|
-
|
|
139
|
-
MIT
|
mem1-0.0.5.dist-info/RECORD
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
mem1/__init__.py,sha256=D-o5MjyTl565vqyVOyigm9LiZmsk9S5DfyV4QobLNo8,219
|
|
2
|
-
mem1/config.py,sha256=Tr91k9P9T-znshYAFGSyL3KGNy38xQLjbSJWlHxxeIo,4309
|
|
3
|
-
mem1/langchain_middleware.py,sha256=h2mG7K2Tq1N7IovXMvCyvOhsAwTWOR1NAqivF4db2AE,6648
|
|
4
|
-
mem1/llm.py,sha256=EKsZHxLrRn-OTxCsPHOYcUTjnEF5RVMnEM8fqWzdkbg,1114
|
|
5
|
-
mem1/memory_es.py,sha256=nVkZ2W-MWL0ZLzNqDoZ7e5Ccgde8SKzy2ogMm1CKZu8,27787
|
|
6
|
-
mem1/memory_md.py,sha256=uu_TvdBoUpAncT1eissOSe1Y3vCy3iWMcuvCy3vCjEA,26258
|
|
7
|
-
mem1/memory_tools.py,sha256=b1YBiRNet0gXnW-KGIZ2KQclluB9Q6dli_DbWLS571k,3646
|
|
8
|
-
mem1/prompts.py,sha256=L0JGVa--V_h9KqqZnb1n1N9oaPqxBhxHFL71Us9J5qM,8685
|
|
9
|
-
mem1-0.0.5.dist-info/METADATA,sha256=ZbNrxTHxutoP5zK65emlcymBSFagU2AE2dLhpX74bjE,3957
|
|
10
|
-
mem1-0.0.5.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
11
|
-
mem1-0.0.5.dist-info/RECORD,,
|
|
File without changes
|