0
0
LangChainframework~10 mins

PromptTemplate basics 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 PromptTemplate with a template string.

LangChain
from langchain import PromptTemplate

template = "Hello, my name is {name}."
prompt = PromptTemplate(template=[1], input_variables=["name"])
Drag options to blanks, or click blank then click option'
A"Hello, my name is name."
BHello, my name is {name}.
C'Hello, my name is name.'
D"Hello, my name is {name}."
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the template string.
Using single quotes without curly braces for variables.
2fill in blank
medium

Complete the code to specify the input variables for the PromptTemplate.

LangChain
from langchain import PromptTemplate

prompt = PromptTemplate(template="Hello, {name}!", input_variables=[1])
Drag options to blanks, or click blank then click option'
A["name"]
B{"name"}
C"name"
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a list.
Using curly braces which create a set, not a list.
3fill in blank
hard

Fix the error in the code to correctly create a PromptTemplate with two input variables.

LangChain
from langchain import PromptTemplate

template = "My name is {name} and I am {age} years old."
prompt = PromptTemplate(template=template, input_variables=[1])
Drag options to blanks, or click blank then click option'
A["name", "age"]
B[name, age]
C{"name", "age"}
D[name, "age"]
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting variable names inside the list.
Using curly braces which create a set, not a list.
4fill in blank
hard

Fill both blanks to create a PromptTemplate and format it with variables.

LangChain
from langchain import PromptTemplate

prompt = PromptTemplate(template=[1], input_variables=["animal", "sound"])
result = prompt.format(**[2])
Drag options to blanks, or click blank then click option'
A"The {animal} goes {sound}."
B{"animal": "dog", "sound": "woof"}
C"The animal goes sound."
D{"animal", "dog", "sound", "woof"}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a set instead of a dictionary to format.
Not using curly braces in the template string.
5fill in blank
hard

Fill all three blanks to create a PromptTemplate, format it, and print the result.

LangChain
from langchain import PromptTemplate

prompt = PromptTemplate(template=[1], input_variables=["city", "weather"])
result = prompt.format(**[2])
print([3])
Drag options to blanks, or click blank then click option'
A"The weather in {city} is {weather}."
B{"city": "Paris", "weather": "sunny"}
Cresult
D"The city is {city} and weather is {weather}."
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the dictionary instead of the formatted result.
Not using curly braces in the template string.