cat /en/the-cricket-answers/
MDN-121:39
SERIES Inside the Puppet · Chapter 15

The Cricket answers

· Artificial Intelligence · 7 min read

The GPT of Workshop C has read all of Collodi and can continue a text. But it does not answer a question: it continues it, as it would continue any sentence. Chapter 15 of the book turns it into an assistant with three ingredients. A fixed format, “Domanda: … Risposta: …” (Question: … Answer: …). A new token, ⟨fine⟩ (end), which the model learns to write when it has finished. And a masked loss: during training it counts only on the characters of the answer, because the model must not learn the question, only read it.

The data are Le domande del Grillo (The Cricket’s questions), written for the book: 60 facts from Pinocchio, each with three questions. The model is trained on 106 pairs; then it is tested on 48 questions that ask about the same facts in other words and on 36 questions about facts it has never seen in this format.

DEMO The Cricket answers · about this demo · full screen ↗

The book’s assistant runs in the page. Choose a question from the three groups, or write one of your own, and watch it answer one character at a time, in greedy mode, up to the end token. Below the answer is the verdict, with the same criterion as the book’s report card: correct, from another fact (the answer, word for word, of another question) or invented. On the right are the full report card and the exact text the model reads. To fit in a web page, the weights are quantized to 8 bits, as we will see in chapter 16: the answers are identical to those of the original model.

Three experiments

1. A question it has seen. Press “1”: “Chi è Pinocchio?” (Who is Pinocchio?). The answer is perfect, “È un burattino di legno, fatto da Geppetto.” (He is a wooden puppet, made by Geppetto.), followed by the end token. On the 106 training questions the model answers correctly 100% of the time. It looks like a success.

2. The same, in other words. Press “2”: “Pinocchio è un bambino vero?” (Is Pinocchio a real boy?) asks about the same fact. The answer is “È il burattino di legno, fatto da Geppetto.” (He is the wooden puppet, made by Geppetto.): one word is different, and by the exact criterion it is invented. Try other rephrased questions: in 79% of cases the model replies with the answer of another question, word for word, and only in 6% with the correct one. It has learnt 106 answers by heart and, faced with new words, recites one that looks similar.

3. A new fact. Press “3”: “Chi ha costruito il burattino?” (Who built the puppet?). The model answers confidently “È il burattinaio, con una gran barba nera lunga fino in terra.” (He is the puppet master, with a great black beard reaching down to the ground.): it is the answer to “Chi è Mangiafoco?” (Who is Mangiafoco?). On questions about new facts it gets none right, and 86% of the time it recites the answer of another fact. It never says “non lo so” (I don’t know), because nobody has taught it to. That will be the problem of chapter 17.

The model has learnt the format and to stop, but not to use what it has read in Collodi. With 2.7 million parameters and a single book, knowledge and the way of expressing it remain separate.

The same experiment in Python

The code downloads from the book’s repository the assistant, the starting model, the vocabulary and the questions, and uses the tools in grillo.py to repeat the three experiments and the report card.

!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/minigpt.py
!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/grillo.py
!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/modelli/vocabolario_collodi.json
!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/modelli/minigpt_collodi_pinocchio.pt
!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/modelli/assistente_grillo.pt
!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/dati/grillo/domande_grillo.jsonl

import torch
from grillo import base, gruppi, allarga, rispondi, pagella    # the tools of chapter 15

assistente = allarga(base)                                      # one more token: ⟨fine⟩
assistente.load_state_dict(torch.load("assistente_grillo.pt", map_location="cpu"))
assistente.eval()

# 1, 2 and 3: a question it has seen, the same in other words, a new fact
for domanda in ["Chi è Pinocchio?", "Pinocchio è un bambino vero?", "Chi ha costruito il burattino?"]:
    print(domanda, "->", rispondi(assistente, domanda)[0])
# Chi è Pinocchio? -> È un burattino di legno, fatto da Geppetto.
# Pinocchio è un bambino vero? -> È il burattino di legno, fatto da Geppetto.
# Chi ha costruito il burattino? -> È il burattinaio, con una gran barba nera lunga fino in terra.

# The report card on the three groups (a few minutes without a GPU)
for g in ["addestramento", "riformulata", "nuovo"]:
    print(g, pagella(assistente, gruppi[g]))
# addestramento {'giusta': 1.0, 'di un altro fatto': 0.0, 'inventata': 0.0, 'fermata': 1.0}
# riformulata {'giusta': 0.06, 'di un altro fatto': 0.79, 'inventata': 0.15, 'fermata': 1.0}
# nuovo {'giusta': 0.0, 'di un altro fatto': 0.86, 'inventata': 0.14, 'fermata': 1.0}

In the book

This lab accompanies chapter 15, “From model to assistant”. The chapter builds Le domande del Grillo, adds the end token by enlarging the vocabulary, introduces the masked loss and compares training with and without the mask, and shows why a low loss on the answers does not guarantee correct answers.

COVER COMING SOON
dltp · 2026

Deep Learning. Teoria, matematica e pratica delle reti neurali

Michele di Nuzzo · in Italian

Over the course of the book we build a small language model, a GPT, from scratch and grow it one piece at a time. At the start it is a model that counts which letter follows which. At the end it is a complete Transformer that writes dialogue, answers questions, reads images and draws the digits we ask for in words.

[the book’s page] [shop links coming soon]

The “Inside the Puppet” series