0
0
LangChainframework~5 mins

Memory-augmented retrieval in LangChain

Choose your learning style9 modes available
Introduction

Memory-augmented retrieval helps your program remember past information to answer questions better. It keeps useful details saved and uses them when you ask something new.

When building a chatbot that needs to remember previous conversations.
When you want to improve search results by using past user queries.
When your app needs to keep track of important facts over time.
When you want to combine stored knowledge with new information for better answers.
Syntax
LangChain
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings

# Create memory to store conversation
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

# Create vector store for retrieval
vector_store = FAISS.load_local("path_to_index", OpenAIEmbeddings())

# Create retrieval chain with memory
retrieval_chain = ConversationalRetrievalChain.from_llm(
    llm=your_llm_instance,
    retriever=vector_store.as_retriever(),
    memory=memory
)

# Use retrieval_chain to ask questions and keep memory

The ConversationBufferMemory stores chat history to help context.

The ConversationalRetrievalChain combines memory and retrieval for better answers.

Examples
This creates an empty memory buffer to start fresh.
LangChain
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Empty memory at start
Adds messages to memory manually to simulate conversation history.
LangChain
memory.chat_memory.add_user_message("Hello!")
memory.chat_memory.add_ai_message("Hi, how can I help?")
Uses the chain to ask a question that uses past memory for context.
LangChain
retrieval_chain = ConversationalRetrievalChain.from_llm(
    llm=your_llm_instance,
    retriever=vector_store.as_retriever(),
    memory=memory
)

response = retrieval_chain.run({"question": "What did I ask before?"})
Sample Program

This program creates a memory-augmented retrieval chain that remembers past questions. It first asks about LangChain, then asks the AI to recall the previous question using stored memory.

LangChain
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.llms import OpenAI

# Initialize memory to store conversation history
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

# Load or create vector store for retrieval (mock path here)
vector_store = FAISS.load_local("./faiss_index", OpenAIEmbeddings())

# Initialize language model
llm = OpenAI(temperature=0)

# Create retrieval chain with memory
retrieval_chain = ConversationalRetrievalChain.from_llm(
    llm=llm,
    retriever=vector_store.as_retriever(),
    memory=memory
)

# Simulate conversation
print("User: What is LangChain?")
response1 = retrieval_chain.run({"question": "What is LangChain?"})
print(f"AI: {response1}")

print("User: Can you remind me what I asked before?")
response2 = retrieval_chain.run({"question": "Can you remind me what I asked before?"})
print(f"AI: {response2}")
OutputSuccess
Important Notes

Memory-augmented retrieval improves answers by using past conversation context.

Time complexity depends on retrieval and memory size; usually efficient for typical use.

Common mistake: forgetting to pass memory to the chain, so context is lost.

Use memory-augmented retrieval when you want your app to remember and use past info, instead of starting fresh each time.

Summary

Memory-augmented retrieval helps keep track of past conversations or data.

It combines stored memory with retrieval to give better, context-aware answers.

Use it to build smarter chatbots or apps that remember user history.