Bird
Raised Fist0
LangChainframework~10 mins

Handling follow-up questions in LangChain - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a LangChain ConversationChain that can handle follow-up questions.

LangChain
from langchain.chains import ConversationChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=[1])

response = conversation.predict(input="Hello, who won the world series in 2020?")
print(response)
Drag options to blanks, or click blank then click option'
AOpenAI(temperature=0)
Btemperature=0.7
COpenAI()
Dllm
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class OpenAI instead of an instance.
Forgetting to set temperature=0 for deterministic output.
2fill in blank
medium

Complete the code to add memory to the ConversationChain for follow-up question handling.

LangChain
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory(memory_key="chat_history")
conversation = ConversationChain(llm=llm, memory=[1])

response = conversation.predict(input="What about the MVP?")
print(response)
Drag options to blanks, or click blank then click option'
AConversationBufferMemory()
BConversationBufferMemory(memory_key="chat_history")
Cmemory
DConversationBufferMemory(memory_key="history")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class instead of the instance.
Using a wrong memory_key that does not match the chain's expectations.
3fill in blank
hard

Fix the error in the code to correctly handle follow-up questions with memory.

LangChain
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory

llm = OpenAI(temperature=0)
memory = ConversationBufferMemory(memory_key="chat_history")
conversation = ConversationChain(llm=llm, memory=[1])

response1 = conversation.predict(input="Who won the world series in 2020?")
response2 = conversation.predict(input="Who was the MVP?")
print(response2)
Drag options to blanks, or click blank then click option'
AConversationBufferMemory()
Bmemory
CConversationBufferMemory(memory_key="history")
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None or a new memory instance without the correct key.
Forgetting to import ConversationBufferMemory.
4fill in blank
hard

Fill both blanks to create a ConversationChain that uses memory and a custom prompt template for follow-up questions.

LangChain
from langchain.prompts import PromptTemplate

prompt = PromptTemplate(template=[1], input_variables=["input", "chat_history"])
conversation = ConversationChain(llm=llm, memory=memory, prompt=[2])

response = conversation.predict(input="Tell me about the 2020 World Series.")
print(response)
Drag options to blanks, or click blank then click option'
A"{chat_history}\nHuman: {input}\nAI:"
B"Human: {input}\nAI:"
Cprompt
DPromptTemplate()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a prompt string instead of a PromptTemplate instance.
Omitting chat_history in the template string.
5fill in blank
hard

Fill all three blanks to build a LangChain ConversationChain that handles follow-up questions with memory and a custom prompt.

LangChain
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationChain

llm = OpenAI(temperature=0)
memory = ConversationBufferMemory(memory_key=[1])
prompt = PromptTemplate(template=[2], input_variables=["input", "chat_history"])
conversation = ConversationChain(llm=llm, memory=memory, prompt=prompt)

response = conversation.predict(input=[3])
print(response)
Drag options to blanks, or click blank then click option'
A"chat_history"
B"{chat_history}\nHuman: {input}\nAI:"
C"What is the capital of France?"
D"history"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong memory_key that does not match the prompt.
Passing a prompt string instead of a PromptTemplate instance.
Passing input without quotes or as a variable.

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

  1. Step 1: Understand conversation flow

    Handling follow-up questions means the system remembers previous messages to keep the chat natural.
  2. Step 2: Identify the purpose of context

    Context helps the system answer related questions without losing track of the topic.
  3. Final Answer:

    To keep the conversation natural and connected by remembering context -> Option B
  4. 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

  1. Step 1: Recall LangChain components

    ConversationChain is designed to handle conversations and keep track of history.
  2. Step 2: Match class to purpose

    TextSplitter and DocumentLoader are for documents, VectorStoreRetriever for searching, not conversation management.
  3. Final Answer:

    ConversationChain -> Option A
  4. Quick Check:

    ConversationChain manages chat history = B [OK]
Hint: ConversationChain handles chat history in LangChain [OK]
Common Mistakes:
  • Confusing ConversationChain with document tools
  • Choosing unrelated classes like TextSplitter
  • Assuming VectorStoreRetriever manages conversations
  • Mixing up class purposes
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

  1. Step 1: Understand ConversationChain behavior

    ConversationChain keeps track of previous inputs and outputs to provide context-aware answers.
  2. Step 2: Analyze the code flow

    response2 uses the previous question 'What is AI?' to answer 'Can you explain more?' with context.
  3. Final Answer:

    An answer to 'Can you explain more?' using the context of 'What is AI?' -> Option C
  4. 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

  1. Step 1: Check method call syntax

    ConversationChain.predict expects a named argument 'input=...'. The second call misses this keyword.
  2. Step 2: Understand consequences

    Without 'input=', Python raises a TypeError because the argument is positional but not expected.
  3. Final Answer:

    Missing 'input=' keyword in second predict call causes error -> Option D
  4. Quick Check:

    predict requires input= keyword = D [OK]
Hint: Always use input= keyword in predict calls [OK]
Common Mistakes:
  • Calling predict without input= keyword
  • Thinking ConversationChain disallows multiple calls
  • Believing temperature=0 is invalid
  • Assuming print syntax is wrong
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

  1. Step 1: Understand requirement to reset context

    The app needs to keep context but reset after 3 questions to avoid long history.
  2. Step 2: Choose method to reset memory

    ConversationChain supports memory clearing; clearing after 3 predictions resets context properly.
  3. Step 3: Evaluate other options

    Creating new instances loses all context immediately; keeping all history ignores reset; using OpenAI directly lacks conversation memory.
  4. Final Answer:

    Use ConversationChain and clear its memory after every 3 predictions -> Option A
  5. Quick Check:

    Reset context by clearing ConversationChain memory = A [OK]
Hint: Clear ConversationChain memory to reset context after set questions [OK]
Common Mistakes:
  • Recreating ConversationChain loses partial context
  • Not resetting memory causes long context buildup
  • Using OpenAI directly loses conversation flow
  • Ignoring how to clear memory properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard