LLMs Β· how ChatGPT works
Guess the next word
π School maths Β· Years 7β8Probability β Pick outcomes by how often they occurred (frequency).Maths by year β
The Case. ChatGPT looks like magic, but its one trick is guessing the next word β again and again. Let's build a baby version.
Your tools. Plain Python learns which words tend to follow which, then chains guesses into a sentence.
Investigate. Press Run. Your tiny model writes a sentence by guessing one word at a time.
- Run it a few times β it wanders differently each time, like a real chatbot.
- Change the
textit learns from and it starts ‘talking’ about that instead.
your_code.py
Starting Pythonβ¦
The Reveal
Starting Pythonβ¦ (first time only β a few seconds)
ποΈ Detective's notes
Bundles your findings, chart and code into one PDF you can keep, print or hand in.
π§© How this code works
The tools it uses
- random β makes lucky, unpredictable choices β like rolling a dice.
What this one does
- β’ It uses chance and randomness.
- β’ It repeats a step many times with a loop.
How to read it, top to bottom
- 1. Import β bring in the tools (listed above).
- 2. The data β the lists and numbers near the top. These are the bits you change.
- 3. The work β a little maths, or a loop that repeats a step.
- 4. Show it β
print(...)writes words;plt.show()draws the picture.
Use it for your own problems
- Change the numbers at the top and press Run β nothing breaks, so experiment!
- Ask your own version of the question β swap in your family, your scores, your week.
- The same tools work on any numbers β that's the superpower you're learning.
Need a hint?
nxt['the'] is the list of words that came after 'the'. The model just picks one of them.