From e081c62aac8561f2066ef2f2895871087cebd2f1 Mon Sep 17 00:00:00 2001 From: Tim Connors Date: Wed, 12 Apr 2023 17:05:54 -0700 Subject: [PATCH] Fixed k=0 bug on ConversationBufferWindowMemory (#2796) Updated the "load_memory_variables" function of the ConversationBufferWindowMemory to support a window size of 0 (k=0). Previous behavior would return the full memory instead of an empty array. --- langchain/memory/buffer_window.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/langchain/memory/buffer_window.py b/langchain/memory/buffer_window.py index aaa69b0967..c9c0178736 100644 --- a/langchain/memory/buffer_window.py +++ b/langchain/memory/buffer_window.py @@ -28,11 +28,10 @@ class ConversationBufferWindowMemory(BaseChatMemory): def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]: """Return history buffer.""" - if self.return_messages: - buffer: Any = self.buffer[-self.k * 2 :] - else: + buffer: Any = self.buffer[-self.k * 2 :] if self.k > 0 else [] + if not self.return_messages: buffer = get_buffer_string( - self.buffer[-self.k * 2 :], + buffer, human_prefix=self.human_prefix, ai_prefix=self.ai_prefix, )