📖 Scenario: You are building a simple chatbot using Langchain that can remember the context of previous questions and handle follow-up questions smoothly.
🎯 Goal: Create a Langchain chatbot that stores conversation history and uses it to answer follow-up questions correctly.
📋 What You'll Learn
Create a list to store conversation history
Set a maximum number of previous messages to keep
Use Langchain's ConversationalRetrievalChain to process questions with context
Add the final call to run the chatbot with a sample question
💡 Why This Matters
🌍 Real World
Chatbots and virtual assistants need to remember previous messages to answer follow-up questions naturally.
💼 Career
Understanding how to manage conversation history is key for building intelligent chatbots in customer support, education, and personal assistant applications.
Progress0 / 4 steps
1
Create conversation history list
Create a list called chat_history and initialize it as an empty list to store previous messages.
LangChain
Hint
Think of chat_history as a notebook where you write down all previous messages.
2
Set maximum history length
Create a variable called max_history and set it to 5 to limit how many previous messages to keep.
LangChain
Hint
This helps the chatbot remember only the last few messages, like a short-term memory.
3
Create ConversationalRetrievalChain with history
Create a variable called qa_chain and assign it to ConversationalRetrievalChain.from_llm(llm, retriever). Use chat_history as the conversation history input when calling qa_chain.run() later.
LangChain
Hint
This chain helps the chatbot answer questions using previous conversation context.
4
Run the chatbot with follow-up question handling
Call qa_chain.run() with the question "What is Langchain?" and pass chat_history as the chat_history argument. Append the question and answer to chat_history. Keep only the last max_history messages in chat_history.
LangChain
Hint
Think of chat_history as a queue that keeps only the last few messages.
Practice
(1/5)
1. What is the main purpose of handling follow-up questions in LangChain conversations?
easy
A. To speed up the response time by skipping context
B. To keep the conversation natural and connected by remembering context
C. To reset the conversation after every question
D. To avoid using any previous conversation history
Solution
Step 1: Understand conversation flow
Handling follow-up questions means the system remembers previous messages to keep the chat natural.
Step 2: Identify the purpose of context
Context helps the system answer related questions without losing track of the topic.
Final Answer:
To keep the conversation natural and connected by remembering context -> Option B
Quick Check:
Follow-up questions need context = A [OK]
Hint: Follow-up means remembering past chat context [OK]
Common Mistakes:
Thinking follow-ups reset conversation
Ignoring conversation history
Assuming follow-ups speed up responses
Believing context is not needed
2. Which LangChain class is commonly used to manage conversation history for follow-up questions?
easy
A. ConversationChain
B. TextSplitter
C. DocumentLoader
D. VectorStoreRetriever
Solution
Step 1: Recall LangChain components
ConversationChain is designed to handle conversations and keep track of history.
Step 2: Match class to purpose
TextSplitter and DocumentLoader are for documents, VectorStoreRetriever for searching, not conversation management.
Final Answer:
ConversationChain -> Option A
Quick Check:
ConversationChain manages chat history = B [OK]
Hint: ConversationChain handles chat history in LangChain [OK]
3. Given this code snippet using LangChain's ConversationChain:
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
conv = ConversationChain(llm=llm)
response1 = conv.predict(input="What is AI?")
response2 = conv.predict(input="Can you explain more?")
print(response2)
What does response2 represent?
medium
A. An error because ConversationChain does not support multiple predicts
B. An answer to 'Can you explain more?' ignoring previous questions
C. An answer to 'Can you explain more?' using the context of 'What is AI?'
D. The same answer as response1 repeated
Solution
Step 1: Understand ConversationChain behavior
ConversationChain keeps track of previous inputs and outputs to provide context-aware answers.
Step 2: Analyze the code flow
response2 uses the previous question 'What is AI?' to answer 'Can you explain more?' with context.
Final Answer:
An answer to 'Can you explain more?' using the context of 'What is AI?' -> Option C
Quick Check:
ConversationChain uses history for follow-ups = C [OK]
Hint: ConversationChain.predict uses past chat context automatically [OK]
Common Mistakes:
Assuming each predict is independent
Expecting an error on multiple calls
Thinking response2 repeats response1
Ignoring conversation history
4. Identify the error in this LangChain code snippet for handling follow-up questions:
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
conv = ConversationChain(llm=llm)
response1 = conv.predict(input="Hello, who won the world cup?")
response2 = conv.predict("Who scored the most goals?")
print(response2)
medium
A. The print statement is invalid syntax
B. ConversationChain cannot be called twice
C. OpenAI LLM requires temperature > 0
D. Missing 'input=' keyword in second predict call causes error
Solution
Step 1: Check method call syntax
ConversationChain.predict expects a named argument 'input=...'. The second call misses this keyword.
Step 2: Understand consequences
Without 'input=', Python raises a TypeError because the argument is positional but not expected.
Final Answer:
Missing 'input=' keyword in second predict call causes error -> Option D
Quick Check:
predict requires input= keyword = D [OK]
Hint: Always use input= keyword in predict calls [OK]
5. You want to build a LangChain app that answers follow-up questions about a user's previous queries but resets context after 3 questions. Which approach best achieves this?
hard
A. Use ConversationChain and clear its memory after every 3 predictions
B. Create a new ConversationChain instance for each question
C. Use ConversationChain without clearing memory to keep all history
D. Use OpenAI directly without ConversationChain
Solution
Step 1: Understand requirement to reset context
The app needs to keep context but reset after 3 questions to avoid long history.