LLMs, Generative AI & Safety
Machines that write, draw and talk, and the rules for building them responsibly.

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?
Text is split into tokens: words or pieces of words. For example:
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.
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):
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
- Embeddings: each token becomes a vector of numbers. Similar meanings end up close together in this space.
- 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".
- Positional information tells the model the word order, because attention by itself ignores order.
- Stacked layers: dozens of attention + feed-forward layers (Chapter 9 neurons) build richer representations.
- Output: a softmax over the whole vocabulary gives P(next token).
How LLMs are trained
- 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.
- Instruction fine-tuning: continue training on examples of helpful question → answer pairs.
- 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.
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:
| Issue | Example from our journey |
|---|---|
| Specification / reward hacking | An RL agent maximises the reward number, not the real goal (Ch. 5) |
| Fairness & bias | A CV-screening classifier learns historical discrimination (Ch. 8) |
| Uncertainty & calibration | A "95% accurate" test still gives mostly false alarms for rare faults (Ch. 6) |
| Transparency & explainability | Deep nets are hard to explain (Ch. 9); users should know when AI is used |
| Privacy | Pasting confidential data into a public chatbot |
| Misuse | Deepfakes, 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
Two axes: risk and autonomy
Failure has minimal impact on users
More significant consequences, so stronger controls
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.
- What does the AI do, and who can be affected?
- What data does it use, and are we allowed to use it?
- What is its risk level and autonomy level?
- Do users know AI is involved, where required?
- Who can intervene, audit and explain what happened?
- What evidence proves our controls work?
1. What is an LLM fundamentally trained to do?
2. An AI ranks job applicants and the recruiter always accepts the top score. Which concern is the strongest?
3. Under Kazakhstan's AI Law, synthetic AI results must…
4. Self-attention lets each token…

The whole journey on one page
PEAS, rationality, environment types 2 · Search
BFS, DFS, UCS, A*, admissible h 3 · Local search
Hill climbing, annealing, CSP 4 · Games
Minimax, alpha–beta, expectimax 5 · MDP & RL
Bellman, value iteration, Q-learning 6 · Probability
Bayes' theorem, Bayesian networks 7 · HMMs
Filtering, smoothing, Viterbi 8 · Decisions & ML
Expected utility, regression, trees, metrics 9 · Neural nets
Perceptron, XOR, backprop 10 · LLMs & safety
Transformers, hallucination, KZ AI Law
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.