DEV Community

Cover image for How I Built an Offline AI Tutor That Actually Understands Textbooks (LFM2 + RAG)
Ksirailway Base
Ksirailway Base

Posted on • Originally published at github.com

How I Built an Offline AI Tutor That Actually Understands Textbooks (LFM2 + RAG)

I was studying English from Murphy's Grammar in Use and kept running into the same problem: every AI I tried wanted to explain grammar like it was giving a TED talk. Long preambles, theatrical examples, confident hallucinations about rules that aren't in my book.

I wanted something simpler. Open the book. Give me exercise 47. Check my answer against the actual rule on that page. No internet. No subscription. No GPU.

The problem I actually had to solve
Standard RAG pipelines chunk blindly. A fill-in-the-blank exercise that spans two pages becomes nonsense after splitting. I wrote a regex parser that extracts exercises directly from the PDF before they touch the LLM — the task is copied from the book, not generated. No hallucinations possible on the exercise itself.

For retrieval: ChromaDB + BM25 hybrid search.
For inference: LFM2-2.6B via llama.cpp. I chose LFM2 specifically because I wanted to test it — see how it handles factual constraint, whether it stays inside the textbook or wanders off. First time using it in a real pipeline. Turns out it's well-behaved on RAM-only hardware.
Conversational memory covers 24 messages (12 per side) — enough to do a full exercise session with follow-up questions without losing context.

What I don't know yet
The parser works for Murphy-style grammar books. I have no idea what happens with math textbooks or scientific papers — that's next on the list.

If you try it on something weird, open an issue and tell me what breaks. And link on HuggingFace Space there

Top comments (0)