BaseMemory Class — langchain Architecture
Architecture documentation for the BaseMemory class in base_memory.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 9071c852_df07_5016_b7fc_1000d3b44bc4["BaseMemory"] f3658565_d05c_7f49_b7f8_622b7ef34f33["Serializable"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|extends| f3658565_d05c_7f49_b7f8_622b7ef34f33 637dec43_1eaa_2788_4cea_3a0d8a0045d9["base_memory.py"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|defined in| 637dec43_1eaa_2788_4cea_3a0d8a0045d9 3d50963b_6174_a365_a6eb_d96063aa9e79["memory_variables()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| 3d50963b_6174_a365_a6eb_d96063aa9e79 c6603e82_b15a_c8b9_7bf3_e9cbca110ff8["load_memory_variables()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| c6603e82_b15a_c8b9_7bf3_e9cbca110ff8 fe047b21_4645_2004_47d3_75f6382c2365["aload_memory_variables()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| fe047b21_4645_2004_47d3_75f6382c2365 a4ac5755_13f6_21cb_5e8e_a3bae56510f1["save_context()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| a4ac5755_13f6_21cb_5e8e_a3bae56510f1 832ffdc9_3470_e997_b609_c4f1206e6a1f["asave_context()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| 832ffdc9_3470_e997_b609_c4f1206e6a1f 3118ff2b_8a4c_7306_d8ae_3a32aa5125eb["clear()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| 3118ff2b_8a4c_7306_d8ae_3a32aa5125eb 0dae4b3f_c564_10c3_e532_695b74dc4206["aclear()"] 9071c852_df07_5016_b7fc_1000d3b44bc4 -->|method| 0dae4b3f_c564_10c3_e532_695b74dc4206
Relationship Graph
Source Code
libs/langchain/langchain_classic/base_memory.py lines 27–116
class BaseMemory(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:
```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
```
"""
model_config = ConfigDict(
arbitrary_types_allowed=True,
)
@property
@abstractmethod
def memory_variables(self) -> list[str]:
"""The string keys this memory class will add to chain inputs."""
@abstractmethod
def load_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.
"""
async def aload_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.
"""
return await run_in_executor(None, self.load_memory_variables, inputs)
@abstractmethod
def save_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.
"""
async def asave_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.
"""
Extends
Source
Frequently Asked Questions
What is the BaseMemory class?
BaseMemory is a class in the langchain codebase, defined in libs/langchain/langchain_classic/base_memory.py.
Where is BaseMemory defined?
BaseMemory is defined in libs/langchain/langchain_classic/base_memory.py at line 27.
What does BaseMemory extend?
BaseMemory extends Serializable.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free