WEEK 10
CHAPTER 10 · FINALE

LLMs, Generative AI & Safety

Machines that write, draw and talk, and the rules for building them responsibly.

Manga page: Almaty, at last. / The capital of Kazakhstan is Almaty. / But it's ASTANA! Why did it sound so sure? / It predicts LIKELY words, not VERIFIED facts.

Generative AI

Most models in Chapter 8 were discriminative: they map an input to a label ("spam / not spam"). Generative AI learns the patterns of its data well enough to produce new samples: text (LLMs), images (diffusion models), audio, code, video.

What a language model does

A language model assigns probabilities to sequences of text. In practice it answers one question again and again: given the text so far, what is the probability of each possible next token?

P("Bota rides to Almaty") = P(Bota) · P(rides | Bota) · P(to | Bota rides) · P(Almaty | Bota rides to)

Text is split into tokens: words or pieces of words. For example:

Bota's journey across Kazakhstan was unforgettable.

To generate, the model picks a next token (often by sampling from the distribution), appends it, and repeats. The temperature setting reshapes the distribution: low temperature means safe, repetitive choices; high temperature means more surprising and more error-prone ones.

TRY IT! Bota's tiny language model (a bigram model)

Trained on 14 sentences about this book. It only looks at the previous word (a bigram model), the simplest possible LM. Real LLMs look at thousands of previous tokens through attention, but the core loop is the same: predict → sample → append → repeat.

Next-word probabilities after "" (at this temperature):

WHAT TO NOTICE

At temperature 0.1 the most frequent next word is strongly favoured, so the text becomes repetitive (but it is still random and can end). At 3.0 it picks rare words and the text gets weird. It can produce "Bota learns the capital of Almaty": fluent, confident and false. A bigram model has no idea what is true, only which words tended to follow which. Big LLMs are vastly better, but they are still trained to predict plausible text.

Inside an LLM: the Transformer

  1. Embeddings: each token becomes a vector of numbers. Similar meanings end up close together in this space.
  2. Self-attention: every token computes a query, key and value vector. Each token compares its query with the keys of the tokens it may see, decides how much to "attend" to each, then mixes their values. In an LLM that writes left to right, a causal mask hides future tokens, so every token sees only itself and the tokens before it. In "Bota charged its battery", attention links "its" to "Bota".
  3. Positional information tells the model the word order, because attention by itself ignores order.
  4. Stacked layers: dozens of attention + feed-forward layers (Chapter 9 neurons) build richer representations.
  5. Output: a softmax over the whole vocabulary gives P(next token).
Attention(Q, K, V) = softmax( Q·Kᵀ / √d + M ) · V (Vaswani et al., 2017) M = causal mask: 0 for earlier/current tokens, −∞ for future ones

How LLMs are trained

  1. Pre-training: predict the next token on a huge text corpus. This is self-supervised: the labels come from the text itself. It's gradient descent and backpropagation (Ch. 9) at enormous scale.
  2. Instruction fine-tuning: continue training on examples of helpful question → answer pairs.
  3. RLHF (reinforcement learning from human feedback): people rank answers, a reward model learns those preferences, and RL (Ch. 5) tunes the LLM to score higher.

Images are often made by diffusion models: they learn to remove noise step by step, so generation starts from pure noise and gradually "denoises" it into a picture that matches the prompt.

Limitations you must know

  • Hallucination: fluent, confident, wrong statements, including invented citations. Always verify facts.
  • Bias: models absorb biases in their training data.
  • Knowledge cut-off: the model doesn't know about events after its training data unless connected to search or documents (retrieval-augmented generation, RAG).
  • Prompt injection: hidden instructions in a web page or file can try to hijack an AI assistant.
  • No built-in "truth" objective: next-token prediction rewards plausibility. Alignment training reduces errors but doesn't eliminate them.
MYTH"ChatGPT looks facts up in a database."
FACTA plain LLM generates text from patterns stored in its weights. It looks things up only if it is connected to search or documents (RAG).
MYTH"If an AI sounds confident, it's probably right."
FACTFluency and correctness are different things. Hallucinations are often stated with full confidence, so verify important claims.
MYTH"AI-generated text has no legal rules in Kazakhstan."
FACTThe 2025 AI Law requires informing users about AI use and labelling synthetic results with machine-readable marks.

