ConversationBufferWindowMemory
Author: Kenny Jung
Design: Kenny Jung
Peer Review : Wooseok Jeong, JeongGi Park
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
This tutorial introduces ConversationBufferWindowMemory
, which maintains a sliding window of recent conversation interactions over time.
Instead of utilizing all conversation content, ConversationBufferWindowMemory
retains only the most recent K interactions.
This prevents the memory buffer from growing too large.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorial
is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorial
for more details.
%%capture --no-stderr
%pip install langchain-opentutorial
# Install required packages
from langchain_opentutorial import package
package.install(
[
"langsmith",
"langchain",
"langchain_core",
"langchain-anthropic",
"langchain_community",
"langchain_text_splitters",
"langchain_openai",
],
verbose=False,
upgrade=False,
)
# Set environment variables
from langchain_opentutorial import set_env
set_env(
{
"OPENAI_API_KEY": "",
"LANGCHAIN_API_KEY": "",
"LANGCHAIN_TRACING_V2": "true",
"LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
"LANGCHAIN_PROJECT": "ConversationBufferWindowMemory",
}
)
Environment variables have been set successfully.
You can alternatively set OPENAI_API_KEY
in .env
file and load it.
[Note] This is not necessary if you've already set OPENAI_API_KEY
in previous steps.
from dotenv import load_dotenv
load_dotenv(override=True)
True
Online Bank Account Opening Conversation Example
This example demonstrates using ConversationBufferWindowMemory
to simulate a virtual banking assistant conversation. The flow covers a typical online bank account opening process, from initial greeting to account creation confirmation, retaining only the most recent interactions in memory.
from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=2, return_messages=True)
memory.save_context(
inputs={
"human": "Hello, I'd like to open a bank account online. How do I get started?"
},
outputs={
"ai": "Hello! I'm glad you want to open an account. First, could you please prepare your ID for verification?"
},
)
memory.save_context(
inputs={"human": "Yes, I have my ID ready. What should I do next?"},
outputs={
"ai": "Thank you. Please take clear photos of both sides of your ID and upload them. Then we'll proceed with the identity verification process."
},
)
memory.save_context(
inputs={
"human": "I've uploaded the photos. How does the identity verification work?"
},
outputs={
"ai": "I've confirmed your uploaded photos. Now, please proceed with phone verification. You'll receive a verification code via text message - please enter that code."
},
)
memory.save_context(
inputs={
"human": "I've entered the verification code. How do I proceed with opening the account?"
},
outputs={
"ai": "Your identity has been verified. Now, please select the type of account you want and enter the required information. You can choose the type of deposit, currency, and other options."
},
)
memory.save_context(
inputs={"human": "I've entered all the information. What's the next step?"},
outputs={
"ai": "I've confirmed your information. The account opening process is almost complete. Finally, please agree to the terms of service and confirm the account opening."
},
)
memory.save_context(
inputs={"human": "I've completed all the steps. Has the account been opened?"},
outputs={
"ai": "Yes, your account has been successfully opened. Your account number and related information have been sent to your registered email. Please let us know if you need any further assistance. Thank you!"
},
)
Retrieving Conversation History
Let's examine the stored conversation history using the load_memory_variables()
method to verify our window-based memory retention.
# Check the conversation history
memory.load_memory_variables({})["history"]
[HumanMessage(content="I've entered all the information. What's the next step?", additional_kwargs={}, response_metadata={}),
AIMessage(content="I've confirmed your information. The account opening process is almost complete. Finally, please agree to the terms of service and confirm the account opening.", additional_kwargs={}, response_metadata={}),
HumanMessage(content="I've completed all the steps. Has the account been opened?", additional_kwargs={}, response_metadata={}),
AIMessage(content='Yes, your account has been successfully opened. Your account number and related information have been sent to your registered email. Please let us know if you need any further assistance. Thank you!', additional_kwargs={}, response_metadata={})]
Last updated