0
0
LangchainHow-ToBeginner ยท 3 min read

How to Add Memory to Chain in Langchain: Simple Guide

To add memory to a chain in langchain, create a memory instance such as ConversationBufferMemory and pass it to the chain's memory parameter. This allows the chain to remember past inputs and outputs, maintaining context across calls.
๐Ÿ“

Syntax

To add memory to a Langchain chain, you typically:

  • Create a memory object like ConversationBufferMemory.
  • Pass this memory object to the chain constructor using the memory argument.
  • Use the chain as usual; it will now keep track of conversation history.
python
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI

memory = ConversationBufferMemory()
llm = OpenAI(temperature=0)

chain = ConversationChain(llm=llm, memory=memory)
๐Ÿ’ป

Example

This example shows how to create a conversation chain with memory that remembers previous user inputs and responses.

python
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI

memory = ConversationBufferMemory()
llm = OpenAI(temperature=0)
chain = ConversationChain(llm=llm, memory=memory)

print(chain.run("Hello!"))
print(chain.run("How are you?"))
print(chain.run("What did I say first?"))
Output
Hi! How can I assist you today? I'm doing well, thank you! How can I help you? You said: Hello!
โš ๏ธ

Common Pitfalls

Common mistakes when adding memory to a Langchain chain include:

  • Not passing the memory object to the chain, so no context is saved.
  • Using incompatible memory types with the chain.
  • Forgetting to install or configure the LLM properly, causing errors unrelated to memory.

Always ensure the memory instance matches the chain's expected memory interface.

python
from langchain.chains import ConversationChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)

# Wrong: No memory passed, so no context is kept
chain = ConversationChain(llm=llm)

# Right: Memory passed to keep context
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
chain = ConversationChain(llm=llm, memory=memory)
๐Ÿ“Š

Quick Reference

Summary tips for adding memory to Langchain chains:

  • Use ConversationBufferMemory for simple chat history memory.
  • Pass the memory instance to the chain via the memory parameter.
  • Memory enables the chain to remember past inputs and outputs.
  • Check Langchain docs for other memory types like ConversationSummaryMemory or CombinedMemory.
โœ…

Key Takeaways

Create a memory instance like ConversationBufferMemory to store conversation history.
Pass the memory instance to the chain using the memory parameter to enable context retention.
Without memory, chains do not remember past interactions and act statelessly.
Choose the memory type that fits your use case for better conversation management.
Always test your chain to ensure memory is working as expected.