Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 3 0 LLM

Loading ā³
Fetching content, this won’t take long…


šŸ’” Did you know?

🦄 Sloths can hold their breath longer than dolphins 🐬.

šŸŖ This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ā³
Fetching content, this won’t take long…


šŸ’” Did you know?

šŸ™ Octopuses have three hearts and blue blood.
AI-GenAI

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI
    • NVIDIA AI-LLM Developers Certification Path


    • Understanding Generative AI


    • What is AI Models and How to pick the right one?


    • How to Choose the Right AI Model for Your Use Case


    • What are Transformer Models?


    • Retrieval-Augmented Generation (RAG) for AI Applications


    • LLMs & Foundation Models Explained


    • Using LLMs in Development


    • Using LLMs in Production


    • Ethical AI vs Responsible AI vs Trustworthy AI


    • Generative Adversarial Networks (GANs) Explained


    • U-Net Explained


    • Understanding CLIP: Connecting Images and Text in Generative AI


    • Diffusion Models Explained


    • The Economic Impact of Generative AI


    • NVIDIA Certified Associate Generative AI (NCA-GENL) Practice Questions


    • AI-GenAI Index


    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

Cover Image for LLMs & Foundation Models Explained
AI-GenAI

LLMs & Foundation Models Explained

A practical guide to Large Language Models (LLMs) and foundation models, covering architectures, training concepts, fine-tuning, inference, embeddings, RAG, and real-world AI application development.

LLM
Foundation Models
Generative AI
Artificial Intelligence
Transformers
Machine Learning
← Previous

NVIDIA NGC Catalog: GPU Optimized Containers, AI Models and Enterprise AI Infrastructure

Next →

Understanding Generative AI

What is Large Language Model (LLM)

A Large Language Model is a sophisticated mathematical function that predicts what word comes next for any piece of text"

LLM is a type of foundation model specifically designed to understand and generate human language.

White Paper: https://web.stanford.edu/~jurafsky/slp3/ed3book.pdf

🧱 Foundation Models (FMs)

Large-scale models trained on broad data that can be adapted to a wide range of downstream tasks.

  • Examples: GPT-3, BERT, DALL-E, Stable Diffusion

Characteristics:

  • Trained on massive datasets (text, images, code)
  • Capable of zero-shot and few-shot learning
  • Serve as a base for fine-tuning on specific tasks

Autoregressive language model

A type of language model that generates text by predicting the next word in a sequence based on the previous words.

  • Example: GPT-3, LLaMA, Mistral, Falcon

🧠 Large Language Models (LLMs)

A subset of foundation models that are specifically designed to understand and generate human language.

How Transformer Store Information
ProviderTypeDescription
AWS Bedrockaws_bedrockAWS Bedrock API
Azure OpenAIazure_openaiAzure OpenAI API
Hugging FacehuggingfaceHugging Face API
Hugging Face Inferencehuggingface_inferenceHugging Face Inference API, Endpoints, and TGI
LiteLLMlitellmLiteLLM API
NVIDIA NIMnimNVIDIA Inference Microservice (NIM)
OCI Generative AIociOCI Generative AI
OpenAIopenaiOpenAI API

Examples: GPT-3, BERT, T5

  • Characteristics:
    • Trained on vast amounts of text data
    • Able to recognize and interpret human language
    • Flexible: can perform tasks like text generation, translation, summarization, and question-answering

Parameter Tuning in LLM

Use low temperature and low top-p for agents, planners, tool calls, and structured outputs.

Use higher temperature and top-p for creative content generation and brainstorming.

from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-5.5",
    input="Generate deployment steps for a Grafana dashboard import.",
    temperature=0.1,
    top_p=0.2
)

print(response.output_text)

Choosing Top-P & Temperature

Use CasešŸŒ”ļø TemperatureTop-p šŸŽ²
JSON generation0.00.1
Tool calling0.00.1
Code generation0.10.2
RAG QA0.20.8
Summarization0.30.9
Creative writing0.80.95
Brainstorming1.01.0

1. Temperature šŸŒ”ļø

