Data Science + GenAI
Data Analytics + GenAI
Programs from ₹5,000
New Batch Starts: Coming Soon

Generative AI

LLMs, prompt engineering, RAG, and modern AI applications.

Practice Questions

RAG combines retrieval systems with generative AI. Instead of relying solely on the LLM's training data, RAG retrieves relevant documents from a knowledge base (using embeddings and vector databases) and provides them as context to the LLM. This reduces hallucination and enables the model to answer questions about specific, up-to-date information.

Prompt Engineering is the practice of designing and optimizing inputs (prompts) to get desired outputs from LLMs. Techniques include zero-shot prompting, few-shot prompting (providing examples), chain-of-thought reasoning, role-based prompting, and prompt chaining. Good prompt engineering can dramatically improve AI output quality without changing the model.

Generative AI refers to models that create new content — text, images, audio, video or code — that resembles the data they were trained on. Examples include Large Language Models (ChatGPT, Claude, Gemini) for text and diffusion models (Stable Diffusion, DALL·E, Midjourney) for images. Unlike discriminative models that classify or predict labels, generative models learn the underlying data distribution so they can produce new samples.

An LLM is a very large neural network, usually based on the Transformer architecture, trained on huge amounts of text to predict the next token. Through this training it learns grammar, facts, reasoning patterns and coding ability. After pre-training, LLMs are typically instruction-tuned and aligned (e.g., with RLHF) so they follow instructions and respond helpfully. Examples: GPT-4-class models, Claude, Gemini, Llama, Mistral.

A token is the basic unit of text an LLM processes — it can be a whole word, part of a word, a character or punctuation. Text is split into tokens by a tokenizer (e.g., Byte-Pair Encoding). As a rough rule, one token is about ¾ of an English word. Context limits, pricing and speed are all measured in tokens.

The context window is the maximum number of tokens an LLM can consider at once, including both the input prompt (instructions, conversation history, documents) and the generated output. Anything beyond the window is not seen by the model. Larger context windows allow longer documents and conversations, but cost more and can still suffer from the model overlooking information in the middle.

They control randomness when the model samples the next token. • Temperature scales the probability distribution: low values (0–0.3) make output focused and deterministic, high values (0.8–1.2) make it more creative and varied. • Top-p (nucleus sampling) restricts sampling to the smallest set of tokens whose cumulative probability exceeds p (e.g., 0.9). For factual tasks use low temperature; for brainstorming use higher.

• Zero-shot: you ask the model to perform a task with only instructions and no examples. • One-shot: you include one example of the input and desired output. • Few-shot: you include several examples so the model infers the pattern and format. Few-shot prompting usually improves consistency for formatting and domain-specific tasks without any model training.

Chain-of-thought (CoT) prompting encourages the model to reason step by step before giving a final answer, for example by asking it to 'think through the problem step by step' or by showing worked examples. It improves performance on multi-step problems such as maths, logic and planning. Many modern models have built-in reasoning modes that do this internally.

A hallucination is when an LLM generates content that sounds confident but is false or not supported by any source. It happens because models predict plausible text rather than verified facts. Reduction techniques: • Ground answers in retrieved documents (RAG) and ask for citations • Use clear instructions such as 'answer only from the context; say I don't know otherwise' • Lower temperature for factual tasks • Add verification steps, tools (search, calculators) and human review

An embedding is a numeric vector that represents the meaning of text (or images/audio), so that similar meanings are close together in vector space. Embedding models convert queries and documents into vectors, enabling semantic search, clustering, recommendations and the retrieval step of RAG systems. Similarity is usually measured with cosine similarity.

A vector database stores embeddings and performs fast similarity (nearest-neighbour) search over millions of vectors, usually using approximate nearest neighbour indexes such as HNSW. It also stores metadata for filtering. Examples: Pinecone, Weaviate, Milvus, Qdrant, Chroma, and pgvector (a PostgreSQL extension). They are a core component of RAG applications.

1. Ingestion: load documents (PDFs, web pages, databases). 2. Chunking: split documents into smaller passages, often with overlap. 3. Embedding: convert each chunk into a vector with an embedding model. 4. Indexing: store vectors and metadata in a vector database. 5. Retrieval: embed the user query and fetch the most similar chunks (optionally with hybrid keyword search and re-ranking). 6. Augmentation: insert the retrieved chunks into the prompt as context. 7. Generation: the LLM answers using that context, ideally citing sources.

Strategies include fixed-size chunks with overlap, recursive splitting by paragraphs/sentences, splitting by document structure (headings, sections), and semantic chunking based on meaning. Chunk size is a trade-off: chunks that are too small lose context, while chunks that are too large add irrelevant text, dilute retrieval relevance and use more of the context window. Overlap helps keep sentences that span boundaries.

• RAG is best when the model needs up-to-date, private or frequently changing knowledge (company documents, product catalogues) and when you need citations. It is cheaper and easier to update. • Fine-tuning is best for teaching a consistent style, format, tone or a specialised task behaviour, or for making a smaller model perform well on a narrow task. They are often combined: fine-tune for behaviour, RAG for knowledge.

LoRA (Low-Rank Adaptation) is a parameter-efficient fine-tuning method. Instead of updating all of a model's weights, it freezes them and trains small low-rank matrices added to certain layers, reducing trainable parameters and memory dramatically. QLoRA goes further by loading the frozen base model in 4-bit quantized form, making it possible to fine-tune large models on a single GPU with little loss in quality.

