📖 Scenario: You are building a chatbot that can understand follow-up questions by remembering the previous conversation. This helps the bot give better answers by reformulating the current question with the chat history.
🎯 Goal: Create a LangChain setup that reformulates a follow-up question using the chat history. You will build the data, configure the chain, apply the reformulation logic, and complete the chain for use.
📋 What You'll Learn
Create a list called chat_history with two example chat turns
Create a variable called question with the follow-up question string
Use LangChain's load_qa_with_sources_chain to create a chain with combine_docs_chain_kwargs containing question_generator
Add the final call to chain.run passing question and chat_history
💡 Why This Matters
🌍 Real World
Chatbots often need to understand follow-up questions by remembering previous conversation. This project shows how to use LangChain to reformulate questions with chat history for better answers.
💼 Career
Knowing how to use LangChain for question reformulation is useful for building advanced conversational AI, customer support bots, and virtual assistants.
Progress0 / 4 steps
1
Set up chat history and question
Create a list called chat_history with these exact two tuples: ("User: What is LangChain?", "Assistant: LangChain is a framework for building language model apps.") and ("User: How does it help?", "Assistant: It helps by chaining LLM calls together."). Then create a variable called question with the string "Can you explain question reformulation?".
LangChain
Hint
Remember to create a list named chat_history with two tuples exactly as shown, and a string variable question.
2
Configure the question reformulation chain
Import load_qa_with_sources_chain from langchain.chains. Then create a variable called question_generator by calling load_qa_with_sources_chain with chain_type="stuff". Next, create a variable called chain by calling load_qa_with_sources_chain with chain_type="map_reduce" and passing combine_docs_chain_kwargs={'question_generator': question_generator}.
LangChain
Hint
Import load_qa_with_sources_chain and create question_generator and chain exactly as described.
3
Apply question reformulation logic
Use the chain variable to call its run method. Pass the arguments question=question and chat_history=chat_history. Assign the result to a variable called reformulated_question.
LangChain
Hint
Call chain.run with question=question and chat_history=chat_history, assign to reformulated_question.
4
Complete the LangChain question reformulation setup
Add a final line that prints the reformulated_question variable to see the reformulated question output.
LangChain
Hint
Use print(reformulated_question) to display the reformulated question.
Practice
(1/5)
1. What is the main purpose of question reformulation with history in LangChain?
easy
A. To store user personal data securely
B. To speed up the chatbot response time
C. To translate questions into multiple languages
D. To help chatbots understand follow-up questions better
Solution
Step 1: Understand the role of question reformulation
It helps the chatbot by using previous conversation history to clarify follow-up questions.
Step 2: Identify the main benefit
This makes the chatbot understand user intent better and respond more naturally.
Final Answer:
To help chatbots understand follow-up questions better -> Option D
Hint: Focus on improving chatbot understanding of follow-ups [OK]
Common Mistakes:
Confusing reformulation with speed optimization
Thinking it stores personal data
Assuming it translates languages
2. Which of the following is the correct way to create a ConversationChain in LangChain that uses question reformulation with history?
easy
A. chain = ConversationChain(llm=llm, memory=ConversationBufferMemory())
B. chain = ConversationChain(memory=llm, llm=ConversationBufferMemory())
C. chain = ConversationChain(llm=llm, history=ConversationBufferMemory())
D. chain = ConversationChain(buffer=llm, memory=ConversationBufferMemory())
Solution
Step 1: Recall the correct parameter names
ConversationChain expects an LLM instance as 'llm' and a memory object as 'memory'.
Step 2: Match the correct syntax
chain = ConversationChain(llm=llm, memory=ConversationBufferMemory()) correctly assigns 'llm=llm' and 'memory=ConversationBufferMemory()'. Others mix parameter names or use incorrect ones.
Final Answer:
chain = ConversationChain(llm=llm, memory=ConversationBufferMemory()) -> Option A
Quick Check:
Correct parameters = llm and memory [OK]
Hint: Remember 'llm' and 'memory' are required parameters [OK]
Common Mistakes:
Swapping 'llm' and 'memory' parameters
Using 'history' instead of 'memory'
Passing wrong argument names
3. Given the following code snippet, what will be the output after the second question?
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
llm = SomeLLM()
memory = ConversationBufferMemory()
chain = ConversationChain(llm=llm, memory=memory)
print(chain.run("What is AI?"))
print(chain.run("And how does it learn?"))
medium
A. The memory will reset after each question
B. The second answer will ignore the first question
C. The second answer will consider the first question's context
B. ConversationBufferMemory is not instantiated with parentheses
C. The run method does not exist on ConversationChain
D. The print statement is missing parentheses
Solution
Step 1: Check how ConversationBufferMemory is used
It must be instantiated with parentheses to create an object, but here it's passed as a class.
Step 2: Verify other parts
llm is defined, run method exists, and print syntax is correct in Python 3.
Final Answer:
ConversationBufferMemory is not instantiated with parentheses -> Option B
Quick Check:
Instantiate classes with () [OK]
Hint: Always add () when creating memory instance [OK]
Common Mistakes:
Forgetting parentheses when instantiating classes
Assuming run method is missing
Confusing print syntax in Python 3
5. You want to improve a chatbot to handle multi-turn conversations with question reformulation using LangChain. Which approach best achieves this?
hard
A. Use ConversationChain with ConversationBufferMemory to keep history and reformulate questions
B. Use a simple LLM call without memory for faster responses
C. Store conversation history manually in a list and pass only the last question to the LLM
D. Use ConversationChain but reset memory after each question to avoid confusion
Solution
Step 1: Understand multi-turn conversation needs
Keeping conversation history helps the chatbot understand context and reformulate questions properly.
Step 2: Evaluate options
Use ConversationChain with ConversationBufferMemory to keep history and reformulate questions uses ConversationChain with memory, which automates history management and reformulation. Others either ignore history or reset it, losing context.
Final Answer:
Use ConversationChain with ConversationBufferMemory to keep history and reformulate questions -> Option A
Quick Check:
Memory + ConversationChain = best multi-turn handling [OK]
Hint: Use ConversationChain with memory for natural multi-turn chats [OK]
Common Mistakes:
Ignoring conversation history
Resetting memory too often
Manually managing history inefficiently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.