Changes the shape of the probability distribution.

Temperature = changes the probabilities of tokens.

Temperatureā†“ā€…ā€Šā†’ā€…ā€ŠRandomnessā†“ā€…ā€Šā†’ā€…ā€ŠDeterminism↑\text{Temperature} \downarrow \;\rightarrow\; \text{Randomness} \downarrow \;\rightarrow\; \text{Determinism} \uparrowTemperature↓→Randomness↓→Determinism↑

ā„Ā®Ā LowĀ Temperatureā€…ā€Šā†’ā€…ā€ŠLessĀ Randomnessā€…ā€Šā†’ā€…ā€ŠMoreĀ ConfidentĀ Choices\text{ā„ļø Low Temperature} \;\rightarrow\; \text{Less Randomness} \;\rightarrow\; \text{More Confident Choices}ā„Rā—ÆĀ LowĀ Temperature→LessĀ Randomness→MoreĀ ConfidentĀ Choices

šŸ”„Ā HighĀ Temperatureā€…ā€Šā†’ā€…ā€ŠMoreĀ Randomnessā€…ā€Šā†’ā€…ā€ŠMoreĀ CreativeĀ Choices\text{šŸ”„ High Temperature} \;\rightarrow\; \text{More Randomness} \;\rightarrow\; \text{More Creative Choices}šŸ”„Ā HighĀ Temperature→MoreĀ Randomness→MoreĀ CreativeĀ Choices


2. Top-p / Nucleus Sampling šŸŽ²

Top K dynamically adjusts the number of tokens considered based on their cumulative probability.

Top=P sampling selects the smallest set of tokens whose cumulative probability exceeds a threshold P (a value between 0 and 1).

Top-p = changes which tokens are allowed to participate in sampling.

šŸŽ²Ā Top-pĀ (NucleusĀ Sampling)ā†“ā€…ā€Šā†’ā€…ā€ŠFewerĀ CandidateĀ Tokensā€…ā€Šā†’ā€…ā€ŠMoreĀ DeterministicĀ Output\text{šŸŽ² Top-p (Nucleus Sampling)} \downarrow \;\rightarrow\; \text{Fewer Candidate Tokens} \;\rightarrow\; \text{More Deterministic Output}šŸŽ²Ā Top-pĀ (NucleusĀ Sampling)↓→FewerĀ CandidateĀ Tokens→MoreĀ DeterministicĀ Output

šŸŽ²Ā Top-pĀ (NucleusĀ Sampling)ā†‘ā€…ā€Šā†’ā€…ā€ŠMoreĀ CandidateĀ Tokensā€…ā€Šā†’ā€…ā€ŠMoreĀ DiverseĀ Output\text{šŸŽ² Top-p (Nucleus Sampling)} \uparrow \;\rightarrow\; \text{More Candidate Tokens} \;\rightarrow\; \text{More Diverse Output}šŸŽ²Ā Top-pĀ (NucleusĀ Sampling)↑→MoreĀ CandidateĀ Tokens→MoreĀ DiverseĀ Output

Example

Tokens:

TokenProbability
"the"40%
"a"25%
"this"15%
"that"10%
Others10%

Top-p = 1.0

All tokens remain eligible. Maximum diversity.

Eg: Where every bean begins a new adventure.

Top-p = 0.9

The model will consider the smallest number of tokens whose combined probability is 90%.

the   40%
a     25%   -> 65%
this  15%   -> 80%
that  10%   -> 90%

The remaining low-probability tokens are discarded.

Eg. Wake up to a cup of inspiration.

Top-p = 0.3

the 40%

Only the highest-probability token is eligible.

Output becomes highly deterministic.

Eg. Fresh coffee, every day.

Typical Values

Top-pBehaviorMeaning
0.1 - 0.3Very deterministicLess Choices
0.5 - 0.7ControlledFresh coffee, every day.
0.8 - 0.95BalancedMedium
1.0Maximum diversityCreative

3. Max Tokens šŸ—Ø

Limits the number of output tokens the model can generate.

Example: Max Tokens = 50

