LangChain - Memory Module
Bharani Srinivas
Senior Manager - AIML Delivery Lead | Machine Learning | R&D | Mentor at Great Lakes
What is a Memory?
Chains and Agents operate as stateless, treating each query independently. However, in applications like chatbots, its crucial to remember past interactions. The concept of "Memory" serves that purpose.
Different Types of Memories
Let's delve into a few of these in more detail.
ConversationBufferMemory
Imagine you're having a conversation with someone, and you want to remember what you've discussed so far. The ConversationBufferMemory does exactly that in a chatbot or similar system. It keeps a record, or "buffer," of the past parts of the conversation.
from langchain import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)
The buffer plays a crucial role in the context, aiding the chatbot in generating improved responses.
What sets this memory apart is its ability to store previous conversations exactly as they occurred, without any alterations.
It preserves the raw form of the conversation, allowing the chatbot to refer back to specific parts accurately. In summary, the ConversationBufferMemory helps the chatbot remember the conversation history, enhancing the overall conversational experience.
Pros of ConversationBufferMemory:
Cons of ConversationBufferMemory:
ConversationBufferWindowMemory
Imagine you have a limited space in your memory to remember recent conversations.
The ConversationBufferWindowMemory is like having a short-term memory that only keeps track of the most recent interactions. It intentionally drops the oldest ones to make room for new ones.
This helps manage the memory load and reduces the number of tokens used. The important thing is that it still keeps the latest parts of the conversation in their original form, without any modifications. So, it retains the most recent information for the chatbot to refer to, ensuring a more efficient and up-to-date conversation experience.
from langchain import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferWindowMemory(k=3)
)
Pros of ConversationBufferWindowMemory:
领英推荐
Cons of ConversationBufferWindowMemory:
ConversationSummaryMemory
With the ConversationBufferMemory, the length of the conversation keeps increasing, which can become a problem if it becomes too large for our LLM to handle.
To overcome this, we introduce ConversationSummaryMemory. It keeps a summary of our past conversation snippets as our history. But how does it summarize? Here comes the LLM to the rescue! The LLM (Language Model) helps in condensing or summarizing the conversation, capturing the key information.
So, instead of storing the entire conversation, we store a summarized version. This helps manage the token count and allows the LLM to process the conversation effectively. In summary, ConversationSummaryMemory keeps a condensed version of previous conversations using the power of LLM summarization.
from langchain import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationSummaryMemory(llm=llm)
)
Pros of ConversationSummaryMemory:
Cons of ConversationSummaryMemory:
ConversationTokenBufferMemory
ConversationTokenBufferMemory is a memory mechanism that stores recent interactions in a buffer within the system's memory.
Unlike other methods that rely on the number of interactions, this memory system determines when to clear or flush interactions based on the length of tokens used. Tokens are units of text, like words or characters, and the buffer is cleared when the token count exceeds a certain threshold. By using token length as a criterion, the memory system ensures that the buffer remains manageable in terms of memory usage.
This approach helps maintain efficient memory management and enables the system to handle conversations of varying lengths effectively.
from langchain import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationTokenBufferMemory(llm=llm, max_token_limit=60),
)
Pros of ConversationTokenBufferMemory:
Cons of ConversationTokenBufferMemory:
There are few other memory options available for which we can refer the official documentation Memory types | ????? Langchain
Next article I will try to bring other components of Langchain
#llm #langchain-memory
ML Engineer | MLOps | Data Scientist
4 个月?How is memory managed in production when a new user enters the system? For example, if two users log in simultaneously, will the first user's chat history be considered the second user's chat history?
Software Engineering Specialist at Amdocs
1 年Discovering the power of Langchain and Chainlit for a seamless chatbot experience! Check out my latest project at PrivateDocBot. https://github.com/Abhi5h3k/PrivateDocBot Great, Will incorporate memory soon. ??#langchain #chatbot
Data Engineer
1 年your post is good! however, question one thing,, Where is LangChain memory class stored? Is it DB? Or is it a class called memory inside?