Project: A Lesson Tutor Bot
Time to build something real.
We will make a Fox tutor. You give it a lesson from this site as a text file, and it answers your questions about that lesson.
It remembers the conversation, streams its answers, and explains like a patient teacher.
This is very close to how the Fox AI helper on this website works. Here is the plan.
A system prompt that sets the teacher voice
A prompt with history, the lesson text, and the question
A loader that reads the lesson file
A chain that streams a clean string
Memory, so follow up questions make sense
A small terminal loop to chat with it
Every piece comes from an earlier lesson. Today we just stack them.
Step 1: The teacher voice
The {lesson} hole will hold the whole lesson file. The rules above it shape every answer.
Step 2: The prompt with history
MessagesPlaceholder is where past messages go. The new question comes last.
Step 3: Load the lesson
Save a lesson as lesson.txt. Then read it once and keep it in a variable.
One file, one Document. The words live in page_content.
Step 4: The chain
We use RunnablePassthrough.assign to add the lesson to the input, so the caller only has to send a question.
The input is a dictionary with a question and a history. assign adds a lesson key. Then prompt, model, parser.
Step 5: Memory
Each session id gets its own notebook of messages. LangChain fills the history hole and saves new messages for you.
Step 6: The chat loop
Type a question, watch the answer arrive word by word, ask a follow up. Type quit to stop.
All together: tutor.py
Run it with python tutor.py. Ask what a list is. Then ask why. The second answer knows what the first one said.
Step 7: Make it free, then grow it
Swap one line and the tutor runs on your own computer with no key.
Everything else stays the same. That is the reward for building with LangChain pieces.
When it works, try one of these.
Load many lessons and use a retriever to pick the right one for each question
Add with_fallbacks so it switches to Ollama when the cloud is down
Turn on LangSmith to see which lessons students ask about most
Wrap it in a small web page with FastAPI or Streamlit
Remember: you did not learn one big thing today. You learned that small pieces snap together. That is what makes LangChain worth it.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.