The model stops after approximately 50 output tokens.

Small Value

Max Tokens = 20

Response may be cut off.

The migration plan consists of three phases:
1. Assessment
2. Pilot
3...

Large Value:

Max Tokens = 2000

The model can provide a detailed answer.

ParameterControlsEffect
Top-pRandomness / token selectionHow creative or deterministic the output is
Max TokensResponse lengthHow long the model is allowed to generate

Prompting Techniques in Generative AI šŸ’¬

TechniqueMental ModelExamples Provided?
Zero-Shot"Just do it"No
One-Shot"Here is one example"One
Few-Shot"Learn from these examples"Multiple
CoT"Think step by step"Optional
System Prompt"Behave like this"Persistent instruction

1. Zero-Shot Prompting šŸ‘Øā€šŸ¦Æ

Blindly asking LLM to generate text without giving a direction or example

  • useful for simple and well-defined tasks
  • Most common
  • fast inference

Example: "Suggest newborn baby name"

Expected: Random baby names

Aarav — peaceful, calm
Vihaan — dawn, new beginning
Ivaan — God’s gracious gift
Reyansh — ray of light
...

2. One-Shot Prompting ā˜

When a single example clarifies task format or style; helps guide the model with minimal context

The model receives:

  • one example
  • then the real task

Best For

  • formatting guidance
  • classification tasks
  • lightweight context steering

Example: "Suggest newborn baby name starting with A eg: Aaryan"

Expected: Indian Baby names starting with A

Aarav — peaceful, calm
Aadvik — unique
Ayaan — gift of God
Atharv — wisdom, knowledge
...

3. Few-Shot Prompting šŸ“

When multiple examples are needed to teach the model patterns or nuanced behavior

The model learns patterns from multiple examples.

Best For

  • nuanced tasks
  • structured outputs
  • custom formatting
  • behavior steering

Example: "Suggest newborn baby name starting with A eg: Aaryan"

Expected:

Aarav — peaceful
Aaryan — noble
Ayaan — gift of God
...

4. Chain-of-Thought (CoT) šŸ”—

When reasoning or multi-step logic is required; improves reasoning accuracy by generating intermediate steps Chain-of-Thought prompting encourages:

  • intermediate reasoning
  • multi-step thinking

CoT improves:

  • reasoning accuracy
  • logical consistency
  • math performance
  • planning tasks

Especially useful for:

  • LLM agents
  • coding tasks
  • complex workflows

Example

Question:
If a train travels 60 km/h for 2 hours,
how far does it travel?

Let's think step by step.

Expected reasoning:

Distance = Speed Ɨ Time
60 Ɨ 2 = 120 km

5. System Prompting šŸ“œ

When you want to control model behavior, tone, safety, or output formatting consistently

System prompts define:

  • model behavior
  • personality
  • rules
  • tone
  • response style

Best For

  • chatbots
  • enterprise AI
  • compliance
  • formatting rules
  • safety policies

Example

You are a professional support assistant.
Always respond politely and concisely.

šŸ” Transfer Learning

Using a model pretrained on a large dataset and adapting it for a related task with limited new data.

A model trained on millions of images already understands edges, textures, faces, animals, etc.

You fine-tune it to detect:

  • cancer cells
  • defective products
  • cats vs dogs
  • traffic signs

Advantages

  • Faster training
  • Less data required (1000 vs 1 million)
  • Better accuracy
  • Lower compute cost
  • Works well for small datasets

Disadvantage

  • Source task and target task should be somewhat related
  • Biases from pretrained data can transfer
  • Large models may still be expensive

Popular models

Computer Vision

  • ResNet
  • VGGNet
  • EfficientNet
  • YOLO

NLP

  • BERT
  • GPT
  • T5

šŸŽ› Fine-Tuning

Fine-tuning adapts a model to a specific task.

  • Tune model to understand domain-specific language eg medical, legal, finance
  • Adapt model using smaller domain dataset

Transfer learning vs Fine-tuning

  • Transfer learning = broader concept
  • Fine-tuning = one implementation approach

