Skip to content
Pixel
العربية

7-3Unit 7 · Machine Learning and Artificial Intelligence

Large Language Models (LLM) and Generative AI

13 min readStart exercises
In this lesson
  1. The Big Idea
  2. Language Models and Predicting the Next Token
  3. Hallucination: When Fluency Misleads
  4. Large Language Models (LLMs): What Does "Large" Mean?
  5. Tuning with RLHF
  6. Multimodality
  7. Responsible Use: Check Before You Publish
  8. Try It Yourself
  9. Common Mistakes
  10. Key Points
  11. FAQ

The Big Idea

When a chat system writes you a whole paragraph, the language model behind it is doing something that sounds simple: it works out the probabilities of the next token given the context, picks one, adds it, and repeats. That explains its strength and its weakness at once: the text comes out fluent and convincing, but the prediction goal itself does not guarantee the information is true, so hallucinations happen. Large language models scale this idea up with more parameters, data and computing; tuning methods such as RLHF improve how well they follow instructions; and some multimodal systems also handle images and audio. The golden rule: fluency is not truth, so checking is essential.

Language Models and Predicting the Next Token

A language model is a model that learns the patterns in sequences of tokens in data. A generative language model can estimate the probability distribution of the next token based on the context.

What is a token?

A token is the unit of text the model works with. It can be a whole word, part of a word or a punctuation mark, depending on how the text is split. A long word may be broken into several tokens. That is why the Arabic edition speaks of the "next token" rather than the "next word".

How is text built step by step?

Text is generated by adding one token after another and updating the context at each step, until the system reaches a stop point or the set length:

| Step | Input context | Next token predicted | |---|---|---| | 1st | "Today's weather is" | "sunny" | | 2nd | "Today's weather is sunny" | "and" | | 3rd | "Today's weather is sunny and" | "warm" |

Generating text one token at a time Three steps. In each step the context goes into the model, which computes next-token probabilities, shown as bars for three candidate tokens with the longest one chosen. Step 1: context "Today's weather is"; candidates sunny 0.46, hot 0.31, rainy 0.08; sunny is chosen. Step 2: context "…weather is sunny"; candidates and 0.52, full stop 0.30, today 0.12; and is chosen. Step 3: context "…is sunny and"; candidates warm 0.44, mild 0.28, hot 0.17; warm is chosen. Result: Today's weather is sunny and warm. A red warning: the model picks what is likely in context, not what is verified true. The probabilities are illustrative only. Generating text one token at a time 1 Today's weather is Model next-token odds sunny 0.46 hot 0.31 rainy 0.08 2 …weather is sunny Model next-token odds and 0.52 . 0.30 today 0.12 3 …is sunny and Model next-token odds warm 0.44 mild 0.28 hot 0.17 Result: "Today's weather is sunny and warm" The model picks what is likely in context, not what is verified true Probabilities are illustrative only
Each step: context → probabilities → choose a token → add it to the context → repeat.

From probabilities to a choice

The model gives each candidate token a score or a probability conditioned on the context. Then the generation algorithm picks the next token according to settings such as sampling and temperature. So the most probable token isn't always the one chosen, and the same tool can give you two different wordings for the same question.

Don't imagine the model just counting word frequencies like a simple statistics table: during training the network learns complex representations and parameters from a huge number of contexts, which is what lets it handle sentences it has never seen word for word.

Systems built on language models can do tasks such as summarising, translating, answering questions and generating code, and some systems can connect to external tools or sources.

Hallucination: When Fluency Misleads

Hallucination is when a system produces information that is incorrect, made up, or not supported by its source, worded in a way that may sound convincing.

Why does it happen? Because the model produces tokens according to its probability distribution, and the prediction goal alone contains no guarantee that its claims are true. The Arabic edition stresses that hallucination has several causes, so it should not be blamed on one single factor.

What it can look like:

  • A wrong date for an event or for when a landmark in your city opened.
  • An invented name of a person, book or reference that looks completely real.
  • A number that looks precise (say, a percentage to two decimal places) but has no source.
  • A quotation attributed to someone who never said it.

Large Language Models (LLMs): What Does "Large" Mean?

A large language model (LLM) is a language model with a large number of parameters, trained on broad data that may include text, code and varied sources depending on the model. Many modern models are built on the Transformer architecture. There is no single official threshold that makes a model "large".

Parameters are numerical values the model learns during training; they correspond to the weights of the neural network you met in the previous lesson. Many large models have billions of them.

Three connected aspects of size

The lesson describes "large" in three aspects:

  1. The number of model parameters.
  2. The amount of training data, which for modern text is usually measured in tokens, not just words.
  3. The computing power used for training.

These vary a lot from one model to another.

