Generative AI
Generative AI creates new content text, images, audio, video, code by learning the underlying distribution of training data. Foundation models like GPT-4, Claude, Gemini, and DALL-E have transformed what's possible with AI in production.
Contents 5 Sections
1How Generative Models Work
The Core Idea Generative models learn P(data) the probability distribution of training examples so they can sample new, realistic instances from that distribution.
Major Model Families
- Transformer-Based LLMs (GPT, Claude, Gemini, LLaMA)
- Decoder-only architecture for autoregressive generation
- Predict next token given all previous tokens
- Trained on trillions of tokens from the internet
- Scale (parameters × data × compute) drives emergent capabilities
- Diffusion Models (Stable Diffusion, DALL-E, Midjourney)
- Forward pass: add Gaussian noise to image in T steps
- Reverse pass: train model to denoise step by step
- At inference: start with random noise, iteratively denoise
- Conditioning: guide generation with text prompts via CLIP
- GANs (Generative Adversarial Networks)
- Generator: creates fake samples
- Discriminator: distinguishes real vs fake
- Adversarial training: generator improves until discriminator can't tell the difference
- Best for: high-fidelity images, video, style transfer
- VAEs (Variational Autoencoders)
- Encode data into a compressed latent space
- Decode latent vectors back to data
- Controllable generation by manipulating latent space
2Text Generation & LLMs
Token Prediction
``
Input: "The capital of France is"
Model: P(next_token | context)
Output: " Paris" (highest probability token)
``
Sampling Strategies ```python from openai import OpenAI client = OpenAI()
response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Explain quantum computing"}], temperature=0.7, # 0 = deterministic, 1 = creative, 2 = chaotic top_p=0.95, # nucleus sampling: sample from top 95% probability mass max_tokens=500, presence_penalty=0.1, # penalize repeated topics frequency_penalty=0.1 # penalize repeated exact words ) print(response.choices[0].message.content) ```
Context Window the model's "working memory" | Model | Context Window | |---|---| | GPT-4o | 128K tokens (~96K words) | | Claude 3.5 Sonnet | 200K tokens | | Gemini 1.5 Pro | 1M tokens | | LLaMA 3.1 | 128K tokens |
Tokenization
``python
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4o")
tokens = enc.encode("Hello, world!")
print(tokens) # [9906, 11, 1917, 0]
print(len(tokens)) # 4 tokens
# Rule of thumb: ~4 chars per token in English
``
3Image Generation
Stable Diffusion Pipeline ```python from diffusers import StableDiffusionPipeline import torch
pipe = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16 ).to("cuda")
image = pipe( prompt="A serene mountain lake at sunset, photorealistic, 8K", negative_prompt="blurry, low quality, cartoon", num_inference_steps=50, # more steps = better quality guidance_scale=7.5, # how closely to follow the prompt width=512, height=512 ).images[0]
image.save("output.png") ```
DALL-E 3 API
``python
response = client.images.generate(
model="dall-e-3",
prompt="A professional photo of a modern office with plants and natural light",
size="1024x1024",
quality="hd",
n=1
)
image_url = response.data[0].url
``
- Key Concepts
- Text-to-Image: CLIP encodes prompt → guides diffusion process
- Inpainting: replace masked regions of existing image
- Outpainting: extend image beyond its borders
- Image-to-Image: transform existing image with prompt guidance
- LoRA: fine-tune diffusion models on a specific style/subject with minimal compute
4Production Deployment
Inference Optimization ```python # Quantization: reduce model size (4-bit or 8-bit) from transformers import AutoModelForCausalLM, BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(load_in_4bit=True) model = AutoModelForCausalLM.from_pretrained( "meta-llama/Llama-3-8B", quantization_config=bnb_config, device_map="auto" ) # 8B model: ~16GB fp16 → ~4GB int4 ```
- Serving Infrastructure
- vLLM: PagedAttention 24x higher throughput than naive serving
- TGI (Text Generation Inference): HuggingFace's production server
- Ollama: run models locally (llama.cpp backend)
- LiteLLM: unified API over OpenAI, Anthropic, Gemini
Cost Estimation (OpenAI pricing) ``` gpt-4o-mini: $0.15 per 1M input tokens gpt-4o: $2.50 per 1M input tokens
Monthly cost for 10K users × 500 tokens/day: = 10,000 × 500 × 30 = 150M tokens/month = gpt-4o-mini: $22.50/month = gpt-4o: $375/month ```
- Guardrails & Safety
- Content filtering: OpenAI moderation API
- Output validation: Guardrails AI, NeMo Guardrails
- Hallucination detection: RAG with source citation
- PII detection: Presidio, AWS Comprehend
5Fine-Tuning & Alignment Techniques
LoRA Fine-Tuning on Custom Data ```python from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments from peft import get_peft_model, LoraConfig, TaskType from trl import SFTTrainer import torch
# Load base model with 4-bit quantization from transformers import BitsAndBytesConfig bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, )
model = AutoModelForCausalLM.from_pretrained( "meta-llama/Llama-3-8B-Instruct", quantization_config=bnb_config, device_map="auto", ) tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3-8B-Instruct")
# LoRA configuration lora_config = LoraConfig( r=16, # rank: trade-off between quality and parameters lora_alpha=32, # scaling factor (usually 2×r) target_modules=["q_proj", "v_proj", "k_proj", "o_proj"], lora_dropout=0.1, bias="none", task_type=TaskType.CAUSAL_LM, ) model = get_peft_model(model, lora_config) model.print_trainable_parameters() # Trainable: 0.1% of 8B params fast and memory efficient
# Training training_args = TrainingArguments( output_dir="./fine-tuned-llama", num_train_epochs=3, per_device_train_batch_size=4, gradient_accumulation_steps=4, learning_rate=2e-4, warmup_ratio=0.03, lr_scheduler_type="cosine", fp16=True, report_to="wandb", )
trainer = SFTTrainer( model=model, tokenizer=tokenizer, args=training_args, train_dataset=train_dataset, dataset_text_field="text", max_seq_length=2048, ) trainer.train() trainer.save_model("./fine-tuned-llama/final") ```
Explore the Full Portfolio
This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.
The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.
For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.