Skip to content
Module 1 · Foundations
Chapter 2 of 17
Run it in your browser

Chapter 2: What Is an LLM?

The map with Module 1 highlighted Figure 2.1: What the whole system does, before we zoom in.

With our environment set up in the previous chapter, we turn to the model itself (Figure 2.1). A Language Model (LM) seems like a magic box that understands what you type, but it performs one specific, simple task.

In this chapter you will:

  • Learn the single rule that drives all language models.
  • See how models generate long answers one step at a time.
  • Understand how reading data replaces human teachers.

Words to Know

  • Token: one small piece of text, here a single character.
  • Language Model: a system that predicts the next token in a sequence.
  • Autoregressive: building an answer one piece at a time, feeding each new piece back in before predicting the next one.
  • Parameters: the numbers inside the model that adjust during training.

Theory: The Next-Token Engine

Imagine you are texting a friend and you type: "I am so hungry, I could eat a". Before you finish, your phone suggests: horse. Or pizza.

A text box with incomplete text points to a prediction box showing horse Figure 2.2: The core task of a language model is guessing what comes next.

Your phone is doing next-word prediction (Figure 2.2), guessing what word is most likely to come next based on everything you typed so far. A Large Language Model (LLM) does the same thing. Given a sequence of tokens, it predicts what comes next. Everything else in this book builds a machine that can do this well.

How It Talks Back

If the model only predicts the next token, how does it write an essay? It predicts one token, adds it to the text, and predicts again, a loop called autoregressive generation.

  1. You type: "What is the capital of France?"
  2. The model predicts the next token: "Paris".
  3. That output is added to the input: "What is the capital of France? Paris".
  4. It predicts the next token: " is".
  5. And repeats until it decides to stop.

The output text fed back into the model as input Figure 2.3: In autoregressive generation, each predicted token is added to the input.

The model never thinks about the whole answer at once (Figure 2.3). It keeps predicting the next piece, over and over, and the sentence builds up step by step.

What is a Token?

The smallest unit that our model will work with is called a token. This could be a word or a portion of a word or just one character.

In this book we will be working with characters as our tokens. So each character including spaces and punctuation is considered a token. We choose to work with characters because our vocabulary size is very small (65 characters total in our Shakespeare dataset). And it's easy to follow what each character means. Real models will use a portion of words called subwords instead of just characters. But the math is the same.

The Power of Self-Supervised Learning

When you want to make your model "Large", you give it millions of parameters. Parameters are just values in the model that we can adjust. We train our model by adjusting these values until it can generate decent responses.

There is a type of training where you don't need a teacher to tell the model if it's right or wrong. This is known as self-supervised learning.

Because we're working with language, we have the answer already. So if we have a sentence that says "Hello world", the model gets these practice questions automatically:

  • See H, predict e.
  • See He, predict l.
  • See Hel, predict l.

Text subsets predicting the next character Figure 2.4: The text itself provides the practice questions.

Because no human labels are needed, you can train a model on millions of pages of raw text (Figure 2.4). The model reads the data and learns the patterns.

Try It: The Numbers Game

We can test next-character prediction on real data. If we see the letters "the ", what is the most likely next character in Shakespeare?

We wrote a tiny script to scan our training data and count every character that follows "the ".

Terminal
$ python src/examples/ch02_next_char.py
What follows 'the ' in Shakespeare?
's': 573 times
'w': 459 times
'c': 448 times
'p': 426 times
'm': 376 times

This is what the model learns to do, but instead of counting by hand, it uses math to work out how likely each next character is.

In Business

A company that builds an AI assistant to draft emails in its own house style uses the same idea. The model trains on the company's archive of past writing and learns what character or word typically follows another, which is how the assistant picks up the company's voice and terminology.

Watch Out

It is easy to think the model "understands" the text. All it does is calculate the most probable next token from patterns in its training data.

Key Takeaways

  • A language model predicts the next token.
  • Autoregressive generation refers to feeding the model’s own outputs back as input.
  • Tokens are segments of text; they could be characters or words.
  • We use the raw text itself as the “answer key” in self-supervised learning.
  • As you can see, everything here revolves around next-token prediction! The first stop on our map will cover the math and tools we use to create a language model.

Check Your Understanding

  1. What does a language model predict?
  2. Why is it called "autoregressive"?
  3. How does self-supervised learning differ from having a teacher grade the work?

Assignment

Assignment 2: New word, new guess

About 15 minutes. You change one word and write no new code. Work in this chapter's Colab notebook or on your own laptop.

  1. Run it. Run src/examples/ch02_next_char.py as it comes. Cut and paste the five letters and counts.
  2. Change one word. Locate the line prefix = "the ". The prefix is what the program searches for. Replace the with my and keep the space before the closing quotation mark, so that your line looks like prefix = "my ". Run the program again.
  3. Compare. What letter is first now? Which letters appear in both groups? Think of one word Shakespeare could write after my that starts with the new first letter.
  4. Explain it. In two or three sentences a manager could understand, explain why the most common next letter changed when you changed one word.

Hand in: both lists of five letters and counts, your answers to step 3, and your sentences.

Further Reading

One model, many jobs, no retraining. The researchers wanted to know if a model trained exclusively to predict the next word would learn capabilities nobody had trained it for. Because it was trained on such a broad, diverse swath of web pages, it started answering questions, summarizing, and translating with zero task-specific training when given a prompt that made those tasks obvious. That answered the architectural question of how to build a system for text generation in favor of a decoder architecture that predicts the next token. That's the model we cover in Chapter 10.

Memory, before attention. Sequential models read a sentence one word at a time and had mostly forgotten it by the end, due to the vanishing gradient as error flowed back through time steps. This work introduced a cell with gates that controlled what to remember, what to forget and what to output. Gate cells like this powered essentially all serious language applications for two decades. The cell poses the question of what the model should remember from earlier in the sentence. Attention later modeled the same concept using a different mechanism.

Hochreiter, S., & Schmidhuber, J. (1997). Long short-term memory. Neural Computation, 9(8), 1735–1780. https://doi.org/10.1162/neco.1997.9.8.1735

Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., & Sutskever, I. (2019). Language models are unsupervised multitask learners [Technical report]. OpenAI. https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf

Next up: Chapter 3: Tensors and PyTorch