Skip to content
Open
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
16 changes: 6 additions & 10 deletions mem0/memory/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

def get_fact_retrieval_messages(message, is_agent_memory=False):
"""Get fact retrieval messages based on the memory type.

Args:
message: The message content to extract facts from
is_agent_memory: If True, use agent memory extraction prompt, else use user memory extraction prompt

Returns:
tuple: (system_prompt, user_prompt)
"""
Expand Down Expand Up @@ -45,11 +45,9 @@ def format_entities(entities):
if not entities:
return ""

formatted_lines = []
for entity in entities:
simplified = f"{entity['source']} -- {entity['relationship']} -- {entity['destination']}"
formatted_lines.append(simplified)

formatted_lines = [
f"{entity['source']} -- {entity['relationship']} -- {entity['destination']}" for entity in entities
]
return "\n".join(formatted_lines)


Expand All @@ -64,11 +62,10 @@ def remove_code_blocks(content: str) -> str:
"""
pattern = r"^```[a-zA-Z0-9]*\n([\s\S]*?)\n```$"
match = re.match(pattern, content.strip())
match_res=match.group(1).strip() if match else content.strip()
match_res = match.group(1).strip() if match else content.strip()
return re.sub(r"<think>.*?</think>", "", match_res, flags=re.DOTALL).strip()



def extract_json(text):
"""
Extracts JSON content from a string, removing enclosing triple backticks and optional 'json' tag if present.
Expand Down Expand Up @@ -205,4 +202,3 @@ def sanitize_relationship_for_cypher(relationship) -> str:
sanitized = sanitized.replace(old, new)

return re.sub(r"_+", "_", sanitized).strip("_")