Unit 7 · Machine Learning and Artificial Intelligence
7-3 Large Language Models (LLM) and Generative AI
When a chatbot writes a fluent paragraph, it is not looking up an answer — it is guessing the next word, then the next, using patterns learned from enormous amounts of text. That simple trick, scaled up to billions of parameters, gives large language models their power to translate, summarize and converse. But because each word is chosen for being statistically plausible rather than certainly true, these models can state falsehoods convincingly — a failure called hallucination.
Guiding question
How does a language model generate text, what makes a model “large”, and why can it still get facts wrong?
Learning outcome. Explain how a language model generates text by predicting the next word and why this causes hallucination, and describe what makes a language model large and the roles of RLHF tuning and multimodality.
1 · What a language model is
A language model is a model created to learn from large amounts of text and predict which word is likely to come next in a sentence. Through training it acquires patterns of word usage and phrasing according to context.
2 · Text generation is that, repeated
Text generation is achieved by repeating the prediction of the next word. The book’s own worked chain:
| Step | Input sentence | Word predicted next |
|---|---|---|
| 1st | “Today’s weather is” | “sunny” |
| 2nd | “Today’s weather is sunny” | “and” |
| 3rd | “Today’s weather is sunny and” | “warm” |
In this way, a complete sentence is built up by connecting the results of predicting one word at a time.
3 · How the word is chosen
For each candidate word, the language model calculates a “probability of coming next” and selects words based on that probability. These probabilities come from patterns the model learned during training, so it can also handle wordings that never appeared in the training data.
4 · And why that causes hallucination
What a language model selects is not the correct word, but a statistically plausible word. Because of this property, hallucination — the phenomenon of returning responses that differ from the facts but are stated in a plausible way — occurs.
Write a sentence one word at a time, and watch what happens
A language model writes by choosing one word at a time. At each step it has candidates and a probability for each. Pick a word — or let it take the likeliest one.
The tallest building in Cairo is ▌
The percentages here are made up for the demonstration. What is not made up is the mechanism: a real model does exactly this, over a far larger vocabulary, at every word.
1 · What LLM means
A large language model (LLM) is a language model based on a very large neural network, trained using vast amounts of text from the Internet.
2 · Two kinds of “large”
| Aspect | Description |
|---|---|
| Amount of training data | Trained using vast amounts of text containing billions to trillions of words |
| Number of parameters | The numerical values corresponding to the weights of the neural network number in the billions to trillions |
Read the second row against lesson 7-2. A parameter is a weight — the same thing you dragged along a slider. There are simply billions of them.
3 · What scale brought
Researchers have observed that when the number of parameters or the amount of training data exceeds a certain scale, qualitatively new abilities begin to appear: handling complex reasoning; summarizing text; translating into foreign languages; generating program code.
Major services built on large language models include ChatGPT, Claude and Gemini, which provide natural conversation and a wide range of text generation.
You have already driven the generator by choosing words. Now watch it run with nobody driving — because the part that matters is what happens between the words.
Step 1 of 7 — The model has only the words so far
A question was asked, and this is what has been written up to now. The model has no plan and no finished sentence waiting to be revealed. All it has is this text.
Two things to carry away. The candidate list is rebuilt from nothing at every step — the model is never working towards a sentence it already has. And the probabilities score how well a word fits, never whether it is true, which is why the book says what is selected is “not the correct word, but a statistically plausible word”.
Each percentage is the model’s estimate of how likely that word is to come next — given only the text written so far. Not how true it is. Not how good it is. How well it fits.
Why they add to 100%. They are shares of one certainty: something must come next, so the whole of that certainty is divided between the candidates. This is a useful check on any probability question — a set that does not total 1 has something wrong or missing.
Why the list is rebuilt every time. The model is not working towards a sentence it has already decided on. At each step it has the text so far and nothing else, so a brand new set of candidates is scored from scratch. That is why the same question asked twice can produce two different answers.
And at the year: 38%, 31% and 19% went to three different years. Those three numbers measure how well each fits the sentence. None of them measures whether the tower opened that year, because nothing in the loop has any way to find that out.
Read the book’s own definition again, because it is easy to skim past: a parameter is a weight. Not something new. It is the same kind of number you met twice already:
- • in lesson 1-2, the four importance scores that decided whether an email was spam — 4 weights;
- • in lesson 7-2, the four numbers you multiplied by hand inside one neuron — 4 weights;
- • here, in a large language model — billions to trillions of weights.
Nothing about the kind of number has changed between those three. Only how many there are. That is genuinely the whole difference, and it is why the book says “large” refers to size in two senses — the training data, and the parameter count.
The book’s wording, which is what an exam wants: parameter — a weight; LLMs have billions to trillions. (KEY TERMS, p.79)
🧮 How big is “a billion parameters”, really?
- 1
Suppose you tried to write the parameters out by hand, one number every second, never stopping to eat or sleep.
Why this step: “Billions” is a word students read straight past. Turning a count into a length of time is the quickest honest way to feel a number that is too big to picture.
- 2
One billion seconds ÷ (60 × 60 × 24 × 365.25) ≈ 31.7 years.
Why this step: Dividing by the number of seconds in a year converts a count of numbers into a human span. A single billion — the very smallest model the book calls “large” — would take you most of a working life.
Most people slip here: A billion is a thousand million, not a hundred million. Getting that wrong shrinks the answer to about 3 years and destroys the point.
- 3
And one trillion seconds ≈ 31,688 years.
Why this step: The book’s upper figure is trillions, so this is not an exaggeration for effect. Writing out the parameters of the largest models would have had to start in the Stone Age.
- 4
Conclude: nobody has read these numbers, and nobody ever will.
Why this step: This is the honest reason for the black-box problem from lesson 7-1, and it is worth stating in an exam. It is not that companies keep the numbers secret. It is that there are too many of them, each one meaningless on its own, for reading them to tell anybody anything.
🧮 How the next word is actually picked — and why hallucination is built in
- 1
The model takes the text so far and produces a probability for each candidate next word. In the demonstration above, step 1 gives: “the” 0.52, “called” 0.24, “a” 0.16, “located” 0.08.
Why this step: The book says on p.79 that the model “calculates a probability of coming next” for each candidate. It does not pick a word and then justify it — it scores every candidate first.
- 2
Check the four probabilities: 0.52 + 0.24 + 0.16 + 0.08 = 1.00
Why this step: They must add to exactly 1, because they are shares of one certainty: something has to come next. This is a useful check on any probability question — if a set of probabilities does not total 1, one of them is wrong or one is missing.
Most people slip here: Adding to 1 does not mean the words are equally likely, and it says nothing at all about whether any of them is TRUE. It only means the model has divided its confidence between them.
- 3
Choose a word using those probabilities, then add it to the sentence and start again.
Why this step: This is why the book says text is generated by “repeating the prediction of the next word”. It is also why asking a chatbot the same question twice can give two different answers: it selects based on the probabilities rather than always taking the top one.
- 4
Now look at step 6 of the demonstration: “in 2019” 0.38, “in 2021” 0.31, “in 2017” 0.19.
Why this step: This is the moment the whole lesson turns on. All three are year-shaped and all three fit the sentence beautifully. The model is not choosing between a true year and a false one — it has no idea which is which. It is choosing between three shapes that all fit.
Most people slip here: Look for a fact-checking step anywhere in these four lines. There isn’t one. Not a missing one — there is nowhere in this process it could go.
- 5
Conclude: the model selects what is statistically plausible, not what is true.
Why this step: That is the book’s own sentence on p.79, and it now has a reason behind it rather than being a warning to memorise. Hallucination is not a bug that a better version will remove; it is what next-word prediction does when a plausible-looking wrong answer scores well.
Where the comparison breaks down: A person who bluffs knows they are bluffing. A language model has no sense of the difference — it is doing exactly the same thing when it is right as when it is wrong. So you cannot catch it by listening for hesitation, which is how you would catch a person. There is none.
This is the exam-style answer to the book’s Think It Through on hallucination made concrete: the danger is not that the output looks wrong, it is that it looks exactly as right as a correct answer would. Which is also why the honest rule for your own study is to use these tools to explain things to you, and to check any fact, date or figure against your book before you write it down.
A widely reported 2023 case in a New York federal court, in which the lawyers involved were fined.
1 · The problem RLHF solves
Just by learning from vast amounts of text, large language models do not necessarily return responses that are desirable for humans. They may answer without grasping the intent of the question, or return responses containing inappropriate content.
A representative method for solving such problems is RLHF (Reinforcement Learning from Human Feedback) — a method that applies reinforcement learning to the tuning of language models.
Three stages — and one thing they never do
Just learning from vast amounts of text does not make a model return responses that are desirable to humans. It may answer without grasping the intent of the question, or return inappropriate content. RLHF is the fix. Step through it.
2 · What it achieves
Through RLHF, language models begin to return natural and polite responses, responses aligned with the intent of the question, and responses that avoid inappropriate content. The fact that generative AIs such as ChatGPT feel “easy to use” owes much to such tuning.
1 · One model, many kinds of data
Multimodality is the property of being able to handle multiple kinds of data with a single model — text, images, audio and video. Each kind of data is called a modality.
2 · What it makes possible
Early language models handled only text, but recent generative AIs can handle different kinds of data in combination: looking at an image and describing its content in text; listening to audio and transcribing it into text; giving instructions in text and generating an image; summarizing the content of a video.
3 · How it is possible at all
The background is that neural networks have become able to handle different kinds of data by converting them into a common numerical representation. Inside the network, text, images and audio are all processed in the same way, as sets of numerical values.
Through multimodality, generative AI is evolving from a technology that handles only language into one that integrally handles the various kinds of information humans deal with daily.
How the real paper is built
The ministry sets half multiple choice and half written answer in every subject. The two halves are not the same skill. In the multiple-choice half you have to spot the wrong options quickly — and the wrong options are built out of the mistakes students really make. In the written half nobody gives you the answer to recognise; you have to produce it, in your own words, with a reason.
Practise both. Being good at one half and weak at the other costs you exactly half the paper.
- language model
- — predicts the likely next word.
- hallucination
- — a plausible answer that is factually wrong.
- large language model (LLM)
- — a huge network trained on vast text.
- parameter
- — a weight; LLMs have billions to trillions.
- RLHF
- — tuning by human feedback used as reward.
- multimodality
- — one model handling many kinds of data.
- modality
- — one kind of data — text, image, audio.
- generative AI
- — AI that creates new text, images or audio.
- fact-check
- — verifying a claim against a reliable source.
Worked Example
(1) Mark each statement with ○ if it is true or × if it is false.
A A language model is a model that predicts which word is likely to come next in a sentence.
B A language model finishes processing after predicting just one word and does not generate text.
C In a large language model, when the number of parameters or the amount of training data exceeds a certain scale, qualitatively new abilities sometimes appear.
D Multimodality is the property of handling only text.
(2) Match each description with a term: A Hallucination, B Large language model, C RLHF.
a A language model based on a very large neural network trained using vast amounts of text from the Internet
b A method that uses human feedback as a reward to tune a language model to return more desirable responses
c The phenomenon in which a language model returns responses that differ from the facts in a plausible way
1 · Answer the following questions.
2(1) · Which most appropriately describes how a language model works?
Mark ○ for the correct description, and × for the ones that describe something else entirely.
It memorizes the input sentence as is and returns the same sentence.
It generates text by repeatedly predicting the next word in the sentence.
It selects and returns the closest match from prepared template sentences.
It fully understands the meaning of the sentence and returns the single correct response.
2(2) · Which most appropriately describes the cause of hallucination?
Mark ○ for the real cause, and × for the others.
Because the language model always retrieves and returns sentences from the training data as is.
Because the language model selects and returns words that are statistically plausible rather than correct.
Because the language model avoids answering questions and returns unrelated text.
Because the language model is built to return only fact-based responses.
2(3) · Which most appropriately describes RLHF?
Mark ○ for the correct description, and × for the others.
A method of building a language model from scratch using a large amount of text.
A method of tuning a language model to return more desirable responses, using human evaluations as rewards.
A method that removes the neural network structure from a language model and simplifies the processing.
A method that has a language model solve math problems to improve its computational accuracy.
2(4) · Which is NOT an appropriate application of multimodal AI?
Mark ○ for the real applications, and × for the one that is not.
Showing an image and having it described in text.
Listening to audio and transcribing it into text.
Giving instructions in text and generating an image.
Computing the result of input numerical values using only the rules of the four arithmetic operations and returning it.
Think as an Engineer — Investigate & Decide
A school club wants to use a generative-AI chatbot to help write short articles about local history for the school website. You advise on using it responsibly.
1 · Decide where it helps.
Name one part of the task where the chatbot genuinely saves effort, and one part where its output must not be trusted as it is. Explain each in one line.2 · Analyze the risk.
Using how a language model chooses words, explain why the chatbot might state a wrong date or an invented name for a local landmark very confidently, and describe exactly how the club would catch such a hallucination before publishing — a check the lesson does not spell out for you.3 · Decide the safeguards.
State the rule the club should follow for every AI-written article before it goes live, and say what role RLHF-style tuning does and does not solve here. In pairs, compare rules and agree on the one check you would never skip.
In a New Context
A student uploads a photo of a handwritten note and asks an AI assistant to read it aloud and then summarize it in text. Name the property that lets one model move between image, text and audio, and explain in one line how the network can handle all three.
Lesson Question — Answered
A language model learns from large amounts of text to predict the likely next word, and generates text by repeating that prediction, connecting one word at a time into a whole sentence. It chooses each word by a probability of coming next — the statistically plausible word, not the guaranteed-correct one — which is exactly why hallucination (fluent but false responses) can occur.
A large language model scales this up: trained on vast Internet text with billions to trillions of parameters, and beyond a certain scale it gains qualitatively new abilities such as reasoning, summarizing, translating and generating code. ChatGPT, Claude and Gemini are examples.
Because raw training does not guarantee helpful answers, RLHF tunes the model with human evaluations as rewards, making responses natural, on-intent and safer. And multimodality lets a single model handle text, images and audio together by converting them all into a common numerical representation.
The lasting caution: fluency is not truth, so outputs must still be checked.
2 · Read the passage and fill in the blanks (a)–(f).
A model that predicts which word is likely to come next in a sentence is called a ( a ). Text generation is achieved by repeating the prediction of the ( b ). A language model based on a very large neural network trained using vast amounts of text from the Internet is called a ( c ); when the number of ( d ) or the amount of training data exceeds a certain scale, qualitatively new abilities sometimes appear. Since just learning from vast text does not necessarily lead to responses desirable for humans, ( e ), a method of tuning by reinforcement learning using human evaluations as rewards, is used. Also, the property of being able to handle multiple kinds of data — text, images, audio, video and so on — with a single model is called ( f ).
3(1) · Which most appropriately describes the mechanism of text generation?
Mark ○ for the correct description, and × for the others.
Text is generated by connecting the results of repeatedly predicting the next word.
Text is generated by selecting from thousands of prepared templates.
Text is generated by first deciding the length of the sentence and then deciding all words simultaneously.
Text is generated by machine-translating the input sentence.
3(2) · Which most appropriately describes the background to LLMs acquiring new abilities?
Mark ○ for the correct statement, and × for the ones that get the direction backwards.
Because reducing the number of parameters below a certain level simplified the processing.
Because limiting training data to a small amount of high-quality text raised quality.
Because qualitatively new abilities appear when the number of parameters or the amount of training data exceeds a certain scale.
Because humans wrote down the rules of language one by one and taught them to the model.
3(3) · Which most appropriately describes the purpose for which RLHF is performed?
Mark ○ for the correct purpose, and × for the others.
To increase the number of words a language model can handle.
To make a language model’s responses natural and aligned with the intent of the question.
To enable a language model to handle data other than text.
To reduce the number of parameters used in a language model’s calculations.
Key takeaway
A language model generates text by predicting the next word — plausible, not always true, so hallucination happens. LLMs use vast data and billions to trillions of parameters; RLHF tunes them by human feedback; multimodality unifies text, image and audio. Fluency ≠ truth — always verify.
Each of you take the generator and choose a different word at the first step. Compare the two sentences you end up with. Both were built the same way, by the same rules, and only one of them can be true — which is the entire argument of this lesson in one experiment.
Sign In to Keep Going
You've reached the end of the free preview. Sign in — and upgrade to Plus or Pro — to keep going with Programming and Artificial Intelligence.
Full Access
Unlock all 30 lessons