+ Coverage for tinytroupe / agent / memory.py: + 0% +
+ ++ 189 statements + + + +
++ « prev + ^ index + » next + + coverage.py v7.13.4, + created at 2026-02-28 17:48 +0000 +
+ +diff --git "a/htmlcov/z_357ee38f49d3e320_memory_py.html" "b/htmlcov/z_357ee38f49d3e320_memory_py.html" new file mode 100644--- /dev/null +++ "b/htmlcov/z_357ee38f49d3e320_memory_py.html" @@ -0,0 +1,844 @@ + + +
+ ++ « prev + ^ index + » next + + coverage.py v7.13.4, + created at 2026-02-28 17:48 +0000 +
+ +1import json
+ +3from tinytroupe.agent import logger
+4from tinytroupe.agent.mental_faculty import TinyMentalFaculty
+5from tinytroupe.agent.grounding import BaseSemanticGroundingConnector
+6import tinytroupe.utils as utils
+ + +9from llama_index.core import Document
+10from typing import Any
+11import copy
+12from typing import Union
+ +14#######################################################################################################################
+15# Memory mechanisms
+16#######################################################################################################################
+ +18class TinyMemory(TinyMentalFaculty):
+19 """
+20 Base class for different types of memory.
+21 """
+ +23 def _preprocess_value_for_storage(self, value: Any) -> Any:
+24 """
+25 Preprocesses a value before storing it in memory.
+26 """
+27 # by default, we don't preprocess the value
+28 return value
+ +30 def _store(self, value: Any) -> None:
+31 """
+32 Stores a value in memory.
+33 """
+34 raise NotImplementedError("Subclasses must implement this method.")
+ +36 def store(self, value: dict) -> None:
+37 """
+38 Stores a value in memory.
+39 """
+40 self._store(self._preprocess_value_for_storage(value))
+ +42 def store_all(self, values: list) -> None:
+43 """
+44 Stores a list of values in memory.
+45 """
+46 logger.debug(f"Storing {len(values)} values in memory: {values}")
+47 for i, value in enumerate(values):
+48 logger.debug(f"Storing value #{i}: {value}")
+49 self.store(value)
+ +51 def retrieve(self, first_n: int, last_n: int, include_omission_info:bool=True, item_type:str=None) -> list:
+52 """
+53 Retrieves the first n and/or last n values from memory. If n is None, all values are retrieved.
+ +55 Args:
+56 first_n (int): The number of first values to retrieve.
+57 last_n (int): The number of last values to retrieve.
+58 include_omission_info (bool): Whether to include an information message when some values are omitted.
+59 item_type (str, optional): If provided, only retrieve memories of this type.
+ +61 Returns:
+62 list: The retrieved values.
+ +64 """
+65 raise NotImplementedError("Subclasses must implement this method.")
+ +67 def retrieve_recent(self, item_type:str=None) -> list:
+68 """
+69 Retrieves the n most recent values from memory.
+ +71 Args:
+72 item_type (str, optional): If provided, only retrieve memories of this type.
+73 """
+74 raise NotImplementedError("Subclasses must implement this method.")
+ +76 def retrieve_all(self, item_type:str=None) -> list:
+77 """
+78 Retrieves all values from memory.
+ +80 Args:
+81 item_type (str, optional): If provided, only retrieve memories of this type.
+82 """
+83 raise NotImplementedError("Subclasses must implement this method.")
+ +85 def retrieve_relevant(self, relevance_target:str, top_k=20) -> list:
+86 """
+87 Retrieves all values from memory that are relevant to a given target.
+88 """
+89 raise NotImplementedError("Subclasses must implement this method.")
+ +91 def summarize_relevant_via_full_scan(self, relevance_target: str, batch_size: int = 20, item_type: str = None) -> str:
+92 """
+93 Performs a full scan of the memory, extracting and accumulating information relevant to a query.
+ +95 This function processes all memories (or memories of a specific type if provided),
+96 extracts information relevant to the query from each memory, and accumulates this
+97 information into a coherent response.
+ +99 Args:
+100 relevance_target (str): The query specifying what information to extract from memories.
+ +102 item_type (str, optional): If provided, only process memories of this type.
+103 batch_size (int): The number of memories to process in each extraction step. The larger it is, the faster the scan, but possibly less accurate.
+104 Also, a too large value may lead to prompt length overflows, though current models can handle quite large prompts.
+ +106 Returns:
+107 str: The accumulated information relevant to the query.
+108 """
+109 logger.debug(f"Starting FULL SCAN for relevance target: {relevance_target}, item type: {item_type}")
+ +111 # Retrieve all memories of the specified type
+112 memories = self.retrieve_all(item_type=item_type)
+ +114 # Initialize accumulation
+115 accumulated_info = ""
+ +117 # Process memories in batches of qty_of_memories_per_extraction
+118 for i in range(0, len(memories), batch_size):
+119 batch = memories[i:i + batch_size]
+120 logger.debug(f"Processing memory batch #{i} in full scan")
+ +122 # Concatenate memory texts for the batch
+123 batch_text = "# Memories to be processed\n\n"
+124 batch_text += "\n\n ".join(str(memory) for memory in batch)
+ +126 # Extract information relevant to the query from the batch
+127 extracted_info = utils.semantics.extract_information_from_text(
+128 relevance_target,
+129 batch_text,
+130 context="""
+131 You are extracting information from the an agent's memory,
+132 which might include actions, stimuli, and other types of events. You want to focus on the agent's experience, NOT on the agent's cognition or internal processes.
+ +134 Assume that:
+135 - "actions" refer to behaviors produced by the agent,
+136 - "stimulus" refer to events or information from the environment or other agents that the agent perceived.
+ +138 If you read about "assistant" and "user" roles, you can ignore them, as they refer to the agent's internal implementation mechanisms, not to the agent's experience.
+139 In any case, anything related to "assistant" is the agent's output, and anything related to "user" is the agent's input. But you never refer to these roles in the report,
+140 as they are an internal implementation detail of the agent, not part of the agent's experience.
+141 """
+142 )
+ +144 logger.debug(f"Extracted information from memory batch: {extracted_info}")
+ +146 # Skip if no relevant information was found
+147 if not extracted_info:
+148 continue
+ +150 # Accumulate the extracted information
+151 accumulated_info = utils.semantics.accumulate_based_on_query(
+152 query=relevance_target,
+153 new_entry=extracted_info,
+154 current_accumulation=accumulated_info,
+155 context="""
+156 You are producing a report based on information from an agent's memory.
+157 You will put together all facts and experiences found that are relevant for the query, as a kind of summary of the agent's experience.
+158 The report will later be used to guide further agent action. You focus on the agent's experience, NOT on the agent's cognition or internal processes.
+ +160 Assume that:
+161 - "actions" refer to behaviors produced by the agent,
+162 - "stimulus" refer to events or information from the environment or other agents that the agent perceived.
+163 - if you read about "assistant" and "user" roles, you can ignore them, as they refer to the agent's internal implementation mechanisms, not to the agent's experience.
+164 In any case, anything related to "assistant" is the agent's output, and anything related to "user" is the agent's input. But you never refer to these roles in the report,
+165 as they are an internal implementation detail of the agent, not part of the agent's experience.
+ +167 Additional instructions for the accumulation process:
+168 - If the new entry is redundant with respect to some information in the current accumulation, you update the current accumulation by adding to a special counter right by
+169 the side of where the redundant information is found, so that the final report can later be used to guide further agent action (i.e., know which elements appeared more often).
+170 The special counter **must** be formated like this: "[NOTE: this information appeared X times in the memory in different forms]". If the counter was not there originally, you add it. If it was there, you update
+171 it with the new count.
+172 * Example (first element was found 3 times, the second element only once, so no counter):
+173 "I play with and feed my cat [NOTE: this information appeared 3 times in the memory in different forms]. Cats are proud animals descendant from big feline hunters.".
+ +175 """
+176 )
+177 logger.debug(f"Accumulated information so far: {accumulated_info}")
+ +179 logger.debug(f"Total accumulated information after full scan: {accumulated_info}")
+ +181 return accumulated_info
+ + +184 ###################################
+185 # Auxiliary methods
+186 ###################################
+ +188 def filter_by_item_type(self, memories:list, item_type:str) -> list:
+189 """
+190 Filters a list of memories by item type.
+ +192 Args:
+193 memories (list): The list of memories to filter.
+194 item_type (str): The item type to filter by.
+ +196 Returns:
+197 list: The filtered list of memories.
+198 """
+199 return [memory for memory in memories if memory["type"] == item_type]
+ +201 def filter_by_item_types(self, memories:list, item_types:list) -> list:
+202 """
+203 Filters a list of memories by multiple item types.
+ +205 Args:
+206 memories (list): The list of memories to filter.
+207 item_types (list): The list of item types to filter by.
+ +209 Returns:
+210 list: The filtered list of memories containing any of the specified types.
+211 """
+212 return [memory for memory in memories if memory["type"] in item_types]
+ + +215class EpisodicMemory(TinyMemory):
+216 """
+217 Provides episodic memory capabilities to an agent. Cognitively, episodic memory is the ability to remember specific events,
+218 or episodes, in the past. This class provides a simple implementation of episodic memory, where the agent can store and retrieve
+219 messages from memory.
+ +221 Subclasses of this class can be used to provide different memory implementations.
+222 """
+ +224 MEMORY_BLOCK_OMISSION_INFO = {'role': 'assistant', 'content': "Info: there were other messages here, but they were omitted for brevity.", 'simulation_timestamp': None}
+ +226 def __init__(
+227 self, fixed_prefix_length: int = 20, lookback_length: int = 100
+228 ) -> None:
+229 """
+230 Initializes the memory.
+ +232 Args:
+233 fixed_prefix_length (int): The fixed prefix length. Defaults to 20.
+234 lookback_length (int): The lookback length. Defaults to 100.
+235 """
+236 self.fixed_prefix_length = fixed_prefix_length
+237 self.lookback_length = lookback_length
+ +239 # the definitive memory that records all episodic events
+240 self.memory = []
+ +242 # the current episode buffer, which is used to store messages during an episode
+243 self.episodic_buffer = []
+ + +246 def commit_episode(self):
+247 """
+248 Ends the current episode, storing the episodic buffer in memory.
+249 """
+250 self.memory.extend(self.episodic_buffer)
+251 self.episodic_buffer = []
+ +253 def get_current_episode(self, item_types:list=None) -> list:
+254 """
+255 Returns the current episode buffer, which is used to store messages during an episode.
+ +257 Args:
+258 item_types (list, optional): If provided, only retrieve memories of these types. Defaults to None, which retrieves all types.
+ +260 Returns:
+261 list: The current episode buffer.
+262 """
+263 result = copy.copy(self.episodic_buffer)
+264 result = self.filter_by_item_types(result, item_types) if item_types is not None else result
+265 return result
+ +267 def count(self) -> int:
+268 """
+269 Returns the number of values in memory.
+270 """
+271 return len(self._memory_with_current_buffer())
+ +273 def clear(self, max_prefix_to_clear:int=None, max_suffix_to_clear:int=None):
+274 """
+275 Clears the memory, generating a permanent "episodic amnesia".
+276 If max_prefix_to_clear is not None, it clears the first n values from memory.
+277 If max_suffix_to_clear is not None, it clears the last n values from memory. If both are None,
+278 it clears all values from memory.
+ +280 Args:
+281 max_prefix_to_clear (int): The number of first values to clear.
+282 max_suffix_to_clear (int): The number of last values to clear.
+283 """
+ +285 # clears all episodic buffer messages
+286 self.episodic_buffer = []
+ +288 # then clears the memory according to the parameters
+289 if max_prefix_to_clear is not None:
+290 self.memory = self.memory[max_prefix_to_clear:]
+ +292 if max_suffix_to_clear is not None:
+293 self.memory = self.memory[:-max_suffix_to_clear]
+ +295 if max_prefix_to_clear is None and max_suffix_to_clear is None:
+296 self.memory = []
+ +298 def _memory_with_current_buffer(self) -> list:
+299 """
+300 Returns the current memory, including the episodic buffer.
+301 This is useful for retrieving the most recent memories, including the current episode.
+302 """
+303 return self.memory + self.episodic_buffer
+ +305 ######################################
+306 # General memory methods
+307 ######################################
+308 def _store(self, value: Any) -> None:
+309 """
+310 Stores a value in memory.
+311 """
+312 self.episodic_buffer.append(value)
+ +314 def retrieve(self, first_n: int, last_n: int, include_omission_info:bool=True, item_type:str=None) -> list:
+315 """
+316 Retrieves the first n and/or last n values from memory. If n is None, all values are retrieved.
+ +318 Args:
+319 first_n (int): The number of first values to retrieve.
+320 last_n (int): The number of last values to retrieve.
+321 include_omission_info (bool): Whether to include an information message when some values are omitted.
+322 item_type (str, optional): If provided, only retrieve memories of this type.
+ +324 Returns:
+325 list: The retrieved values.
+ +327 """
+ +329 omisssion_info = [EpisodicMemory.MEMORY_BLOCK_OMISSION_INFO] if include_omission_info else []
+ +331 # use the other methods in the class to implement
+332 if first_n is not None and last_n is not None:
+333 return self.retrieve_first(first_n, include_omission_info=False, item_type=item_type) + omisssion_info + self.retrieve_last(last_n, include_omission_info=False, item_type=item_type)
+334 elif first_n is not None:
+335 return self.retrieve_first(first_n, include_omission_info, item_type=item_type)
+336 elif last_n is not None:
+337 return self.retrieve_last(last_n, include_omission_info, item_type=item_type)
+338 else:
+339 return self.retrieve_all(item_type=item_type)
+ +341 def retrieve_recent(self, include_omission_info:bool=True, item_type:str=None) -> list:
+342 """
+343 Retrieves the n most recent values from memory.
+ +345 Args:
+346 include_omission_info (bool): Whether to include an information message when some values are omitted.
+347 item_type (str, optional): If provided, only retrieve memories of this type.
+348 """
+349 omisssion_info = [EpisodicMemory.MEMORY_BLOCK_OMISSION_INFO] if include_omission_info else []
+ +351 # Filter memories if item_type is provided
+352 memories = self._memory_with_current_buffer() if item_type is None else self.filter_by_item_type(self._memory_with_current_buffer(), item_type)
+ +354 # compute fixed prefix
+355 fixed_prefix = memories[: self.fixed_prefix_length] + omisssion_info
+ +357 # how many lookback values remain?
+358 remaining_lookback = min(
+359 len(memories) - len(fixed_prefix) + (1 if include_omission_info else 0), self.lookback_length
+360 )
+ +362 # compute the remaining lookback values and return the concatenation
+363 if remaining_lookback <= 0:
+364 return fixed_prefix
+365 else:
+366 return fixed_prefix + memories[-remaining_lookback:]
+ +368 def retrieve_all(self, item_type:str=None) -> list:
+369 """
+370 Retrieves all values from memory.
+ +372 Args:
+373 item_type (str, optional): If provided, only retrieve memories of this type.
+374 """
+375 memories = self._memory_with_current_buffer() if item_type is None else self.filter_by_item_type(self._memory_with_current_buffer(), item_type)
+376 return copy.copy(memories)
+ +378 def retrieve_relevant(self, relevance_target: str, top_k:int) -> list:
+379 """
+380 Retrieves top-k values from memory that are most relevant to a given target.
+381 """
+382 raise NotImplementedError("Subclasses must implement this method.")
+ +384 def retrieve_first(self, n: int, include_omission_info:bool=True, item_type:str=None) -> list:
+385 """
+386 Retrieves the first n values from memory.
+ +388 Args:
+389 n (int): The number of values to retrieve.
+390 include_omission_info (bool): Whether to include an information message when some values are omitted.
+391 item_type (str, optional): If provided, only retrieve memories of this type.
+392 """
+393 omisssion_info = [EpisodicMemory.MEMORY_BLOCK_OMISSION_INFO] if include_omission_info else []
+ +395 memories = self._memory_with_current_buffer() if item_type is None else self.filter_by_item_type(self._memory_with_current_buffer(), item_type)
+396 return memories[:n] + omisssion_info
+ +398 def retrieve_last(self, n: int=None, include_omission_info:bool=True, item_type:str=None) -> list:
+399 """
+400 Retrieves the last n values from memory.
+ +402 Args:
+403 n (int): The number of values to retrieve, or None to retrieve all values.
+404 include_omission_info (bool): Whether to include an information message when some values are omitted.
+405 item_type (str, optional): If provided, only retrieve memories of this type.
+406 """
+407 omisssion_info = [EpisodicMemory.MEMORY_BLOCK_OMISSION_INFO] if include_omission_info else []
+ +409 memories = self._memory_with_current_buffer() if item_type is None else self.filter_by_item_type(self._memory_with_current_buffer(), item_type)
+410 memories = memories[-n:] if n is not None else memories
+ +412 return omisssion_info + memories
+ + +415@utils.post_init
+416class SemanticMemory(TinyMemory):
+417 """
+418 In Cognitive Psychology, semantic memory is the memory of meanings, understandings, and other concept-based knowledge unrelated to specific
+419 experiences. It is not ordered temporally, and it is not about remembering specific events or episodes. This class provides a simple implementation
+420 of semantic memory, where the agent can store and retrieve semantic information.
+421 """
+ +423 serializable_attributes = ["memories", "semantic_grounding_connector"]
+ +425 def __init__(self, memories: list=None) -> None:
+426 self.memories = memories
+ +428 self.semantic_grounding_connector = None
+ +430 # @post_init ensures that _post_init is called after the __init__ method
+ +432 def _post_init(self):
+433 """
+434 This will run after __init__, since the class has the @post_init decorator.
+435 It is convenient to separate some of the initialization processes to make deserialize easier.
+436 """
+ +438 if not hasattr(self, 'memories') or self.memories is None:
+439 self.memories = []
+ +441 if not hasattr(self, 'semantic_grounding_connector') or self.semantic_grounding_connector is None:
+442 self.semantic_grounding_connector = BaseSemanticGroundingConnector("Semantic Memory Storage")
+ +444 # TODO remove?
+445 #self.semantic_grounding_connector.add_documents(self._build_documents_from(self.memories))
+ + +448 def _preprocess_value_for_storage(self, value: dict) -> Any:
+449 logger.debug(f"Preprocessing value for storage: {value}")
+ +451 if isinstance(value, dict):
+452 engram = {"role": "assistant",
+453 "content": value['content'],
+454 "type": value.get("type", "information"), # Default to 'information' if type is not specified
+455 "simulation_timestamp": value.get("simulation_timestamp", None)}
+ +457 # Refine the content of the engram is built based on the type of the value to make it more meaningful.
+458 if value['type'] == 'action':
+459 engram['content'] = f"# Action performed\n" +\
+460 f"I have performed the following action at date and time {value['simulation_timestamp']}:\n\n"+\
+461 f" {value['content']}"
+ +463 elif value['type'] == 'stimulus':
+464 engram['content'] = f"# Stimulus\n" +\
+465 f"I have received the following stimulus at date and time {value['simulation_timestamp']}:\n\n"+\
+466 f" {value['content']}"
+467 elif value['type'] == 'feedback':
+468 engram['content'] = f"# Feedback\n" +\
+469 f"I have received the following feedback at date and time {value['simulation_timestamp']}:\n\n"+\
+470 f" {value['content']}"
+471 elif value['type'] == 'consolidated':
+472 engram['content'] = f"# Consolidated Memory\n" +\
+473 f"I have consolidated the following memory at date and time {value['simulation_timestamp']}:\n\n"+\
+474 f" {value['content']}"
+475 elif value['type'] == 'reflection':
+476 engram['content'] = f"# Reflection\n" +\
+477 f"I have reflected on the following memory at date and time {value['simulation_timestamp']}:\n\n"+\
+478 f" {value['content']}"
+479 else:
+480 engram['content'] = f"# Information\n" +\
+481 f"I have obtained following information at date and time {value['simulation_timestamp']}:\n\n"+\
+482 f" {value['content']}"
+ +484 # else: # Anything else here?
+ +486 else:
+487 # If the value is not a dictionary, we just store it as is, but we still wrap it in an engram
+488 engram = {"role": "assistant",
+489 "content": value,
+490 "type": "information", # Default to 'information' if type is not specified
+491 "simulation_timestamp": None}
+ +493 logger.debug(f"Engram created for storage: {engram}")
+ +495 return engram
+ +497 def _store(self, value: Any) -> None:
+498 logger.debug(f"Preparing engram for semantic memory storage, input value: {value}")
+499 self.memories.append(value) # Store the value in the local memory list
+ +501 # then econduct the value to a Document and store it in the semantic grounding connector
+502 # This is the actual storage in the semantic memory to allow semantic retrieval
+503 engram_doc = self._build_document_from(value)
+504 logger.debug(f"Storing engram in semantic memory: {engram_doc}")
+505 self.semantic_grounding_connector.add_document(engram_doc)
+ +507 def retrieve_relevant(self, relevance_target:str, top_k=20) -> list:
+508 """
+509 Retrieves all values from memory that are relevant to a given target.
+510 """
+511 return self.semantic_grounding_connector.retrieve_relevant(relevance_target, top_k)
+ +513 def retrieve_all(self, item_type:str=None) -> list:
+514 """
+515 Retrieves all values from memory.
+ +517 Args:
+518 item_type (str, optional): If provided, only retrieve memories of this type.
+519 """
+ +521 memories = []
+ +523 logger.debug(f"Retrieving all documents from semantic memory connector, a total of {len(self.semantic_grounding_connector.documents)} documents.")
+524 for document in self.semantic_grounding_connector.documents:
+525 logger.debug(f"Retrieving document from semantic memory: {document}")
+526 memory_text = document.text
+527 logger.debug(f"Document text retrieved: {memory_text}")
+ +529 try:
+530 memory = json.loads(memory_text)
+531 logger.debug(f"Memory retrieved: {memory}")
+532 memories.append(memory)
+ +534 except json.JSONDecodeError as e:
+535 logger.warning(f"Could not decode memory from document text: {memory_text}. Error: {e}")
+ +537 if item_type is not None:
+538 memories = self.filter_by_item_type(memories, item_type)
+ +540 return memories
+ +542 #####################################
+543 # Auxiliary compatibility methods
+544 #####################################
+ +546 def _build_document_from(self, memory) -> Document:
+547 # TODO: add any metadata as well?
+ +549 # make sure we are dealing with a dictionary
+550 if not isinstance(memory, dict):
+551 memory = {"content": memory, "type": "information"}
+ +553 # ensures double quotes are used for JSON serialization, and maybe other formatting details
+554 memory_txt = json.dumps(memory, ensure_ascii=False)
+555 logger.debug(f"Building document from memory: {memory_txt}")
+ +557 return Document(text=memory_txt)
+ +559 def _build_documents_from(self, memories: list) -> list:
+560 return [self._build_document_from(memory) for memory in memories]
+ + +563###################################################################################################
+564# Memory consolidation and optimization mechanisms
+565###################################################################################################
+566class MemoryProcessor:
+567 """
+568 Base class for memory consolidation and optimization mechanisms.
+569 """
+ +571 def process(self, memories: list, timestamp: str=None, context:Union[str, list, dict] = None, persona:Union[str, dict] = None, sequential: bool = True) -> list:
+572 """
+573 Transforms the given memories. Transformation can be anything from consolidation to optimization, depending on the implementation.
+ +575 Each memory is a dictionary of the form:
+576 {
+577 'role': role,
+578 'content': content,
+579 'type': 'action'/'stimulus'/'feedback',
+580 'simulation_timestamp': timestamp
+581 }
+ +583 Args:
+584 memories (list): The list of memories to consolidate.
+585 sequential (bool): Whether the provided memories are to be interpreted sequentially (e.g., episodes in sequence) or not (e.g., abstract facts).
+ +587 Returns:
+588 list: A list with the consolidated memories, following the same format as the input memories, but different in content.
+589 """
+590 raise NotImplementedError("Subclasses must implement this method.")
+ +592class EpisodicConsolidator(MemoryProcessor):
+593 """
+594 Consolidates episodic memories into a more abstract representation, such as a summary or an abstract fact.
+595 """
+ +597 def process(self, memories: list, timestamp: str=None, context:Union[str, list, dict] = None, persona:Union[str, dict] = None, sequential: bool = True) -> list:
+598 logger.debug(f"STARTING MEMORY CONSOLIDATION: {len(memories)} memories to consolidate")
+ +600 enriched_context = f"CURRENT COGNITIVE CONTEXT OF THE AGENT: {context}" if context else "No specific context provided for consolidation."
+ +602 result = self._consolidate(memories, timestamp, enriched_context, persona)
+603 logger.debug(f"Consolidated {len(memories)} memories into: {result}")
+ +605 return result
+ +607 @utils.llm(enable_json_output_format=True, enable_justification_step=False)
+608 def _consolidate(self, memories: list, timestamp: str, context:str, persona:str) -> dict:
+609 """
+610 Given a list of input episodic memories, this method consolidates them into more organized structured representations, which however preserve all information and important details.
+ +612 For this process, you assume:
+613 - This consolidation is being carried out by an agent, so the memories are from the agent's perspective. "Actions" refer to behaviors produced by the agent,
+614 while "stimulus" refer to events or information from the environment or other agents that the agent has perceived.
+615 * Thus, in the consoldation you write "I have done X" or "I have perceived Y", not "the agent has done X" or "the agent has perceived Y".
+616 - The purpose of consolidation is to restructure and organize the most relevant information from the episodic memories, so that any facts learned therein can be used in future reasoning processes.
+617 * If a `context` is provided, you can use it to guide the consolidation process, making sure that the memories are consolidated in the most useful way under the given context.
+618 For example, if the agent is looking for a specific type of information, you can focus the consolidation on that type of information, preserving more details about it
+619 than you would otherwise.
+620 * If a `persona` is provided, you can use it to guide the consolidation process, making sure that the memories are consolidated in a way that is consistent with the persona.
+621 For example, if the persona is that of a cat lover, you can focus the consolidation on the agent's experiences with cats, preserving more details about them than you would otherwise.
+622 - If the memory contians a `content` field, that's where the relevant information is found. Otherwise, consider the whole memory as relevant information.
+ +624 The consolidation process follows these rules:
+625 - Each consolidated memory groups together all similar entries: so actions are grouped together, stimuli go together, facts are grouped together, impressions are grouped together,
+626 learned processes are grouped together, and ad-hoc elements go together too. Noise, minor details and irrelevant elements are discarded.
+627 In all, you will produce at most the following consolidated entries (you can avoid some if appropriate, but not add more):
+628 * Actions: all actions are grouped together, giving an account of what the agent has done.
+629 * Stimuli: all stimuli are grouped together, giving an account of what the agent has perceived.
+630 * Facts: facts are extracted from the actions and stimuli, and then grouped together in a single entry, consolidating learning of objective facts.
+631 * Impressions: impressions, feelings, or other subjective experiences are also extracted, and then grouped together in a single entry, consolidating subjective experiences.
+632 * Procedural: learned processes (e.g., how to do certain things) are also extracted, formatted in an algorithmic way (i.e., pseudo-code that is self-explanatory), and then grouped together in a
+633 single entry, consolidating learned processes.
+634 * Ad-Hoc: important elements that do not correspond to these options are also grouped together in an ad-hoc single entry, consolidating other types of information.
+635 - Each consolidated memory is a comprehensive report of the relevant information from the input memories, preserving all details. The consolidation merely reorganizes the information,
+636 but does not remove any relevant information. The consolidated memories are not summaries, but rather a more organized and structured representation of the information in the input memories.
+ + +639 Each input memory is a dictionary of the form:
+640 ```
+641 {
+642 "role": role,
+643 "content": content,
+644 "type": "action"/"stimulus"/"feedback"/"reflection",
+645 "simulation_timestamp": timestamp
+646 }
+647 ```
+ +649 Each consolidated output memory is a dictionary of the form:
+650 ```
+651 {
+652 "content": content,
+653 "type": "consolidated",
+654 "simulation_timestamp": timestamp of the consolidation
+655 }
+656 ```
+ + +659 So the final value outputed **must** be a JSON composed of a list of dictionaries, each representing a consolidated memory, **always** with the following structure:
+660 ```
+661 {"consolidation":
+662 [
+663 {
+664 "content": content_1,
+665 "type": "consolidated",
+666 "simulation_timestamp": timestamp of the consolidation
+667 },
+668 {
+669 "content": content_2,
+670 "type": "consolidated",
+671 "simulation_timestamp": timestamp of the consolidation
+672 },
+673 ...
+674 ]
+675 }
+676 ```
+ +678 Note:
+679 - because the output is a JSON, you must use double quotes for the keys and string values.
+680 ## Example (simplified)
+ +682 Here's a simplified example. Suppose the following memory contents are provided as input (simplifying here as just a bullet list of contents):
+683 - stimulus: "I have seen a cat, walking beautifully in the street"
+684 - stimulus: "I have seen a dog, barking loudly at a passerby, looking very aggressive"
+685 - action: "I have petted the cat, run around with him (or her?), saying a thousand times how cute it is, and how much I seem to like cats"
+686 - action: "I just realized that I like cats more than dogs. For example, look at this one, it is so cute, so civilized, so noble, so elegant, an inspiring animal! I had never noted this before! "
+687 - stimulus: "The cat is meowing very loudly, it seems to be hungry"
+688 - stimulus: "Somehow a big capivara has appeared in the room, it is looking at me with curiosity"
+ +690 Then, this would be a possible CORRECT output of the consolidation process (again, simplified, showing only contents in bullet list format):
+691 - consolidated actions: "I have petted the cat, run around with it, and expressed my admiration for cats."
+692 - consolidated stimuli: "I have seen a beautiful but hungry cat, a loud and agressive-looking dog, and - surprisingly - a capivara"
+693 - consolidated impressions: "I felt great admiration for the cat, they look like such noble and elegant animals."
+694 - consolidated facts: "I like cats more than dogs because they are cute and noble creatures."
+ +696 These are correct because they focus on the agent's experience. In contrast, this would be an INCORRECT output of the consolidation process:
+697 - consolidated actions: "the user sent messages about a cat, a dog and a capivara, and about playing with the cat."
+698 - consolidated facts: "the assistant has received various messages at different times, and has performed actions in response to them."
+ +700 These are incorrect because they focus on the agent's cognition and internal implementation mechanisms, not on the agent's experience.
+ +702 Args:
+703 memories (list): The list of memories to consolidate.
+704 timestamp (str): The timestamp of the consolidation, which will be used in the consolidated memories instead of any original timestamp.
+705 context (str, optional): Additional context to guide the consolidation process. This can be used to provide specific instructions or constraints for the consolidation.
+706 persona (str, optional): The persona of the agent, which can be used to guide the consolidation process. This can be used to provide specific instructions or constraints for the consolidation.
+ +708 Returns:
+709 dict: A dictionary with a single key "consolidation", whose value is a list of consolidated memories, each represented as a dictionary with the structure described above.
+710 """
+711 # llm annotation will handle the implementation
+ +713# TODO work in progress below
+ +715class ReflectionConsolidator(MemoryProcessor):
+716 """
+717 Memory reflection mechanism.
+718 """
+ +720 def process(self, memories: list, timestamp: str=None, context:Union[str, list, dict] = None, persona:Union[str, dict] = None, sequential: bool = True) -> list:
+721 return self._reflect(memories, timestamp)
+ +723 def _reflect(self, memories: list, timestamp: str) -> list:
+724 """
+725 Given a list of input episodic memories, this method reflects on them and produces a more abstract representation, such as a summary or an abstract fact.
+726 The reflection process follows these rules:
+727 - Objective facts or knowledge that are present in the set of memories are grouped together, abstracted (if necessary) and summarized. The aim is to
+728 produce a semantic memory.
+729 - Impressions, feelings, or other subjective experiences are summarized into a more abstract representation, such as a summary or an abstract subjective fact.
+730 - Timestamps in the consolidated memories refer to the moment of the reflection, not to the source events that produced the original episodic memories.
+731 - No episodic memory is generated, all memories are consolidated as more abstract semantic memories.
+732 - In general, the reflection process aims to reduce the number of memories while preserving the most relevant information and removing redundant or less relevant information.
+733 """
+734 pass # TODO
+735 def _reflect(self, memories: list, timestamp: str) -> list:
+736 """
+737 Given a list of input episodic memories, this method reflects on them and produces a more abstract representation, such as a summary or an abstract fact.
+738 The reflection process follows these rules:
+739 - Objective facts or knowledge that are present in the set of memories are grouped together, abstracted (if necessary) and summarized. The aim is to
+740 produce a semantic memory.
+741 - Impressions, feelings, or other subjective experiences are summarized into a more abstract representation, such as a summary or an abstract subjective fact.
+742 - Timestamps in the consolidated memories refer to the moment of the reflection, not to the source events that produced the original episodic memories.
+743 - No episodic memory is generated, all memories are consolidated as more abstract semantic memories.
+744 - In general, the reflection process aims to reduce the number of memories while preserving the most relevant information and removing redundant or less relevant information.
+745 """
+746 pass # TODO
+ +