02. LLM Concepts, Prompting, Structured Outputs & APIs

Sections 11-21: Instruction Tuning, Alignment, Inference Decoding, Temperature, Prompt Engineering, Messages, Few-Shot/CoT, JSON Schemas, Function Calling, APIs & Quantization.

11. Instruction Tuning and Alignment

🇬🇧 English Definition
Instruction tuning (SFT) fine-tunes base pretrained models on curated instruction-response pairs. Alignment techniques like RLHF and DPO shape model behavior toward helpfulness, honesty, and safety.
🇮🇳 Hinglish Definition
Base pre-trained model sirf text complete karta hai. Instruction tuning use assistant banana sikhata hai. Alignment (RLHF / DPO) toxic ya harmful outputs ko filter out karke safe responses align karta hai.
// Supervised Fine-Tuning Data Structure Example sft_dataset_sample = { "instruction": "Summarize the key benefit of transformers.", "input": "Transformers allow parallel token processing via attention.", "output": "The core benefit of transformers is parallelized processing using attention mechanisms." } print("SFT Target:", sft_dataset_sample["output"])
🔍 Line-by-Line Code Breakdown
  • sft_dataset_sample: Standard JSON key-value format for instruction tuning LLMs.
🎯 Interview Focus & Questions

Q: What is the difference between SFT and RLHF?
A: SFT trains the model directly on human-written gold responses, while RLHF optimizes model policies based on human preference feedback using a Reward Model.

12. Inference and Decoding

🇬🇧 English Definition
Inference runs a trained model to generate predictions. Decoding algorithms (Greedy search, Beam search, Temperature sampling) determine how tokens are selected from predicted output probability distributions.
🇮🇳 Hinglish Definition
Training ke baad trained model par user query chalana inference hai. Decoding strategy decide karti hai ki high probability token direct lein (Greedy) ya creative variations add karein (Sampling).
// Greedy vs Random Sampling Decoding Simulation import numpy as np logits = np.array([0.1, 0.7, 0.2]) // Token A, B, C // 1. Greedy Choice greedy_choice = np.argmax(logits) // 2. Softmax Sampling probs = np.exp(logits) / np.sum(np.exp(logits)) sampled_choice = np.random.choice(len(probs), p=probs) print("Greedy Choice Index:", greedy_choice) print("Sampled Choice Index:", sampled_choice)
🔍 Line-by-Line Code Breakdown
  • np.argmax(logits): Always picks token with highest probability (deterministic greedy decoding).
  • np.random.choice(..., p=probs): Samples probabilistically, introducing creative response diversity.
🎯 Interview Focus & Questions

Q: What is Beam Search?
A: Beam search maintains a fixed number (beam width) of top partial candidate sequences at each decoding step, yielding higher quality generations for translation or summarization.

13. Temperature, Top-k and Top-p

🇬🇧 English Definition
Temperature scales logit sharpness (lower = deterministic, higher = creative). Top-k restricts sampling to the top k probable tokens. Top-p (nucleus sampling) dynamic cuts off the cumulative probability threshold p.
🇮🇳 Hinglish Definition
Temperature sampling ki randomness control karta hai. Top-k top k choices tak limit karta hai. Top-p (nucleus sampling) cumulative probability limit (like 0.9) rakhta hai taaki rare non-sense words eliminate ho sakein.
// Temperature Scaling Function Implementation import numpy as np def apply_temperature(logits, temperature=0.7): if temperature <= 0: // Argmax fallback for zero temperature res = np.zeros_like(logits) res[np.argmax(logits)] = 1.0 return res // Scale logits by temperature scaled_logits = logits / temperature probs = np.exp(scaled_logits) / np.sum(np.exp(scaled_logits)) return probs raw_logits = np.array([2.0, 1.0, 0.5]) print("Temp 0.2 Probs:", apply_temperature(raw_logits, 0.2)) print("Temp 1.5 Probs:", apply_temperature(raw_logits, 1.5))
🔍 Line-by-Line Code Breakdown
  • scaled_logits = logits / temperature: Dividing by temperature > 1 flattens probability distribution (higher randomness); dividing by temperature < 1 sharpens peaks (higher determinism).
🎯 Interview Focus & Questions

Q: Which decoding settings are best for SQL generation vs Creative Writing?
A: SQL generation requires low temperature (0.0 - 0.2) and low Top-p for strict precision. Creative writing benefits from higher temperature (0.7 - 0.9) and Top-p (0.9).

14. Prompt Engineering

