Challenge - 5 Problems
Langchain Prompt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this chained prompt?
Consider a Langchain prompt chain where the first prompt extracts a user's name, and the second prompt generates a greeting using that name. What will be the final output if the input is 'Alice'?
LangChain
from langchain.prompts import PromptTemplate from langchain.chains import SimpleSequentialChain name_prompt = PromptTemplate(input_variables=["user_input"], template="Extract the name from: {user_input}") greet_prompt = PromptTemplate(input_variables=["name"], template="Say hello to {name}!") class NameExtractor: def run(self, user_input): # Simulate extraction by returning the input directly return user_input class Greeter: def run(self, name): return f"Hello, {name}!" extractor = NameExtractor() greeter = Greeter() # Simulate chaining name = extractor.run("Alice") output = greeter.run(name) print(output)
Attempts:
2 left
💡 Hint
Think about how the output of the first prompt is used as input for the second.
✗ Incorrect
The first prompt extracts the name 'Alice' which is passed to the second prompt that generates the greeting 'Hello, Alice!'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this prompt chain setup
Which option contains the syntax error when defining a Langchain prompt template with multiple input variables?
LangChain
from langchain.prompts import PromptTemplate # Intended to have two input variables: 'name' and 'age' # Option A prompt_a = PromptTemplate(input_variables=["name", "age"], template="Name: {name}, Age: {age}") # Option B prompt_b = PromptTemplate(input_variables=["name", "age"], template="Name: {name}, Age: {age}") # Option C prompt_c = PromptTemplate(input_variables=["name", "age"], template="Name: {name}, Age: {age}") # Option D prompt_d = PromptTemplate(input_variables=["name", "age"], template="Name: {name}, Age: {age}")
Attempts:
2 left
💡 Hint
Check the template string for balanced braces.
✗ Incorrect
Option C has a missing closing brace '}' in the template string causing a syntax error.
❓ state_output
advanced2:00remaining
What is the final output after chaining these prompts with state passing?
Given two prompt templates chained where the first extracts a city from input text and the second generates a weather report for that city, what is the output if the input is 'Weather in Paris'?
LangChain
from langchain.prompts import PromptTemplate extract_city = PromptTemplate(input_variables=["text"], template="Extract city from: {text}") weather_report = PromptTemplate(input_variables=["city"], template="The weather in {city} is sunny.") class CityExtractor: def run(self, text): # Extract city by splitting return text.split()[-1] class WeatherReporter: def run(self, city): return f"The weather in {city} is sunny." extractor = CityExtractor() reporter = WeatherReporter() city = extractor.run("Weather in Paris") result = reporter.run(city) print(result)
Attempts:
2 left
💡 Hint
Look at how the city is extracted and passed to the weather report prompt.
✗ Incorrect
The city 'Paris' is extracted and used in the second prompt to produce the full weather report.
🔧 Debug
advanced2:00remaining
Why does this chained prompt raise a KeyError?
This Langchain prompt chain raises a KeyError when running. What is the cause?
LangChain
from langchain.prompts import PromptTemplate prompt1 = PromptTemplate(input_variables=["input_text"], template="Extract keyword from: {input_text}") prompt2 = PromptTemplate(input_variables=["keyword"], template="Define {keyword}.") class Chain: def run(self, text): # Incorrectly passing variable name extracted = prompt1.format(input_text=text) # Here, 'extracted' is a string, but prompt2 expects 'keyword' return prompt2.format(keyword=extracted) chain = Chain() output = chain.run("Explain recursion") print(output)
Attempts:
2 left
💡 Hint
Check the variable names used in prompt2.format() call.
✗ Incorrect
prompt2 expects a variable named 'keyword' but the code passes 'input_text', causing a KeyError.
🧠 Conceptual
expert2:00remaining
Which option best describes prompt chaining in Langchain?
Select the most accurate description of prompt chaining in Langchain.
Attempts:
2 left
💡 Hint
Think about how data flows between prompts in a chain.
✗ Incorrect
Prompt chaining in Langchain means linking prompts so each uses the previous output as input, creating a sequence.