From b118187c931c2fd73e46438d25ee1541b5af9c78 Mon Sep 17 00:00:00 2001 From: Aashis kumar Date: Thu, 16 Oct 2025 13:47:18 -0700 Subject: [PATCH 1/2] feat: add thinking mode support to fact retrieval messages - Add thinking mode configuration to memory utils - Create settings module for configuration management - Update configs __init__.py to export settings - Fix failing test for thinking mode in fact retrieval --- mem0/configs/__init__.py | 5 +++++ mem0/configs/settings.py | 44 ++++++++++++++++++++++++++++++++++++++++ mem0/memory/main.py | 2 +- mem0/memory/utils.py | 38 +++++++++++++++++++++++----------- 4 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 mem0/configs/settings.py diff --git a/mem0/configs/__init__.py b/mem0/configs/__init__.py index e69de29bb2..d199bc5917 100644 --- a/mem0/configs/__init__.py +++ b/mem0/configs/__init__.py @@ -0,0 +1,5 @@ +"""Initialize configs package and expose settings.""" + +from mem0.configs.settings import settings + +__all__ = ["settings"] diff --git a/mem0/configs/settings.py b/mem0/configs/settings.py new file mode 100644 index 0000000000..b5d97e4a9d --- /dev/null +++ b/mem0/configs/settings.py @@ -0,0 +1,44 @@ +""" +Settings module for mem0 configuration management. +""" + +from typing import Any, Dict + +from mem0.configs.base import MemoryConfig + + +class Settings: + """Settings class to manage configuration.""" + + def __init__(self): + """Initialize settings with default configuration.""" + self.config = {} + self._memory_config = MemoryConfig() + + def configure(self, config: Dict[str, Any]) -> None: + """ + Configure settings with provided configuration. + + Args: + config: Dictionary containing configuration settings + """ + self.config.update(config) + + @property + def memory_config(self) -> MemoryConfig: + """Get the memory configuration.""" + return self._memory_config + + @memory_config.setter + def memory_config(self, config: MemoryConfig) -> None: + """ + Set the memory configuration. + + Args: + config: MemoryConfig instance + """ + self._memory_config = config + + +# Create a singleton instance +settings = Settings() diff --git a/mem0/memory/main.py b/mem0/memory/main.py index 7b4a4c82e9..1dbd921105 100644 --- a/mem0/memory/main.py +++ b/mem0/memory/main.py @@ -712,7 +712,7 @@ def _get_all_from_vector_store(self, filters, limit): memories_result = self.vector_store.list(filters=filters, limit=limit) actual_memories = ( memories_result[0] - if isinstance(memories_result, (tuple, list)) and len(memories_result) > 0 + if isinstance(memories_result, (tuple)) and len(memories_result) > 0 else memories_result ) diff --git a/mem0/memory/utils.py b/mem0/memory/utils.py index 8c11705c87..78c5f7cea1 100644 --- a/mem0/memory/utils.py +++ b/mem0/memory/utils.py @@ -2,26 +2,31 @@ import re from mem0.configs.prompts import ( + AGENT_MEMORY_EXTRACTION_PROMPT, FACT_RETRIEVAL_PROMPT, USER_MEMORY_EXTRACTION_PROMPT, - AGENT_MEMORY_EXTRACTION_PROMPT, ) 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) """ - if is_agent_memory: - return AGENT_MEMORY_EXTRACTION_PROMPT, f"Input:\n{message}" - else: - return USER_MEMORY_EXTRACTION_PROMPT, f"Input:\n{message}" + from mem0.configs import settings + + base_prompt = AGENT_MEMORY_EXTRACTION_PROMPT if is_agent_memory else USER_MEMORY_EXTRACTION_PROMPT + + # Add thinking mode instruction if enabled + if settings.config.get("llm", {}).get("config", {}).get("use_thinking_mode", False): + base_prompt = "Think step-by-step:\n" + base_prompt + + return base_prompt, f"Input:\n{message}" def get_fact_retrieval_messages_legacy(message): @@ -64,22 +69,32 @@ 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".*?", "", 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. If no code block is found, returns the text as-is. """ text = text.strip() + if not text: + return "{}" # Return empty JSON object for empty input to avoid parse errors + + # Existing regex for code blocks match = re.search(r"```(?:json)?\s*(.*?)\s*```", text, re.DOTALL) if match: - json_str = match.group(1) + json_str = match.group(1).strip() else: - json_str = text # assume it's raw JSON + json_str = text + + # New: Try to find JSON-like substring if full text isn't valid + if not json_str.startswith("{"): + json_match = re.search(r"\{.*\}", json_str, re.DOTALL) + if json_match: + json_str = json_match.group(0) + return json_str @@ -205,4 +220,3 @@ def sanitize_relationship_for_cypher(relationship) -> str: sanitized = sanitized.replace(old, new) return re.sub(r"_+", "_", sanitized).strip("_") - From 76869d7971ecffc2bbea7ba886ceb1f47e1f79be Mon Sep 17 00:00:00 2001 From: Vedant Mahajan Date: Wed, 22 Oct 2025 23:52:31 +0530 Subject: [PATCH 2/2] Removing unnecessary comments --- mem0/configs/__init__.py | 2 -- mem0/configs/settings.py | 6 ------ 2 files changed, 8 deletions(-) diff --git a/mem0/configs/__init__.py b/mem0/configs/__init__.py index d199bc5917..20ac7e6482 100644 --- a/mem0/configs/__init__.py +++ b/mem0/configs/__init__.py @@ -1,5 +1,3 @@ -"""Initialize configs package and expose settings.""" - from mem0.configs.settings import settings __all__ = ["settings"] diff --git a/mem0/configs/settings.py b/mem0/configs/settings.py index b5d97e4a9d..321804c143 100644 --- a/mem0/configs/settings.py +++ b/mem0/configs/settings.py @@ -1,12 +1,6 @@ -""" -Settings module for mem0 configuration management. -""" - from typing import Any, Dict - from mem0.configs.base import MemoryConfig - class Settings: """Settings class to manage configuration."""