Master AI/ML from first principles
107 articles
33 production RAG patterns organized across 9 tiers β from basic chunking to multi-tenant federation. Each pattern links to a working notebook.
How machines find meaning by turning words into coordinates β and why search changed forever when they did.
What embedding models are, how they produce vectors, why context changes the output, and how to generate and cache embeddings efficiently.
How the three main distance functions work, when each one wins, and the key insight that cosine ignores magnitude β which is almost always what you want for text.
Why brute-force kNN breaks at scale, how HNSW solves it with a multi-layer graph, and which three parameters control the accuracy vs. speed tradeoff.
Transformer-based model trained on massive text corpora that generates text by predicting the next token.
The atomic unit of text that an LLM processes β a sub-word piece that balances vocabulary size and semantic meaning.
Converting raw text into token IDs the model can process β and back again.
Dense numerical vectors that encode semantic meaning β similar concepts cluster together in high-dimensional space.
The high-dimensional space where embeddings live and organize by semantic meaning.
The learned numerical weights that store everything the model knows β billions of numbers shaped by training.
The core training objective: given prior tokens, predict the most probable next one.
Three terms that look alike but work very differently β and the gap between them is exactly what separates a connected database from a system that can actually reason.
A biomedical AI system has 31 entity types, 37 relationship types, and data across clinical trials, drug batches, patient outcomes, and compliance records. Without a knowledge graph, no AI can reliably answer questions that span those domains.
A biomedical AI agent that can answer 'Is there a quality issue with this drug?' must route across three separate graph databases, apply business rules, and synthesise a grounded answer β all in one turn. That is what a context graph enables.
Standard RAG retrieves isolated text chunks. GraphRAG traverses relationships β surfacing connected context that no single chunk could contain.
The schema β the ontology β is what separates a useful knowledge graph from a pile of connected records. How to design one that serves AI systems, not just human browsers.
The foundation β load a PDF, split into chunks, embed with Bedrock Titan, store in Qdrant, and retrieve by similarity.
Same as Simple RAG but with smarter scissors β splits where the topic actually changes instead of every 1000 characters.
Index documents at multiple granularities β summaries at the top, sections in the middle, sentences at the bottom β and retrieve at the right level for each query.
Index small child chunks for precise retrieval, but return their larger parent chunk to the LLM β combining retrieval precision with generation context.
Index individual sentences for precise embedding match, then expand each match to its surrounding N-sentence window before passing to the LLM.
The full lifecycle of dense semantic search β from document ingestion through embedding, HNSW indexing, and query β with benchmarks on what it gets right and where it fails.
TF-IDF sparse vectors explained β why 99.97% of the vector is zeros, how that makes search 10x faster than dense, and when sparse consistently beats semantic.
How combining three search engines β keyword, sparse, and dense β pushes NDCG from 0.85 to 0.91, with score normalization, weight tuning, and the architecture that makes it fast.
The neural architecture underlying all modern LLMs β processes all tokens in parallel using self-attention.
The mechanism that lets every token dynamically focus on every other token based on relevance.
Empirical rules predicting LLM performance as a function of model size, data, and compute.
Raw unnormalized scores from the model output layer, converted to probabilities via softmax.
Architecture where only a subset of parameters activate per token β enabling massive scale efficiently.
Pattern #34 - Hybrid Search RAG - Best of All Worlds. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Generate hypothetical ideal answers, embed them, and use those embeddings to find better source documents. Improves retrieval for complex queries.
The quality filter β retrieves 10 docs, then a second model re-scores them. Two methods: fast cross-encoder or smarter LLM reranking.
Re-score retrieved candidates with a cross-encoder model that jointly encodes query and document for highly accurate relevance ranking.
Intelligently compress retrieved context by removing irrelevant information while preserving key facts. Reduces tokens and improves response quality.
How vector indexes work under the hood β collections, payload filtering, snapshot strategy, and the operational details that matter when serving real search traffic.
INT8, FP16, and binary quantization explained β the memory-recall tradeoff, how rescoring recovers lost precision, and when compression actually hurts.
How to measure search quality with NDCG@10 and recall@k, build a ground-truth dataset, benchmark your pipeline, and run A/B tests to validate improvements.
Training a model from scratch on massive text corpora using next-token prediction as the objective.
The raw pre-trained model β powerful but not instruction-following. The foundation everything else builds on.
A base model fine-tuned to follow instructions as a helpful assistant.
Continued training on a smaller task-specific dataset to adapt the model for particular behaviors.
Parameter-Efficient Fine-Tuning β adapt only a tiny fraction of parameters (0.1β1%) rather than the full model.
Reducing weight precision from FP32 to INT8/INT4 to shrink memory footprint and speed up inference.
Pattern #9 - Query Decomposition - Break Complex Queries. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Transform a specific, narrow query into a broader abstract question first, retrieve general principles, then combine abstract and specific context for a grounded answer.
Combine multiple retrieval strategies (vector, keyword, graph) with reciprocal rank fusion for robust search results across diverse query types.
Generate several distinct sub-queries from a complex question, retrieve for each in parallel, then merge and deduplicate before generation.
Classify each incoming query and route it to the most appropriate retrieval backend β vector store, SQL, graph, or web β instead of always using the same index.
Training the model to be helpful, honest, and harmless β making model behavior match human values.
Reinforcement Learning from Human Feedback β using human preference rankings to guide model behavior.
A simpler alternative to RLHF that directly optimizes from preference pairs without a reward model.
Safety mechanisms that block harmful or off-policy inputs and outputs in production systems.
Malicious input that overrides developer instructions to hijack model behavior.
Automatically detect and correct irrelevant or incorrect retrievals. Uses relevance grading and web search fallback for improved accuracy.
The smart decision maker β decides if retrieval is needed, filters irrelevant docs, generates multiple answers, and picks the best one.
Retrieve, inspect intermediate results, refine the query, retrieve again β iterate until the retrieved context is sufficient to answer the question.
Pattern #10 - Recursive RAG - Iterative Deep Search. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Empower LLMs to autonomously decide when to retrieve, what to search, and how to combine information. Advanced pattern for complex multi-step reasoning.
The full text input sent to the model β everything it sees before generating a response.
Developer-set instructions that define the model's role, rules, and behavior for a session.
The end user's actual question or instruction sent at runtime.
Asking the model to perform a task without any examples β relying purely on its training.
Providing examples in the prompt to guide model behavior without changing model weights.
Prompting the model to show step-by-step reasoning before answering, dramatically improving accuracy on complex tasks.
The model's ability to adapt to new tasks purely from examples in the prompt β no weight updates needed.
Persist conversation history and user context across turns so retrieval is conditioned on the full dialogue, not just the latest query.
The maximum number of tokens the model can see and process at once β its working memory.
The process of generating output from a trained model β what happens when you call the API.
Time-to-first-token and total generation time β the key performance metrics for production LLM systems.
The parameter controlling randomness in token sampling β 0 is deterministic, higher values are more creative.
Sampling strategies that filter which tokens can be selected as the next output token.
Cached attention key-value states enabling efficient autoregressive generation without recomputing past tokens.
Delivering tokens incrementally as they are generated rather than waiting for the full response.
Models that use extended internal 'thinking' β visible chain-of-thought before answering β for complex tasks.
Run multiple retrieval strategies in parallel and merge results using Reciprocal Rank Fusion for higher recall than any single retriever.
Pattern #8 - Adaptive RAG - Dynamic Strategy Selection. Complete implementation with AWS Bedrock, code examples, and performance metrics.
When the model confidently generates false, fabricated, or unsupported content as if it were fact.
Constraining model outputs to provided, verifiable information to prevent hallucination.
Pattern #32 - Streaming RAG - Real-Time Responses. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Pattern #33 - Cached RAG - Performance Optimization. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Pattern #35 - Production RAG - Enterprise Deployment. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Retrieving relevant external documents at query time and injecting them into the prompt to ground generation.
A fixed, predefined sequence of LLM-powered steps with deterministic control flow.
An LLM used as a dynamic reasoning engine that plans, uses tools, and adapts based on feedback.
The model outputs structured requests to invoke external tools β APIs, databases, code executors.
Constraining model output to a predefined schema β JSON, XML β for reliable downstream processing.
Anthropic's open protocol for connecting LLMs to external tools, data sources, and services.
Reusing precomputed KV states for repeated prompt prefixes to slash latency and cost.
Recursive Abstractive Processing for Tree-Organized Retrieval β builds a hierarchical summary tree for multi-level querying.
Enhance RAG with knowledge graphs to capture entity relationships and improve contextual understanding. Learn how to build graph-based retrieval systems.
Pattern #15 - Tree of Thoughts RAG - Branching Reasoning. Complete implementation with AWS Bedrock, code examples, and performance metrics.
Augment RAG with a knowledge graph of entities and relationships for multi-hop reasoning, precise entity lookups, and explainable answers.
Pattern #11 - Multimodal RAG - Text, Images & Beyond. Complete implementation with AWS Bedrock, code examples, and performance metrics.
At index time, generate hypothetical questions each chunk could answer. At query time, match the user query to these questions for dramatically better semantic alignment.
When enough child chunks from the same parent are retrieved, automatically merge them back into the parent chunk for richer, coherent context.
Generate multiple query variants from a single question, retrieve for each, then fuse ranked results with RRF to surface the most relevant documents.
The trust-but-verify system β adds 3 verification layers: relevance grading, hallucination detection, and source highlighting.
The learning system β collects user feedback after each answer and uses it to improve future retrievals. Gets smarter the more you use it.
The context reconstructor β instead of returning scattered chunks, finds the best contiguous section of the document that answers the query.
Recursively summarize a document bottom-up β chunk summaries β section summaries β document summary β creating a compression tree for efficient retrieval at any level.
Force the LLM to cite specific source chunks inline while generating, making every claim verifiable and reducing hallucination by anchoring to retrieved evidence.
Standardized tests used to compare model capabilities across tasks and providers.
Custom evaluation frameworks for measuring model performance on your specific use case.
Using a powerful model to evaluate another model's outputs β scalable alternative to human evaluation.
Models that process and generate across text, images, audio, and video simultaneously.
Capabilities that appear suddenly and unpredictably in models above certain scale thresholds.