ConversationTokenBufferMemory Keep the buffer in memory for the history of recent conversations, not the number of conversations Token length Use to determine when to flush the conversation.
# API KEY A configuration file for managing environment variables
from dotenv import load_dotenv
# API KEY Load information
load_dotenv()
True
max_token_limit : Set the length of the maximum token to store the conversation.
from langchain.memory import ConversationTokenBufferMemory
from langchain_openai import ChatOpenAI
# LLM Create a model
llm = ChatOpenAI()
# memory settings
memory = ConversationTokenBufferMemory(
llm=llm, max_token_limit=150, return_messages=True # 최대 토큰 길이를 50개로 제한
)
Add random conversations.
Length of maximum token 150 Let's set it up and see how it works when you save the conversation.
memory.save_context(
inputs={
"human": "Hi, I recently purchased a machine tool from your company. Can you tell me how to install it?"
},
outputs={
"ai": "Hello! Thank you for your purchase. Could you please tell me the model number of the machine?"
},
)
memory.save_context(
inputs={"human": "Yes, the model number is XG-200."},
outputs={
"ai": "먼Thank you. I will help you with the installation guide for the XG-200 model. First, please check the power supply status of the installation location. The machine requires 220V power."
},
)
memory.save_context(
inputs={"human": "Power is checked. What's the next step?"},
outputs={
"ai": "Okay. Next, place the machine on a flat and stable floor. After that, proceed with the cable connection according to the provided user manual."
},
)
memory.save_context(
inputs={"human": "How do I connect?"},
outputs={
"ai": "Please refer to page 5 of the manual. There are detailed instructions on connecting the cables. If you have any difficulties with this process, we will be happy to assist you further."
},
)
memory.save_context(
inputs={"human": "What should I do once the installation is complete?"},
outputs={
"ai": "Once the installation is complete, please power on and perform an initial operation test. The test procedure is described on page 10 of the manual. If you have any problems with the machine or need additional support, please contact us at any time."
},
)
memory.save_context(
inputs={"human": "Thank you, it helped a lot!"},
outputs={
"ai": "We are always ready to help. If you have any additional questions or need assistance, please contact us at any time. Have a nice day!"
},
)
# check the conversation.
memory.load_memory_variables({})["history"]
[HumanMessage (content='Thank you, it helped me a lot!'), AIMessage (content=' Ready to help anytime. Please feel free to contact us for further questions or assistance. Have a nice day!')]