All notes
2026-11-28 6 min read

My notes from a GenAI class by Piyush Garg.

Lecture 1: LLM Foundation

What is AI?

The very first question: what is AI? These terms are often heard together:

text
AI
ML
Deep Learning
LLM
CV
NN

They are not all the same thing. The hierarchy looks like this:

text
Artificial Intelligence
    ↓
Machine Learning
    ↓
Deep Learning
    ↓
Large Language Models

AI means making a machine intelligent. It has many fields:

  • Computer Vision
  • Robotics
  • NLP
  • Recommendation
  • Speech Recognition

An LLM is only one small part of AI.

What does GPT mean

text
Generative
Pretrained
Transformer

All three words are very important.

G = Generative

It generates things. Images, text, code, poems, stories: it generates all of them.

P = Pretrained

The model has already read a lot of data from the internet. During training it has seen things like:

  • Books
  • Wikipedia
  • GitHub
  • Research Papers

That is why it has knowledge about the world.

T = Transformer

This is the name of the architecture. In 2017 Google published a paper:

text
Attention is all you need

This paper made ChatGPT possible.

What is an LLM?

text
Input
  ↓
Engine
  ↓
Output

Here both the input and the output are in natural language. An LLM is an engine.

Just like:

text
Fuel
  ↓
Engine
  ↓
Car moves

In the same way:

text
Prompt
  ↓
LLM
  ↓
Answer

The car analogy

This was the best analogy of the lecture:

text
GPT     = Engine
ChatGPT = Car

GPT is only the engine. ChatGPT was built by adding all of this on top of it:

  • UI
  • Conversation
  • Memory
  • Tools
  • Files
  • Voice
  • Image

In exactly the same way, Toyota, Honda and BMW all build different cars, but every one of them has an engine inside.

Application Developer vs ML Engineer

On one side is the Application Developer, on the other side ML Engineering. This is a very important point.

Application Developer

Simply uses an API:

text
OpenAI API
  ↓
Prompt
  ↓
Response

That is it. They do not have to build a transformer.

ML Engineer

  • Trains the model
  • Designs the transformer
  • Optimises attention
  • Calculates loss
  • Trains the weights

These are two different careers.

OpenAI API

js
const client = new OpenAI({
  apiKey: "",
});

This is the Application Developer side. You are not building a model, you are using one.

Then:

js
client.chat.completions.create(...)

This means:

text
Message
  ↓
OpenAI Server
  ↓
GPT
  ↓
Prediction
  ↓
Answer

Prompting

text
System
User
Prompt

System Prompt

It decides how the model behaves. Example:

text
Never be rude.

This is an instruction to the model.

User Prompt

text
How are you?

The model now combines both:

text
SYSTEM
  +
USER
  ↓
Answer

Role Prompting

Here we give the model a persona:

text
Give Persona

Example:

text
You are Hitesh Sir.

The model will now answer as that persona. Example (question: "Can we do DSA in HTML?", answer in his style: "It is a free country."):

text
DSA HTML me kar sakte hain?

Azad desh hai.

That is why role prompting is powerful.

Few Shot Prompting

We first give the model a few examples:

text
Example

Question
Answer

Question
Answer

Question

The model learns the pattern. This is called few-shot prompting.

GIGO

text
Garbage In
Garbage Out

If the prompt itself is bad, the answer will be bad too.

Input Tokens

Now on to tokenization.

An LLM does not see words

This is a very important line. An LLM does not know English, it only knows numbers. Example:

text
Hello  →  13225
I      →  11
am     →  357

Tokenization

In code, we start with the tiktoken library:

js
import { get_encoding } from "tiktoken";

encode()

encode() converts a sentence into numbers. Example:

text
Hello I am Piyush
        ↓
13225
11
357
939

decode()

decode() turns the numbers back into text. So the flow is:

text
Encode
  ↓
Tokens
  ↓
Decode

Next Token Prediction

text
GPT / LLM
    ↓
Next Token Prediction

The whole LLM is based on this. Prompt:

text
I love

The model will guess:

text
Ice  →  Cream  →  very  →  much