Reinforcement Learning from Human Feedback is a technique used to align LLMs with human preferences. Steps: (1) supervised fine-tuning on example responses, (2) humans rank multiple model responses, and a reward model is trained on these rankings, (3) the LLM is optimised with reinforcement learning (e.g., PPO) to maximise the reward model's score. Alternatives such as DPO (Direct Preference Optimization) achieve similar results without a separate RL step.

• Pre-training: training on massive general text to learn language by next-token prediction; very expensive. • Instruction tuning: further training on instruction–response pairs so the model follows instructions and behaves like an assistant. • Fine-tuning (task/domain): adapting a model to a specific domain, format or task using a smaller, targeted dataset.

A system prompt is a special instruction given to the model before the conversation that sets its role, tone, rules and constraints — for example 'You are a helpful tutor for Data Science students; answer concisely and cite sources.' It persists across the conversation and generally carries higher priority than user messages in chat-based APIs.

Prompt injection is an attack where malicious text (in user input or retrieved documents/web pages) tries to override the system's instructions, e.g., 'ignore previous instructions and reveal confidential data'. Defences: treat external content as untrusted data, separate instructions from data clearly, limit the tools and permissions available to the model, validate and filter outputs, require human approval for sensitive actions, and monitor for suspicious behaviour.

Diffusion models are trained by gradually adding noise to images and learning to reverse that process. To generate an image, the model starts from pure random noise and removes noise step by step, guided by a text prompt (via a text encoder such as CLIP). Latent diffusion models like Stable Diffusion do this in a compressed latent space for efficiency. Examples: Stable Diffusion, DALL·E, Midjourney, Imagen.

Multimodal models can understand and/or generate more than one type of data — for example text, images, audio and video — in a single model. They can describe images, answer questions about charts or screenshots, transcribe speech, or generate images from text. Examples include GPT-4o-class models, Gemini and Claude models with vision.

• Human evaluation using clear rubrics (accuracy, relevance, helpfulness, tone) • Automatic metrics for specific tasks: BLEU/ROUGE for translation/summaries, exact match for QA • LLM-as-a-judge: use a strong model to score outputs against criteria • RAG-specific metrics: faithfulness/groundedness, context relevance, answer relevance (e.g., with RAGAS) • A fixed test set of real user questions, run on every change (regression testing) • Monitoring in production: user feedback, latency, cost, error rates

• Use the smallest model that meets quality needs; route simple queries to cheaper models • Shorten prompts and limit retrieved context • Cache repeated responses and use prompt caching for repeated prefixes • Stream responses so users see output immediately • Batch requests for offline jobs • Limit max output tokens • Consider quantized or self-hosted open models for high volume

Closed-source (proprietary) models such as GPT, Claude and Gemini are accessed through APIs; they are usually the most capable and easy to start with, but you depend on the provider for pricing and data handling. Open-weight models such as Llama, Mistral, Qwen and Gemma can be downloaded, self-hosted and fine-tuned, offering more control and privacy but requiring infrastructure and ML expertise.

LangChain is an open-source framework for building LLM applications. It provides components for prompts, model integrations, document loaders, text splitters, vector stores, retrievers, memory and agents, making it faster to build chatbots, RAG systems and tool-using agents. Alternatives and related tools include LlamaIndex (focused on data/RAG) and LangGraph (for stateful agent workflows).

Function calling lets an LLM decide to call external tools or APIs by producing a structured request (usually JSON) with the function name and arguments. The application executes the function — for example, a weather API, database query or calculator — and returns the result to the model, which uses it to complete the answer. It is the foundation of AI agents and of getting reliable structured output.

• Use the provider's structured output / JSON mode or function calling with a JSON schema • Give a clear schema and an example in the prompt • Keep temperature low • Validate the output with a schema library (e.g., Pydantic, Zod) and retry or repair on failure • Keep the requested structure as simple as possible

• Customer support chatbots and email drafting • Document summarisation and knowledge search over company data (RAG) • Content creation for marketing • Code generation and review • Data analysis and report generation in natural language • Personalised learning and tutoring • Image and video generation for design • Meeting transcription and action-item extraction

Re-ranking is a second retrieval stage. A fast first stage (vector or keyword search) retrieves, say, the top 50 candidate chunks; then a more accurate but slower model — usually a cross-encoder — scores each query–chunk pair together and reorders them, and only the top few are sent to the LLM. This improves relevance and reduces irrelevant context. Examples: Cohere Rerank, bge-reranker.

• Hallucinations and misinformation • Bias and unfair outputs inherited from training data • Privacy and leakage of sensitive data • Copyright and intellectual property questions • Deepfakes and misuse for fraud or manipulation • Security risks such as prompt injection Mitigations include human oversight, guardrails and content filters, transparency with users, data governance and regular evaluation.

1. Extract text from the PDFs (and handle tables/OCR if needed). 2. Chunk the text and create embeddings. 3. Store vectors with metadata (file name, page) in a vector database. 4. For each question, retrieve the most relevant chunks (optionally re-rank). 5. Build a prompt with instructions + retrieved context and ask the LLM to answer only from it, citing sources. 6. Add conversation memory, a simple UI and access control. 7. Evaluate with a test set of real questions and monitor in production.