load_memory_variables() — langchain Function Reference
Architecture documentation for the load_memory_variables() function in entity.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD b961d203_0a7e_862d_a270_668983457309["load_memory_variables()"] f6985512_c22e_0ae9_c629_acb233df5408["ConversationEntityMemory"] b961d203_0a7e_862d_a270_668983457309 -->|defined in| f6985512_c22e_0ae9_c629_acb233df5408 6560dbd6_8588_9d8f_d467_ab24a097ad9c["get()"] b961d203_0a7e_862d_a270_668983457309 -->|calls| 6560dbd6_8588_9d8f_d467_ab24a097ad9c style b961d203_0a7e_862d_a270_668983457309 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/memory/entity.py lines 502–565
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Load memory variables.
Returns chat history and all generated entities with summaries if available,
and updates or clears the recent entity cache.
New entity name can be found when calling this method, before the entity
summaries are generated, so the entity cache values may be empty if no entity
descriptions are generated yet.
"""
# Create an LLMChain for predicting entity names from the recent chat history:
chain = LLMChain(llm=self.llm, prompt=self.entity_extraction_prompt)
if self.input_key is None:
prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
else:
prompt_input_key = self.input_key
# Extract an arbitrary window of the last message pairs from
# the chat history, where the hyperparameter k is the
# number of message pairs:
buffer_string = get_buffer_string(
self.buffer[-self.k * 2 :],
human_prefix=self.human_prefix,
ai_prefix=self.ai_prefix,
)
# Generates a comma-separated list of named entities,
# e.g. "Jane, White House, UFO"
# or "NONE" if no named entities are extracted:
output = chain.predict(
history=buffer_string,
input=inputs[prompt_input_key],
)
# If no named entities are extracted, assigns an empty list.
if output.strip() == "NONE":
entities = []
else:
# Make a list of the extracted entities:
entities = [w.strip() for w in output.split(",")]
# Make a dictionary of entities with summary if exists:
entity_summaries = {}
for entity in entities:
entity_summaries[entity] = self.entity_store.get(entity, "")
# Replaces the entity name cache with the most recently discussed entities,
# or if no entities were extracted, clears the cache:
self.entity_cache = entities
# Should we return as message objects or as a string?
if self.return_messages:
# Get last `k` pair of chat messages:
buffer: Any = self.buffer[-self.k * 2 :]
else:
# Reuse the string we made earlier:
buffer = buffer_string
return {
self.chat_history_key: buffer,
"entities": entity_summaries,
}
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does load_memory_variables() do?
load_memory_variables() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/memory/entity.py.
Where is load_memory_variables() defined?
load_memory_variables() is defined in libs/langchain/langchain_classic/memory/entity.py at line 502.
What does load_memory_variables() call?
load_memory_variables() calls 1 function(s): get.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free