"""**Memory** maintains Chain state, incorporating context from past runs.This module contains memory abstractions from LangChain v0.0.x.These abstractions are now deprecated and will be removed in LangChain v1.0.0."""# noqa: E501from__future__importannotationsfromabcimportABC,abstractmethodfromtypingimportAnyfrompydanticimportConfigDictfromlangchain_core._apiimportdeprecatedfromlangchain_core.load.serializableimportSerializablefromlangchain_core.runnablesimportrun_in_executor@deprecated(since="0.3.3",removal="1.0.0",message=("Please see the migration guide at: ""https://python.langchain.com/docs/versions/migrating_memory/"),)classBaseMemory(Serializable,ABC):"""Abstract base class for memory in Chains. Memory refers to state in Chains. Memory can be used to store information about past executions of a Chain and inject that information into the inputs of future executions of the Chain. For example, for conversational Chains Memory can be used to store conversations and automatically add them to future model prompts so that the model has the necessary context to respond coherently to the latest input. Example: .. code-block:: python class SimpleMemory(BaseMemory): memories: Dict[str, Any] = dict() @property def memory_variables(self) -> List[str]: return list(self.memories.keys()) def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]: return self.memories def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None: pass def clear(self) -> None: pass """# noqa: E501model_config=ConfigDict(arbitrary_types_allowed=True,)@property@abstractmethoddefmemory_variables(self)->list[str]:"""The string keys this memory class will add to chain inputs."""@abstractmethoddefload_memory_variables(self,inputs:dict[str,Any])->dict[str,Any]:"""Return key-value pairs given the text input to the chain. Args: inputs: The inputs to the chain. Returns: A dictionary of key-value pairs. """asyncdefaload_memory_variables(self,inputs:dict[str,Any])->dict[str,Any]:"""Async return key-value pairs given the text input to the chain. Args: inputs: The inputs to the chain. Returns: A dictionary of key-value pairs. """returnawaitrun_in_executor(None,self.load_memory_variables,inputs)@abstractmethoddefsave_context(self,inputs:dict[str,Any],outputs:dict[str,str])->None:"""Save the context of this chain run to memory. Args: inputs: The inputs to the chain. outputs: The outputs of the chain. """asyncdefasave_context(self,inputs:dict[str,Any],outputs:dict[str,str])->None:"""Async save the context of this chain run to memory. Args: inputs: The inputs to the chain. outputs: The outputs of the chain. """awaitrun_in_executor(None,self.save_context,inputs,outputs)@abstractmethoddefclear(self)->None:"""Clear memory contents."""asyncdefaclear(self)->None:"""Async clear memory contents."""awaitrun_in_executor(None,self.clear)