🇬🇧 English Definition
Prompt engineering is the strategic design of instructions, context, constraints, and formatting rules to guide language models toward optimal, consistent outputs without changing model weights.
🇮🇳 Hinglish Definition
Prompt engineering ka matlab hai clear instruction, task description, context, constraints, and output format specification likhna taaki LLM exact desired answer de sake.
// Structured Prompt Template Construction def build_prompt(role, task, context, constraints, output_format): prompt = f"""Role: {role} Task: {task} Context: {context} Constraints: {constraints} Output Format: {output_format}""" return prompt prompt = build_prompt( role="Senior Python Engineer", task="Review code snippet for memory leaks", context="High-concurrency FastAPI server under heavy load", constraints="Must list top 3 issues with fix recommendations", output_format="JSON object with keys: issues, severity, fix" ) print(prompt)
🔍 Line-by-Line Code Breakdown
  • build_prompt(...): Standardized template function structuring system role, task, context, constraints, and target schema.
🎯 Interview Focus & Questions

Q: What are the core pillars of effective Prompt Engineering?
A: Explicit role assignment, unambiguous task definition, relevant domain context, strict boundary constraints, and explicit output formatting examples.

15. System, User and Assistant Messages

🇬🇧 English Definition
Modern chat APIs separate prompts into System (developer policy/guidance), User (end-user request), and Assistant (model responses or conversational history) roles.
🇮🇳 Hinglish Definition
Chat format mein 3 roles hote hain: System (App guidelines/rules), User (End user query), and Assistant (AI model response/conversation history).
// Standard OpenAI-style Message Array Payload messages = [ {"role": "system", "content": "You are a helpful customer support bot for an e-commerce platform."}, {"role": "user", "content": "My order ID-12345 has not arrived yet."}, {"role": "assistant", "content": "I apologize for the delay! Let me look up order ID-12345 in our database."}, {"role": "user", "content": "Thank you, please update me quickly."} ] print("Last user message:", messages[-1]["content"])
🔍 Line-by-Line Code Breakdown
  • messages payload: Array of structured dictionary objects maintaining role attribution and conversation memory.
🎯 Interview Focus & Questions

Q: Why is it crucial to treat User messages as untrusted input?
A: Malicious users can attempt prompt injection inside the User role to override system instructions defined in the System role.

16. Few-shot and Chain-of-Thought Concepts

