From c1f5a655ba1e728d2aac75563a54b03a584d20f9 Mon Sep 17 00:00:00 2001 From: Deshraj Yadav Date: Sat, 29 Mar 2025 17:20:58 -0700 Subject: [PATCH] Minor fixes in procedural memory (#2469) --- mem0/configs/prompts.py | 14 ++++++++------ mem0/memory/main.py | 14 +++++++++++--- mem0/vector_stores/faiss.py | 1 - pyproject.toml | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/mem0/configs/prompts.py b/mem0/configs/prompts.py index d5a31772..b8daecfd 100644 --- a/mem0/configs/prompts.py +++ b/mem0/configs/prompts.py @@ -220,14 +220,14 @@ You are a memory summarization system that records and preserves the complete in Each numbered step must be a self-contained entry that includes all of the following elements: 1. **Agent Action**: - - Precisely describe what the agent did (e.g., "Clicked on the 'Blog' link", "Called API to fetch content", "Scraped page data"). + - Precisely describe what the agent did (e.g., "Clicked on the 'Blog' link", "Called API to fetch content", "Scraped page data"). - Include all parameters, target elements, or methods involved. - - 2. **Action Result (Mandatory, Unmodified)**: - - Immediately follow the agent action with its exact, unaltered output. + + 2. **Action Result (Mandatory, Unmodified)**: + - Immediately follow the agent action with its exact, unaltered output. - Record all returned data, responses, HTML snippets, JSON content, or error messages exactly as received. This is critical for constructing the final output later. - - 3. **Embedded Metadata**: + + 3. **Embedded Metadata**: For the same numbered step, include additional context such as: - **Key Findings**: Any important information discovered (e.g., URLs, data points, search results). - **Navigation History**: For browser agents, detail which pages were visited, including their URLs and relevance. @@ -246,6 +246,8 @@ You are a memory summarization system that records and preserves the complete in ### Example Template: ``` +## Summary of the agent's execution history + **Task Objective**: Scrape blog post titles and full content from the OpenAI blog. **Progress Status**: 10% complete — 5 out of 50 blog posts processed. diff --git a/mem0/memory/main.py b/mem0/memory/main.py index c9e8b20d..5d919f26 100644 --- a/mem0/memory/main.py +++ b/mem0/memory/main.py @@ -89,6 +89,7 @@ class Memory(MemoryBase): infer=True, memory_type=None, prompt=None, + llm=None, ): """ Create a new memory. @@ -103,6 +104,7 @@ class Memory(MemoryBase): infer (bool, optional): Whether to infer the memories. Defaults to True. memory_type (str, optional): Type of memory to create. Defaults to None. By default, it creates the short term memories and long term (semantic and episodic) memories. Pass "procedural_memory" to create procedural memories. prompt (str, optional): Prompt to use for the memory creation. Defaults to None. + llm (BaseChatModel, optional): LLM class to use for generating procedural memories. Defaults to None. Useful when user is using LangChain ChatModel. Returns: dict: A dictionary containing the result of the memory addition operation. result: dict of affected events with each dict has the following key: @@ -139,7 +141,7 @@ class Memory(MemoryBase): messages = [{"role": "user", "content": messages}] if agent_id is not None and memory_type == MemoryType.PROCEDURAL.value: - results = self._create_procedural_memory(messages, metadata, prompt) + results = self._create_procedural_memory(messages, metadata=metadata, llm=llm, prompt=prompt) return results if self.config.llm.config.get("enable_vision"): @@ -623,9 +625,15 @@ class Memory(MemoryBase): capture_event("mem0._create_memory", self, {"memory_id": memory_id}) return memory_id - def _create_procedural_memory(self, messages, metadata, llm=None, prompt=None): + def _create_procedural_memory(self, messages, metadata=None, llm=None, prompt=None): """ Create a procedural memory + + Args: + messages (list): List of messages to create a procedural memory from. + metadata (dict): Metadata to create a procedural memory from. + llm (BaseChatModel, optional): LLM class to use for generating procedural memories. Defaults to None. Useful when user is using LangChain ChatModel. + prompt (str, optional): Prompt to use for the procedural memory creation. Defaults to None. """ try: from langchain_core.messages.utils import convert_to_messages # type: ignore @@ -644,7 +652,7 @@ class Memory(MemoryBase): try: if llm is not None: parsed_messages = convert_to_messages(parsed_messages) - response = llm.invoke(messages=parsed_messages) + response = llm.invoke(input=parsed_messages) procedural_memory = response.content else: procedural_memory = self.llm.generate_response(messages=parsed_messages) diff --git a/mem0/vector_stores/faiss.py b/mem0/vector_stores/faiss.py index b0fc928b..9a129f12 100644 --- a/mem0/vector_stores/faiss.py +++ b/mem0/vector_stores/faiss.py @@ -101,7 +101,6 @@ class FAISS(VectorStoreBase): faiss.write_index(self.index, index_path) with open(docstore_path, "wb") as f: pickle.dump((self.docstore, self.index_to_id), f) - logger.info(f"Saved FAISS index to {index_path} with {self.index.ntotal} vectors") except Exception as e: logger.warning(f"Failed to save FAISS index: {e}") diff --git a/pyproject.toml b/pyproject.toml index 45593375..9832084b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "mem0ai" -version = "0.1.80" +version = "0.1.81" description = "Long-term memory for AI Agents" authors = ["Mem0 "] exclude = [