Generative AI
How generative AI works, built in plain Python: sampling, tokens, next-token prediction, decoding, embeddings, retrieval, prompting, tools, evaluation, diffusion, fine-tuning and safety.
- Level
- Intermediate
- Time
- About 20 hours
- Modules
- 5
- Lessons
- 15
- Exercises
- 45
Generative AI creates new text, images and code instead of only labelling data. This course opens the box with small programs you can run and step through in your browser: a model that learns letter and word probabilities and samples from them, then tokens, next-token prediction, decoding with temperature, top-k, top-p and beam search, embeddings, semantic search and retrieval-augmented generation.
The second half covers how people build with and judge these models: prompts and templates, validated JSON and tool-calling loops, scores such as BLEU and perplexity, a tiny diffusion demo for images, fine-tuning with LoRA and preference data, and the safety checks that every real system needs. No real AI service is called anywhere: every model here is a plain function or a table, so you can see every number. Real library code appears only as text marked shown, not run.
What you will learn
- Explain generation as sampling from a learned probability distribution
- Turn text into tokens and predict the next token with softmax, loss and perplexity
- Control output with temperature, top-k, top-p and beam search
- Represent meaning with embeddings and build semantic search and retrieval-augmented generation
- Write prompts, validate structured output and run a tool-calling loop
- Evaluate generated text, and describe diffusion, fine-tuning, alignment and the main safety risks
Skills you will gain
- Sampling and probability models of text
- Tokens, softmax and decoding
- Embeddings, semantic search and RAG
- Prompt templates, JSON validation and tools
- Evaluation metrics, fine-tuning ideas and safety guards
Before you start
- Python: functions, lists, dictionaries, loops and string formatting (the Python course)
- Basic probability: what a probability is and why probabilities add up to 1 (the Statistics & Probability course helps)
- Comfort reading short programs; the machine-learning course helps with losses and evaluation but is not required
Where it is used
- Chat assistants and writing tools
- Search and question answering over documents
- Data extraction into structured records
- Image generation and editing
Modules
Module 1: Generation as sampling
What generative models are, how random numbers pick outcomes, and a first text generator that learns word pairs.
- 1
What generative AI is
Models that create new text, images and sound: generative versus discriminative models, learning a probability distribution, and sampling new examples from it.
- 2
Sampling and randomness Intermediate
How a computer makes random choices: pseudo-random number generators and seeds, sampling from a categorical distribution with running totals, and why samples only match their probabilities on average.
- 3
Generating text with a Markov model Intermediate
The first model that writes phrases: count which word follows which (a bigram model), turn the counts into next-word probabilities, and generate text greedily or by sampling.
Module 2: How language models work
Tokens and context windows, probabilities and loss for the next token, and the decoding methods that choose words.
- 1
Tokens and context windows Intermediate
How language models read text: splitting it into tokens, mapping tokens to numbers, subword pieces for rare words, and the context window that limits how much a model can see at once.
- 2
Next-token prediction Intermediate
What a language model really computes: a score (logit) for every token, softmax probabilities, the cross-entropy loss used in training, and perplexity as a measure of how surprised a model is.
- 3
Decoding: temperature, top-k, top-p and beam search Intermediate
How a model chooses each token from its probabilities: greedy decoding, temperature, top-k and nucleus (top-p) sampling, and beam search, with what each one does to creativity and reliability.
Module 3: Embeddings, search and retrieval
Vectors that stand for meaning, semantic search over chunks, and retrieval-augmented generation with sources.
- 1
Embeddings and similarity Intermediate
How models represent meaning as lists of numbers: embedding vectors, the dot product, cosine similarity, nearest neighbours and analogies by vector arithmetic.
- 2
Semantic search Intermediate
Finding text by meaning instead of exact words: splitting documents into chunks, embedding chunks and queries, ranking by cosine similarity, and comparing keyword and semantic search.
- 3
Retrieval-augmented generation (RAG) Intermediate
Giving a language model the right facts at the right time: retrieve relevant chunks, put them in the prompt with instructions to cite them, and generate an answer grounded in your own documents.
Module 4: Building with models and judging them
Prompt templates, validated JSON and tool loops, and the scores used to evaluate generated text.
- 1
Prompt engineering Intermediate
Writing instructions a model can follow: the parts of a good prompt, zero-shot and few-shot prompting, templates with placeholders, system and user messages, and how to test a prompt.
- 2
Structured output and tool calling Intermediate
Making a model's answer usable by programs: asking for JSON, validating it, retrying on errors, and the tool-calling loop in which a model requests functions and your code runs them.
- 3
Evaluating generative models Intermediate
How to tell whether generated text is good: exact match, n-gram overlap scores such as BLEU and ROUGE, perplexity, and human or model-as-judge ratings, with the strengths and limits of each.
Module 5: Images, tuning and safety
Diffusion on tiny images, fine-tuning and preference training, and the guards used for responsible use.
- 1
Image generation Intermediate
How machines make pictures: images as grids of numbers, diffusion models that learn to remove noise, and the ideas behind GANs and VAEs, shown on tiny one-dimensional images.
- 2
Fine-tuning and alignment Advanced
How a general model becomes a useful assistant: pretraining, fine-tuning on examples, preference data with RLHF and DPO as ideas, and LoRA, which trains only a tiny share of the weights.
- 3
Safety and responsible use Advanced
Using generative AI without harm: hallucination, prompt injection, privacy, bias and copyright, and simple checks in code that reduce each risk but never remove it.
Projects
- A question-answering assistant over your notes Build a small retrieval-augmented assistant in plain Python that answers questions from your own notes, cites its sources, refuses when the notes do not contain the answer, and is tested on questions you wrote.
- A prompt, tool and evaluation test bench Build a small test bench that fills prompt templates, runs a scripted model through a validated tool-calling loop, and scores the results, so you can see which prompt wording and which checks work best.
What to learn next
- Natural Language Processing Tokenizing, regular expressions, TF-IDF and search, Naive Bayes, sentiment, language models, HMM tagging, entities, spelling, embeddings, BPE and attention in Python.
- Machine Learning Machine learning from scratch in plain Python: regression, gradient descent, regularization, k-NN, logistic regression, metrics, trees, ensembles and k-means.
- Deep Learning Neurons, backpropagation, optimizers, normalization, CNNs, RNNs, embeddings, attention and transformers, every network built from scratch in Python.
- Artificial Intelligence Search and A*, game playing, constraint solving, logic, Bayesian networks, hidden Markov models, MDPs and Q-learning, each built in Python.