Generative AI, Large Language Models, and Prompt Engineering
Generative AI
What generative means
Discriminative models learn P(y|x) - they label. Generative models learn the data distribution so they can create new samples (text, images, audio, code).
Model families
- GANs: a generator produces fakes and a discriminator tries to detect them; they train adversarially. At inference you use the generator only.
- VAEs: an encoder compresses input to a latent distribution and a decoder reconstructs; sampling the latent space yields new outputs.
- Diffusion models: training adds noise progressively; generation starts from pure noise and reverses the denoising steps. Dominant for images and video.
- Autoregressive transformers: predict the next token from previous tokens; this is how LLMs generate text.
Foundation models and adaptation
A foundation model is pretrained on broad data and reused for many tasks. Three adaptation routes, in increasing cost and data requirement: prompt engineering (no weight change), retrieval-augmented generation (external documents injected into context, no weight change), fine-tuning (weights updated on your labeled examples). Vector embeddings plus a vector store enable semantic retrieval for RAG.
Sampling controls
Temperature scales randomness; top-k restricts to the k most likely tokens; top-p keeps the smallest set of tokens whose probabilities exceed p. Low temperature and low top-p for factual, repeatable output; higher for brainstorming.
Traps
RAG does not change weights and cannot teach a new skill - it supplies context. Fine-tuning is unreliable for injecting fresh facts and can cause catastrophic forgetting. Temperature 0 is not a guaranteed byte-identical output across all providers. Hallucination cannot be fully prompted away; it needs grounding and validation.
Rule of thumb
Ask what the task needs: new knowledge (RAG), new behavior or format (fine-tuning), or just better instructions (prompting).
Large Language Models
Architecture
An LLM is a stack of transformer decoder blocks using self-attention, trained to predict the next token. Text is split by a tokenizer (commonly byte-pair encoding) into subword tokens; each token maps to an embedding vector, and positional information is injected so order matters. The context window is the total tokens the model can attend to in one call - prompt plus generated output combined.
From base model to assistant
Pretraining on large corpora yields a base next-token model. Instruction tuning teaches it to follow requests; RLHF/preference optimization aligns tone, safety and helpfulness. These stages shape behavior; they do not install a reliable factual database.
Inference parameters
max tokens caps output length; temperature, top-p and top-k control sampling; stop sequences end generation; presence/frequency penalties reduce repetition. Output is produced token by token, so latency and cost scale with output length - input and output tokens are typically priced separately.
Evaluation and efficiency
Perplexity measures how well a model predicts text (lower is better). Task benchmarks and human review judge quality, factuality and safety. Quantization reduces weight precision to cut memory and cost; distillation trains a smaller student from a larger teacher; adapter tuning such as LoRA updates a small parameter subset.
Traps
A large context window is not the same as current knowledge or a recent knowledge cutoff. Tokens are not words: as a rule of thumb, 1 token ≈ 4 characters ≈ 0.75 English words, so a 2,000-word document is roughly 2,700 tokens. A bigger model is not automatically the right answer - latency and cost per token matter. Very long prompts can degrade instruction-following.
Prompt Engineering
Structure of a good prompt
Separate a system message (role, rules, safety constraints, output contract) from the user content. State the task, provide context, define the output format, and delimit untrusted data with markers such as triple backticks or XML-style tags so the model does not confuse data with instructions.
Techniques
- Zero-shot: instructions only. Start here; it is cheapest and often sufficient.
- Few-shot: 2–5 representative input/output examples to lock format and edge-case handling.
- Chain-of-thought: ask for intermediate reasoning before the final answer for multi-step math, logic or planning. It costs tokens and latency, so skip it for simple extraction.
- Role prompting: assigns expertise and tone; it shapes style more than it adds facts.
- Structured output: supply an explicit schema plus an example, then validate and parse programmatically.
Reliability practices
Ground answers in supplied context and instruct: answer only from the context; if the answer is not present, return a fixed sentinel such as NOT_FOUND. Positive instructions beat vague negatives. Iterate against a small labeled evaluation set, changing one variable at a time.
Security
Prompt injection is the key trap: retrieved documents, web pages or emails may contain instructions. Treat them as data, never as commands; gate privileged actions behind allow-lists and human approval; never place secrets in a prompt.
Worked example
Invoice extraction. System: you are a data extraction engine; return only JSON matching the schema. User: the schema, the document inside delimiters, and a rule to use null for missing fields. Set temperature to 0 and validate the JSON before use.
Traps
More examples can crowd the context and hurt quality; high temperature breaks deterministic extraction; 'do not hallucinate' is weaker than a grounding rule with an explicit fallback.