Ask it something
The model runs on a public Hugging Face Space. Ask about Westeros: it searches the books, reads the best passages, then answers, and the passages are listed under the answer.
- My role
- Solo, end to end
- When
- July to August 2025, retrained and given a book search October 2026
- Built with
Python
PyTorch
tiktoken
OpenAI API
Google Colab
Kaggle
NumPy
BM25
bge-small embeddings
Reciprocal rank fusion
MiniLM cross-encoder
ONNX Runtime
FastAPI
Docker
Hugging Face Spaces
pytest
- Code and demo
- Live demoFine-tuning notebookService code
GPT-2, in one line
GPT-2 is the language model OpenAI released in 2019: a much smaller, older relative of the models behind ChatGPT. Its weights are public, so you can train it further yourself.
What I built
The whole path: books to text, text to pieces, practice questions written by a second AI model, the GPT-2 network classes, written in PyTorch while following a build-an-LLM course and loaded with OpenAI's released weights, fine-tuning in Colab and on Kaggle, a search over the five books (26,112 short passages, found by their words and by their meaning, then re-ranked), a second round of training so the model uses what the search finds, a FastAPI service on a free Hugging Face Space and a question box, the one on this page.
How it works
- The five books are read into plain text, cleaned of copyright pages and contents lists, and cut into pieces.
- A second AI model, gpt-4o-mini, writes one fact question and one full-sentence answer per piece.
- GPT-2 medium, in network code I wrote myself, is fine-tuned on book passages and question-answer pairs mixed in the same training steps.
- The books are cut again into 26,112 passages of about 128 tokens, each tagged with its book and chapter, and indexed twice: by the words in it and by its meaning. The index is private, because the books are copyrighted.
- When you ask, the service searches both indexes, a small ranking model re-reads the ten best passages, and the best three go in front of your question.
- Pasting passages in front of the model alone did not help: it ignored them. So I trained it a second time, on free Kaggle GPU time, on 19,289 questions that come with their passage.
- A small FastAPI service on a free Hugging Face Space answers in about five to twenty seconds. The answer comes back with its sources: book, chapter and a snippet of at most 25 words. The first question after a quiet spell wakes it, which takes about a minute.
5books, all of A Song of Ice and Fire so far
34,001cleaned question-answer pairs, from 61,650 first written
26,112book passages in the search, each with its book and chapter
MediumGPT-2 size, from OpenAI's released weights
Retraining changed the voice
| Measure | 2025 model | First retrain | Second retrain |
|---|---|---|---|
| Loss on 1,233 test questions | 2.734 | 2.146 | 2.119 |
| Loss on one book passage | 3.072 | 2.659 | 2.615 |
| Ends its own answer (10 questions, 3 tries) | 0 of 30 | 30 of 30 | 30 of 30 |
The book search changed the facts
| Measure | No search | Search, same model | Search, retrained (live) |
|---|---|---|---|
| Fact questions right (of 30) | 12 | 10 | 17 |
| New fact questions right (of 42) | 2 | 8 | 13 |
| Four old mistakes fixed (of 4) | 1 | 1 | 4 |
| Answers with an invented name | 36% | 28% | 12% |
The same network, two more ways
I re-wired the same GPT-2 network into a spam classifier for text messages: 48.75% accuracy before training, 84.00% after, on 300 messages it had never seen. My build notebooks go from the tokenizer to fine-tuning, one step at a time.
Try the spam classifier
Paste a text message, or tap an example. The model answers with one word.


