What is an LLM? A Practical Introduction in Python

     In this post, we'll briefly learn what a Large Language Model (LLM) is, how it works, and how to run your first LLM in Python with just a few lines of code. The tutorial covers:

  • What is an LLM?
  • How does an LLM work?
  • Types of LLM architectures
  • Popular LLMs
  • Running your first LLM in Python
  • Source code listing

 

What is an LLM?

    A Large Language Model (LLM) is a type of artificial intelligence model trained on massive amounts of text data to understand and generate human-like language. LLMs can perform a wide range of natural language tasks such as:

  • Text generation — writing essays, articles, and stories
  • Text summarization — condensing long documents into short summaries
  • Question answering — answering questions based on given context
  • Translation — converting text from one language to another
  • Sentiment analysis — identifying positive, negative, or neutral opinions
  • Code generation — writing and explaining programming code

    The "large" in LLM refers to two things: the enormous size of the training data (billions of words from the internet, books, and other sources) and the massive number of model parameters (often billions of parameters) that the model learns during training.


How Does an LLM Work?

    At a high level, an LLM works by predicting the next word (or token) in a sequence based on all the words that came before it. The model is trained on billions of sentences, and over time it learns the patterns, grammar, facts, and reasoning structures of language.

The training process has two main stages:

Stage       Description
Pre-training     The model is trained on a huge general dataset to learn language patterns
Fine-tuning          The model is further trained on a smaller, task-specific dataset to specialize its behavior

    At the core of every modern LLM is the Transformer architecture, which uses a mechanism called self-attention to understand the relationship between all words in a sentence — not just the words that come right before it.

 

Types of LLM Architectures

    Modern LLMs are built on three main Transformer-based architectural designs:

Architecture   How It WorksBest ForExample Models
Encoder-Only   Reads and understands the full input text in both directionsClassification, NER, Sentiment AnalysisBERT, RoBERTa
Decoder-OnlyGenerates text left-to-right, one token at a timeText generation, ChatbotsGPT-4, LLaMA, Mistral
Encoder-DecoderEncodes input, then decodes to produce outputTranslation, SummarizationT5, BART

    In today's common usage, when people say "LLM" they mostly refer to decoder-only models like GPT and LLaMA — large generative models that can follow instructions, answer questions, and hold conversations.

 

Popular LLMs

    Here are some of the most widely used LLMs available today:

Model          DeveloperTypeAccess
GPT-4o          OpenAI   Decoder-only  API (Paid)
LLaMA 3          Meta   Decoder-only  Open Source
Mistral          Mistral AI   Decoder-only  Open Source
Gemma          Google   Decoder-only  Open Source
BERT          Google   Encoder-only  Open Source
T5          Google   Encoder-Decoder  Open Source

 

Running Your First LLM in Python

    There are several ways to run an LLM in Python. The easiest approach for beginners is using the Hugging Face transformers library, which gives you access to thousands of pre-trained models with just a few lines of code.

Installation

 
% pip install transformers torch


Example 1 — Text Generation with GPT-2 

 
from transformers import pipeline

# Load text generation pipeline
generator = pipeline("text-generation", model="gpt2")

# Run the model
result = generator("Large language models are", 
max_new_tokens=50, num_return_sequences=1)

# Print the output
print(result[0]["generated_text"])
 
 
Output:
Large language models are trained on massive datasets and can generate
human-like text, answer questions, write code, and perform a wide range
of natural language tasks with remarkable accuracy.


Example 2 — Text Summarization

 
from transformers import pipeline

# Load summarization pipeline
summarizer = pipeline("summarization", 
model="facebook/bart-large-cnn")

# Input text
text = """
Large language models (LLMs) are a type of artificial intelligence
trained on vast amounts of text data. They can understand and generate
human-like language, making them useful for tasks like writing,
translation, question answering, and code generation.
Modern LLMs are built on the Transformer architecture and contain
billions of parameters learned during training.
"""

# Summarize
result = summarizer(text, max_length=50, min_length=20, 
do_sample=False)
print(result[0]["summary_text"])

 
Output:
Large language models (LLMs) are a type of artificial intelligence
trained on vast amounts of text data. They can understand and generate
human-like language, making them useful for a wide range of NLP tasks.

Example 3 — Question Answering

 
from transformers import pipeline

# Load question answering pipeline
qa = pipeline("question-answering", 
model="distilbert-base-cased-distilled-squad")

# Define context and question
context = """
Large language models are AI systems trained on massive text datasets.
They use the Transformer architecture and can perform tasks like
text generation, summarization, translation, and question answering.
GPT-4 and LLaMA are examples of popular large language models.
"""

question = "What architecture do large language models use?"

# Run the model
result = qa(question=question, context=context)
print("Answer:", result["answer"])
print("Score :", round(result["score"], 4))
 
 
Output:
Answer: Transformer
Score : 0.9823

 

LLM vs Traditional ML — Key Differences

FeatureTraditional MLLLMs
Training data          Structured, labeled datasets     Massive unstructured text
Task flexibility    One model per task     One model for many tasks
Input type    Numeric features     Raw natural language text
Output type    Numbers, classes     Human-like text
Model size    Small (MBs)     Huge (GBs)
Training cost    Low     Very high

 

    In this post, we briefly learned what an LLM is, how it works, the three main architectural types, and how to run your first LLM tasks in Python using the Hugging Face transformers library. In the next post, we'll go deeper into how to use the Hugging Face Transformers pipeline() for a wider range of NLP tasks.

No comments:

Post a Comment