Understanding Generative AI: The Science Behind ChatGPT

The Big Question: Is Generative AI Just Math, Statistics, and Probability?

Here's the thing that might surprise you: generative AI isn't some mystical technology that requires a PhD in theoretical physics to understand. At its core, it's an incredibly sophisticated pattern-matching system that learned to predict what comes next in a sentence. That's it. No magic, no consciousness, no secret sauce—just very clever math applied at massive scale.

But before we dive into how it works, let's clear up a common misconception about who actually builds these systems.

Two Worlds, One Technology

In the AI field, there are essentially two types of people working with very different goals, and understanding this distinction helps explain why AI seems both incredibly complex and surprisingly simple at the same time.

Researchers and ML Engineers are like master craftsmen specializing in one very specific area. They might spend years perfecting how neural networks learn patterns or optimizing algorithms to run faster. They know everything about their particular domain—the mathematical foundations, the theoretical limits, the cutting-edge techniques. But here's the catch: their expertise is so specialized that they often can't build the full applications that people actually use. It's like being a world-class engine designer who has never assembled a complete car.

Developers, on the other hand, are like skilled architects and builders. They might not know every detail about how the engine works internally, but they understand enough about many different components to build complete, functional products. They know how to connect the AI engine to databases, user interfaces, and real-world applications. They're generalists who create the tools people actually interact with.

The beautiful intersection between these two worlds is where the magic happens—but as we'll see, it's not really magic at all.

Breaking Down "Generative AI": It's All in the Name

Let's start with the basics. The term "Generative AI" literally tells us what it does:

Generative means "to create" or "to produce." These systems generate new content—text, images, code, music—based on patterns they've learned.

