0
0
LangChainframework~10 mins

Question reformulation with history in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Question reformulation with history
User asks new question
Retrieve conversation history
Combine history + new question
Pass combined input to reformulation chain
Generate reformulated question
Return reformulated question for next processing
The system takes the new question and past conversation, combines them, then reformulates the question to include context.
Execution Sample
LangChain
from langchain.chains import ConversationChain
from langchain.llms import OpenAI

conversation = ConversationChain(llm=OpenAI())
response = conversation.predict(input="What is AI?")
This code uses LangChain's ConversationChain to reformulate a question considering conversation history.
Execution Table
StepActionInputOutputNotes
1User inputs new question"What is AI?"New question storedStart of interaction
2Retrieve conversation historyPrevious Q&A (empty initially)History retrievedNo prior history on first question
3Combine history + new question'' + 'What is AI?'Combined input: 'What is AI?'First question, so just the question
4Pass to reformulation chainCombined inputReformulated question: 'What is AI?'No change for first question
5Return reformulated questionReformulated questionOutput ready for next stepReady for answer generation
6User inputs follow-up question"How does it work?"New question storedSecond question in conversation
7Retrieve conversation historyPrevious Q&A: 'What is AI?'History retrievedHistory now includes first question
8Combine history + new question'What is AI?' + 'How does it work?'Combined input with contextContext added to new question
9Pass to reformulation chainCombined inputReformulated question: 'How does AI work?'Question reformulated with history
10Return reformulated questionReformulated questionOutput ready for next stepReady for answer generation
💡 Execution stops after reformulated question is returned for each user input.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
new_questionNone"What is AI?""How does it work?"NoneNoneNone
conversation_history''''"What is AI?""What is AI?""What is AI? + How does it work?""What is AI? + How does it work?"
combined_input''"What is AI?""What is AI? + How does it work?""What is AI?""What is AI? + How does it work?""What is AI? + How does it work?"
reformulated_question''"What is AI?""How does AI work?""What is AI?""How does AI work?""How does AI work?"
Key Moments - 3 Insights
Why does the first question not change after reformulation?
Because there is no prior conversation history, the reformulation chain returns the question as is, as shown in execution_table step 4.
How does the conversation history affect the reformulated question?
The history is combined with the new question before reformulation, adding context so the chain can produce a clearer question, as seen in steps 8 and 9.
What happens if the history is empty or missing?
The combined input is just the new question, so reformulation does not add context, resulting in the original question returned, as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 9. What is the reformulated question?
A"How does it work?"
B"What is AI?"
C"How does AI work?"
D"What is AI? How does it work?"
💡 Hint
Check the 'Output' column at step 9 in the execution_table.
At which step does the system combine the conversation history with the new question for the second user input?
AStep 6
BStep 8
CStep 7
DStep 9
💡 Hint
Look for the step where 'Combine history + new question' happens for the second question.
If the conversation history was empty, how would the reformulated question at step 9 change?
AIt would be the same as the new question 'How does it work?'
BIt would be empty
CIt would remain 'How does AI work?'
DIt would combine both questions anyway
💡 Hint
Refer to the behavior at step 4 when history is empty.
Concept Snapshot
Question reformulation with history:
- Take new question + conversation history
- Combine them as input
- Pass to reformulation chain
- Get clearer question with context
- Use reformulated question for next steps
- First question returns as is (no history)
Full Transcript
This visual execution shows how LangChain reformulates a user's question by including conversation history. First, the user inputs a question. The system retrieves any past conversation history and combines it with the new question. This combined input is sent to the reformulation chain, which outputs a clearer, context-aware question. For the first question, with no history, the reformulated question is the same as the input. For follow-up questions, the history helps produce a better question. Variables like new_question, conversation_history, combined_input, and reformulated_question change step-by-step as shown. Key moments include understanding why the first question is unchanged and how history affects reformulation. The quiz tests understanding of these steps and outputs.