How AI Language Models Work ยท Inside a Language Model

Predicting the Next Word

A language model guesses the next word, again and again. Build a tiny one by counting which word follows which, then let it write a sentence.

next_word.py

# A language model's one job: given the words so far, guess the next word.
# This tiny model learns by counting which word follows which in its training text.
text = "i like tea . i like cake . i want tea ."
words = text.split(" ")

follows = {}
for a, b in zip(words, words[1:]):
    follows.setdefault(a, []).append(b)

# After "i", what came next, and how often?
seen = follows["i"]
options = list(dict.fromkeys(seen))
chances = [round(seen.count(w) / len(seen), 2) for w in options]
print(options, chances)

# Generate: start with "i", then keep adding the most likely next word.
sentence = ["i"]
for _ in range(3):
    nexts = follows[sentence[-1]]
    best = max(nexts, key=nexts.count)
    sentence.append(best)
print(" ".join(sentence))

Step by step

  1. The training text: three short sentences. "." marks the end of a sentence.
  2. Split into words.
  3. follows will map each word to the list of words that came after it.
  4. Walk through every pair of neighbouring words: (i, like), (like, tea), (tea, .), ...
  5. After "i" came "like": add it to i's list. setdefault creates the list the first time.
  6. a changes from 'i' to 'like'. b changes from 'like' to 'tea'.
  7. After "like" came "tea".
  8. a changes from 'like' to 'tea'. b changes from 'tea' to '.'.
  9. After "tea" came ".".
  10. a changes from 'tea' to '.'. b changes from '.' to 'i'.
  11. After "." came "i": a new sentence started.
  12. a changes from '.' to 'i'. b changes from 'i' to 'like'.
  13. "i" again, followed by "like" again. i's list is now [like, like].
  14. a changes from 'i' to 'like'. b changes from 'like' to 'cake'.
  15. "like" was followed by "cake" this time.
  16. a changes from 'like' to 'cake'. b changes from 'cake' to '.'.
  17. A new key cake with the value a list is added to the dict that follows points to.
  18. a changes from 'cake' to '.'. b changes from '.' to 'i'.
  19. 'i' is added to the end of the list object.
  20. a changes from '.' to 'i'. b changes from 'i' to 'want'.
  21. "i" is followed by "want": i's list is [like, like, want].
  22. a changes from 'i' to 'want'. b changes from 'want' to 'tea'.
  23. A new key want with the value a list is added to the dict that follows points to.
  24. a changes from 'want' to 'tea'. b changes from 'tea' to '.'.
  25. '.' is added to the end of the list object.
  26. Every pair has been counted.
  27. Everything that came after "i".
  28. The different options, each once: like and want.
  29. "like" came 2 times out of 3, "want" 1 time out of 3. Those are the chances.
  30. It prints ['like', 'want'] [0.67, 0.33].
  31. Now generate. Start the sentence with "i".
  32. _ is created with the value 0.
  33. What came after the last word, "i"? [like, like, want].
  34. Pick the one seen most often: "like".
  35. Add it. The sentence is "i like".
  36. _ changes from 0 to 1.
  37. After "like": [tea, cake].
  38. A tie, one each. max keeps the first one, "tea".
  39. "i like tea".
  40. _ changes from 1 to 2.
  41. After "tea": [., .].
  42. The end of a sentence.
  43. '.' is added to the end of the list that sentence points to.
  44. Python moves to the next item of the loop: for _ in range(3).
  45. "i like tea ." A sentence the model wrote one word at a time, using nothing but counts. A large language model does exactly this, guessing one token at a time, just with a far better guesser.