0
0
LangChainframework~20 mins

Why templates create reusable prompts in LangChain - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prompt Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use templates in prompt creation?
Which of the following best explains why templates help create reusable prompts in Langchain?
ATemplates allow you to define a prompt structure with placeholders, so you can easily insert different values without rewriting the whole prompt.
BTemplates automatically generate new prompts without any input from the user.
CTemplates store the entire conversation history to reuse previous answers.
DTemplates convert prompts into code that runs faster.
Attempts:
2 left
💡 Hint
Think about how you can change parts of a message without typing it all again.
component_behavior
intermediate
2:00remaining
How does a Langchain prompt template behave when reused?
Given a prompt template with placeholders, what happens when you reuse it with different input values?
LangChain
from langchain.prompts import PromptTemplate

template = PromptTemplate(
    input_variables=["animal"],
    template="Tell me a joke about a {animal}."
)

print(template.format(animal="cat"))
print(template.format(animal="dog"))
AIt prints two different prompts, one about a cat and one about a dog, by filling the placeholder each time.
BIt prints the same joke twice because the template does not change.
CIt raises an error because the template can only be used once.
DIt prints the placeholder text {animal} instead of replacing it.
Attempts:
2 left
💡 Hint
Look at how the format method uses different values for the same placeholder.
📝 Syntax
advanced
2:00remaining
Identify the correct way to define a prompt template with multiple variables
Which option correctly defines a Langchain PromptTemplate with two input variables 'name' and 'hobby'?
APromptTemplate(variables=["name", "hobby"], template="My name is {name} and I like {hobby}.")
BPromptTemplate(input_variables=["name", "hobby"], template="My name is {name} and I like {hobby}.")
CPromptTemplate(input_vars=["name", "hobby"], template="My name is {name} and I like {hobby}.")
DPromptTemplate(input_variables=["name", "hobby"], template="My name is name and I like hobby.")
Attempts:
2 left
💡 Hint
Check the exact parameter names and placeholder syntax.
state_output
advanced
2:00remaining
What is the output after formatting a prompt template with missing variables?
What happens if you try to format a Langchain PromptTemplate but forget to provide a required input variable?
LangChain
from langchain.prompts import PromptTemplate

template = PromptTemplate(
    input_variables=["city", "activity"],
    template="In {city}, people enjoy {activity}."
)

print(template.format(city="Paris"))
AIt prints 'In {city}, people enjoy {activity}.' without any replacements.
BIt prints 'In Paris, people enjoy {activity}.' without replacing the placeholder.
CIt prints 'In Paris, people enjoy .' with the missing variable replaced by empty string.
DIt raises a KeyError because 'activity' is missing.
Attempts:
2 left
💡 Hint
Think about what happens when a required input is not given for a placeholder.
🔧 Debug
expert
2:00remaining
Debug why a prompt template does not replace placeholders correctly
Why does this code print the placeholders literally instead of replacing them?
LangChain
from langchain.prompts import PromptTemplate

template = PromptTemplate(
    input_variables=["food"],
    template='I love {food}!'
)

print(template.format(food='pizza'))
AThe template string uses single quotes instead of double quotes, so placeholders are not recognized.
BThe template string uses single quotes but placeholders require double curly braces to be replaced.
CThe template string uses single quotes but placeholders are recognized correctly; the code should print 'I love pizza!'.
DThe template string is correct; the problem is that the format method is not called properly.
Attempts:
2 left
💡 Hint
Check if the quotes affect placeholder replacement in Python strings.