| Aspect | Description | |---|---| | Amount of training data | Can be enormous; usually measured in tokens | | Number of parameters | Values learned during training; many large models have billions, and sizes differ between models |

Does a bigger model mean guaranteed abilities?

Several abilities may improve with a suitable increase in model size, data and computing and in training quality, but there is no fixed threshold that guarantees a particular ability will appear. A model can still make mistakes in reasoning or facts even when it does well at summarising, translating and generating code. Size does not guarantee truth.

Real-world examples

Many chat systems are built on large language models, such as ChatGPT, Claude and Gemini. But these are product names whose abilities and versions keep changing, so we focus on the general concept and test each system and its limits before relying on it.

Tuning with RLHF

The base model that comes out of pre-training has learned language patterns, but it may not reliably follow instructions or the user's intent, and it can sometimes produce unsuitable or unsafe output. So later tuning stages are used, alongside system design and testing.

Reinforcement Learning from Human Feedback (RLHF) is one tuning method, not the only one. In its classic form it uses human preferences to train a reward model, and then reinforcement learning (from lesson 7-1) uses that signal to improve the model's behaviour.

| Stage | What happens | |---|---| | ① Initial training | Build a language model that has learned how words are used from large amounts of text | | ② Human evaluation | Raters compare or rank the model's outputs, creating preference data according to set criteria | | ③ Tuning by reinforcement learning | A reward model is trained on the preference data, and its signal is used in reinforcement learning to tune the model |

Notice the link to lesson 7-1: in reinforcement learning an agent learns from rewards; here, human preferences are turned into a reward signal.

RLHF stages and limits Three stages from left to right. Stage 1, pre-training: huge amounts of text produce a base language model that may not follow instructions. Stage 2, human preferences: one prompt with two replies, reply A preferred and reply B less preferred, building preference data. Stage 3, RL tuning: a reward model gives a reward signal that produces a tuned model which follows instructions better. Below, two boxes: it may improve following instructions and intent, style and tone, and some aspects of safety; it does not guarantee that facts are true, complete safety, or that every output is suitable. Note: RLHF is one tuning method, not the only one. RLHF: tuning with human feedback ① Pre-training huge amounts of text ↓ a base language model may not follow instructions ② Human preferences One prompt, two replies: Reply A: preferred Reply B: less preferred preference data ③ RL tuning a reward model ↓ reward signal a tuned model follows instructions better May improve following instructions and intent style and tone some aspects of safety Does not guarantee that facts are true complete safety that every output is suitable RLHF is one tuning method, not the only one
RLHF improves behaviour, but it doesn't turn the model into a guaranteed source of truth.

What RLHF may improve, and what it doesn't guarantee

It may improve following instructions, style and some aspects of safety. But it does not guarantee truth, safety, or that every output is suitable. An assistant's quality also depends on pre-training, other tuning methods, system design and tools, and ongoing evaluation.

Multimodality

Multimodality is the ability of a model or a system to handle more than one modality, such as text and images or audio. A modality is one kind of data (text, image, audio…). Multimodality can concern the inputs, the outputs, or both, and it does not mean every system supports every kind.

Many language models used to be text-only; then models and systems appeared that connect text with other media. Some systems may support:

  • Describing an image in text.
  • Turning audio into text.
  • Generating an image from a written description.
  • Analysing video.

What is available differs from system to system.

How can one system handle different kinds of data?

Inside a computer all data is numbers, but the way text, images and audio are encoded and processed can differ. Multimodal systems use architectures that link or fuse these representations, and they can be a single model or a system combining specialised components.

Multimodality On one side, three inputs: text, image and audio, with arrows into a central box titled multimodal system, which encodes each medium as numbers and then links the representations; it may be one model or several parts. On the other side, three outputs: text with a solid arrow, image and audio with dashed arrows, because those abilities differ from system to system. At the bottom: for every medium, still check accuracy, privacy, usage rights, bias and safety. Multimodality: more than one kind of data Inputs Text Image Audio Outputs Text Image Audio Multimodal system encodes each medium as numbers then links the representations one model or parts Dashed arrows: abilities that differ from system to system For every medium, still check: accuracy · privacy · usage rights · bias · safety
Multimodality widens what generative AI can do, but each medium brings its own risks.

Example: a student uploads a photo of a handwritten note and asks an assistant to read it aloud and then summarise it in writing. That is multimodality: an image goes in, audio and text come out. But watch for two risks: reading errors if the handwriting is unclear, and privacy if the photo contains names or personal details.

Multimodality widens some generative-AI applications, but it does not remove the need to check accuracy, privacy, usage rights, bias and safety for every medium.

Responsible Use: Check Before You Publish

