buffer_window.py — langchain Source File
Architecture documentation for buffer_window.py, a python file in the langchain codebase. 5 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR fbc9e121_0c24_f36f_971f_2f0eb9d4238a["buffer_window.py"] feec1ec4_6917_867b_d228_b134d0ff8099["typing"] fbc9e121_0c24_f36f_971f_2f0eb9d4238a --> feec1ec4_6917_867b_d228_b134d0ff8099 2485b66a_3839_d0b6_ad9c_a4ff40457dc6["langchain_core._api"] fbc9e121_0c24_f36f_971f_2f0eb9d4238a --> 2485b66a_3839_d0b6_ad9c_a4ff40457dc6 9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"] fbc9e121_0c24_f36f_971f_2f0eb9d4238a --> 9444498b_8066_55c7_b3a2_1d90c4162a32 f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"] fbc9e121_0c24_f36f_971f_2f0eb9d4238a --> f85fae70_1011_eaec_151c_4083140ae9e5 0631e07f_ced9_09bd_71a1_4630e7d30d26["langchain_classic.memory.chat_memory"] fbc9e121_0c24_f36f_971f_2f0eb9d4238a --> 0631e07f_ced9_09bd_71a1_4630e7d30d26 style fbc9e121_0c24_f36f_971f_2f0eb9d4238a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
from typing import Any
from langchain_core._api import deprecated
from langchain_core.messages import BaseMessage, get_buffer_string
from typing_extensions import override
from langchain_classic.memory.chat_memory import BaseChatMemory
@deprecated(
since="0.3.1",
removal="1.0.0",
message=(
"Please see the migration guide at: "
"https://python.langchain.com/docs/versions/migrating_memory/"
),
)
class ConversationBufferWindowMemory(BaseChatMemory):
"""Use to keep track of the last k turns of a conversation.
If the number of messages in the conversation is more than the maximum number
of messages to keep, the oldest messages are dropped.
"""
human_prefix: str = "Human"
ai_prefix: str = "AI"
memory_key: str = "history"
k: int = 5
"""Number of messages to store in buffer."""
@property
def buffer(self) -> str | list[BaseMessage]:
"""String buffer of memory."""
return self.buffer_as_messages if self.return_messages else self.buffer_as_str
@property
def buffer_as_str(self) -> str:
"""Exposes the buffer as a string in case return_messages is False."""
messages = self.chat_memory.messages[-self.k * 2 :] if self.k > 0 else []
return get_buffer_string(
messages,
human_prefix=self.human_prefix,
ai_prefix=self.ai_prefix,
)
@property
def buffer_as_messages(self) -> list[BaseMessage]:
"""Exposes the buffer as a list of messages in case return_messages is True."""
return self.chat_memory.messages[-self.k * 2 :] if self.k > 0 else []
@property
def memory_variables(self) -> list[str]:
"""Will always return list of memory variables."""
return [self.memory_key]
@override
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Return history buffer."""
return {self.memory_key: self.buffer}
Domain
Subdomains
Classes
Dependencies
- langchain_classic.memory.chat_memory
- langchain_core._api
- langchain_core.messages
- typing
- typing_extensions
Source
Frequently Asked Questions
What does buffer_window.py do?
buffer_window.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, MessageInterface subdomain.
What does buffer_window.py depend on?
buffer_window.py imports 5 module(s): langchain_classic.memory.chat_memory, langchain_core._api, langchain_core.messages, typing, typing_extensions.
Where is buffer_window.py in the architecture?
buffer_window.py is located at libs/langchain/langchain_classic/memory/buffer_window.py (domain: LangChainCore, subdomain: MessageInterface, directory: libs/langchain/langchain_classic/memory).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free