AI safety and ethics

As AI systems act more autonomously, "Can we build it?" turns into "Can we deploy it responsibly?" Key issues across all chapters of this book:

IssueExample from our journey
Specification / reward hackingAn RL agent maximises the reward number, not the real goal (Ch. 5)
Fairness & biasA CV-screening classifier learns historical discrimination (Ch. 8)
Uncertainty & calibrationA "95% accurate" test still gives mostly false alarms for rare faults (Ch. 6)
Transparency & explainabilityDeep nets are hard to explain (Ch. 9); users should know when AI is used
PrivacyPasting confidential data into a public chatbot
MisuseDeepfakes, manipulation, automated scams
Accountability & human oversight"The model decided" is not an excuse: a person or organisation stays responsible

Kazakhstan's AI Law (No. 230-VIII, adopted 17 November 2025)

Kazakhstan adopted a dedicated law "On Artificial Intelligence". It sits on top of existing rules on personal data, informatisation, intellectual property, cybersecurity and sector regulation. It is part of the Digital Qazaqstan strategy to 2029. (Summary for education only, not legal advice. The official Kazakh and Russian texts on Adilet are authoritative.)

Seven principles to remember

Human firstLife, rights, freedoms and autonomy come first
FairnessNo unjustified discrimination
TransparencyUsers know when AI is used and when content is synthetic
AccountabilityOrganisations stay responsible
PrivacyProtect personal data and confidentiality
SafetyManage foreseeable risks, failures and misuse
SecurityProtect systems and data from abuse

Two axes: risk and autonomy

Minimal risk
Failure has minimal impact on users
Medium risk
More significant consequences, so stronger controls
High risk
Possible serious impact on people, society, infrastructure, economy or security

Autonomy: low means AI recommends and a person always decides; medium means AI decides and a person can correct or cancel; high means human correction is excluded or technically impossible. Risk and autonomy are different questions, so assess both.

Key duties

  • Prohibited ("red zone"): AI using subconscious or manipulative methods that distort behaviour or push people toward harmful decisions.
  • Transparency: inform users when goods or services are produced using AI; mark synthetic results in machine-readable form with a visible warning.
  • Data: lawful, relevant, representative, protected training data. No unauthorised collection of personal data.
  • Copyright: AI-assisted works are protected only where there is a creative human contribution.
  • Audit & responsibility: owners/holders manage risk, documentation and user support. Audits check training-data quality and prohibited functions.
  • Ecosystem: a National AI Platform for developing and testing models.
THE 60-SECOND COMPLIANCE TEST (from the course)
  1. What does the AI do, and who can be affected?
  2. What data does it use, and are we allowed to use it?
  3. What is its risk level and autonomy level?
  4. Do users know AI is involved, where required?
  5. Who can intervene, audit and explain what happened?
  6. What evidence proves our controls work?
MINI-QUIZ

1. What is an LLM fundamentally trained to do?

Pre-training is next-token prediction on huge text corpora. Everything else is built on top.

2. An AI ranks job applicants and the recruiter always accepts the top score. Which concern is the strongest?

Recruitment is high-impact. The course example asks: if a person always accepts the AI's choice, is there real human oversight?

3. Under Kazakhstan's AI Law, synthetic AI results must…

Transparency duty: synthetic results are marked in machine-readable form with a warning, and users are told when AI is used.

4. Self-attention lets each token…

In an LLM the visible tokens are all the earlier ones (a causal mask hides the future). The previous-token-only model is our bigram toy; Transformers attend to the whole context.
Manga page: You learned so much, and you know your limits. / Not bad, little robot. / Next journey: teaching others!

The whole journey on one page

One thread connects everything: an agent choosing actions to maximise expected performance. Search does it when the world is known and deterministic, games when there's an opponent, MDPs when outcomes are random, probability when the state is hidden, machine learning when the rules must be learned from data, and neural networks when the patterns are too complex to write down.

10