0
0
LangChainframework~10 mins

Comparing prompt versions 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 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'
AHello, {name}!
B"Hello name!"
C'Hello, name!'
D"Hello, {name}!"
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.
2fill in blank
medium

Complete 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'
Atemplate
Binput_variables
Cformat
Dtext
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.
3fill in blank
hard

Fix 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'
Aformat
Bformat_prompt
Crender
Dgenerate
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.
4fill in blank
hard

Fill 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'
Atemplate
Bformat
Cinput_variables
Dtext
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.
5fill in blank
hard

Fill 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'
A:
Bname
Ctemplate
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using ',' instead of ':' in dictionary comprehension.
Checking for the wrong keyword.
Accessing a wrong attribute instead of 'template'.