CS 212, AI Programming 1

Generative AI

Contents

[TOC]

Overview

Generative AI is a subset of machine learning (ML) that is used for creating new content. Generative models use neural networks to generate data based on, but different from, the data they were trained on.

graph TD
    subgraph AI ["Artificial Intelligence"]
        subgraph ML ["Machine Learning"]
            subgraph NN ["Neural Networks"]
                GenAI["<b>Generative AI and LLMs</b><br>(Creating New Content)"]
            end
        end
    end

    style AI fill:#f8f9fa,stroke:#adb5bd,stroke-width:2px
    style ML fill:#e9ecef,stroke:#6c757d,stroke-width:2px
    style NN fill:#dee2e6,stroke:#495057,stroke-width:2px
    style GenAI fill:#d0ebff,stroke:#1971c2,stroke-width:3px,color:#1864ab

Figure: Generative AI sits within the broader fields of Neural Networks, Machine Learning, and Artificial Intelligence (Source: Wikimedia Commons, CC BY-SA 4.0).

OpenAI released a public demo of ChatGPT on November 30, 2022 that took the world by storm and suddenly generative AI was the new sensation.

A Generative Pretrained Transformer (GPT) chatbot, like ChatGPT, works by taking a prompt (often a question or instruction) as input and generating a response (an answer) based on statistical probabilities.

Large Language Models

A Large Language Model (LLM) is an AI model built on the transformer architecture, scaled up across billions of parameters and immense amounts of text data. What makes an LLM unique compared to older, single-purpose language software is its versatility: rather than being trained for only one specific task, a single LLM can draft essays, write and debug software code, translate between languages, summarize research, and reason through conversational problem-solving out of the box.

Tokens

A token is the basic unit of text that a language model reads and generates. Tokens can be whole words, subwords, or even individual characters and punctuation marks, depending on the tokenizer used.

For example, a common phrase might tokenize word-for-word, but longer, rare, or compound words are broken into smaller chunks (e.g., “unbreakable” might split into “un”, “break”, and “able”). A helpful rule of thumb for English text is that 1 token roughly corresponds to 0.75 words (or 100 tokens ≈ 75 words).

Training an LLM

A large language model is primarily trained using self-supervised learning on vast amounts of text data from diverse sources like books, articles, code repositories, and websites.

In self-supervised pretraining, the model learns grammar, facts, and reasoning patterns simply by repeatedly predicting masked or missing next tokens without needing human labels. Training frontier models requires massive computational clusters running thousands of GPUs for weeks or months.

Self-supervised learning (SSL) is a form of machine learning where the system creates its own labels directly from raw, unlabelled data by hiding parts of the input and trying to predict them. Unlike traditional unsupervised learning, which simply looks for patterns or groupings across data without grading itself, self-supervised learning invents a prediction task with a clear right-or-wrong answer so it can automatically check its work.

After initial pretraining, models undergo fine-tuning and Reinforcement Learning from Human Feedback (RLHF). This alignment step teaches the model to behave as a safe, helpful conversational assistant rather than just an unpredictable text predictor. The result is a tool capable of generating coherent, human-like responses.1

Parameters

LLM parameters are the internal numerical values that define how the network processes information. These parameters primarily consist of the weights and biases across the neural network layers, which are adjusted during training to minimize prediction error.

The number of parameters in models varies widely: compact models built to run on personal computers or phones typically have 3 to 8 billion parameters, whereas flagship frontier2 models that power tools like ChatGPT, Claude, and Gemini have 1 to 2 trillion or more parameters. (The exact numbers aren’t revealed by the companies providing these models.)

Using an LLM (Inference)

When a user submits a prompt, the trained model generates output through a process called inference.

The process of generating output is iterative:

  1. The prompt is converted into tokens.
  2. Based on the prompt’s tokens, the model calculates the probability distribution across its entire vocabulary—determining how likely each possible token is to come next.
  3. The model samples one candidate token based on these probabilities (governed by settings such as temperature).
  4. The newly selected token is appended to the prompt, and the model repeats step 2 to choose the next token.
  5. This cycle continues until the model predicts a dedicated End of Sequence (EOS / Stop) token, or reaches a user-specified token limit.
%%{init: {"xyChart": {"width": 900, "height": 450}}}%%
xychart-beta
    title "Next Token Probability: The best thing about cats is that they ..."
    x-axis ["are", "purr", "make", "don't", "can", "sleep", "require", "fly"]
    y-axis "Probability (%)" 0 --> 40
    bar [32, 21, 14, 11, 9, 7, 5, 0.001]

Context Length

The context length (or context window) of an LLM refers to the maximum number of tokens it can hold in its active memory at one time, encompassing both the input prompt and the generated response.

Early generative models like GPT-3 had a context length of 4,096 tokens, while GPT-4 initially expanded to 8,192 and 32,768 tokens. Modern frontier models feature context windows ranging from 128,000 tokens up to 1,000,000+ tokens—allowing them to analyze hundreds of pages of documentation, entire books, or extensive codebases in a single session.

Practical Considerations & FAQ

Interactive Web Pages

Tokenizer—OpenAI. Visualizes the tokenization process.

Generative AI Exists because of the Transformer— Visual Storytelling Team and Madhumita Murgia . Financial Times. 2023

Transformer Explainer—Aeree Cho, Grace C. Kim, Alexander Karpekov, Alec Helbling, Jay Wang, Seongmin Lee, Benjamin Hoover, and Polo Chau. Georgia Institute of Technology. 2024.

References

How Transformers Work: A Detailed Exploration of Transformer Architecture—Josep Ferrer, DataCamp, 2024

Large language model—Wikipedia

AI’s understanding and reasoning skills can’t be assessed by current tests—Ananya, Science Direct, 2024.

Levels of AGI: Operationalizing Progress Toward Artificial General Intelligence—Morris, M. R., Sohl-Dickstein, J., Fiedel, N., Warkentin, T., Dafoe, A., Schmitt, M., et al. ACM Queue, 22(2). 2024

GPT-4—OpenAI

Microsoft Copilot with GPT-4 was used to draft parts of these notes, Gemini 4.0 Argon was used to assist with revisions.


Creative Commons License Intro to AI lecture notes by Brian Bird, written in 2024, revised in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


  1. LLMs don’t actually “understand” text in the way humans do. Instead, they analyze and process text based on patterns they have learned from training data. They are sophisticated tools that excel at mimicking human-like text but do not possess genuine understanding, comprehension or awareness. ↩

  2. A frontier model is an AI system at the leading edge of current capabilities, trained on the largest scale of compute and data to deliver state-of-the-art performance across broad reasoning, coding, and problem-solving tasks. ↩