Challenge - 5 Problems
LangChain Question Reformulation 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 LangChain question reformulation code?
Given the following LangChain code snippet that reformulates a follow-up question using chat history, what will be the output when the input question is "What is the capital?" and the chat history contains one previous question-answer pair?
LangChain
from langchain.chains import TransformChain from langchain.prompts import PromptTemplate prompt_template = """ Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. Chat History: {chat_history} Follow Up Input: {question} Standalone question:""" prompt = PromptTemplate(input_variables=["chat_history", "question"], template=prompt_template) chain = TransformChain(input_variables=["chat_history", "question"], output_variables=["rephrased_question"], transform=lambda inputs: {"rephrased_question": f"Standalone: {inputs['question']}"}) inputs = {"chat_history": "Q: Who is the president? A: Joe Biden.", "question": "What is the capital?"} output = chain.run(inputs) print(output)
Attempts:
2 left
💡 Hint
Look at how the lambda function formats the output dictionary using the input question.
✗ Incorrect
The TransformChain uses a lambda that returns a dictionary with key 'rephrased_question' and value 'Standalone: ' plus the input question. The input question is 'What is the capital?', so the output dictionary is {"rephrased_question": "Standalone: What is the capital?"}.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in this LangChain prompt template?
Consider the following prompt template string for question reformulation. Which option contains a syntax error that will prevent the PromptTemplate from being created?
LangChain
prompt_template = """ Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. Chat History: {chat_history} Follow Up Input: {question} Standalone question:"""
Attempts:
2 left
💡 Hint
Check if the input variables match the placeholders in the template string.
✗ Incorrect
Option D replaces '{question}' with '{follow_up}', but 'follow_up' is not listed in input_variables, causing a KeyError or template error when rendering.
❓ state_output
advanced2:00remaining
What is the value of 'rephrased_question' after running this LangChain chain?
Given a LangChain TransformChain that reformulates a question by appending chat history, what is the value of 'rephrased_question' after running with the inputs below?
LangChain
from langchain.chains import TransformChain chain = TransformChain( input_variables=["chat_history", "question"], output_variables=["rephrased_question"], transform=lambda inputs: {"rephrased_question": f"{inputs['chat_history']} Then ask: {inputs['question']}"} ) inputs = {"chat_history": "Q: Who won the game? A: Team A.", "question": "What was the score?"} output = chain.run(inputs) rephrased = output["rephrased_question"]
Attempts:
2 left
💡 Hint
Look at how the lambda formats the string using chat_history and question keys.
✗ Incorrect
The lambda returns a dictionary with key 'rephrased_question' and value combining chat_history, ' Then ask: ', and question. So the value is 'Q: Who won the game? A: Team A. Then ask: What was the score?'.
🔧 Debug
advanced2:00remaining
Which option causes a runtime error when running this LangChain chain?
Given this LangChain TransformChain code, which option will cause a runtime error when calling chain.run with the provided inputs?
LangChain
from langchain.chains import TransformChain chain = TransformChain( input_variables=["chat_history", "question"], output_variables=["rephrased_question"], transform=lambda inputs: {"rephrased_question": inputs["question"].upper()} ) inputs = {"chat_history": "Previous Q&A", "question": "hello"}
Attempts:
2 left
💡 Hint
Check which inputs dictionary is missing required keys.
✗ Incorrect
Option C is missing the 'question' key, so accessing inputs["question"] raises a KeyError at runtime.
🧠 Conceptual
expert3:00remaining
How does LangChain's question reformulation with history improve conversational AI?
Why is it important to reformulate follow-up questions into standalone questions using chat history in LangChain conversational chains?
Attempts:
2 left
💡 Hint
Think about how standalone questions help the model understand context better.
✗ Incorrect
Reformulating follow-up questions into standalone questions provides the model with all necessary context in a single input, avoiding confusion and improving response quality.