Use cases:

  • domain-specific language
  • structured outputs
  • company-specific style

šŸ§— Pretraining

Train on massive internet text

  • Only makes sense for large organizations with unique data and resources

When Should You Pretrain a Model?

Pretraining an LLM is extremely expensive.

Typical requirements:

  • hundreds of billions of tokens
  • months of training
  • tens of millions of dollars

For most application teams, pretraining should be an option of last resort. It only makes sense when the domain is highly specialized and existing models cannot be adapted effectively.

Typical scale:

StageData Size
Pretrainingbillions of tokens
Fine-tuningthousands of examples

āš—ļø Knowledge Distillation

Large, powerful model (Teacher) transfers learned behavior to a smaller model (Student), enabling similar performance with lower compute and memory usage.

  • Knowledge Distillation → senior employee mentoring a junior employee

Example

  • Using a large GPT model to train a lightweight chatbot model for mobile devices.

Use case:

  • Mainly used to deploy efficient models on edge/mobile devices
ConceptMain Goal
Transfer LearningReuse learned knowledge
Fine-TuningAdapt pretrained model to specific task
Knowledge DistillationCompress knowledge into smaller model

Decision Ladder

flowchart TD
    A[Start with prompting] --> B{Good enough?}
    B -- Yes --> Z[Deploy]
    B -- No --> C[Try RAG]
    C --> D{Good enough?}
    D -- Yes --> Z
    D -- No --> E[Try fine-tuning]
    E --> F{Good enough?}
    F -- Yes --> Z
    F -- No --> G[Consider pretraining as last resort]

Therefore, it should be considered a last resort.

Most applications use:

  • Prompting
  • RAG
  • Fine-tuning

RLHF (Reinforcement Learning From Human Feedback)

RLHF trains a reward model that scores answers.

  • Higher scores go to responses that are more helpful, honest, and harmless.

We can describe the reward idea as:

r=Reward(response∣prompt)r = \text{Reward}(\text{response} \mid \text{prompt})r=Reward(response∣prompt)

Then the model is optimized to produce responses with higher expected reward:

max⁔πE[r]\max_{\pi} \mathbb{E}[r]Ļ€max​E[r]

where Ļ€\piĻ€ is the model’s response policy.

RLHF Flow Diagram

flowchart TD
    P[Prompt] --> G[Model generates candidate responses]
    G --> H[Humans score responses]
    H --> RM[Train reward model]
    RM --> FT[Further train model to prefer high-reward responses]

This is one reason chat systems feel more aligned, polite, and useful than raw base models.

šŸ•µšŸ» Agents

Agents use LLMs to perform multi-step reasoning and actions.

Example task:

Research BetterBurgers competitors

Agent plan:

  1. Search competitors
  2. Visit websites
  3. Summarize each company

Agent Workflow Diagram

flowchart TD
    U[User goal] --> P[LLM plans steps]
    P --> S[Search]
    S --> V[Visit websites]
    V --> R[Read content]
    R --> M[Summarize findings]
    M --> O[Return final answer]

Agents are still an active research area, but the core idea is already useful: combine reasoning, planning, and tools to solve multi-step tasks.

The LLM acts as a controller that decides which tools to use.


Related Posts

  • Retrieval-Augmented Generation (RAG) for AI Applications — the most common pattern for grounding these foundation models
  • Using LLMs in Development — using these foundation models day-to-day while building
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Wed May 13 2026

Share This on

← Previous

NVIDIA NGC Catalog: GPU Optimized Containers, AI Models and Enterprise AI Infrastructure

Next →

Understanding Generative AI

AI-GenAI/3-0-LLM
Let's work together
hiteshkrsahu@gmail.com
Munich 🄨, Germany šŸ‡©šŸ‡Ŗ, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
Ā  Home/About
Ā  Skills
Ā  Work/Projects
Ā  Lab/Experiments
Ā  Contribution
Ā  Awards
Ā  Art/Sketches
Ā  Thoughts
Ā  Contact
Links
Ā  Sitemap
Ā  Legal Notice
Ā  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| Ā© 2026 All rights reserved.