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_dotenvload_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 ConversationBufferWindowMemorymemory =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 historymemory.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={})]