It does not write the whole sentence in one go. It generates one token at a time.

Vocabulary

text
The   = 1
Spoon = 2

This is only an example. A real GPT vocabulary has 100k+ tokens, and every token has a unique ID.

Embeddings

This is the next topic, and also the most confusing one. To understand it, picture a graph with these points:

  • Eiffel Tower
  • Paris
  • Statue of Liberty
  • US

This graph is actually the embedding space.

The idea is that the model does not keep only the token ID. It turns every word into a vector:

text
King
  ↓
[0.25, 0.71, 0.93, ....]

This vector has hundreds of dimensions. If the meanings are similar, the vectors are close together:

text
Paris
France
London
England

All of these will be close to each other.

Vector intuition

text
King - Man + Woman = Queen

This is the magic of embeddings.

Positional Encoding

text
Position Encoding

Why does position matter? Look at these two sentences:

text
Piyush loves ChatGPT

and

text
ChatGPT loves Piyush

The words are the same, but the meaning is different. So the model has to be told the position too.

Self Attention

This was the main hero of the lecture. In one line:

text
You let tokens talk to each other.

Example:

text
River Bank
ICICI Bank

"Bank" means something different in each. The model looks at the word next to it:

text
River  →  Nature   →  bank means the edge of a river
ICICI  →  Finance  →  bank means a financial institution

This is called self-attention. Words talk to each other.

Transformer Diagram

The pipeline of the original transformer architecture:

text
Embedding
    ↓
Positional Encoding
    ↓
Attention
    ↓
Feed Forward
    ↓
Output

This is the actual transformer pipeline.

Temperature

The model has a probability distribution (P = 0.6, 0.2, 0.1). Example:

text
I love
  ↓
Ice     0.6
Pizza   0.2
Coding  0.1

Temperature decides how much randomness there will be.

Temperature = 0

Almost deterministic. The same answer every time:

text
2+2  →  4

Temperature = 1

Creative. Different answers.

Temperature = 2

Very random. Sometimes even nonsense.

Top P

The idea is similar. Instead of taking every token, consider only the most probable ones:

text
Apple   0.40
Orange  0.30
Banana  0.20
Dog     0.001

Top P will ignore "Dog".

Training vs Inference

  • Training: the model learns.
  • Inference: the model answers.

When you are using ChatGPT right now, that is inference.

Cross Entropy Loss

text
Expected
  ↓
Actual
  ↓
Loss
  ↓
Backpropagation

Example: the expected output was

text
Cute

The model said

text
Acha Ji

Wrong. A loss was calculated, then backpropagation ran and the weights were updated. Through this process the model gradually became intelligent.

Context Window

A model can remember only a limited number of tokens, for example:

text
8K
32K
128K
200K

If you send a longer prompt than that, older text starts getting removed. That is why ChatGPT sometimes "forgets" what was said earlier.

The lecture's biggest takeaway

If the whole lecture is summarised in just 10 steps, the flow looks like this:

text
User Prompt
        ↓
Tokenization
        ↓
Numbers (Token IDs)
        ↓
Embeddings (Vectors)
        ↓
Positional Encoding
        ↓
Self Attention
        ↓
Transformer Layers
        ↓
Next Token Prediction
        ↓
Temperature / Top-p Sampling
        ↓
Generated Response

Why the two code examples

1. The OpenAI SDK code

js
client.chat.completions.create({
  model: "gpt-4",
  messages: [{ role: "user", content: "Hello, How are you?" }],
});

The only goal of this code is to show how simple it is to use an LLM as an Application Developer. You send a prompt, the model generates a response through next-token prediction, and the API gives you the final answer. You never touch the transformer or the training.

2. The tiktoken code

js
const encoded = encoderForGpt2.encode("Hello, I am Piyush");
console.log(encoded);

This code proves that:

  • An LLM does not process text directly.
  • Text is first converted into token IDs.
  • The model works on those numbers.
  • decode() converts the same token IDs back into readable text.

It is the practical demonstration of the lecture topic "Tokenization and how text becomes numbers".

Whiteboard

The Eraser board for this lecture