What a Chatbot Really Does When It Answers You

4 min read

Here is the simplest way I know to think about a chatbot. Imagine a short movie script where someone talks to an AI assistant, except the assistant's replies have been torn off. Now imagine a machine that can look at any text and guess what word comes next. Feed it the script, let it guess the first word of the missing reply, add that word to the script, and repeat. That is what happens every time you chat with a bot. Underneath the friendly interface, a large language model (LLM) is doing one job: predicting the next word.

It does not pick one word with certainty. It gives a probability to every possible next word. And it does not always take the most likely one. If it did, every answer would be dull and identical. Instead it samples, picking less likely words at random along the way. The model itself is deterministic, but the sampling makes the same prompt give a different answer each time you run it. That randomness is a feature, not a bug. It is what makes the output read like a person instead of a lookup table.

So how does it learn to guess so well? By reading an enormous amount of text from the internet. For GPT-3, a person reading non-stop would need over 2600 years to get through the training text. Newer models train on much more. You can think of training as tuning dials on a giant machine. The model's behaviour is set by hundreds of billions of continuous values called parameters, or weights. Nobody sets them by hand. They start random, so the model outputs gibberish, and an algorithm called backpropagation nudges them, example after example, to make the true next word a little more likely and all the others a little less. After trillions of examples, the model starts guessing well on text it has never seen.

The scale is hard to picture. If you could do a billion additions and multiplications every single second, training the largest models would still take you well over 100 million years. It only works because GPUs do huge numbers of operations in parallel.

That whole process is called pre-training, and it only teaches the model to complete text. A chatbot needs a second stage, reinforcement learning with human feedback. Workers flag unhelpful or bad predictions, and those corrections shift the parameters toward answers people prefer. Pre-training builds the brain. The feedback stage teaches it manners.

Then there is the transformer, the design that made all of this practical. Before 2017, most models read text one word at a time. Transformers soak it all in at once, in parallel. The first step turns each word into a long list of numbers, a vector that encodes its meaning. Then attention lets those vectors talk to each other and refine what they mean based on context. The word bank, for example, can shift toward riverbank when the surrounding words are about rivers. A feed-forward network adds extra capacity to store patterns. The data flows through many rounds of these two operations, and at the end, the last vector produces a probability for every possible next word.

What strikes me most is that nobody programmed the specific behaviour. The researchers designed the framework, but the behaviour emerges from how the parameters got tuned. That is why it is so hard to say exactly why a model gives a particular answer. The words it produces are fluent and often useful, and the mechanism behind them is a next-word guesser trained at a scale no human can picture.

Try this

bash
pip install transformers
python -c "import transformers; print(transformers.__version__)"

Sources

Source: Large Language Models explained briefly - 3Blue1Brown