Generative AI creates new text, images or audio. It is an excellent helper for wording, organising, summarising and brainstorming. But the parts that carry facts (dates, names, places, numbers, quotations) are exactly where it can be confidently wrong.

Practical rules in the spirit of the lesson:

  1. Fact-check: compare every important claim with a reliable, independent source, such as your textbook, an official website or a published reference.
  2. Record the source you checked.
  3. Make a person responsible for reviewing the text before it is published or used for a decision.
  4. Retrieval and search tools connected to the system can help, but they don't remove the need to check.
  5. Protect privacy and respect usage rights for images and text.

Try It Yourself

Try
How does a model write a sentence?
Put the steps of text generation in a generative language model in order, from start to finish.
1. The chosen token is added to the end of the context
2. The model receives the prompt as a context made of tokens
3. The process repeats until a stop point or the set length
4. The generation algorithm picks a token according to its settings, such as temperature
5. The model computes a probability for each candidate token based on the context
Use the up and down buttons to move each step, then press Check.
Try
What does RLHF really do?
Sort each statement: is it something RLHF may improve, or something it does not guarantee?
Items (7)
Tap an item to select it, then tap the category it belongs to.
Nothing here yet
Nothing here yet
Try
The history club's article
The situation
Your school's history club in Assiut wants to use a chatbot to write a short article about an ancient landmark in the city for the school website. The chatbot wrote a lovely article including the year it was built, the architect's name and a quotation from a historian. What is the best rule before publishing?
What would you do? You can try more than one choice.

Common Mistakes

"A language model looks up the correct answer in a database and returns it"

A generative model doesn't retrieve ready-made sentences as they are, nor choose from prepared templates.

It generates text token by token from a probability distribution conditioned on the context, so it can produce things no source ever wrote, true or false.

"If the answer is fluent and confident, it must be correct"

Fluency comes from learning language patterns; the prediction goal includes no guarantee that claims are true.

Fluency is not truth: check important claims in reliable, independent sources.

"RLHF makes AI always tell the truth"

RLHF uses human preferences to improve behaviour, and a preference is not a fact-check.

RLHF may improve instruction-following, style and some aspects of safety, but it guarantees neither truth nor safety.

"Once a model passes a certain size, new abilities must appear and it stops making mistakes"

According to the Arabic edition, no fixed threshold guarantees a particular ability.

Abilities may improve with a suitable increase in size, data, computing and training quality, and the model can still get facts and reasoning wrong.

"A token is always a word, and every AI system understands images and audio"

A token can be part of a word or a punctuation mark, and multimodality is not a feature of every system.

A token is a unit of split text, and multimodality is the ability of some models or systems to handle more than one modality, varying from system to system.

Key Points

  • A language model learns patterns in token sequences; a generative one estimates the probability of the next token from the context. A token can be a word, part of a word or a punctuation mark.
  • Text is generated by adding one token after another and updating the context until a stop point; the generation algorithm chooses using settings such as sampling and temperature.
  • Hallucination: incorrect, made-up or unsupported information worded convincingly; it happens because the prediction goal doesn't guarantee truth, and it has several causes.
  • A large language model has many parameters and broad training, and many are built on the Transformer; "large" = parameters + training data + computing, with no official threshold.
  • A bigger model may improve several abilities, but no fixed threshold guarantees a particular ability, and size doesn't guarantee truth.
  • RLHF is one tuning method: human preferences → reward model → reinforcement learning; it improves instruction-following but doesn't guarantee truth or safety.
  • Multimodality: a model's or system's ability to handle more than one modality in its inputs or outputs, varying by system, with accuracy, privacy, rights, bias and safety still to check.
  • The golden rule: fluency is not truth; check sources, record them, and keep a human responsible.

FAQ

Why does ChatGPT sometimes make up information that doesn't exist?

Because a language model generates text token by token according to what is likely in context, and the prediction goal itself has no built-in check that facts are true. A name or a date can come out that sounds convincing but is invented; that is called hallucination. It has several causes, so always check reliable sources.

What is the difference between a language model and a large language model?

A language model learns patterns in token sequences and estimates the probability of the next token. A large language model is a language model with a very large number of parameters, trained on broad data with a lot of computing power, often built on the Transformer architecture. There is no single official threshold that makes a model large.

Does RLHF stop hallucinations?

No. RLHF uses human preferences to train a reward model and then tunes the model with reinforcement learning, improving instruction-following, style and some aspects of safety. It does not guarantee that information is correct or safe, so checking is still essential.

What is a token in AI?

A token is the unit of text a model works with; it can be a whole word, part of a word or a punctuation mark, depending on how the text is split. The training data of modern models is usually measured in tokens.

Done reading? Test yourself