🇬🇧 English Definition
Few-shot prompting provides input-output examples inside the prompt. Chain-of-Thought (CoT) guides models to decompose complex reasoning tasks step-by-step before producing the final answer.
🇮🇳 Hinglish Definition
Few-shot mein model ko 2-3 input-output examples diye jaate hain pattern samjhane ke liye. Chain-of-Thought (CoT) prompt model ko step-by-step reason karne bolta hai (e.g. 'Let's think step by step').
// Few-Shot Prompt Template few_shot_prompt = """Classify customer feedback sentiment. Example 1: Input: "The product delivered two days early and works great!" Sentiment: Positive Example 2: Input: "Broken package, terrible service." Sentiment: Negative Input: "The device is okay, but delivery was average." Sentiment:""" print(few_shot_prompt)
🔍 Line-by-Line Code Breakdown
  • few_shot_prompt: Formats past examples to anchor the LLM's output style and classification boundaries.
🎯 Interview Focus & Questions

Q: How does Chain-of-Thought improve reasoning in LLMs?
A: CoT forces the autoregressive model to generate intermediate reasoning tokens, giving the transformer additional computational steps to reach complex mathematical or logical conclusions.

17. Structured Outputs and JSON

🇬🇧 English Definition
Structured output enforces or constrains model generation to conform strictly to a machine-readable JSON Schema or Pydantic class, preventing parsing errors in API integrations.
🇮🇳 Hinglish Definition
Structured output se model ka response strict JSON schema / Pydantic model mein milta hai, taaki backend code raw text parsing failures se bach sake.
// Pydantic Schema Definition for Structured Output from pydantic import BaseModel, Field from typing import List class TechSkillExtraction(BaseModel): candidate_name: str = Field(description="Name of the job applicant") primary_skills: List[str] = Field(description="List of top programming languages") years_experience: float = Field(description="Total years of technical experience") // Simulating validated JSON conversion sample_json = '{"candidate_name": "Gagan", "primary_skills": ["Python", "PyTorch"], "years_experience": 6.5}' parsed_data = TechSkillExtraction.model_validate_json(sample_json) print("Parsed Candidate:", parsed_data.candidate_name)
🔍 Line-by-Line Code Breakdown
  • TechSkillExtraction(BaseModel): Defines strict static schema layout with field type hints.
  • model_validate_json(...): Validates raw LLM response against Pydantic schema.
🎯 Interview Focus & Questions

Q: How do modern provider APIs guarantee valid JSON outputs?
A: Provider APIs use grammar-guided decoding (e.g., JSON schema constraints directly applied to token probability sampling at inference time).

18. Function Calling / Tool Use

🇬🇧 English Definition
Function calling allows an LLM to detect when external code execution is required, outputting structured function names and arguments for the client application to run safely.
🇮🇳 Hinglish Definition
Function calling se LLM application ko batata hai ki kaunsa local function (e.g. database query, weather API, calculator) specific arguments ke sath call karna hai.
// Function Calling Spec Definition in Python tools_schema = [ { "name": "get_stock_price", "description": "Retrieves live stock price for a given ticker symbol", "parameters": { "type": "object", "properties": { "ticker": {"type": "string", "description": "Stock symbol, e.g. GOOGL"} }, "required": ["ticker"] } } ] // Simulated Model Tool Call Output model_tool_call = { "name": "get_stock_price", "arguments": {"ticker": "GOOGL"} } print("Executing Tool:", model_tool_call["name"], "with args:", model_tool_call["arguments"])
🔍 Line-by-Line Code Breakdown
  • tools_schema: Standard OpenAPI JSON specification supplied to the LLM.
  • model_tool_call: Model-generated function call intent consumed and executed by the backend server.
🎯 Interview Focus & Questions

Q: Does the LLM directly execute the external function during Function Calling?
A: No! The LLM only generates the JSON function call proposal. The hosting application validates arguments, executes the actual code, and returns the result back to the LLM.

19. LLM APIs with Python

🇬🇧 English Definition
LLM APIs allow Python applications to send HTTP requests to remote model inference servers securely using SDK clients, managing API keys via environment variables.
🇮🇳 Hinglish Definition
Python application API SDKs (like OpenAI, Anthropic, Gemini, Groq) to use karke securely environment variables se keys read karke model calls run karta hai.
// Generic LLM API Call Pattern in Python import os def call_llm_api(prompt_text): // Read secret API Key from environment api_key = os.getenv("GENAI_API_KEY", "mock_key_for_demo") // Simulating SDK client request payload = { "model": "genai-v1-pro", "messages": [{"role": "user", "content": prompt_text}], "temperature": 0.3 } return f"Response to '{prompt_text}' processed with key present ({bool(api_key)})" print(call_llm_api("Summarize GenAI Roadmap"))
🔍 Line-by-Line Code Breakdown
  • os.getenv('GENAI_API_KEY'): Reads credentials safely from runtime OS environment without hardcoding secrets in source code.
🎯 Interview Focus & Questions

Q: Why should API Keys never be committed to Git repositories?
A: Exposed API keys can be scraped by automated bots, leading to unauthorized API usage, quota exhaustion, data leakage, and unexpected billings.

20. Open-Source Models & Hugging Face

🇬🇧 English Definition
Open-source foundation models (e.g. Llama 3, Mistral, Qwen) allow developers to inspect model weights, host private inference pipelines, and customize architectures using the Hugging Face Transformers ecosystem.
🇮🇳 Hinglish Definition
Open-source models (Llama, Mistral) developers ko full control, privacy aur custom hosting ki capability dete hain. Hugging Face `transformers` library se in models ko load aur run kiya ja sakta hai.
// Hugging Face Transformers Pipeline Concept from transformers import pipeline // Load lightweight text generation pipeline concept // generator = pipeline("text-generation", model="gpt2") print("Hugging Face pipeline loaded successfully for local/cloud deployment.")
🔍 Line-by-Line Code Breakdown
  • pipeline('text-generation'): High-level Hugging Face abstraction for tokenization, model forward pass, and decoding.
🎯 Interview Focus & Questions

Q: What are the main trade-offs between Open-Source Models and Proprietary API Models?
A: Proprietary APIs offer instant state-of-the-art capability with zero GPU management. Open-source models offer complete data privacy, offline capability, zero per-token cost at scale, and full fine-tuning control.

21. Local LLMs & Model Quantization

🇬🇧 English Definition
Quantization compresses model parameter precision (e.g., from FP16 to INT8 or INT4), reducing VRAM memory requirements dramatically to enable running LLMs on local consumer hardware.
🇮🇳 Hinglish Definition
Quantization neural weights ki numerical precision ko reduce karta hai (FP16 -> INT4). Isse memory usage 70% tak ghat jaati hai aur laptop GPU/CPU par local LLMs (Ollama/llama.cpp) smoothly chalte hain.
// Memory Reduction Estimation for Quantization def calculate_model_vram(params_billions, precision_bits=16): // 1 billion parameters at 16-bit (2 bytes) = 2 GB base weight size bytes_per_param = precision_bits / 8 vram_gb = params_billions * bytes_per_param // Add 20% overhead for activation context return vram_gb * 1.2 print("7B Model FP16 VRAM:", calculate_model_vram(7, 16), "GB") print("7B Model INT4 VRAM:", calculate_model_vram(7, 4), "GB")
🔍 Line-by-Line Code Breakdown
  • bytes_per_param = precision_bits / 8: FP16 uses 2 bytes per parameter; INT4 uses 0.5 bytes per parameter.
  • INT4 VRAM: Reduces 7B model memory footprint from ~16.8 GB down to ~4.2 GB.
🎯 Interview Focus & Questions

Q: What is GGUF and AWQ?
A: GGUF is a file format optimized for CPU/GPU local inference with llama.cpp. AWQ (Activation-aware Weight Quantization) is a GPU quantization method that preserves model accuracy by protecting salient weight channels.