0
0
LangChainframework~20 mins

Context formatting and injection in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Context Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does context injection affect prompt output?
Given a LangChain prompt template that injects user context, what will be the output when the user input is "weather"?
LangChain
from langchain.prompts import PromptTemplate

context = "Today is sunny."
prompt = PromptTemplate(
    input_variables=["context", "query"],
    template="Context: {context}\nUser Query: {query}\nAnswer:"  
)

formatted_prompt = prompt.format(context=context, query="weather")
print(formatted_prompt)
A
Context: Today is sunny.
User Query: weather
Answer:
B
Context: weather
User Query: Today is sunny.
Answer:
C
User Query: weather
Answer: Today is sunny.
DAnswer: Context: Today is sunny. User Query: weather
Attempts:
2 left
💡 Hint
Look at how the variables are mapped in the template and what values are passed to format().
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in context injection
Which option contains a syntax error when defining a LangChain prompt template with context injection?
LangChain
from langchain.prompts import PromptTemplate

# Template with context and query variables
APromptTemplate(input_variables=["context", "query"], template="Context: {context}\nQuery: {query}\nAnswer:"
BPromptTemplate(input_variables=["context", "query"], template="Context: {context}\nQuery: {query}\nAnswer:")
C)":rewsnAn\}yreuq{ :yreuQn\}txetnoc{ :txetnoC"=etalpmet ,]"yreuq" ,"txetnoc"[=selbairav_tupni(etalpmeTtpmorP
DromptTemplate(input_variables=["context", "query"], template="Context: {context}\nQuery: {query}\nAnswer:")
Attempts:
2 left
💡 Hint
Check for missing closing parentheses or quotes.
state_output
advanced
2:00remaining
What is the final prompt string after context injection?
Given this code, what is the printed prompt string?
LangChain
from langchain.prompts import PromptTemplate

context = "User is interested in AI."
query = "Explain transformers."
prompt = PromptTemplate(
    input_variables=["context", "query"],
    template="Context info: {context}\nQuestion: {query}\nResponse:"
)

result = prompt.format(context=context, query=query)
print(result)
AResponse: Context info: User is interested in AI. Question: Explain transformers.
B
Context info: Explain transformers.
Question: User is interested in AI.
Response:
C
User is interested in AI.
Explain transformers.
Response:
D
Context info: User is interested in AI.
Question: Explain transformers.
Response:
Attempts:
2 left
💡 Hint
Match the variables in the template with the arguments passed to format().
🔧 Debug
advanced
2:00remaining
Why does this context injection raise a KeyError?
What causes the KeyError in this LangChain prompt formatting?
LangChain
from langchain.prompts import PromptTemplate

context = "Data science"
prompt = PromptTemplate(
    input_variables=["context", "query"],
    template="Topic: {context}\nQuestion: {query}\nAnswer:"
)

# Missing 'query' variable in format
result = prompt.format(context=context)
print(result)
ABecause the template string is invalid syntax
BBecause 'context' variable is missing in the template
CBecause 'query' variable is not provided in prompt.format() call
DBecause prompt.format() does not accept keyword arguments
Attempts:
2 left
💡 Hint
Check which variables the template expects and which are passed.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of context injection in LangChain prompts?
Why do we inject context into LangChain prompt templates before sending to a language model?
ATo reduce the size of the prompt for faster processing
BTo provide relevant background information that guides the model's response
CTo encrypt the user input for security reasons
DTo automatically generate code without user input
Attempts:
2 left
💡 Hint
Think about how context helps the model understand the question better.