Find a course

Module 3: Generative AILesson 6 of 1112 min

How Chatbots Work

Learn how large language models turn text into tokens, predict the next token, and are trained and fine-tuned, and why they can sound sure while being wrong.

Step 1 of 12Learn

The world's biggest autocomplete

Your phone keyboard suggests the next word as you type. After "See you," it might offer "tomorrow" or "later." It learned those suggestions from patterns in lots of text. A chatbot is built on a similar core idea, scaled up enormously.

The engine inside a chatbot is a large language model (LLM): a huge neural network trained on a vast amount of text from books, websites, articles and code. Its basic skill is predicting what comes next. Given some text, it estimates which continuation is most likely, adds a piece, then repeats, building an answer one step at a time.

It doesn't work with whole words exactly, but with tokens: chunks of text that might be a whole word, part of a word or a punctuation mark. "Unbelievable" might be split into "un," "believ" and "able." In English, a token averages roughly three quarters of a word.

When you ask, "What's the capital of France?", the model has seen countless texts where questions like that are followed by "Paris," so "Paris" is an extremely likely continuation. When you ask it to write a poem about your dog, no exact match exists, but it combines patterns about poems, dogs and your details into new text.

This prediction process is why chatbots are so fluent. It's also the root of their biggest weakness, which you'll meet shortly.

Step 2 of 12Tap to reveal

One token at a time (illustrative probabilities)

  1. Prompt

    "The capital of France is"

  2. Model's guesses

    " Paris" 92%, " a" 3%, " located" 2%, everything else 3%.

  3. Pick and append

    It picks " Paris", so the text becomes "The capital of France is Paris".

  4. Repeat

    Now it predicts the next token again: "." is most likely. The loop continues until the answer is done.

  5. Key point

    Nothing was looked up. The answer came from patterns learned in training, which is why it is usually right for common facts and less reliable for rare ones.

Step 3 of 12Practice · Fill in the blanks

Fill in the blanks

A large language model generates text by repeatedly predicting the next , a chunk of text that may be a word or part of a word.

Step 4 of 12Learn

How a chatbot is trained

Building a modern chatbot usually happens in stages.

1. Pretraining. The model reads an enormous amount of text and practices one task over and over: predict the next token, check the real one, adjust its billions of weights. Through this, it absorbs grammar, facts, writing styles, reasoning patterns and also the errors and biases present in its text. Pretraining takes huge amounts of computing power, energy and time.

2. Fine-tuning. A pretrained model is good at continuing text, but not necessarily at being a helpful assistant. So it's trained further on examples of good conversations: questions paired with high-quality answers.

3. Learning from human feedback. People compare different answers from the model and rate which is more helpful, honest and safe. The model is adjusted to produce more answers like the preferred ones. This is a form of reinforcement learning, and it's also how developers teach chatbots to refuse harmful requests.

Two consequences matter for you:

  • A model's built-in knowledge has a cutoff: it only knows about the world up to when its training data was collected. Some chatbots can also search the web or read files you give them, which helps with recent information, but the model itself doesn't automatically know today's news.
  • The model isn't a database of facts. Its knowledge is spread across its weights as patterns, which is powerful for language and unreliable for exact details.

Step 5 of 12Compare

Three stages of building a chatbot

AspectPretrainingFine-tuningHuman feedback
DataA vast amount of textExample conversations with good answersPeople ranking pairs of answers
LearnsLanguage, facts, styles, and also errors and biasesHow to act like a helpful assistantWhich answers are more helpful, honest and safe
CostHuge computing power and energyMuch smallerMany hours of human rating

Step 6 of 12Practice · Put in order

Put the stages of building a typical chatbot in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Collect a huge amount of text
  2. Adjust it with human ratings of its answers
  3. Fine-tune it on examples of helpful conversations
  4. Release it and keep monitoring its behavior
  5. Pretrain the model to predict the next token

Step 7 of 12Learn

Confident and wrong

Because a language model produces likely-sounding text rather than looking up verified facts, it can generate statements that are fluent, confident and false. This is often called hallucination.

Typical examples:

  • Inventing a quote and attributing it to a real person.
  • Citing a book or research paper that doesn't exist, complete with author, title and page numbers.
  • Getting a date, a statistic or a step in a math problem wrong while explaining it smoothly.
  • Describing a small local event or a rare topic with made-up details, because the training data had little about it.

Why does this happen? The model's goal during training was to produce text that fits, and a confident-sounding answer usually fits better than "I don't know." Developers have made models much better at admitting uncertainty, and tools that let models search the web or check sources reduce errors, but no chatbot is fully reliable.

Chatbots also vary their answers. Ask the same question twice and you may get different wording, or even different facts, because the model picks among likely tokens with some randomness, which keeps answers from being repetitive. That variety is great for brainstorming and a reason for caution with facts.

The practical rule: treat a chatbot like a fast, well-read helper who sometimes makes things up. Useful for explaining, drafting and brainstorming; never the final word on facts that matter.

Step 8 of 12Flashcards

Hallucinations and how to catch them

Tap a card to flip it.

Step 9 of 12Note

Step 10 of 12Practice · Sort into groups

How reliable is a chatbot likely to be for each task, used on its own without checking?

Tap an item, then tap its group (or drag it there).

Step 11 of 12Practice · True or false

If a chatbot gives a citation with an author, title and page number, the source must exist.

Step 12 of 12Practice · Choose one

Why might a chatbot not know about an event that happened last week?