The most famous example is GPT, which stands for Generative Pre-Trained Transformer. Let's unpack this:

  • Generative: Creates new content

  • Pre-Trained: Learned patterns from massive amounts of existing data

  • Transformer: The specific architecture (we'll explain this soon) that makes it all possible

Think of it like this: imagine you had a friend who read every book, article, and website on the internet, then lost all memory of the specific content but retained an intuitive sense of how language flows. When you start a sentence, this friend can predict what you're likely to say next with uncanny accuracy. That's essentially what GPT does.

The Foundation: The Transformer Revolution

The entire modern AI revolution stands on the shoulders of a single research paper published by Google researchers in 2017 called "Attention is All You Need." This paper introduced the Transformer architecture, which was originally designed for something much simpler than chatbots: Google Translate.

The original goal was straightforward:

English: "Hello, how are you?"
↓ (Transformer)
Spanish: "Hola, ¿cómo estás?"

But here's where it gets interesting. The researchers discovered that this architecture was remarkably good at understanding relationships between words and predicting what should come next in a sequence. And "what comes next" turned out to be the key to everything.

The Surprisingly Simple Secret: Just Predict the Next Word

Here's the part that blows most people's minds: these incredibly sophisticated AI systems that can write essays, solve math problems, and engage in complex conversations are fundamentally doing one simple task: predicting the next word in a sequence.

Let me show you exactly how this works:

Input: "Hello, my name is Ar"
GPT thinks: "Based on everything I've learned, the next character is probably 's'"
Output: "s"

Input: "Hello, my name is Ars"
GPT thinks: "Now the next character is probably 'h'"
Output: "h"

Input: "Hello, my name is Arsh"
GPT thinks: "The next character should be 'n'"
Output: "n"

And so on, one prediction at a time, until it builds: "Hello, my name is Arshnoor."

You might be thinking, "That seems incredibly simple—how can this create coherent essays or solve complex problems?" The answer lies in the scale and sophistication of the pattern recognition, plus the incredible processing power of modern GPUs.

The GPU Revolution: Why NVIDIA Became the Gold Rush

Here's a perfect analogy: when everyone was rushing to find gold in California, the people who made the most money weren't the miners—they were the ones selling shovels and pickaxes. Today, while everyone is rushing to build AI applications, NVIDIA is selling the "shovels"—the Graphics Processing Units (GPUs) that make AI training possible.

GPUs were originally designed to render video game graphics, which requires doing many simple calculations simultaneously. It turns out this same parallel processing power is perfect for training AI models, which need to perform millions of similar calculations at once. This is why NVIDIA's stock price has exploded alongside the AI boom.

Step-by-Step: How Your Text Becomes AI Understanding

Now let's walk through exactly what happens when you type a message to ChatGPT. We'll use a real example to make this concrete.

Step 1: Tokenization - Breaking Language Into Digestible Pieces

When you type "Hello, I am Arshnoor Singh Sohi," the AI doesn't see letters or even words the way humans do. Instead, it breaks your text into "tokens"—small units that could be parts of words, whole words, or even punctuation marks.

Why? Because computers only understand numbers, not language. So every piece of text needs to be converted into numbers before the AI can work with it.

Here's a real example using OpenAI's tokenizer:

import tiktoken

# This is the actual tokenizer used by GPT-4
enc = tiktoken.encoding_for_model("gpt-4o")

text = "Hello, I am Arshnoor Singh Sohi"

# Convert text to numbers (tokens)
tokens = enc.encode(text)
print("Tokens: ", tokens)
# Output: [13225, 11, 357, 939, 1754, 1116, 1750, 267, 44807, 336, 95083]

# Convert numbers back to text
decoded = enc.decode(tokens)
print("Decoded Text: ", decoded)
# Output: "Hello, I am Arshnoor Singh Sohi"

Each number corresponds to a specific token in the AI's vocabulary. For example, token 13225 might represent "Hello", token 11 represents ",", and so on. Think of it like a massive dictionary where every possible word fragment has been assigned a unique number.

The vocabulary size—the total number of tokens the model knows—typically ranges from 50,000 to 100,000+ tokens. This covers not just English words, but pieces of words, punctuation, numbers, and even tokens for other languages.

Step 2: Vector Embeddings - Giving Words Meaning in Mathematical Space

Here's where things get really interesting. Those tokens (numbers) still don't capture the meaning of words. The number 13225 might represent "Hello," but the computer needs to understand that "Hello" is similar to "Hi" and "Greetings" but different from "Goodbye."

This is where vector embeddings come in. Think of embeddings as coordinates in a multi-dimensional space where similar concepts are placed close together. It's like a massive library where related books are stored on nearby shelves.

from openai import OpenAI

client = OpenAI()

text = "dog chases cat"

# Convert text to a vector embedding
response = client.embeddings.create(
    model="text-embedding-3-small",
    input=text
)

print("Vector Embedding:", response.data[0].embedding[:10])  # Show first 10 dimensions
print("Total dimensions:", len(response.data[0].embedding))
# Output: 1536 dimensions

Each word or phrase gets converted into a list of 1,536 numbers (in this example). These numbers represent the word's position in a 1,536-dimensional space. Words with similar meanings will have similar coordinates.

For example:

  • "dog" and "puppy" would have embeddings that are very close to each other

  • "dog" and "automobile" would have embeddings that are far apart

  • "run" (as in jogging) and "run" (as in operate a business) would have different embeddings based on context

This is how the AI learns that "bank" in "river bank" is different from "bank" in "Chase Bank"—the surrounding words provide context that changes the embedding.

Step 3: Positional Encoding - Teaching AI That Order Matters

Here's a crucial insight: the meaning of a sentence depends heavily on word order. Consider these two sentences:

  • "The dog chases the cat"

  • "The cat chases the dog"

Same words, completely different meanings! But how does the AI know which word comes first, second, third, and so on?

This is where positional encoding comes in. The AI adds special mathematical patterns to each word's embedding that represent its position in the sentence. Think of it like adding GPS coordinates to each word—not just what the word means, but where it sits in the sentence.

For example:

Position 1: "The" + position_encoding_1
Position 2: "dog" + position_encoding_2  
Position 3: "chases" + position_encoding_3
Position 4: "the" + position_encoding_4
Position 5: "cat" + position_encoding_5

The position encodings use mathematical functions (specifically sine and cosine waves) that create unique patterns for each position. This allows the model to distinguish between the first "the" and the second "the" in our example.

Step 4: Self-Attention - Teaching Words to Talk to Each Other

Now we get to the real breakthrough: self-attention. This is the mechanism that allows each word in a sentence to "look at" and "consider" every other word when determining its meaning.

Think of it like a dinner party conversation. When someone says "bank," everyone at the table considers the entire conversation context to understand whether they mean a financial institution or the side of a river. Each word gets to "listen" to all the other words before deciding what it means.

Here's a simple example of how self-attention works:

Sentence: "The animal didn't cross the street because it was too tired."

When processing the word "it," the self-attention mechanism:

  1. Looks at "animal" (high attention score - "it" probably refers to this)

  2. Looks at "street" (low attention score - "it" probably doesn't refer to this)

  3. Looks at "tired" (medium attention score - this describes the state of "it")

The attention mechanism calculates mathematical scores for how much each word should "pay attention" to every other word. This is what allows the AI to understand that "it" refers to "animal" and not "street."

Step 5: Multi-Head Attention - Looking at Relationships from Multiple Angles

Single-head attention is powerful, but multi-head attention is like having multiple experts examine the same sentence from different perspectives simultaneously.

Imagine you're trying to understand the sentence: "The bank can guarantee deposits." You might want to consider:

  • Grammatical relationships: What's the subject, verb, object?

  • Semantic relationships: What concepts are related?

  • Contextual relationships: What field are we discussing (finance vs. geography)?

Multi-head attention runs several attention mechanisms in parallel, each specializing in different types of relationships. One "head" might focus on grammatical structure, another on semantic meaning, and a third on long-range dependencies between distant words.

The model then combines insights from all these different perspectives to build a comprehensive understanding of the sentence.

Step 6: When Things Go Wrong - Backpropagation and Learning

Even with all this sophisticated machinery, the AI doesn't get things right immediately. It learns through a process called backpropagation—essentially learning from its mistakes.

Here's how the learning process works:

# Simplified learning loop
actual_answer = "The correct response"  # This is the target

for training_example in training_data:
    # Model makes a prediction
    prediction = model.predict(training_example)

    # Calculate how wrong the prediction was
    loss = calculate_difference(prediction, actual_answer)

    # Adjust the model to be more accurate next time
    model.update_weights_based_on_loss(loss)

During training, the model is shown millions of examples of text and learns to predict what comes next. When it makes a wrong prediction, the backpropagation algorithm adjusts the internal weights and parameters to make better predictions in the future.

This is like a student taking practice tests, reviewing their mistakes, and adjusting their study approach to perform better on the next test.

Putting It All Together: The Complete Picture

Let's trace through what happens when you ask ChatGPT: "What's the capital of France?"

  1. Tokenization: Your question gets broken into tokens: ["What", "'s", "the", "capital", "of", "France", "?"]

  2. Embedding: Each token gets converted to a high-dimensional vector that captures its meaning

  3. Positional Encoding: Position information gets added so the model knows word order

  4. Multi-Head Attention: The model examines relationships between all words from multiple perspectives, recognizing this is a factual question about geography

  5. Pattern Matching: Based on training data, the model recognizes this pattern and predicts the most likely continuation: "Paris"

  6. Token Generation: The model generates tokens one by one: ["The", "capital", "of", "France", "is", "Paris", "."]

  7. Detokenization: The tokens get converted back to readable text: "The capital of France is Paris."

The Reality Check: Why This Isn't Actually Magic

After understanding all these components, you might still wonder: "How can this simple next-word prediction create something that seems so intelligent?"

The answer lies in three factors:

Scale: These models are trained on virtually the entire internet—billions of documents, books, articles, and conversations. They've seen almost every possible pattern in human language.

Computation: Modern GPUs can perform trillions of calculations per second, allowing the model to consider incredibly complex relationships between words and concepts.

Emergence: When you combine simple rules (predict the next word) with massive scale and computation, surprisingly sophisticated behaviors emerge—much like how simple rules governing bird flocking create complex, coordinated movement patterns.

The Bottom Line

Generative AI isn't magic—it's pattern recognition at unprecedented scale. It's a system that has learned the statistical patterns of human language so well that it can generate new text that follows those same patterns convincingly.

Understanding this doesn't diminish the achievement; it makes it more impressive. Human engineers have created a system that can capture and reproduce the incredibly complex patterns that emerge from billions of human conversations, writings, and thoughts.

The next time you interact with ChatGPT or any other AI system, remember: you're not talking to a magical oracle. You're interacting with a very sophisticated prediction engine that has learned to speak our language by studying how we speak to each other.

And that, in many ways, is far more remarkable than magic could ever be.

0
Subscribe to my newsletter

Read articles from Arshnoor Singh Sohi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arshnoor Singh Sohi
Arshnoor Singh Sohi

👨‍💻 Master's student in Applied Computing (AI Specialization) at University of Windsor 🏆 1st Place Winner - NLP & Large Language Models Workshop 2025 🔧 I build end-to-end solutions - from distributed file systems in C to production-ready RAG systems achieving 91% precision. Currently exploring the intersection of AI and software engineering. 📊 My projects span the full spectrum: real-time collaborative web apps, intelligent recommendation systems, and machine learning pipelines that actually work in production. 🌱 Always learning, always building. When I'm not debugging code, you'll find me experimenting with the latest AI frameworks or contributing to open source. 🎯 Currently seeking co-op opportunities to apply my skills in real-world challenges. 📍 Windsor, ON | 🔗 Open to connect and collaborate!