A compiled, interactive textbook for training a small language model from scratch on one RTX 4090. The project combines a 20-day curriculum with a scroll-aware Pi chat tutor, saved conversation history, and an SQLite spaced-repetition deck.
The generated pages are self-contained: CSS, JavaScript, and vendored MathJax are
inlined into each HTML file. Reading works from file://; chat, transcripts, and
study scheduling require the localhost server.
Requirements:
make serveThen open:
| Page | URL |
|---|---|
| Textbook and chat | http://localhost:8000/train-your-own-llm.html |
| Chat archive | http://localhost:8000/chats.html |
| Study deck | http://localhost:8000/study.html |
Use another port with make serve PORT=9000. Press Ctrl+C to stop both the
source watcher and localhost server.
The curriculum covers environment setup, tokenization, transformer construction,
pretraining, evaluation, mid-training, SFT, DPO, RLVR, and GRPO. Source prose is
split across sections/*.html.j2; site.toml is the canonical chapter order.
The floating chat modal sends Pi the currently visible chapter context and an explanation of how the generated textbook is structured. It supports:
- Terra, Sol, and Luna at medium or high reasoning
- Streaming Markdown, tables, fenced code, MathJax, and sanitized rich HTML
- Quoting highlighted textbook text into a new prompt
- Explicit textbook edits through Pi's coding tools
- New conversations without losing earlier transcripts
- Animated reading and tool-use status
Pi only edits the textbook when explicitly asked. Source files are changed and
rebuilt; generated files under dist/ are never edited directly.
Readable transcripts live in chats/<chat-id>.json; complete Pi sessions live in
chats/pi-sessions/*.jsonl. Each turn records timestamps, model settings,
chapter/section context, the compiled page SHA-256, and Git commit. The archive
page provides full-text search and chronological sorting.
The Anki-style study interface uses study/study.sqlite3 and supports due queues,
Again/Hard/Good/Easy scheduling, proposal review, browsing, search, and note
editing. Its Future study view is a searchable todo list of concepts to explore
or expand later. The study_notes Pi tool manages cards; the future_study tool
adds, edits, completes, reopens, and lists those todos. Try asking chat:
Propose three atomic cards from this section.
Add a card explaining BPE, with a diagram like section 2.1 on the back.
Add mixture-of-experts routing to my future study list.
Show all my future study todos, including completed ones.
Cards support Markdown, code, tables, MathJax, and study-html blocks. Rich HTML
is rebuilt through an allowlist sanitizer before display; scripts, event handlers,
external assets, unsafe CSS, SVG, forms, and media are discarded.
make # render all three pages under dist/
make serve # rebuild on save and serve the dynamic features
make watch # rebuild on save without a server
make check # fail if any generated page is stale
make clean # remove dist/build.py has PEP 723 metadata, so there is no project environment or lockfile to
install. It can also be called directly:
uv run --script build.py [--check|--watch]- Edit prose in the relevant
sections/*.html.j2file. - Edit
site.tomlfor chapter order, navigation, IDs, and page metadata. - Reuse components from
templates/macros.html.j2for headings, callouts, code, math, ledgers, and milestones. - Edit numbered files under
styles/for presentation; their filenames define cascade order. - Run
makeor keepmake serverunning. - Review source and generated diffs, then run
make check.
The root train-your-own-llm.html is the frozen pre-split original and is not a
build input. Current content intentionally differs from it.
site.toml chapter manifest and output names
sections/ textbook chapter fragments
templates/ textbook, transcript, and study page templates
styles/ numbered CSS cascade
scripts/ chat, archive, study, and safe Markdown clients
vendor/mathjax/ vendored MathJax bundle and license
build.py self-contained Jinja renderer
serve.py localhost server, APIs, and Pi RPC bridge
study.py SQLite schema, scheduler, CRUD, and tool CLI
.pi/extensions/study-notes.ts Pi study-note tool
.pi/extensions/future-study.ts Pi future-study todo tool
chats/ user-owned transcripts and Pi sessions
study/study.sqlite3 user-owned notes, future-study todos, and review history
dist/ generated pages; never edit by hand
See AGENTS.md for coding-agent guidance and CLAUDE.md
for detailed content, component, whitespace, and design conventions.
The server binds to 127.0.0.1; do not expose it publicly. Pi has coding tools
with access to this repository. Transcript and study files are intentionally
repository-local and may contain personal or sensitive material—review them
before sharing or committing. SQLite -wal and -shm files and Pi's RPC log are
ignored.