Skip to content

Commit a6f9649

Browse files
authored
Feat: change mem prompt (#281)
* feat: update prompt and add search logs for mem * fix:ci * fix: sync system prompt
1 parent 48da7ca commit a6f9649

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/memos/mem_os/product.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ def _format_mem_block(memories_all, max_items: int = 20, max_chars_each: int = 3
6767
sequence is [i:memId] i; [P]=PersonalMemory / [O]=OuterMemory
6868
"""
6969
if not memories_all:
70-
return "(none)"
70+
return "(none)", "(none)"
7171

72-
lines = []
72+
lines_o = []
73+
lines_p = []
7374
for idx, m in enumerate(memories_all[:max_items], 1):
7475
mid = _short_id(getattr(m, "id", "") or "")
7576
mtype = getattr(getattr(m, "metadata", {}), "memory_type", None) or getattr(
@@ -80,8 +81,11 @@ def _format_mem_block(memories_all, max_items: int = 20, max_chars_each: int = 3
8081
if len(txt) > max_chars_each:
8182
txt = txt[: max_chars_each - 1] + "…"
8283
mid = mid or f"mem_{idx}"
83-
lines.append(f"[{idx}:{mid}] :: [{tag}] {txt}")
84-
return "\n".join(lines)
84+
if tag == "O":
85+
lines_o.append(f"[{idx}:{mid}] :: [{tag}] {txt}\n")
86+
elif tag == "P":
87+
lines_p.append(f"[{idx}:{mid}] :: [{tag}] {txt}")
88+
return "\n".join(lines_o), "\n".join(lines_p)
8589

8690

8791
class MOSProduct(MOSCore):
@@ -410,7 +414,8 @@ def _build_system_prompt(
410414
sys_body = get_memos_prompt(
411415
date=formatted_date, tone=tone, verbosity=verbosity, mode="base"
412416
)
413-
mem_block = _format_mem_block(memories_all)
417+
mem_block_o, mem_block_p = _format_mem_block(memories_all)
418+
mem_block = mem_block_o + "\n" + mem_block_p
414419
prefix = (base_prompt.strip() + "\n\n") if base_prompt else ""
415420
return (
416421
prefix
@@ -434,8 +439,14 @@ def _build_enhance_system_prompt(
434439
sys_body = get_memos_prompt(
435440
date=formatted_date, tone=tone, verbosity=verbosity, mode="enhance"
436441
)
437-
mem_block = _format_mem_block(memories_all)
438-
return sys_body + "\n\n# Memories\n## PersonalMemory & OuterMemory (ordered)\n" + mem_block
442+
mem_block_o, mem_block_p = _format_mem_block(memories_all)
443+
return (
444+
sys_body
445+
+ "\n\n# Memories\n## PersonalMemory (ordered)\n"
446+
+ mem_block_p
447+
+ "\n## OuterMemory (ordered)\n"
448+
+ mem_block_o
449+
)
439450

440451
def _extract_references_from_response(self, response: str) -> tuple[str, list[dict]]:
441452
"""
@@ -1284,6 +1295,7 @@ def search(
12841295
memories["metadata"]["memory"] = memories["memory"]
12851296
memories_list.append(memories)
12861297
reformat_memory_list.append({"cube_id": memory["cube_id"], "memories": memories_list})
1298+
logger.info(f"search memory list is : {reformat_memory_list}")
12871299
search_result["text_mem"] = reformat_memory_list
12881300
time_end = time.time()
12891301
logger.info(

src/memos/templates/mos_prompts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
## Response Guidelines
128128
129129
### Memory Selection
130-
- Intelligently choose which memories (PersonalMemory or OuterMemory) are most relevant to the user's query
130+
- Intelligently choose which memories (PersonalMemory[P] or OuterMemory[O]) are most relevant to the user's query
131131
- Only reference memories that are directly relevant to the user's question
132132
- Prioritize the most appropriate memory type based on the context and nature of the query
133133
- **Attribution-first selection:** Distinguish memory from user vs from assistant ** before composing. For statements affecting the user’s stance/preferences/decisions/ownership, rely only on memory from user. Use **assistant memories** as reference advice or external viewpoints—never as the user’s own stance unless confirmed.
@@ -143,6 +143,11 @@
143143
- Maintain conversational tone while being informative
144144
- Use memory references to enhance, not disrupt, the user experience
145145
- **Never convert assistant viewpoints into user viewpoints without a user-confirmed memory.**
146+
147+
## Memory Types
148+
- **PersonalMemory[P]**: User-specific memories and information stored from previous interactions
149+
- **OuterMemory[O]**: External information retrieved from the internet and other sources
150+
- ** Some User query is very related to OuterMemory[O],but is not User self memory, you should not use these OuterMemory[O] to answer the question.
146151
"""
147152
QUERY_REWRITING_PROMPT = """
148153
I'm in discussion with my friend about a question, and we have already talked about something before that. Please help me analyze the logic between the question and the former dialogue, and rewrite the question we are discussing about.

0 commit comments

Comments
 (0)