Exploring Conversational Memory Options with LangChain: Enhancing Chatbots with Stateful Interactions

Shikhar aryan
4 min readJun 21, 2024

--

Conversational memory is the backbone of coherent interactions in chatbots, allowing them to respond to queries as part of an ongoing conversation rather than treating each query as an isolated input. Without conversational memory, each interaction is processed independently, making it impossible for chatbots to refer to previous exchanges. LangChain offers various memory options that enhance the conversational abilities of Large Language Models (LLMs). Let’s dive into the different types of conversational memory available in LangChain and their practical applications.

Understanding Conversational Memory

Conversational memory enables LLMs to remember previous interactions with users, creating a stateful environment. This is essential for applications like chatbots, where continuity and context are crucial for meaningful interactions. LangChain provides several ways to implement conversational memory, all built on top of the ConversationChain.

ConversationChain

To begin, let’s initialize the ConversationChain using OpenAI’s text-davinci-003 as the LLM, though other models like gpt-3.5-turbo can also be used:

from langchain import OpenAI
from langchain.chains import ConversationChain

# Initialize the large language model
llm = OpenAI(
temperature=0,
openai_api_key="OPENAI_API_KEY",
model_name="gpt-3.5-turbo-instruct"
)

# Initialize the conversation chain
conversation = ConversationChain(llm=llm)

The prompt template used by ConversationChain is designed to create a friendly, informative conversation:

print(conversation.prompt.template)

Output:

The following is a friendly conversation between a human and an AI.
The AI is talkative and provides lots of specific details from its context.
If the AI does not know the answer to a question, it truthfully says it does
not know.

Current conversation:
{history}
Human: {input}
AI:

Forms of Conversational Memory

LangChain offers several types of conversational memory that modify the text passed to the {history} parameter. Let's explore these options.

1: ConversationBufferMemory

The ConversationBufferMemory stores the raw input of the past conversation, passing it directly to the {history} parameter.

from langchain.chains.conversation.memory import ConversationBufferMemory

conversation_buf = ConversationChain(
llm=llm,
memory=ConversationBufferMemory()
)

Here’s how it works in action:

conversation_buf("Good morning AI!")

Output:

{
'input': 'Good morning AI!',
'history': '',
'response': "Good morning! It's a beautiful day today, isn't it? How can I help you?"
}

2: ConversationSummaryMemory

ConversationSummaryMemory reduces token usage by summarizing the conversation history before it is passed to the {history} parameter.

from langchain.chains.conversation.memory import ConversationSummaryMemory

conversation_sum = ConversationChain(
llm=llm,
memory=ConversationSummaryMemory(llm=llm)
)

The prompt used for summarization looks like this:

print(conversation_sum.memory.prompt.template)

Output:

Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary.

EXAMPLE
Current summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good.

New lines of conversation:
Human: Why do you think artificial intelligence is a force for good?
AI: Because artificial intelligence will help humans reach their full potential.

New summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good because it will help humans reach their full potential.
END OF EXAMPLE

3: ConversationBufferWindowMemory

The ConversationBufferWindowMemory only keeps a specified number of past interactions, defined by k.

from langchain.chains.conversation.memory import ConversationBufferWindowMemory

conversation_bufw = ConversationChain(
llm=llm,
memory=ConversationBufferWindowMemory(k=1)
)

This approach limits memory to the most recent interactions, balancing between token usage and context retention.

4: ConversationSummaryBufferMemory

Combining summarization and a buffer window, ConversationSummaryBufferMemory retains the most recent interactions in raw form while summarizing older exchanges.

from langchain.chains.conversation.memory import ConversationSummaryBufferMemory

conversation_sum_bufw = ConversationChain(
llm=llm,
memory=ConversationSummaryBufferMemory(
llm=llm,
max_token_limit=650
)
)

Despite needing more fine-tuning to decide what to summarize and what to keep within the buffer window, the ConversationSummaryBuffer- Memory offers significant flexibility. It is currently the only memory type that lets us retain distant interactions while also storing the most recent interactions in their raw, most information-rich form.

Here’s a Visual Representation of Tokens vs Number of Interactions with the model (conversation)

Image from pinecone

Choosing the Right Memory Type

Each memory type has its pros and cons, making them suitable for different use cases:

  • ConversationBufferMemory: Simple and intuitive, but can quickly reach token limits.
  • ConversationSummaryMemory: Efficient for long conversations, but relies heavily on summarization quality.
  • ConversationBufferWindowMemory: Good for recent context, but forgets older interactions.
  • ConversationSummaryBufferMemory: Balances between recent and distant context but requires careful tuning.

Conclusion

LangChain’s conversational memory options provide powerful tools for creating chatbots capable of stateful interactions. By choosing the right memory type, developers can enhance the user experience, making interactions more coherent and contextually aware. As we continue to explore and combine different memory modules, the potential for sophisticated, interactive AI systems grows, opening up new possibilities for integrating LLMs with external knowledge and applications.

Stay tuned for future chapters where we will delve into more advanced memory types and their applications, pushing the boundaries of what conversational AI can achieve.

Happy Coding!

Keep Learning

source — LINK

Let’s Connect on LinkedIn — profile

--

--

Shikhar aryan

I'm an AI Engineer, Learning and Growing with the Trend.