0
0
LangChainframework~30 mins

Question reformulation with history in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
Question Reformulation with History in LangChain
📖 Scenario: You are building a chatbot that can understand follow-up questions by remembering the previous conversation. This helps the bot give better answers by reformulating the current question with the chat history.
🎯 Goal: Create a LangChain setup that reformulates a follow-up question using the chat history. You will build the data, configure the chain, apply the reformulation logic, and complete the chain for use.
📋 What You'll Learn
Create a list called chat_history with two example chat turns
Create a variable called question with the follow-up question string
Use LangChain's load_qa_with_sources_chain to create a chain with combine_docs_chain_kwargs containing question_generator
Add the final call to chain.run passing question and chat_history
💡 Why This Matters
🌍 Real World
Chatbots often need to understand follow-up questions by remembering previous conversation. This project shows how to use LangChain to reformulate questions with chat history for better answers.
💼 Career
Knowing how to use LangChain for question reformulation is useful for building advanced conversational AI, customer support bots, and virtual assistants.
Progress0 / 4 steps
1
Set up chat history and question
Create a list called chat_history with these exact two tuples: ("User: What is LangChain?", "Assistant: LangChain is a framework for building language model apps.") and ("User: How does it help?", "Assistant: It helps by chaining LLM calls together."). Then create a variable called question with the string "Can you explain question reformulation?".
LangChain
Need a hint?

Remember to create a list named chat_history with two tuples exactly as shown, and a string variable question.

2
Configure the question reformulation chain
Import load_qa_with_sources_chain from langchain.chains. Then create a variable called question_generator by calling load_qa_with_sources_chain with chain_type="stuff". Next, create a variable called chain by calling load_qa_with_sources_chain with chain_type="map_reduce" and passing combine_docs_chain_kwargs={'question_generator': question_generator}.
LangChain
Need a hint?

Import load_qa_with_sources_chain and create question_generator and chain exactly as described.

3
Apply question reformulation logic
Use the chain variable to call its run method. Pass the arguments question=question and chat_history=chat_history. Assign the result to a variable called reformulated_question.
LangChain
Need a hint?

Call chain.run with question=question and chat_history=chat_history, assign to reformulated_question.

4
Complete the LangChain question reformulation setup
Add a final line that prints the reformulated_question variable to see the reformulated question output.
LangChain
Need a hint?

Use print(reformulated_question) to display the reformulated question.