jbn/llm-textbook

★ 0Forks 0JinjaGitHub ↗Compare

README

One GPU, One Month

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.

Quick start

Requirements:

  • Python 3
  • uv
  • Pi, authenticated with access to the configured models
make serve

Then 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.

What is included

Compiled textbook

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.

Scroll-aware Pi tutor

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.

Saved transcripts

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.

Spaced repetition

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.

Build commands

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]

Editing the textbook

  1. Edit prose in the relevant sections/*.html.j2 file.
  2. Edit site.toml for chapter order, navigation, IDs, and page metadata.
  3. Reuse components from templates/macros.html.j2 for headings, callouts, code, math, ledgers, and milestones.
  4. Edit numbered files under styles/ for presentation; their filenames define cascade order.
  5. Run make or keep make serve running.
  6. 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.

Repository map

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.

Data and security

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.

Contributors

jbn

Issues