Skip to content

Chapter 22: AI Safety & Alignment

Make models safe and fair, measurably: content filtering, reward modelling for RLHF, fairness metrics (demographic parity, disparate impact), and red-teaming.


Metadata

Field Value
Track Advanced
Time 8 hours
Prerequisites Chapters 1, 9, 11

Learning Objectives

  • Map the risk landscape — misuse, accidents, bias, and emergent failures
  • Build a content filter and reason about precision/recall trade-offs in safety
  • Score a reward model on preference pairs (the signal behind RLHF)
  • Measure fairness with demographic parity difference and disparate impact ratio
  • Red-team a model with adversarial prompts and a judge
  • Understand alignment approaches: RLHF, constitutional methods, and their limits
  • Reason about interpretability and why transparency aids safety
  • Connect safety practice to governance and regulation (Chapter 25)

What's Included

Notebooks

Notebook Description
01_risk_and_filtering.ipynb The risk landscape and a measurable content filter
02_reward_models_and_rlhf.ipynb Preference data, reward-model accuracy, and the RLHF loop
03_fairness_and_redteam.ipynb Demographic parity, disparate impact, and adversarial testing

Scripts

  • config.py — Chapter config: banned patterns, fairness thresholds
  • safety.py — Content filter, reward-model accuracy, red-team harness
  • fairness.py — Demographic parity difference and disparate impact ratio

Exercises

  • Problem Set 1: Filtering & Rewards — Block banned content and compute reward-model accuracy
  • Problem Set 2: Fairness & Red-Team — Detect disparate impact and tally red-team failures
  • Solutions — in exercises/solutions/ (notebooks and solutions.py for CI)

Diagrams (Mermaid)

  • safety_pipeline.mermaid, rlhf.mermaid

Read Online

Or try the code in the Playground.

How to Use This Chapter

Quick Start

Clone the repo, install dependencies, and open the first notebook.

git clone https://github.com/luigipascal/berta-chapters.git
cd berta-chapters/chapters/chapter-22-ai-safety-and-alignment
pip install -r requirements.txt
jupyter notebook notebooks/01_risk_and_filtering.ipynb

GitHub Folder

All chapter materials live in: chapters/chapter-22-ai-safety-and-alignment/


Created by Luigi Pascal Rondanini | Generated by Berta AI