Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class OpenAI instead of an instance.
Forgetting to set temperature=0 for deterministic output.
✗ Incorrect
The ConversationChain requires an llm instance. We create an OpenAI llm with temperature=0 and pass it as llm parameter.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
We create a memory instance and pass it to ConversationChain as the memory parameter to keep track of conversation history.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None or a new memory instance without the correct key.
Forgetting to import ConversationBufferMemory.
✗ Incorrect
The ConversationChain needs the memory instance to track conversation history and handle follow-up questions properly.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a prompt string instead of a PromptTemplate instance.
Omitting chat_history in the template string.
✗ Incorrect
The prompt template must include chat_history and input placeholders. We pass the prompt instance to ConversationChain.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The memory_key must be 'chat_history' to match the prompt. The prompt template includes chat_history and input placeholders. The input is a question string.