liveBook · Manning

Chapter 1: How AI works

What you will learn in chapter 1 of The AI Pocket Book

The chapter introduces its scope and explains how AI and machine learning relate.

After this chapter you will be able to

Section by section

Introduction

The chapter introduces its scope and explains how AI and machine learning relate.

Key terms: AI, machine learning

1.1 How LLMs work

This section explains an LLM’s next-word prediction task, why that task supports varied abilities, and how the underlying model differs from the software people use to interact with it.

Key terms: language model, large language model, LLM, prompt, autocomplete, autoregressive, machine learning, self-contained, static, LLM wrapper

1.2 Text generation

This section explains how an LLM wrapper generates sentences by repeatedly adding each predicted word to the prompt and asking for the next one.

Key terms: LLM wrapper, prompt

1.3 End of text

This section explains how an LLM signals that its text is finished and how its wrapper stops generation.

Key terms: special code word, <|end of text|>, LLM wrapper

1.4 Chat

The section explains how a chat wrapper uses boundary markers to make an LLM respond conversationally rather than simply continue the user's text.

Key terms: LLM wrapper, <|end of prompt|>, completions mode, chat mode, history of the entire conversation, <|end of text|>

1.5 The system prompt

This section explains how a system prompt supplies an LLM with context, using current-date answers to show the difference that context makes.

Key terms: system prompt, current date, API, GPT-4o

1.6 Calling external software functions

This section explains how a chatbot can appear to use real-time information by having an LLM suggest a function call and a wrapper supply the function’s result.

Key terms: LLM wrapper, software functions, prompt, function declaration, function inputs, system prompt, get_current_weather

1.7 Retrieval-augmented generation

This section explains how retrieval-augmented generation gives an LLM relevant documents through its prompt, what it is used for, and its main limitations.

Key terms: retrieval-augmented generation, RAG, LLM wrapper, retrieval, augmented, training data, in-house chatbot, up-to-date web content, cite references, relevant documents

1.8 The concept of tokens

The section replaces the idea of LLMs predicting whole words with an explanation of tokens, what they can represent, and how an LLM’s vocabulary is assembled.

Key terms: token, vocabulary, leading space, whitespace token, text patterns, byte pair encoding

1.9 One token at a time

This section explains how an LLM wrapper turns a prompt into token IDs and generates text one token at a time until an end-of-text token stops it.

Key terms: LLM wrapper, tokens, vocabulary, token ID, next token, eat its own output, <|end of text|>, hungryish, hungry, ish

1.10 Billed by the token

Explains how token-based LLM API pricing makes long prompts, long responses, repeated calls, and growing chat histories more expensive.

Key terms: LLM APIs, input tokens, output tokens, chat history, prompt

1.11 What about languages other than English?

The section explains why English-oriented token vocabularies use more tokens for many other languages, what that costs, and why improving tokenization involves tradeoffs.

Key terms: vocabulary, tokens, UTF-8 tokens, billed by the token, contextualize adjacent tokens, vocabulary size, GPT-4o, Telugu

1.12 Why do LLMs need tokens anyway?

The section explains why LLMs use tokens rather than individual characters or an enormous vocabulary of whole phrases.

Key terms: tokens, characters, meaning, context, vocabulary, words, pieces of words

1.13 Embeddings: A way to represent meaning

This section introduces embeddings as numerical representations of token meaning and explains why they matter to an LLM’s next-token prediction.

Key terms: embedding, vector, dimension, token, embedding vector, topic, neutral value, LLMs, contextualized representation, embeddings

1.14 Machine learning and embeddings

This section explains why machine learning creates LLM embeddings and why that makes their dimensions difficult to interpret.

Key terms: machine learning, embeddings, dimensions, topics, explainability, predictive power

1.15 Visualizing embeddings

This section uses a visual analogy to show how embedding vectors place tokens in a meaning space where related tokens cluster.

Key terms: embedding vector, token, space of meaning, high-dimensional

1.16 Why embeddings are useful

This section explains how simple calculations on embeddings can compare meanings, extract selected information, and match content across text and images.

Key terms: embedding vectors, dot product, embedding space, projection, matrix, lower-dimensional space, machine learning, embeddings API, content-retrieval applications, input modalities

1.17 Why LLMs struggle to analyze individual letters

The section explains why token-based inputs and meaning-focused embeddings make individual letters difficult for LLMs to analyze, and notes a possible workaround.

Key terms: tokens, embedding vector, individual letters, LLM wrapper, prompt, words’ spellings

1.18 The transformer architecture

