Skip to content

Commit 4bd373e

Browse files
authored
eat:reoganize prompt with reference in user content (#351)
## Description reoganize prompt with reference in user content <!-- Please include a summary of the changes below; Fill in the issue number that this PR addresses (if applicable); Fill in the related MemOS-Docs repository issue or PR link (if applicable); Mention the person who will review this PR (if you know who it is); Replace (summary), (issue), (docs-issue-or-pr-link), and (reviewer) with the appropriate information. 请在下方填写更改的摘要; 填写此 PR 解决的问题编号(如果适用); 填写相关的 MemOS-Docs 仓库 issue 或 PR 链接(如果适用); 提及将审查此 PR 的人(如果您知道是谁); 替换 (summary)、(issue)、(docs-issue-or-pr-link) 和 (reviewer) 为适当的信息。 --> Summary: (summary) Fix: #(issue) Docs Issue/PR: (docs-issue-or-pr-link) Reviewer: @(reviewer) ## Checklist: - [ √] I have performed a self-review of my own code | 我已自行检查了自己的代码 - [ √] I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释 - [ √] I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常 - [ √] I have created related documentation issue/PR in [MemOS-Docs](https://github.com/MemTensor/MemOS-Docs) (if applicable) | 我已在 [MemOS-Docs](https://github.com/MemTensor/MemOS-Docs) 中创建了相关的文档 issue/PR(如果适用) - [ √] I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用) - [ √] I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人
2 parents 3734b26 + 6bd1135 commit 4bd373e

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/memos/mem_os/product.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,47 @@ def _build_system_prompt(
463463
+ mem_block
464464
)
465465

466+
def _build_base_system_prompt(
467+
self,
468+
base_prompt: str | None = None,
469+
tone: str = "friendly",
470+
verbosity: str = "mid",
471+
mode: str = "enhance",
472+
) -> str:
473+
"""
474+
Build base system prompt without memory references.
475+
"""
476+
now = datetime.now()
477+
formatted_date = now.strftime("%Y-%m-%d (%A)")
478+
sys_body = get_memos_prompt(date=formatted_date, tone=tone, verbosity=verbosity, mode=mode)
479+
prefix = (base_prompt.strip() + "\n\n") if base_prompt else ""
480+
return prefix + sys_body
481+
482+
def _build_memory_context(
483+
self,
484+
memories_all: list[TextualMemoryItem],
485+
mode: str = "enhance",
486+
) -> str:
487+
"""
488+
Build memory context to be included in user message.
489+
"""
490+
if not memories_all:
491+
return ""
492+
493+
mem_block_o, mem_block_p = _format_mem_block(memories_all)
494+
495+
if mode == "enhance":
496+
return (
497+
"# Memories\n## PersonalMemory (ordered)\n"
498+
+ mem_block_p
499+
+ "\n## OuterMemory (ordered)\n"
500+
+ mem_block_o
501+
+ "\n\n"
502+
)
503+
else:
504+
mem_block = mem_block_o + "\n" + mem_block_p
505+
return "# Memories\n## PersonalMemory & OuterMemory (ordered)\n" + mem_block + "\n\n"
506+
466507
def _build_enhance_system_prompt(
467508
self,
468509
user_id: str,
@@ -472,6 +513,7 @@ def _build_enhance_system_prompt(
472513
) -> str:
473514
"""
474515
Build enhance prompt for the user with memory references.
516+
[DEPRECATED] Use _build_base_system_prompt and _build_memory_context instead.
475517
"""
476518
now = datetime.now()
477519
formatted_date = now.strftime("%Y-%m-%d (%A)")
@@ -1002,14 +1044,22 @@ def chat(
10021044
m.metadata.embedding = []
10031045
new_memories_list.append(m)
10041046
memories_list = new_memories_list
1005-
system_prompt = super()._build_system_prompt(memories_list, base_prompt)
1047+
# Build base system prompt without memory
1048+
system_prompt = self._build_base_system_prompt(base_prompt, mode="base")
1049+
1050+
# Build memory context to be included in user message
1051+
memory_context = self._build_memory_context(memories_list, mode="base")
1052+
1053+
# Combine memory context with user query
1054+
user_content = memory_context + query if memory_context else query
1055+
10061056
history_info = []
10071057
if history:
10081058
history_info = history[-20:]
10091059
current_messages = [
10101060
{"role": "system", "content": system_prompt},
10111061
*history_info,
1012-
{"role": "user", "content": query},
1062+
{"role": "user", "content": user_content},
10131063
]
10141064
response = self.chat_llm.generate(current_messages)
10151065
time_end = time.time()
@@ -1079,8 +1129,16 @@ def chat_with_references(
10791129

10801130
reference = prepare_reference_data(memories_list)
10811131
yield f"data: {json.dumps({'type': 'reference', 'data': reference})}\n\n"
1082-
# Build custom system prompt with relevant memories)
1083-
system_prompt = self._build_enhance_system_prompt(user_id, memories_list)
1132+
1133+
# Build base system prompt without memory
1134+
system_prompt = self._build_base_system_prompt(mode="enhance")
1135+
1136+
# Build memory context to be included in user message
1137+
memory_context = self._build_memory_context(memories_list, mode="enhance")
1138+
1139+
# Combine memory context with user query
1140+
user_content = memory_context + query if memory_context else query
1141+
10841142
# Get chat history
10851143
if user_id not in self.chat_history_manager:
10861144
self._register_chat_history(user_id, session_id)
@@ -1091,7 +1149,7 @@ def chat_with_references(
10911149
current_messages = [
10921150
{"role": "system", "content": system_prompt},
10931151
*chat_history.chat_history,
1094-
{"role": "user", "content": query},
1152+
{"role": "user", "content": user_content},
10951153
]
10961154
logger.info(
10971155
f"user_id: {user_id}, cube_id: {cube_id}, current_system_prompt: {system_prompt}"

0 commit comments

Comments
 (0)