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:
AI
ML
Deep Learning
LLM
CV
NNThey are not all the same thing. The hierarchy looks like this:
Artificial Intelligence
↓
Machine Learning
↓
Deep Learning
↓
Large Language ModelsAI 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
Generative
Pretrained
TransformerAll 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:
Attention is all you needThis paper made ChatGPT possible.
What is an LLM?
Input
↓
Engine
↓
OutputHere both the input and the output are in natural language. An LLM is an engine.
Just like:
Fuel
↓
Engine
↓
Car movesIn the same way:
Prompt
↓
LLM
↓
AnswerThe car analogy
This was the best analogy of the lecture:
GPT = Engine
ChatGPT = CarGPT 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:
OpenAI API
↓
Prompt
↓
ResponseThat 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
const client = new OpenAI({
apiKey: "",
});This is the Application Developer side. You are not building a model, you are using one.
Then:
client.chat.completions.create(...)This means:
Message
↓
OpenAI Server
↓
GPT
↓
Prediction
↓
AnswerPrompting
System
User
PromptSystem Prompt
It decides how the model behaves. Example:
Never be rude.This is an instruction to the model.
User Prompt
How are you?The model now combines both:
SYSTEM
+
USER
↓
AnswerRole Prompting
Here we give the model a persona:
Give PersonaExample:
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."):
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:
Example
Question
Answer
Question
Answer
QuestionThe model learns the pattern. This is called few-shot prompting.
GIGO
Garbage In
Garbage OutIf 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:
Hello → 13225
I → 11
am → 357Tokenization
In code, we start with the tiktoken library:
import { get_encoding } from "tiktoken";encode()
encode() converts a sentence into numbers. Example:
Hello I am Piyush
↓
13225
11
357
939decode()
decode() turns the numbers back into text. So the flow is:
Encode
↓
Tokens
↓
DecodeNext Token Prediction
GPT / LLM
↓
Next Token PredictionThe whole LLM is based on this. Prompt:
I loveThe model will guess:
Ice → Cream → very → muchIt does not write the whole sentence in one go. It generates one token at a time.
Vocabulary
The = 1
Spoon = 2This 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:
King
↓
[0.25, 0.71, 0.93, ....]This vector has hundreds of dimensions. If the meanings are similar, the vectors are close together:
Paris
France
London
EnglandAll of these will be close to each other.
Vector intuition
King - Man + Woman = QueenThis is the magic of embeddings.
Positional Encoding
Position EncodingWhy does position matter? Look at these two sentences:
Piyush loves ChatGPTand
ChatGPT loves PiyushThe 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:
You let tokens talk to each other.Example:
River Bank
ICICI Bank"Bank" means something different in each. The model looks at the word next to it:
River → Nature → bank means the edge of a river
ICICI → Finance → bank means a financial institutionThis is called self-attention. Words talk to each other.
Transformer Diagram
The pipeline of the original transformer architecture:
Embedding
↓
Positional Encoding
↓
Attention
↓
Feed Forward
↓
OutputThis is the actual transformer pipeline.
Temperature
The model has a probability distribution (P = 0.6, 0.2, 0.1). Example:
I love
↓
Ice 0.6
Pizza 0.2
Coding 0.1Temperature decides how much randomness there will be.
Temperature = 0
Almost deterministic. The same answer every time:
2+2 → 4Temperature = 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:
Apple 0.40
Orange 0.30
Banana 0.20
Dog 0.001Top 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
Expected
↓
Actual
↓
Loss
↓
BackpropagationExample: the expected output was
CuteThe model said
Acha JiWrong. 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:
8K
32K
128K
200KIf 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:
User Prompt
↓
Tokenization
↓
Numbers (Token IDs)
↓
Embeddings (Vectors)
↓
Positional Encoding
↓
Self Attention
↓
Transformer Layers
↓
Next Token Prediction
↓
Temperature / Top-p Sampling
↓
Generated ResponseWhy the two code examples
1. The OpenAI SDK code
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
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".