Let's think this through for a moment
The RetrievalQA chain we built in Part 1 handles each question in isolation, so if you ask a follow-up like "can you explain that further?", there's no context to work with. In Part 2, we'll use the Memory concept from the Intermediate chapter to upgrade the chatbot into a conversational RAG chain. By using ConversationBufferMemory as chat_history and building a ConversationalRetrievalChain, we can keep the user's previous questions and answers stored in memory. The result is a chatbot the user can hold a back-and-forth conversation with about the document's content. Once this stage is done, we'll add agent tools in Part 3.
Let's build it
Reuse the vectorstore/retriever from Part 1. Create a ConversationBufferMemory(memory_key="chat_history", return_messages=True). Instead of RetrievalQA, build a new chain with ConversationalRetrievalChain.from_llm(llm, retriever, memory=memory). Ask "What is LangChain?" first, then follow up with "Can you give me an example of that?" and test whether it answers correctly by referencing memory. Print the result of chain.invoke() and observe how chat_history gets updated.
Code Example
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
# Part 1 ကနေ vectorstore/retriever ကို ပြန်အသုံးပြု
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
# Conversation memory တစ်ခု ဖန်တီး
memory = ConversationBufferMemory(
memory_key="chat_history", return_messages=True
)
# Memory ပါတဲ့ conversational RAG chain ဆောက်
conv_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=memory,
)
# ပထမ မေးခွန်း
r1 = conv_chain.invoke({"question": "LangChain ဆိုတာဘာလဲ?"})
print("Answer 1:", r1["answer"])
# Follow-up မေးခွန်း (context ကို memory ကနေ ယူသုံးမည်)
r2 = conv_chain.invoke({"question": "အဲဒါနဲ့ ပတ်သက်ပြီး ဥပမာတစ်ခု ပေးပါ"})
print("Answer 2:", r2["answer"])
On the second question, the chatbot will correctly understand what 'that' refers to by drawing on the first question and answer stored in memory.5-Minute Try-It
In 5 minutes, ask two follow-up questions in a row about a topic from Part 1's document and confirm that memory is working as expected.
A Quick Warning
ConversationBufferMemory keeps the entire history, so token cost climbs as the conversation gets longer — for production, consider alternatives like ConversationSummaryMemory.