0
0
LangChainframework~10 mins

Context formatting and injection 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 with a variable placeholder.

LangChain
from langchain import PromptTemplate

prompt = PromptTemplate(template="Hello, [1]!", input_variables=["name"])
Drag options to blanks, or click blank then click option'
Aname
Buser
Cinput
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name in input_variables that does not match the placeholder.
Forgetting to include the variable in input_variables list.
2fill in blank
medium

Complete the code to format the prompt with the variable 'name' set to 'Alice'.

LangChain
formatted_prompt = prompt.format([1]="Alice")
Drag options to blanks, or click blank then click option'
Auser
Bname
Cinput
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than defined in the template.
Passing the variable as a positional argument instead of keyword.
3fill in blank
hard

Fix the error in the code to inject context into the prompt template.

LangChain
context = "You are a helpful assistant."
prompt = PromptTemplate(template="Context: [1]\nQuestion: {question}", input_variables=["context", "question"])
Drag options to blanks, or click blank then click option'
Atext
Bquestion
Ccontext
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using a placeholder that is not in input_variables.
Mixing curly braces syntax for placeholders.
4fill in blank
hard

Fill both blanks to create a prompt template that includes context and question variables.

LangChain
prompt = PromptTemplate(template="Context: [1]\nQuestion: [2]", input_variables=["context", "question"])
Drag options to blanks, or click blank then click option'
Acontext
Bquestion
Cinput
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the variable names in placeholders.
Using variable names not listed in input_variables.
5fill in blank
hard

Fill all three blanks to format the prompt with context and question values.

LangChain
formatted = prompt.format([1]="You are a bot.", [2]="What is AI?", [3]="ignored")
Drag options to blanks, or click blank then click option'
Acontext
Bquestion
Cextra
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names not defined in the prompt template.
Passing positional arguments instead of keyword arguments.