Thuta Learning
ProjectsAIadvanced

Mini Project: Document Chatbot – Part 2 (Adding Memory)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Mini Project: Document Chatbot – Part 2 (Adding Memory) in a hands-on project
  • Write and run the code yourself
  • Build out an entire project step by step

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

python
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"])
You should see
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.

Easy traps

  • The memory_key name (chat_history) doesn't match the variable name used in the chain's prompt template, causing a KeyError
  • Reusing the same memory object instead of resetting it for each new session, mixing one user's context into another user's

Now Try It Yourself

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.

You'll know it worked when: On the second question, the chatbot will correctly understand what 'that' refers to by drawing on the first question and answer stored in memory.