[210K] subscribers · fact-checked by [Name, credential]
[52K] views 1 day ago #AI #ChatGPT #HowAIWorks
How it works · 5 steps · about 4 min · ●●○ Medium
"AI" covers a lot of things. This page is about the kind you talk to: chatbots such as ChatGPT, Claude and Gemini, which are large language models. They do not look answers up. They cut your words into tokens, turn each token into a long list of numbers, pass those numbers up through dozens of layers where every token takes in information from the tokens before it, and finish with a score for every possible next token. One token is picked, added to the text, and the whole pass runs again.
This model is a picture of the maths, not a machine: the real thing is billions of multiplications on GPU chips. Follow all five steps: tokens, the lists of numbers, attention inside the layers, the scores for the next token, and the loop that writes the reply one token at a time. Turn on Cutaway to open the tower, Exploded to pull the layers apart, or open What if… to turn up the temperature, ask it something it never saw, or watch how it learned.
The 5 steps
Tokens. The model does not work on letters. A tokenizer splits your text into tokens: whole common words, or pieces of rarer ones. "The cat sat on the" is five tokens, but "hippopotamus" is three: "hipp", "opot" and "amus". Each token is swapped for its ID number in a fixed list. On average a token is about three quarters of an English word.
Into numbers. The model looks up each token ID in a big table and gets a list of numbers called an embedding. Each brick in the stack is one number: orange is above zero, blue is below. Training has arranged the table so that related words get similar lists, which is why "cat" and "kitten" look alike. Then a second set of numbers is added to mark where each token sits, because word order changes the meaning.
Attention. The stacks rise through the layers. In each layer, attention gives every token a score for each token before it: how useful is that one here? The arcs show the scores for the last token, "the", and a thicker arc means a higher score. The token then mixes in information from those tokens in proportion to the scores, so its bricks change colour. A token can only draw on tokens before it, never after. Next, a feed-forward network works on each token on its own.
Next token. After the last layer, the numbers for the last token are turned into a score for every token in the vocabulary, then into chances that add up to 100%. Here "mat" gets 41%, "floor" 17% and "sofa" 12% (illustrative numbers). The sampler then picks one. It usually picks a likely token, but not always the top one, which is why the same question can get different answers.
Again and again. The picked token is added to the end of the text, and the entire stack runs again to pick the one after it: "mat", then ".", and so on until it picks a stop token. Every pass takes in all the text so far. That is why chatbot replies appear a word or so at a time. None of this is a machine with moving parts: it is billions of multiplications, run on GPU chips in a data centre.
Parts in this model
Token. A small piece of text: a common word, part of a longer word or a punctuation mark. The model takes in and puts out tokens, never letters.
Token ID. The number that stands for a token in the model’s fixed list. These are the real IDs from the tokenizer GPT-2 and GPT-3 use: "The" is 464.
Embedding. The list of numbers a token turns into, drawn as a stack of bricks: orange above zero, blue below. GPT-3 uses 12,288 numbers per token; 8 are drawn.
Position. Numbers added to each embedding to mark where the token sits, so "dog bites man" and "man bites dog" come out different.
Attention. Scores how much each earlier token matters to this one, then mixes their information in. A thicker arc means a higher score. It only ever draws on tokens before this one.
Layer. One attention step plus one feed-forward step. Five layers are drawn; GPT-3 stacks 96. Each layer adjusts every token’s numbers a little.
Feed-forward network. After attention, each token’s numbers pass through a small network of their own, inside the same layer. It holds about two thirds of the layer’s weights.
Weights. The adjustable numbers the model is made of, drawn as dials. Training sets them; using the model never changes them. GPT-3 has 175 billion.
Probabilities. A score for every possible next token, turned into chances that add up to 100%. Only the top five are drawn, and the numbers here are illustrative.
Sampler. Picks the next token from the probabilities. Its temperature setting controls how spread out the picks are: low sticks to the top scores, high spreads the picks out.
Context window. All the tokens the model takes in at once: your message, the conversation so far and its own reply. GPT-3’s window held 2,048 tokens. Anything outside it has no effect on the output.
GPU chip. This tower is a picture of the maths, not a machine. The real thing is billions of multiplications, run on graphics processing units (GPUs) in a data centre.
What if…
What if you turn up the temperature?
Temperature is a setting on the sampler. Turned up, it squeezes the scores closer together, so the bars flatten and unlikely tokens such as "roof" get picked more often. Replies become more varied, more surprising and more often wrong. Turned down to zero, it always takes the top token.
What if it never saw the answer?
Ask for something it has no information about, like this cat’s name, and it still produces scores. No name stands out, but the sampler picks one anyway and it comes out just as fluently as a real fact. The model has no list of facts to check against, only patterns in its weights. That is how made-up answers, called hallucinations, happen.
How did it learn all this?
By predicting the next token in a huge amount of text, over and over. Here it predicts "floor" where the text says "mat". The size of the miss is sent backwards through every layer, and each weight is nudged a tiny amount so that "mat" would score a little higher next time. GPT-3 trained on about 300 billion tokens this way, Llama 3 on over 15 trillion. Chat models then get more training on example conversations and on people’s ratings of answers.
[12] Questions
Pinned to step 3 · Attention
A reader asked1 day ago
What does attention actually do?
[214]
E
[Name, credential]Expert answer
[Answer by named expert.]
A reader asked1 day ago
Why does ChatGPT type its answer out word by word?