One continuous story, from how search engines rank documents to how we automate prompt engineering. Every session solved a gap left by the previous one.
S00 → S01 → S02 → S03 → S04 → S05 → S06 → S07
| Session | Central Question |
|---|---|
| S00 | How does Google decide which result to show? |
| S01 | How does a computer understand the meaning of a word? |
| S02 | How does the same word mean different things in context? |
| S03 | How did next-word prediction become ChatGPT? |
| S04 | How does an LLM decide which word to generate next? |
| S05 | How do we run and own the infrastructure ourselves? |
| S06 | How do we stop guessing at good prompts? |
| S07 | How do we call any LLM with the same code? |
Before embeddings. Before Transformers. Before ChatGPT. This is how computers understood text for 50 years, and why it wasn't enough.
| Concept | What It Is | Why It Matters |
|---|---|---|
| Inverted Index | Word → list of documents map | Makes search sub-millisecond |
| TF-IDF | Word importance score per document | Ranks documents by relevance |
| Semantic Gap | "car" ≠ "automobile" in this model | The gap that led to embeddings |
Instead of scanning every document per query, pre-build a lookup: word → document list. O(1) retrieval regardless of corpus size.
| Word | Documents containing it |
|---|---|
| "python" | Doc 1, Doc 5, Doc 12, Doc 20 |
| "neural" | Doc 3, Doc 7, Doc 12 |
| "model" | Doc 1, Doc 3, Doc 7, Doc 9 |
| "training" | Doc 5, Doc 9, Doc 20 |
TF-IDF = TF × IDF. "the" appears in 200 of 200 docs → IDF = log(200/200) = 0 → score = 0, no matter how often it appears. "RLHF" appears in 3 of 200 docs → IDF = log(200/3) ≈ 4.9 → strong relevance signal. This is why stopwords automatically get filtered out without an explicit stopword list.
What if every word had a location in a 300-dimensional space, and words with similar meanings lived close together? Meaning becomes arithmetic.
No rules were hand-coded. The model learned this purely from text co-occurrence. Meaning is captured as geometry, relationships become vector arithmetic.
| Model | Year | Key Idea |
|---|---|---|
| Word2Vec | 2013 | Learns from a sliding context window. "Know a word by its company." |
| GloVe | 2014 | Learns from global co-occurrence matrix across entire corpus (not local context window). Strong on word analogy tasks like king−man+woman=queen. |
| FastText | 2016 | Trains on character n-grams. Handles typos and unknown words. |
"Attention Is All You Need" (2017) introduced self-attention, which lets every word look at every other word simultaneously. This changed everything.
| Component | Analogy | Role |
|---|---|---|
| Query (Q) | Your search question to the library | What this token is looking for |
| Key (K) | The label on each library book | What this token offers to others |
| Value (V) | The book's actual content | What this token contributes when attended to |
Left = Encoder (BERT). Right = Decoder (GPT). The Nx means these blocks are stacked N times.
Each token projects to Q, K, V vectors. Attention = softmax(QKᵀ/√d) × V.
The same Transformer architecture. Trained on more data. Then fine-tuned with human feedback. That's the complete story of how we got ChatGPT.
Stage 1: Supervised fine-tuning on human-written responses.
Stage 2: Train a reward model, humans rank multiple responses from best to worst. The reward model learns to predict human preference.
Stage 3: RL (PPO) fine-tunes the language model to maximise the reward model's score. This is what makes GPT say "I don't know" instead of making something up.
How text becomes token IDs, how IDs become probabilities, and how probabilities become the next word. This controls cost, randomness, and output quality.
BPE starts with characters and repeatedly merges the most frequent adjacent pairs into a vocabulary of ~50k-100k subwords.
| Parameter | What It Does | Typical Value |
|---|---|---|
| Temperature | Scales logits before softmax. Low = focused. High = creative. | 0 → greedy, 0.7 → balanced |
| Top-K | Keep only the K most likely tokens. Cuts off the long tail. | 40 or 50 typical |
| Top-P (Nucleus) | Keep tokens until cumulative probability ≥ P. Dynamic cut-off. | 0.9 or 0.95 |
Run Llama on your own laptop, free, private, offline. And use one API key to access every cloud provider through OpenRouter.
| Factor | ☁️ Cloud API | 💻 Local (Ollama) |
|---|---|---|
| Cost | Per token | Free |
| Privacy | Data leaves machine | 100% local |
| Internet | Required | Works offline |
| Quality | Best available | Depends on hardware |
| Latency | ~1s (fast GPU) | Slower on CPU |
| Setup | API key only | Install + pull model |
ollama run llama3.2First: how to write a good prompt manually. Then: how to let code find the optimal prompt automatically using DSPy, MIPROv2, and GEPA.
| # | Part | What You Learn | Why It Matters |
|---|---|---|---|
| Part 1 | Manual Prompt Engineering | Role, Task, Context, Examples, Output Format. Zero-shot, Few-shot, CoT. | Foundation skill. Every AI engineer needs this. |
| Part 2 | Automated Optimization | DSPy Signatures, BootstrapFewShot, MIPROv2, GEPA | Removes guesswork. Finds prompts better than human-written. |
| Part | What It Does | Example |
|---|---|---|
| Role | Who the AI should be, activates the right writing patterns from training data | "You are a senior data analyst writing for executives" |
| Task | What exactly you want done, be specific, not vague | "Summarize the following in exactly 3 bullet points" |
| Context | Background the AI needs to answer correctly | "The audience has no technical background" |
| Examples | 2-3 input/output pairs showing the format and style you want | Few-shot examples before the actual question |
| Format | How to structure the output, critical if your code needs to parse it | "Reply ONLY with valid JSON. No markdown. No explanation." |
Summarize this.You are a business analyst writing for non-technical managers.
Summarize the following customer feedback in exactly 3 bullet points.
Each bullet: main complaint + why it matters to the business.No examples. Just the instruction. Works for simple tasks.
Show 2-3 examples. The model infers format and labels from them.
Add "think step by step." Forces visible reasoning before the answer.
Reply ONLY with this JSON. No markdown.
{"sentiment": "positive"|"negative"|"mixed",
"would_recommend": true|false}DSPy treats prompts as learnable parameters. You define what you want in code. The optimizer finds the best prompt automatically.
You've called OpenAI. You've called Gemini. You've called Ollama. Why are you maintaining three different codebases for the same thing?
| Component | Before LangChain | After LangChain |
|---|---|---|
| Provider calls | 3 different SDKs, 3 different response parsers | Same .invoke() for all providers |
| Prompt management | f-strings and manual string building | ChatPromptTemplate with typed variables |
| Conversation history | Manually pass message list each turn | MessagesPlaceholder + InMemoryChatMessageHistory |
| Streaming | Different streaming API per provider | chain.stream() works identically everywhere |
| Output parsing | Access different fields per provider | StrOutputParser() extracts content universally |
| Concept | What to say in 10 seconds |
|---|---|
| TF-IDF | Rare words that appear often in one doc are important. Formula: TF x log(N / df). |
| BM25 | TF-IDF with saturation (k1) and document length normalization (b). Default in Elasticsearch. |
| Inverted Index | Word-to-document lookup for sub-millisecond search. O(1) retrieval instead of full scan. |
| Word Embedding | Dense vector where similar words cluster together. Geometry encodes meaning. |
| Cosine Similarity | Measures angle between vectors, ignores magnitude. Range: -1 to 1. |
| Skip-gram | Word2Vec mode: predict context words from a center word. Better for rare words. |
| CBOW | Word2Vec mode: predict center word from context words. Faster to train. |
| Negative Sampling | Replaces full softmax with binary classifier on noise words. 5000x faster training. |
| GloVe | Embeddings from global co-occurrence statistics instead of local context windows. |
| Self-Attention | Each token weighs every other token for relevance using Q, K, V projections. |
| Multi-Head Attention | Run attention in parallel with different learned projections for different relationship types. |
| Positional Encoding | Adds order information. Transformers process in parallel so position must be injected. |
| BERT vs GPT | BERT: encoder-only, bidirectional, understands context. GPT: decoder-only, generates left-to-right. |
| RLHF | Three stages: SFT, reward model from human rankings, PPO with KL penalty to align LLM. |
| Hallucination | Model predicts plausible text, not verified facts. No built-in "I don't know" signal. |
| BPE | Iteratively merge frequent character pairs until target vocabulary size is reached. |
| Temperature | Divide logits by T before softmax. Low = deterministic. High = creative. |
| Top-K / Top-P | Top-K: keep K most probable. Top-P: keep smallest set summing to P. Use both together. |
| GGUF / Q4_K_M | Quantized model file. Q4 = 4-bit weights, K = group-wise scaling, M = medium accuracy. |
| Prompt Engineering | Role + Task + Context + Examples + Output format. All 5 parts for consistent output. |
| DSPy / MIPROv2 | Prompts as learnable parameters. Optimizer finds best instruction text automatically. |
| LCEL / Runnable | LangChain pipe operator. Every component implements .invoke, .stream, .batch, .ainvoke, .astream. |
| Session | Topic | Status |
|---|---|---|
| S00 | Keyword Search, TF-IDF | Complete |
| S01 | Word Embeddings, Word2Vec | Complete |
| S02 | Transformer Architecture | Complete |
| S03 | GPT Evolution, RLHF, Alignment | Complete |
| S04 | BPE, Temperature, Sampling | Complete |
| S05 | Local LLMs, OpenRouter | Complete |
| S06 | Prompt Engineering, DSPy, MIPROv2 | Complete |
| S07 | LangChain, LCEL, Streaming | Complete |