0
0
LangChainframework~10 mins

Handling follow-up questions in LangChain - Interactive Code Practice

Choose your learning style9 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.