0
0
LangChainframework~20 mins

PromptTemplate basics in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PromptTemplate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this PromptTemplate render?
Given the PromptTemplate below, what will be the rendered output when input variable 'name' is 'Alice'?
LangChain
from langchain.prompts import PromptTemplate

template = "Hello, {name}! Welcome to LangChain."
prompt = PromptTemplate(template=template, input_variables=["name"])
output = prompt.format(name="Alice")
print(output)
AHello, Alice! Welcome to {LangChain}.
BHello, {name}! Welcome to LangChain.
CHello, name! Welcome to LangChain.
DHello, Alice! Welcome to LangChain.
Attempts:
2 left
💡 Hint
Look at how the variable 'name' is used inside curly braces and how format replaces it.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a PromptTemplate with two input variables?
You want to create a PromptTemplate with variables 'city' and 'weather'. Which code snippet is correct?
APromptTemplate(template="The weather in {city} is {weather}.", input_variables="city, weather")
BPromptTemplate(template="The weather in {city} is {weather}.", input_variables=["city", "weather"])
CPromptTemplate(template="The weather in {city} is {weather}.", input_variables={"city", "weather"})
DPromptTemplate(template="The weather in {city} is {weather}.", input_variables=[city, weather])
Attempts:
2 left
💡 Hint
input_variables must be a list of strings.
state_output
advanced
2:00remaining
What is the output of this PromptTemplate with missing variable?
What happens when you try to format a PromptTemplate but omit one required variable?
LangChain
from langchain.prompts import PromptTemplate

template = "Today in {city}, the temperature is {temp} degrees."
prompt = PromptTemplate(template=template, input_variables=["city", "temp"])
output = prompt.format(city="Paris")
print(output)
ARaises a KeyError because 'temp' is missing
BOutputs: Today in Paris, the temperature is {temp} degrees.
COutputs: Today in Paris, the temperature is degrees.
DOutputs: Today in {city}, the temperature is {temp} degrees.
Attempts:
2 left
💡 Hint
Check what happens if a required variable is not provided to format.
🧠 Conceptual
advanced
2:00remaining
Which statement about PromptTemplate input_variables is true?
Choose the correct statement about the input_variables parameter in PromptTemplate.
Ainput_variables must list all variables used in the template string exactly.
Binput_variables can be omitted if the template uses no variables.
Cinput_variables can include variables not present in the template string.
Dinput_variables must be a dictionary mapping variable names to default values.
Attempts:
2 left
💡 Hint
Think about how PromptTemplate validates variables.
🔧 Debug
expert
2:00remaining
Why does this PromptTemplate raise a ValueError?
Examine the code and choose the reason for the ValueError.
LangChain
from langchain.prompts import PromptTemplate

template = "Hello, {name}! Your age is {age}."
prompt = PromptTemplate(template=template, input_variables=["name", "age", "location"])
output = prompt.format(name="Bob", age=30, location="NY")
print(output)
Aformat arguments do not match input_variables types
Bformat is missing a required variable 'location', causing ValueError
Cinput_variables includes 'location' which is not in the template, causing ValueError
Dtemplate string has syntax error with curly braces
Attempts:
2 left
💡 Hint
Check if input_variables matches variables in the template exactly.