feat: 长期记忆改为摘要检索并由模型按需载入

This commit is contained in:
2026-09-20 11:33:35 +08:00
parent 177036cf3d
commit 9b240b35a9
12 changed files with 300 additions and 52 deletions
+10 -4
View File
@@ -14,6 +14,10 @@ from domain.ports import ChatStreamer
_BASE_SYSTEM = (
"你是电商 DMB 系统的经营助手。回答简洁、可执行。"
"需要经营数据时优先调用可用工具。"
"你具备跨会话的用户级长期记忆,但内容不会自动注入对话。"
"需要称呼、偏好或用户事实时:先 memory_search,再对命中 id 调用 memory_load;"
"禁止编造未 load 的记忆内容,也不要声称没有长期记忆能力。"
"用户要求记住某事:可用 memory_save,或若系统提示「本轮已写入长期记忆」则明确确认已跨会话保存。"
"用户说近 N 天、昨天、本周、本月等相对日期时,先调用 get_current_time,"
"用返回的 ranges 或 today 换算 yyyy-MM-dd 后再查数,不要向用户索要日期范围。"
"用户用名称或编码指代店铺、商品、SKU 时,先调用 catalog_search;"
@@ -32,13 +36,15 @@ class OpenAICompatibleStreamer(ChatStreamer):
llm: LlmRuntimeConfig,
history: list[ChatMessage],
user_text: str,
memory_lines: list[str] | None = None,
tools: list[Any] | None = None,
memory_count: int = 0,
memory_just_saved: str | None = None,
) -> AsyncIterator[str]:
system = _BASE_SYSTEM
if memory_lines:
joined = "\n".join(f"- {line}" for line in memory_lines[:20])
system += f"\n\n以下是关于当前用户的长期记忆(可能过时,请谨慎引用):\n{joined}"
if memory_count > 0:
system += f"\n\n当前用户约有 {memory_count} 条长期记忆,需要时用 memory_search / memory_load。"
if memory_just_saved:
system += f"\n\n【本轮已写入长期记忆】{memory_just_saved}"
model = ChatOpenAI(
api_key=llm.api_key,