Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/memos/mem_os/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ def _format_mem_block(memories_all, max_items: int = 20, max_chars_each: int = 3
sequence is [i:memId] i; [P]=PersonalMemory / [O]=OuterMemory
"""
if not memories_all:
return "(none)"
return "(none)", "(none)"

lines = []
lines_o = []
lines_p = []
for idx, m in enumerate(memories_all[:max_items], 1):
mid = _short_id(getattr(m, "id", "") or "")
mtype = getattr(getattr(m, "metadata", {}), "memory_type", None) or getattr(
Expand All @@ -80,8 +81,11 @@ def _format_mem_block(memories_all, max_items: int = 20, max_chars_each: int = 3
if len(txt) > max_chars_each:
txt = txt[: max_chars_each - 1] + "…"
mid = mid or f"mem_{idx}"
lines.append(f"[{idx}:{mid}] :: [{tag}] {txt}")
return "\n".join(lines)
if tag == "O":
lines_o.append(f"[{idx}:{mid}] :: [{tag}] {txt}\n")
elif tag == "P":
lines_p.append(f"[{idx}:{mid}] :: [{tag}] {txt}")
return "\n".join(lines_o), "\n".join(lines_p)


class MOSProduct(MOSCore):
Expand Down Expand Up @@ -410,7 +414,8 @@ def _build_system_prompt(
sys_body = get_memos_prompt(
date=formatted_date, tone=tone, verbosity=verbosity, mode="base"
)
mem_block = _format_mem_block(memories_all)
mem_block_o, mem_block_p = _format_mem_block(memories_all)
mem_block = mem_block_o + "\n" + mem_block_p
prefix = (base_prompt.strip() + "\n\n") if base_prompt else ""
return (
prefix
Expand All @@ -434,8 +439,14 @@ def _build_enhance_system_prompt(
sys_body = get_memos_prompt(
date=formatted_date, tone=tone, verbosity=verbosity, mode="enhance"
)
mem_block = _format_mem_block(memories_all)
return sys_body + "\n\n# Memories\n## PersonalMemory & OuterMemory (ordered)\n" + mem_block
mem_block_o, mem_block_p = _format_mem_block(memories_all)
return (
sys_body
+ "\n\n# Memories\n## PersonalMemory (ordered)\n"
+ mem_block_p
+ "\n## OuterMemory (ordered)\n"
+ mem_block_o
)

def _extract_references_from_response(self, response: str) -> tuple[str, list[dict]]:
"""
Expand Down Expand Up @@ -1284,6 +1295,7 @@ def search(
memories["metadata"]["memory"] = memories["memory"]
memories_list.append(memories)
reformat_memory_list.append({"cube_id": memory["cube_id"], "memories": memories_list})
logger.info(f"search memory list is : {reformat_memory_list}")
search_result["text_mem"] = reformat_memory_list
time_end = time.time()
logger.info(
Expand Down
7 changes: 6 additions & 1 deletion src/memos/templates/mos_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
## Response Guidelines

### Memory Selection
- Intelligently choose which memories (PersonalMemory or OuterMemory) are most relevant to the user's query
- Intelligently choose which memories (PersonalMemory[P] or OuterMemory[O]) are most relevant to the user's query
- Only reference memories that are directly relevant to the user's question
- Prioritize the most appropriate memory type based on the context and nature of the query
- **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.
Expand All @@ -143,6 +143,11 @@
- Maintain conversational tone while being informative
- Use memory references to enhance, not disrupt, the user experience
- **Never convert assistant viewpoints into user viewpoints without a user-confirmed memory.**

## Memory Types
- **PersonalMemory[P]**: User-specific memories and information stored from previous interactions
- **OuterMemory[O]**: External information retrieved from the internet and other sources
- ** 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.
"""
QUERY_REWRITING_PROMPT = """
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.
Expand Down
Loading