Challenge - 5 Problems
Context Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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)
Attempts:
2 left
💡 Hint
Look at how the variables are mapped in the template and what values are passed to format().
✗ Incorrect
The prompt template uses placeholders {context} and {query}. The format method replaces these with the given values. So {context} becomes 'Today is sunny.' and {query} becomes 'weather'.
📝 Syntax
intermediate2: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
Attempts:
2 left
💡 Hint
Check for missing closing parentheses or quotes.
✗ Incorrect
Option A is missing the closing parenthesis for the PromptTemplate constructor, causing a syntax error.
❓ state_output
advanced2: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)
Attempts:
2 left
💡 Hint
Match the variables in the template with the arguments passed to format().
✗ Incorrect
The format method replaces {context} with 'User is interested in AI.' and {query} with 'Explain transformers.'.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check which variables the template expects and which are passed.
✗ Incorrect
The template expects both 'context' and 'query' variables, but only 'context' is provided in format(), causing a KeyError for 'query'.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how context helps the model understand the question better.
✗ Incorrect
Injecting context gives the language model important background or details, so it can generate more accurate and relevant answers.