Unit 5: Generative AI & Ethics; Prompt Engineering

INT428 — Artificial Intelligence Essentials 4 min read

I. Orientation: Fundamentals of Generative AI

Generative AI (mainstreamed 2017–2022 with the Transformer and diffusion models) refers to systems that learn the probability distribution of training data and sample new artefacts—text, images, audio, code—rather than merely classify existing ones.

  • Discriminative vs. generative: A discriminative model learns P(y|x) (label given input); a generative model learns P(x) or P(x|y) and can synthesise x by sampling.
  • Core objective: Maximise likelihood of observed data, or minimise a divergence between the model distribution P_model and the true data distribution P_data.
  • Latent representation: Most generators map a low-dimensional latent vector z (often z ~ N(0, I)) to the high-dimensional data space.
  • Training corpus scale: Modern models train on billions of tokens or images, making emergent capability a function of data + parameters + compute.
  • Sampling, not retrieval: Output is newly composed, so identical prompts can yield different results (stochastic decoding).

II. Generative Model Architectures

A. How generative models work

The shared mechanism is learning a distribution and drawing samples from it, differing only in how the distribution is represented and trained.

  • Density estimation: The model approximates P_data; training tunes parameters θ so P_θ ≈ P_data.
  • Latent-to-data mapping: A learned function G(z; θ) transforms noise z into a plausible sample.
  • Loss signal: Either maximum likelihood (LLMs, diffusion) or an adversarial/game-theoretic signal (GANs).
  • Inference: Generation = repeated sampling; e.g. autoregressive P(x) = ∏ P(xₜ | x_<ₜ).

B. LLMs (Large Language Models)

LLMs are autoregressive Transformers that predict the next token given prior context.

  • Transformer core: Self-attention computes token relevance via
    TEXT
      Attention(Q,K,V) = softmax(QKᵀ / √d_k) V

    where Q,K,V are query/key/value projections and d_k is key dimension.
  • Objective: Minimise cross-entropy of next-token prediction over sequence x₁…xₙ.
  • Scale markers: Parameter counts from ~10⁹ to >10¹¹; context windows of thousands to millions of tokens.
  • Emergent abilities: In-context learning, reasoning, and translation appear only past scale thresholds.
  • Decoding controls: Temperature scales logit sharpness; top-p / top-k truncate the sampling pool.

C. GANs (Generative Adversarial Networks)

Introduced by Goodfellow (2014), GANs pit two networks in a minimax game.

  1. Generator G: Maps noise z to fake samples, trying to fool the discriminator.
  2. Discriminator D: Classifies samples as real or fake, trying to catch G.
    • Value function:
      TEXT
        min_G max_D  E[log D(x)] + E[log(1 − D(G(z)))]
    • Equilibrium: Ideal convergence when G reproduces P_data and D outputs 0.5 everywhere.
    • Failure mode: Mode collapse—G produces limited variety; unstable, oscillating training.
    • Use case: Photorealistic faces (StyleGAN), super-resolution.

D. Diffusion

Diffusion models generate by learning to reverse a gradual noising process.

  • Forward process: Add Gaussian noise over T steps until data becomes pure noise: xₜ = √(αₜ) x₀ + √(1−αₜ) ε.
  • Reverse process: A neural network ε_θ(xₜ, t) predicts the noise to remove, denoising step by step back to a clean sample.
  • Training loss: Minimise ‖ε − ε_θ(xₜ, t)‖² (predicting injected noise).
  • Strength: High sample quality and diversity; stable training compared to GANs.
  • Cost: Slow inference (many sequential denoising steps); latent-space variants reduce this.
  • Use case: Text-to-image systems that condition denoising on a text embedding.

III. Industrial applications (content creation, automation)

Generative AI creates economic value by producing draft artefacts and removing routine human steps.

A. Content creation

  • Text: Marketing copy, articles, summaries, and code generation from natural-language specs.
  • Visual media: Product mock-ups, concept art, and synthetic training images via diffusion/GANs.
  • Personalisation: Tailored email or recommendation text generated per user segment.
  • Concrete anchor: A single prompt can yield dozens of ad variants for A/B testing in seconds.

B. Automation

  • Customer support: LLM chatbots resolving tier-1 queries, escalating edge cases.
  • Code and DevOps: Autocompletion, test scaffolding, and documentation drafting.
  • Data workflows: Synthetic data generation for scarce classes; entity extraction from unstructured text.
  • Limitation: Outputs need human review—hallucination and factual drift make full autonomy risky in high-stakes domains.

