Sitemap
Medium Logo

Understanding Generative AI: How LLMs Work Behind the Scenes

Generative AI is changing the way we interact with technology. Millions of people use AI tools like ChatGPT every day, but very few know what actually happens behind the scenes. In this blog, I'll explain some of the core concepts of Large Language Models (LLMs) in a simple and beginner-friendly way.

I like to think of Generative AI as having two major parts: Application Development and AI Engineering. Imagine a restaurant. The dining area, chairs, menu, and customer experience represent application development because that's what customers interact with. The kitchen, where the actual cooking happens, represents AI engineering. Similarly, applications like ChatGPT are the products users interact with, while the LLM working behind them is the real engine.

One thing I found interesting is the naming. ChatGPT and OpenAI are simple names that immediately give people an idea that they are related to Artificial Intelligence. On the other hand, if someone from a non-technical background hears a name like Claude, they may not immediately recognize that it is an AI model.

Now let's understand how an LLM works.

The first stage is pre-training. During this phase, the model learns from a massive amount of data, including books, websites, research papers, and programming code. This is where the Pre-trained part of GPT (Generative Pre-trained Transformer) comes from.

When we send a prompt to the model, the first step is tokenization, where the text is broken into smaller units called tokens. Every LLM has its own vocabulary, which is simply the collection of tokens it understands. A single word may even be split into multiple tokens.

Next comes token embedding, where every token is converted into a mathematical vector. Tokens with similar meanings are placed close to each other in a vector space, allowing the model to understand relationships between words rather than treating them as plain text.

After that, positional embedding is added. This tells the model the position of every token in the sentence. This is important because sentences like "Dog bites man" and "Man bites dog" contain almost the same words but have completely different meanings. Position helps the model understand this difference.

The token embedding and positional embedding are combined to form the input embedding, which is then passed into the Transformer.

The Transformer is the engine of modern LLMs. Before Transformers, AI models such as RNNs processed text one word at a time, making it difficult to understand long-range relationships and slowing down training. The Transformer changed this by introducing self-attention, where every token can interact with other relevant tokens in the sentence to understand the complete context.

The attention scores are then converted into probabilities using Softmax, helping the model decide how much attention each token deserves. The Transformer also uses multi-head attention, where multiple attention mechanisms work in parallel to capture different types of relationships, and a feed-forward network, which further refines the information. These Transformer layers are repeated many times, sometimes dozens or even hundreds of times in larger models.

Finally, the model predicts a probability distribution over its entire vocabulary, selects the most appropriate next token, appends it to the sentence, and repeats this process until the complete response is generated.

Learning how an LLM works made me realize that AI is not magic. It is an incredible combination of mathematics, probability, neural networks, and engineering. The more I learn about it, the more fascinated I become, and this is just the beginning of my journey into the world of Artificial Intelligence.