[docs]@deprecated(since="0.3.1",removal="1.0.0",message=("Please see the migration guide at: ""https://python.langchain.com/docs/versions/migrating_memory/"),)classConversationBufferWindowMemory(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"#: :meta private:k:int=5"""Number of messages to store in buffer."""@propertydefbuffer(self)->Union[str,List[BaseMessage]]:"""String buffer of memory."""returnself.buffer_as_messagesifself.return_messageselseself.buffer_as_str@propertydefbuffer_as_str(self)->str:"""Exposes the buffer as a string in case return_messages is False."""messages=self.chat_memory.messages[-self.k*2:]ifself.k>0else[]returnget_buffer_string(messages,human_prefix=self.human_prefix,ai_prefix=self.ai_prefix,)@propertydefbuffer_as_messages(self)->List[BaseMessage]:"""Exposes the buffer as a list of messages in case return_messages is True."""returnself.chat_memory.messages[-self.k*2:]ifself.k>0else[]@propertydefmemory_variables(self)->List[str]:"""Will always return list of memory variables. :meta private: """return[self.memory_key]
[docs]defload_memory_variables(self,inputs:Dict[str,Any])->Dict[str,Any]:"""Return history buffer."""return{self.memory_key:self.buffer}