Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a prompt template using LangChain.
LangChain
from langchain.prompts import PromptTemplate prompt = PromptTemplate(template=[1], input_variables=["name"])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the template inside quotes.
Using single quotes without curly braces.
Not including the variable name inside curly braces.
✗ Incorrect
The template must be a string with curly braces around the variable name, and it must be passed as a string literal.
2fill in blank
mediumComplete the code to compare two prompt templates by their template strings.
LangChain
prompt1 = PromptTemplate(template="Hello, {name}!", input_variables=["name"]) prompt2 = PromptTemplate(template="Hi, {name}!", input_variables=["name"]) are_equal = prompt1.[1] == prompt2.[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing input_variables instead of template strings.
Using a method name instead of an attribute.
Trying to compare the whole PromptTemplate objects directly.
✗ Incorrect
The template attribute holds the string template, so comparing prompt1.template and prompt2.template checks if the prompt texts are the same.
3fill in blank
hardFix the error in the code to correctly format a prompt with a variable.
LangChain
prompt = PromptTemplate(template="Hello, {name}!", input_variables=["name"]) message = prompt.[1](name="Alice")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'render' which is not a method of PromptTemplate.
Using 'format_prompt' which returns a PromptValue, not a string.
Using 'generate' which is for chains, not templates.
✗ Incorrect
The correct method to fill in variables in a PromptTemplate is 'format', which returns the formatted string.
4fill in blank
hardFill both blanks to create a new prompt by combining two templates and formatting it.
LangChain
prompt1 = PromptTemplate(template="Hello, {name}!", input_variables=["name"]) prompt2 = PromptTemplate(template="Welcome to {place}.", input_variables=["place"]) combined_template = f"{prompt1.[1] {prompt2.[2]" combined_prompt = PromptTemplate(template=combined_template, input_variables=["name", "place"])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'format' instead of 'template' to get the string.
Mixing up input_variables with template strings.
Trying to call methods instead of accessing attributes.
✗ Incorrect
To combine templates, access the 'template' attribute of prompt1 and prompt2. The first blank is for prompt1's template string, the second blank is for prompt2's template string.
5fill in blank
hardFill both blanks to create a dictionary comprehension that maps variable names to their prompt templates if the template contains a keyword.
LangChain
prompts = {
"greet": PromptTemplate(template="Hello, {name}!", input_variables=["name"]),
"welcome": PromptTemplate(template="Welcome to {place}.", input_variables=["place"]),
"bye": PromptTemplate(template="Goodbye, {name}!", input_variables=["name"])
}
filtered = {k: v.template for k, v in prompts.items() if "{BLANK_2}}" in v.{{BLANK_2}} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ',' instead of ':' in dictionary comprehension.
Checking for the wrong keyword.
Accessing a wrong attribute instead of 'template'.
✗ Incorrect
In a dictionary comprehension, the key and value are separated by ':'. We filter templates containing the keyword 'name' in their 'template' attribute.