How to Build an AI Tutor That Answers From a Textbook
A generic chatbot invents chapter content. Here is the pipeline we used to make a tutor answer from the official textbook instead: OCR, chunking, embeddings, retrieval, and a streamed reply.
By Mutahar Aslam, Founder & CEO of Paraline Tech3 min read
A student stuck on a chapter at home usually has no patient teacher to ask. A general-purpose AI chat can answer, but it answers from what it remembers, not from the book the child is studying. It can state something confidently that the chapter never says, and it tends to reply in formal English.
We built a tutor for students in Classes 4 to 10 of the Punjab Curriculum and Textbook Board (PCTB) syllabus that works the other way round: it looks in the official textbook first, then explains what it found. This is how that pipeline works, in the order the data flows.
Why a generic chatbot is the wrong tool
Three problems show up quickly when a child uses a general chatbot for schoolwork:
- It invents. If the model does not know the chapter, it fills the gap with something plausible.
- It is not aligned with the syllabus. The wording, the examples and the order of ideas differ from the book in front of the student.
- It speaks the wrong register. Our students needed everyday Roman Urdu, not textbook English.
The fix for the first two is grounding: make the book the source of truth.
The five parts of the pipeline
1. Get the text out of the book
Textbooks are usually scanned PDFs, which are images of pages. We ran them through Tesseract OCR to get text. OCR is never perfect, so spot-check pages with tables, formulas and non-English text before you build on top of the output.
2. Split it into chunks
A whole chapter is too big to hand to a model for every question, and a single sentence is too small to carry an idea. Split the text into passages that are small enough to be specific and large enough to make sense on their own, and keep track of which book, chapter and page each passage came from. That metadata is what lets the tutor say where an answer came from.
3. Turn each chunk into an embedding
An embedding is a list of numbers that represents the meaning of a piece of text, so that passages about the same idea sit close together. We used Gemini embeddings and stored them in a vector index, so a question can be matched to the passages closest in meaning.
4. Retrieve before you answer
When a student asks a question, embed the question the same way, ask the index for the closest passages, and give those passages to the model together with the question. This step is what "retrieval-augmented generation" (RAG) means. The prompt then says: answer using these passages, explain gently in Roman Urdu, and if the passages do not contain the answer, say so.
// Simplified shape of one tutor turn (not our production code).
const passages = await index.search(await embed(question), { class: 7, subject, chapter, limit: 5 });
const answer = streamModel({
system:
"You are a patient tutor. Answer only from the passages. " +
"Explain in everyday Roman Urdu. If the passages do not cover the question, say so.",
passages,
question,
});
5. Stream the reply
Waiting for a full answer feels slow to a child. We stream the reply as it is generated so the text appears word by word, using server-sent events from a Supabase edge function. Formulas are typeset in the stream with KaTeX so maths looks like maths.
What else made it usable
- Read aloud. Answers can be spoken with neural text-to-speech, falling back to the browser's own voice.
- Homework photos. A student can photograph a question; the image goes to private storage and the tutor works through it.
- An answer cache. Repeated lesson turns are served from a cache instead of calling the model again.
- Feedback. A thumbs up or down on each answer measures quality and collects rated examples that could be used to fine-tune a model later.
- Per-user model settings. The app can use Gemini, OpenRouter, Groq, xAI, Cerebras or OpenAI, with each user's own keys, so no single provider outage stops a lesson.
What to watch out for
Grounding is not a guarantee. Retrieval can return the wrong passage, and a model can still misread the right one. Keep a way for students to rate answers, read the low-rated ones regularly, and fix the chunks or the prompt when you find a pattern. If you are building something similar for a school or a publisher, the data pipeline deserves as much attention as the model.
If you want a tutor, support assistant or search built on your own documents, talk to Paraline Tech. The project this article is based on is described in the PCTB AI learning case study.
Common questions
Does retrieval stop an AI tutor from making things up?
It lowers the risk a lot, but not to zero. The model can still misread a passage. Tell it to answer only from the passages it is given and to say so when they do not contain the answer.
Do I need a vector database?
You need somewhere to store embeddings and search them by similarity. That can be a dedicated vector database or a vector index inside a database you already run. For one set of textbooks the choice matters less than the quality of the chunks.
Can I use any textbook?
Only content you have the right to use. In our project the books are used only as tutoring context and are never offered as downloads; the textbooks remain the publisher's.
Keep reading
Claude Code Extension Backdoor in Cursor: What to Do
A fake Anthropic.claude-code extension was served from Cursor's marketplace on 29 October 2026. Here is who is affected, how to check your machine, and what to do next.
6 min read
What Is an MCP Server? A Plain Explanation With a Real Example
MCP lets an AI assistant use your application's features through a standard door instead of a one-off integration. This is what it is, how it differs from an API, and what a remote MCP endpoint looks like in practice.
3 min read

