Skip to content
Large Language Models: Transformers, RAG, and Fine-Tuning

31 May 2026

Large Language Models: Transformers, RAG, and Fine-Tuning

How transformers work, practical patterns like RAG and fine-tuning, and how to use vector databases for LLM applications.

AI Software
LLM Transformers RAG Vector Databases

Large Language Models (LLMs) are the foundation of modern AI assistants. Understanding how they work and how to build with them is essential.

How Transformers Work

The Transformer architecture (2017, “Attention Is All You Need”) is the foundation of all modern LLMs.

Input Text → Tokenizer → Embeddings → Transformer Blocks → Output
                                            │
                                     Self-Attention
                                            +
                                     Feed-Forward

Key Concepts

ConceptWhat It Does
TokenizerSplits text into tokens (words or subwords)
EmbeddingConverts tokens into numerical vectors
Self-AttentionEach token “looks at” all other tokens to understand context
Positional EncodingAdds position information (word order matters)
Feed-ForwardProcesses each token independently after attention

Self-Attention (Simplified)

For each word, the model asks: “How much should I pay attention to every other word?”

"The cat sat on the mat"

For "sat": attention weights might be:
  The=0.1  cat=0.5  sat=0.1  on=0.2  the=0.0  mat=0.1

Types of LLM Models

TypeTrainingExamples
Base modelPredict next tokenGPT base, Llama base
Instruction-tunedFollow instructionsChatGPT, Claude, Llama-Chat
Fine-tunedSpecialized for a domainMedical, legal, code models

Retrieval-Augmented Generation (RAG)

RAG solves the problem of LLMs not knowing your private data. Instead of training the model, you give it relevant documents at query time.

User Query
    │
    ↓
┌─────────────┐     ┌──────────────┐
│ Embed Query │────→│ Vector DB     │
└─────────────┘     │ Search        │
                    └──────┬───────┘
                           │ Top-K documents
                           ↓
                    ┌──────────────┐
                    │ LLM Prompt:   │
                    │ Context + Query│
                    └──────┬───────┘
                           │
                           ↓
                       Answer

RAG Pipeline

# 1. Index documents
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma

# Split documents into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(documents)

# Store in vector database
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)

# 2. Query
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
relevant_docs = retriever.get_relevant_documents("What is our return policy?")

# 3. Generate answer
prompt = f"Based on these documents: {relevant_docs}\n\nAnswer: {query}"
response = llm.invoke(prompt)

Fine-Tuning

Adapt a pre-trained model to your specific domain or task.

When to Fine-Tune vs RAG

ApproachWhen to Use
PromptingSimple tasks, no custom data
RAGNeed private/current data, factual answers
Fine-tuningNeed specific style, format, or domain behavior
Both (RAG + FT)Maximum control and accuracy

LoRA (Low-Rank Adaptation)

Fine-tune efficiently by training only small adapter layers:

from peft import LoraConfig, get_peft_model

config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.05,
)

model = get_peft_model(base_model, config)
# Train only ~1% of parameters

Vector Databases

Store and search text by meaning (semantic similarity), not keywords.

"How to deploy an app" → [0.2, 0.8, 0.1, ...] → Similar to:
  - "Application deployment guide" (0.92)
  - "Shipping containers to production" (0.85)
  - "Dog training tips" (0.12)
DatabaseTypeKey Feature
ChromaEmbeddedEasy to start, Python native
PineconeManaged cloudScalable, serverless
WeaviateSelf-hosted/cloudHybrid search
QdrantSelf-hosted/cloudHigh performance
pgvectorPostgreSQL extensionUse existing Postgres

LLM Engineering Patterns

Prompt Engineering

# System prompt sets behavior
messages = [
    {"role": "system", "content": "You are a helpful medical assistant. "
     "Only answer based on provided context. Say 'I don't know' if unsure."},
    {"role": "user", "content": "What are the symptoms of..."}
]

Guardrails

Validate LLM outputs:

  • Check for harmful content
  • Validate JSON structure
  • Ensure answers stay on topic
  • Prevent prompt injection

Evaluation

MethodWhat It Measures
Human evaluationQuality, helpfulness
LLM-as-judgeAutomated quality scoring
BLEU/ROUGEText similarity to reference
Retrieval metricsPrecision/recall of retrieved docs

Summary

ConceptKey Idea
TransformersSelf-attention lets the model understand context
RAGGive the LLM relevant documents at query time
Fine-tuningAdapt model behavior for your domain
LoRAEfficient fine-tuning (small % of parameters)
Vector DBsSearch by meaning, not keywords
GuardrailsValidate and control LLM outputs