base_memory.py — langchain Source File
Architecture documentation for base_memory.py, a python file in the langchain codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 25db842f_c743_b046_e8d1_fa3821a6f56a["base_memory.py"] 50e20440_a135_6be3_a5a5_67791be5a2a6["abc"] 25db842f_c743_b046_e8d1_fa3821a6f56a --> 50e20440_a135_6be3_a5a5_67791be5a2a6 feec1ec4_6917_867b_d228_b134d0ff8099["typing"] 25db842f_c743_b046_e8d1_fa3821a6f56a --> feec1ec4_6917_867b_d228_b134d0ff8099 2485b66a_3839_d0b6_ad9c_a4ff40457dc6["langchain_core._api"] 25db842f_c743_b046_e8d1_fa3821a6f56a --> 2485b66a_3839_d0b6_ad9c_a4ff40457dc6 e082ede2_868f_7149_8977_a2af7e5aeb38["langchain_core.load.serializable"] 25db842f_c743_b046_e8d1_fa3821a6f56a --> e082ede2_868f_7149_8977_a2af7e5aeb38 31eab4ab_7281_1e6c_b17d_12e6ad9de07a["langchain_core.runnables"] 25db842f_c743_b046_e8d1_fa3821a6f56a --> 31eab4ab_7281_1e6c_b17d_12e6ad9de07a dd5e7909_a646_84f1_497b_cae69735550e["pydantic"] 25db842f_c743_b046_e8d1_fa3821a6f56a --> dd5e7909_a646_84f1_497b_cae69735550e style 25db842f_c743_b046_e8d1_fa3821a6f56a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""**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.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any
from langchain_core._api import deprecated
from langchain_core.load.serializable import Serializable
from langchain_core.runnables import run_in_executor
from pydantic import ConfigDict
@deprecated(
since="0.3.3",
removal="1.0.0",
message=(
"Please see the migration guide at: "
"https://python.langchain.com/docs/versions/migrating_memory/"
),
)
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.
"""
await run_in_executor(None, self.save_context, inputs, outputs)
@abstractmethod
def clear(self) -> None:
"""Clear memory contents."""
async def aclear(self) -> None:
"""Async clear memory contents."""
await run_in_executor(None, self.clear)
Domain
Subdomains
Classes
Dependencies
- abc
- langchain_core._api
- langchain_core.load.serializable
- langchain_core.runnables
- pydantic
- typing
Source
Frequently Asked Questions
What does base_memory.py do?
base_memory.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, LanguageModelBase subdomain.
What does base_memory.py depend on?
base_memory.py imports 6 module(s): abc, langchain_core._api, langchain_core.load.serializable, langchain_core.runnables, pydantic, typing.
Where is base_memory.py in the architecture?
base_memory.py is located at libs/langchain/langchain_classic/base_memory.py (domain: LangChainCore, subdomain: LanguageModelBase, directory: libs/langchain/langchain_classic).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free