BaseMemory#

class langchain_core.memory.BaseMemory[source]#

Bases: 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

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
async aclear() None[source]#

Async clear memory contents.

Return type:

None

async aload_memory_variables(inputs: Dict[str, Any]) Dict[str, Any][source]#

Async return key-value pairs given the text input to the chain.

Parameters:

inputs (Dict[str, Any]) – The inputs to the chain.

Returns:

A dictionary of key-value pairs.

Return type:

Dict[str, Any]

async asave_context(inputs: Dict[str, Any], outputs: Dict[str, str]) None[source]#

Async save the context of this chain run to memory.

Parameters:
  • inputs (Dict[str, Any]) – The inputs to the chain.

  • outputs (Dict[str, str]) – The outputs of the chain.

Return type:

None

abstract clear() None[source]#

Clear memory contents.

Return type:

None

abstract load_memory_variables(inputs: Dict[str, Any]) Dict[str, Any][source]#

Return key-value pairs given the text input to the chain.

Parameters:

inputs (Dict[str, Any]) – The inputs to the chain.

Returns:

A dictionary of key-value pairs.

Return type:

Dict[str, Any]

abstract save_context(inputs: Dict[str, Any], outputs: Dict[str, str]) None[source]#

Save the context of this chain run to memory.

Parameters:
  • inputs (Dict[str, Any]) – The inputs to the chain.

  • outputs (Dict[str, str]) – The outputs of the chain.

Return type:

None

abstract property memory_variables: List[str]#

The string keys this memory class will add to chain inputs.