IV. Ethics and Responsible Use of Generative AI

Responsible use balances capability against harms in data, output, and deployment.

A. Ethics and Responsible Use of Generative AI

  • Hallucination: Models assert false statements fluently; mitigation via grounding, retrieval, and citation.
  • Bias and fairness: Training data biases propagate; e.g. skewed occupation–gender associations require auditing and debiasing.
  • Copyright and consent: Training on scraped works raises ownership disputes over both data and outputs.
  • Privacy: Models may memorise and leak personal data present in training sets.
  • Misinformation and deepfakes: Synthetic media enables impersonation and fraud; countered by watermarking and provenance standards.
  • Transparency and accountability: Disclose AI-generated content; keep humans in the loop for consequential decisions.
  • Environmental cost: Large-scale training consumes substantial energy—a sustainability consideration.

V. Prompt Engineering

The discipline of designing inputs that steer a language model toward accurate, useful outputs.

A. Overview of language models

Language models assign probabilities to token sequences and generate text by sampling from them.

  • Autoregressive prediction: P(x) = ∏ₜ P(xₜ | x_<ₜ); each token conditions the next.
  • Tokenisation: Text is split into subword tokens; the model reasons over token IDs, not characters.
  • Context window: Fixed span of tokens the model can attend to; prompt + output must fit within it.
  • In-context learning: The model adapts behaviour from examples in the prompt, without weight updates.

B. Fundamentals of prompt

A prompt is the full input text that conditions the model's generation.

  • Definition: Instruction, context, and any examples supplied to elicit a target response.
  • Components: Instruction (task), context (background/data), input (the item to act on), output indicator (format cue).
  • Stochastic effect: Small wording changes shift output; prompts are the primary control surface for a fixed model.
  • Zero-shot vs. few-shot: Zero-shot gives only the instruction; few-shot adds worked examples to demonstrate the pattern.

C. Importance of Prompt Engineering

Good prompting extracts far better performance from an unchanged model at near-zero cost.

  • Accuracy lift: Clear framing reduces hallucination and off-task answers.
  • Cost efficiency: Cheaper than fine-tuning—no retraining, no labelled dataset.
  • Reliability: Structured prompts yield parseable, consistent outputs for pipelines.
  • Capability unlock: Techniques like chain-of-thought elicit reasoning the model already holds but does not volunteer.

D. Key elements of a good prompt

Effective prompts are specific, contextual, and constrained.

  • Clear instruction: State the exact task using an action verb ("Summarise in three bullets").
  • Sufficient context: Supply background and any reference data the answer depends on.
  • Role/persona: Assign a role ("Act as a security analyst") to set tone and depth.
  • Output format: Specify structure—JSON, table, word limit—to make results usable.
  • Constraints and examples: Bound length, style, and scope; include a sample to anchor the pattern.
  • Worked example:
    TEXT
      Role: You are a legal-tech assistant.
      Task: Classify the clause below as "confidentiality", "liability", or "other".
      Output: Return only the label as JSON: {"label": "..."}
      Clause: "Neither party shall disclose the terms of this agreement."

    The explicit role, single task, and rigid output shape make the response deterministic and machine-readable.

E. Prompt Patterns

Reusable prompt structures that reliably produce a class of behaviour.

  • Persona pattern: "Act as X" to fix expertise and voice.
  • Chain-of-thought: "Think step by step" to expose intermediate reasoning and raise accuracy on multi-step tasks.
  • Few-shot pattern: Provide input→output examples so the model infers the mapping.
  • Template/output-automater: Fix a rigid output skeleton the model must fill.
  • Question-refinement: Ask the model to improve or clarify the request before answering.
  • Flipped interaction: The model asks the user questions to gather what it needs first.

F. Prompt Tuning

Prompt tuning learns optimal prompt vectors while keeping the base model frozen.

  • Soft prompts: Trainable continuous embeddings P_e prepended to the input; only these are optimised.
  • Distinction from prompt engineering: Engineering crafts discrete natural-language text by hand; tuning learns non-human "vectors" via gradient descent.
  • Efficiency: Updates a few thousand parameters vs. billions—a parameter-efficient fine-tuning (PEFT) method.
  • Trade-off: Cheaper and portable across tasks, but soft prompts are not human-readable and are task-specific.
  • Related methods: Prefix tuning and LoRA sit on the same spectrum of lightweight adaptation.