AI from scratch

Module 05 · Generative AI

Make models useful — and keep them honest

Prompting, RAG, tools, hallucinations, and evaluation: the product layer on top of LLMs, plus a quick map of other generative families.

The generative map

Generative models produce new samples — text, images, audio — instead of only labelling inputs.

For most ML / GenAI engineer interviews in 2026, depth on LLMs + RAG + eval beats memorising every generative family.

Prompting that actually helps

Prompting is not magic. If the fact isn’t in the model’s weights or the context you provided, confident prose is still a guess. That’s when you reach for RAG or tools.

RAG: retrieve, then generate

Retrieval-Augmented Generation keeps knowledge outside the model:

1. Ingest docs → chunk → embed → vector index (+ keyword/hybrid)
2. On query → retrieve top-k passages
3. Stuff passages into the prompt with instructions
4. Generate an answer that must cite those passages
5. Evaluate retrieval hit rate + answer faithfulness

When RAG wins: private docs, frequently changing facts, need for citations, compliance. When fine-tune wins: style, format, tool-calling habits — not a weekly FAQ refresh.

RAG failure modes (memorise these)

Your FilingPilot project is this pattern on financial documents — highest GenAI portfolio signal.

Tools & agents (light)

An LLM can call tools: search, SQL, calculator, ticket API. Pattern:

model proposes a tool call → your code runs it →
result returns to the model → final answer

“Agents” chain multiple steps. Start simple: one tool, clear schema, hard timeouts, logged traces. Multi-agent theatre is optional; reliable tool loops are not.

Why models hallucinate

They sample plausible tokens, not truth. Causes: knowledge gaps, bad retrieval, prompts that force an answer, overconfident decoding.

Mitigations: RAG with citations, allow “I don’t know,” constrained decoding / JSON schemas, grounding checks, human review on high stakes, eval sets for factuality.

How to evaluate GenAI features

Offline   fixed question set · exact checks where possible
          retrieval hit rate · citation accuracy · format validity
          rubric / human spot checks · LLM-as-judge only with audits
Online    thumbs, escalation rate, task success, latency, $/request
Safety    injections, PII leaks, jailbreaks — separate test suites

Never ship on vibes. A 20-question eval set beats a demo every time in interviews.

Interview questions

Also in the interview bank.

RAG vs fine-tuning — when do you pick which?

RAG for changing/private/citable knowledge. Fine-tune for style, format, tool-use patterns. Many production systems use both.

Why do LLMs hallucinate?

They predict plausible tokens, not ground truth. Mitigate with retrieval, abstention, constraints, and factuality evals.

How would you evaluate an LLM feature?

Fixed offline eval set + task metrics + human spot checks; track online quality, latency, and cost. Don’t trust a single judge score alone.

Name three RAG failure modes.

Bad chunking, poor retrieval, stale indexes — each can produce fluent wrong answers. Strong candidates also mention weak abstention.

What is prompt injection?

Untrusted content (user or retrieved docs) that tries to override system instructions. Defend with separation of privileges, allowlists for tools, and treating retrieved text as data, not commands.