This section explains how transformers use token context to predict the next token, why that improves on LSTMs, and why context-window limits matter.

Key terms: LSTM, embedding, transformer architecture, context, initial embeddings, contextualization, contextualized embeddings, next token, context window, tokens

1.19 Step 1: Initial embeddings

This section explains how an LLM creates initial token embeddings by lookup, how training determines their values, and why the lookup dictionary can contain many learnable parameters.

Key terms: initial embeddings, token, dictionary, without context, learnable parameters, learning, training, embedding space, vocabulary, dimensions

1.20 Step 2: Contextualization

This section explains how a transformer uses attention to update token embeddings in context, and how layers and attention heads extend that process.

Key terms: contextualization, attention score, attention mechanism, context window, embedding vectors, feed-forward step, contextualized representation, projections, projection matrixes, learnable parameters

1.21 Step 3: Predictions

This section explains that an LLM produces probabilities for every possible next token, then a wrapper uses those probabilities to choose a token.

Key terms: contextualized embeddings, vocabulary, probability value, vector, LLM wrapper, output probabilities, highest probability

1.22 Temperature

This section explains how sampling settings affect which token an LLM wrapper selects next and how conservative or adventurous the output may be.

Key terms: token, vocabulary, output probabilities, sampler, temperature, sampling probabilities, LLM wrapper, Top-p, cumulative probability, Top-k

1.23 Can you get an LLM to always output the same thing?

The section defines reproducible LLM output, explains a theoretical way to achieve it, and shows why current APIs may still produce different results.

Key terms: reproducible outputs, input prompt, benchmark, top-1 sampling, token, highest probability, mostly reproducible outputs, threads, round-off errors

1.24 Where to learn more

The section points readers to resources for exploring LLM implementation details and notes what a skim of the code reveals about its operations.

Key terms: block, attention scores, embeddings, projections, GPT-2, models.py, Python, TensorFlow, The Illustrated Transformer

1.25 Machine learning

This section explains machine learning as designing a model’s architecture and then using data-driven training to set its parameters, rather than specifying the entire program by hand.

Key terms: machine learning, model, architecture, building blocks, parameters, embedding, training, learning, data, data-driven

1.26 Deep learning

This section contrasts deep learning with earlier feature-engineered machine learning and explains when each approach may be useful.

Key terms: deep learning, feature engineering, raw input, manually engineered features, layers, contextualized embeddings, input tokens, shallow learning, effective architecture, explainable model

1.27 Types of machine learning

The section distinguishes four machine-learning paradigms by how they obtain training information and explains how LLMs use self-supervised examples and reinforcement learning with human feedback.

Key terms: supervised learning, input, label, training data, generalize, overfitting, simulated training samples, synthetic data, SMOTE, labels

1.28 How LLMs are trained (and tamed)

The section explains why next-token training alone did not produce reliably helpful and safe LLMs, how human feedback was added to training, and what that means for user privacy.

Key terms: self-supervised learning, next-token-prediction task, misalignment, language modeling objective, next token, InstructGPT, self-supervised, fine-tuned, reinforcement learning with human feedback, prompts

1.29 Loss

This section explains how loss measures a model’s errors on training examples, how an LLM’s next-token loss is calculated, and why low training loss does not guarantee good performance on new data.

Key terms: loss, cost, training examples, parameter values, correct next token, probability, cross-entropy loss, log loss, negative logarithm, training data

1.30 Stochastic gradient descent

This section explains how stochastic gradient descent trains a model, why its results have limits, and how training differs from inference, then clarifies two meanings of “generative AI.”

Key terms: stochastic gradient descent, SGD, parameters, loss, batch, minibatch, stochastic, gradient, local minimum, architecture

1.31 Convolutions (images, video, and audio)

This section explains how CNNs use learned convolutions and downsampling to process images, then briefly extends the approach to image transformation, audio, and video.

Key terms: CNN, convolution, downsampling, embedding, filter, learnable parameters, image categorization, parts, U-Net, image-to-image

1.32 Multimodal AI

The section explains how multimodal AI combines language and image models to connect images with text and generate images from descriptions.

Key terms: multimodal, CNN, LLM, embedding, linear projection, conditional diffusion model, U-Net, noisy image, text embedding, supervised

1.33 No free lunch

This section explains why machine learning relies on human-designed architectures tailored to particular problems rather than one universally best approach.

Key terms: No Free Lunch Theorem, architecture, transformers, CNNs, LSTMs

1.34 Summary

Where this is used later

Concepts explained in this chapter

Chapter 2: Hallucinations →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.