Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
feat(memory): add `limit` parameter in base `LongTermMemroyBase` class (#913)
Osier-Yi04ece235d53a
diff --git a/src/agentscope/memory/_long_term_memory_base.py b/src/agentscope/memory/_long_term_memory_base.pyindex 269c0c56d..7114891c4 100644--- a/src/agentscope/memory/_long_term_memory_base.py+++ b/src/agentscope/memory/_long_term_memory_base.py@@ -35,6 +35,7 @@ class LongTermMemoryBase(StateModule):async def retrieve(self,msg: Msg | list[Msg] | None,+ limit: int = 5,**kwargs: Any,) -> str:"""A developer-designed method to retrieve information from the@@ -68,6 +69,7 @@ class LongTermMemoryBase(StateModule):async def retrieve_from_memory(self,keywords: list[str],+ limit: int = 5,**kwargs: Any,) -> ToolResponse:"""Retrieve the memory based on the given keywords.@@ -77,6 +79,10 @@ class LongTermMemoryBase(StateModule):The keywords to search for in the memory, which should bespecific and concise, e.g. the person's name, the date, thelocation, etc.+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for each keyword. Defaults+ to 5.Returns:`list[Msg]`:diff --git a/src/agentscope/memory/_mem0_long_term_memory.py b/src/agentscope/memory/_mem0_long_term_memory.pyindex fba472de6..b964209c7 100644--- a/src/agentscope/memory/_mem0_long_term_memory.py+++ b/src/agentscope/memory/_mem0_long_term_memory.py@@ -5,6 +5,7 @@ This module provides a long-term memory implementation that integrateswith the mem0 library to provide persistent memory storage and retrievalcapabilities for AgentScope agents."""+import asyncioimport jsonfrom typing import Any, TYPE_CHECKINGfrom importlib import metadata@@ -391,12 +392,15 @@ class Mem0LongTermMemory(LongTermMemoryBase):Args:keywords (`list[str]`):- The keywords to search for in the memory, which should be- specific and concise, e.g. the person's name, the date, the- location, etc.+ Short, targeted search phrases (for example, a person's name,+ a specific date, a location, or a phrase describing something+ you want to retrieve from the memory). Each keyword is issued+ as an independent query against the memory store.limit (`int`, optional):- The maximum number of memories to retrieve per search.-+ The maximum number of memories to retrieve per search,+ defaults to 5.+ i.e.,the number of memories to retrieve for+ each keyword.Returns:`ToolResponse`:A ToolResponse containing the retrieved memories as JSON text.@@ -404,14 +408,18 @@ class Mem0LongTermMemory(LongTermMemoryBase):try:results = []- for keyword in keywords:- result = await self.long_term_working_memory.search(+ search_coroutines = [+ self.long_term_working_memory.search(query=keyword,agent_id=self.agent_id,user_id=self.user_id,run_id=self.run_id,limit=limit,)+ for keyword in keywords+ ]+ search_results = await asyncio.gather(*search_coroutines)+ for result in search_results:if result:results.extend([item["memory"] for item in result["results"]],@@ -537,7 +545,12 @@ class Mem0LongTermMemory(LongTermMemoryBase):specific and concise, e.g. the person's name, the date, thelocation, etc.limit (`int`, optional):- The maximum number of memories to retrieve per search.+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for the message. if the+ message is a list of messages, the limit will be applied to+ each message. If the message is a single message, then the+ limit is the total number of memories to retrieve for the+ message. Defaults to 5.**kwargs (`Any`):Additional keyword arguments.@@ -560,15 +573,21 @@ class Mem0LongTermMemory(LongTermMemoryBase):]results = []- for item in msg_strs:- result = await self.long_term_working_memory.search(+ search_coroutines = [+ self.long_term_working_memory.search(query=item,agent_id=self.agent_id,user_id=self.user_id,run_id=self.run_id,limit=limit,)+ for item in msg_strs+ ]+ search_results = await asyncio.gather(*search_coroutines)+ for result in search_results:if result:- results.extend([item["memory"] for item in result["results"]])+ results.extend(+ [memory["memory"] for memory in result["results"]],+ )return "\n".join(results)diff --git a/src/agentscope/memory/_reme/_reme_personal_long_term_memory.py b/src/agentscope/memory/_reme/_reme_personal_long_term_memory.pyindex b803b1c2c..b5d5a3ecd 100644--- a/src/agentscope/memory/_reme/_reme_personal_long_term_memory.py+++ b/src/agentscope/memory/_reme/_reme_personal_long_term_memory.py@@ -155,6 +155,7 @@ class ReMePersonalLongTermMemory(ReMeLongTermMemoryBase):async def retrieve_from_memory(self,keywords: list[str],+ limit: int = 5,**kwargs: Any,) -> ToolResponse:"""Search and retrieve relevant information from long-term memory.@@ -180,6 +181,10 @@ class ReMePersonalLongTermMemory(ReMeLongTermMemoryBase):multiple keywords for better results. Examples:["travel preferences", "Hangzhou"], ["work habits","morning routine"], ["food preferences", "tea"].+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for each keyword. Defaults+ to 3.**kwargs (`Any`):Additional keyword arguments for the retrieval operation.@@ -205,7 +210,6 @@ class ReMePersonalLongTermMemory(ReMeLongTermMemoryBase):results = []# Search for each keyword- limit = kwargs.get("limit", 3)for keyword in keywords:result = await self.app.async_execute(name="retrieve_personal_memory",@@ -328,6 +332,7 @@ class ReMePersonalLongTermMemory(ReMeLongTermMemoryBase):async def retrieve(self,msg: Msg | list[Msg] | None,+ limit: int = 5,**kwargs: Any,) -> str:"""Retrieve the content from the long-term memory.@@ -337,6 +342,13 @@ class ReMePersonalLongTermMemory(ReMeLongTermMemoryBase):The message to search for in the memory, which should bespecific and concise, e.g. the person's name, the date, thelocation, etc.+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for the message. If the+ message is a list of messages, the limit applies to each+ message. If the message is a single message, the limit is the+ total number of memories to retrieve for that message. Defaults+ to 5.**kwargs (`Any`):Additional keyword arguments.@@ -384,13 +396,11 @@ class ReMePersonalLongTermMemory(ReMeLongTermMemoryBase):return ""# Retrieve using the query from the last message- # Extract top_k from kwargs if available, default to 3- top_k = kwargs.get("top_k", 3)result = await self.app.async_execute(name="retrieve_personal_memory",workspace_id=self.workspace_id,query=query,- top_k=top_k,+ top_k=limit,**kwargs,)diff --git a/src/agentscope/memory/_reme/_reme_task_long_term_memory.py b/src/agentscope/memory/_reme/_reme_task_long_term_memory.pyindex fa1ce76e8..e309aa6de 100644--- a/src/agentscope/memory/_reme/_reme_task_long_term_memory.py+++ b/src/agentscope/memory/_reme/_reme_task_long_term_memory.py@@ -156,6 +156,7 @@ class ReMeTaskLongTermMemory(ReMeLongTermMemoryBase):async def retrieve_from_memory(self,keywords: list[str],+ limit: int = 5,**kwargs: Any,) -> ToolResponse:"""Search and retrieve relevant task experiences.@@ -188,6 +189,10 @@ class ReMeTaskLongTermMemory(ReMeLongTermMemoryBase):specific and use technical terms. Examples:["database optimization", "slow queries"], ["API design","rate limiting"], ["code refactoring", "Python"].+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for each keyword. Defaults+ to 5.**kwargs (`Any`):Additional keyword arguments. Can include 'top_k' (int)to specify number of experiences to retrieve@@ -216,13 +221,12 @@ class ReMeTaskLongTermMemory(ReMeLongTermMemoryBase):results = []# Search for each keyword- top_k = kwargs.get("top_k", 3)for keyword in keywords:result = await self.app.async_execute(name="retrieve_task_memory",workspace_id=self.workspace_id,query=keyword,- top_k=top_k,+ top_k=limit,**kwargs,)@@ -352,6 +356,7 @@ class ReMeTaskLongTermMemory(ReMeLongTermMemoryBase):async def retrieve(self,msg: Msg | list[Msg] | None,+ limit: int = 5,**kwargs: Any,) -> str:"""Retrieve relevant task experiences from memory.@@ -359,6 +364,13 @@ class ReMeTaskLongTermMemory(ReMeLongTermMemoryBase):Args:msg (`Msg | list[Msg] | None`):The message to search for relevant task experiences.+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for the message. If the+ message is a list of messages, the limit applies to each+ message. If the message is a single message, the limit is the+ total number of memories to retrieve for that message. Defaults+ to 3.**kwargs (`Any`):Additional keyword arguments.@@ -406,12 +418,11 @@ class ReMeTaskLongTermMemory(ReMeLongTermMemoryBase):return ""# Retrieve using the query from the last message- top_k = kwargs.get("top_k", 3)result = await self.app.async_execute(name="retrieve_task_memory",workspace_id=self.workspace_id,query=query,- top_k=top_k,+ top_k=limit,**kwargs,)diff --git a/src/agentscope/memory/_reme/_reme_tool_long_term_memory.py b/src/agentscope/memory/_reme/_reme_tool_long_term_memory.pyindex 49d462b7c..e941f6782 100644--- a/src/agentscope/memory/_reme/_reme_tool_long_term_memory.py+++ b/src/agentscope/memory/_reme/_reme_tool_long_term_memory.py@@ -175,6 +175,7 @@ class ReMeToolLongTermMemory(ReMeLongTermMemoryBase):async def retrieve_from_memory(self,keywords: list[str],+ limit: int = 5,**kwargs: Any,) -> ToolResponse:"""Retrieve usage guidelines and best practices for tools.@@ -209,6 +210,10 @@ class ReMeToolLongTermMemory(ReMeLongTermMemoryBase):List of tool names to retrieve guidelines for. Use theexact tool names. Examples: ["search"],["database_query", "cache_get"], ["api_call"].+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for each keyword. Defaults+ to 5.**kwargs (`Any`):Additional keyword arguments for the retrieval operation.@@ -240,6 +245,7 @@ class ReMeToolLongTermMemory(ReMeLongTermMemoryBase):name="retrieve_tool_memory",workspace_id=self.workspace_id,tool_names=tool_names,+ top_k=limit,**kwargs,)@@ -468,6 +474,7 @@ class ReMeToolLongTermMemory(ReMeLongTermMemoryBase):async def retrieve(self,msg: Msg | list[Msg] | None,+ limit: int = 5,**kwargs: Any,) -> str:"""Retrieve tool guidelines from memory.@@ -478,6 +485,13 @@ class ReMeToolLongTermMemory(ReMeLongTermMemoryBase):msg (`Msg | list[Msg] | None`):The message containing tool names or queries toretrieve guidelines for.+ limit (`int`, optional):+ The maximum number of memories to retrieve per search, i.e.,+ the number of memories to retrieve for the message. If the+ message is a list of messages, the limit applies to each+ message. If the message is a single message, the limit is the+ total number of memories to retrieve for that message. Defaults+ to 5.**kwargs (`Any`):Additional keyword arguments.@@ -517,6 +531,7 @@ class ReMeToolLongTermMemory(ReMeLongTermMemoryBase):name="retrieve_tool_memory",workspace_id=self.workspace_id,tool_names=tool_names,+ top_k=limit,**kwargs,)