Theory · Page 33 of 34 · Data for AI, and delivering the architecture

33. Data for AI: features, training sets, embeddings, vector search and RAG

Models learn from data and answer from data, so their quality is the data's. For machine learning that means features computed the same way in training and in serving, each as it was known at the time; for generative AI it means passages found by meaning (embeddings and vector search) and retrieved with their access rights, so the model answers from governed sources.

A question finding its passages by meaning, with their access rights
Playback speed

The clip first trains a claims model on two kinds of feature: one computed as it was known on the day of each claim, and one computed from everything known today, which quietly includes the future. Then a question is asked of Harbour Mutual's assistant: is a leaking pipe covered? The question is turned into a vector; passages from the policy wording, claim notes and the motor handbook sit as points nearby or far away, and the nearest are found by meaning even where they share no word with the question. Before they reach the model, each passage's access rights are checked against the person asking, and one claim note is held back. The model answers from what is left, and cites it.

The intuition: a model only knows what it is shown

Harbour Mutual trains a model to flag suspicious claims. One of its inputs is "how many claims has this customer made". In training it is computed from today's tables, and the model looks brilliant: customers who will go on to claim again score high. In production, on the day a claim arrives, the future claims do not exist yet, and the brilliant feature is worth much less. The model learnt from information it would never have when it mattered.

The claims assistant has the opposite problem. Asked whether a leaking pipe is covered, a language model on its own answers fluently from what it absorbed in training, which knows nothing of Harbour Mutual's wording. Shown the right paragraph of the right policy, it answers well. Shown a claim note about another customer's medical history, it answers too well. What decides the quality of both systems is what data reaches them, as of when, and for whom.

Remember Compute features as they were known at the time, the same way in training and serving; retrieve passages by meaning, with their access rights applied before the model sees them.

The theory: features, embeddings, vector search and retrieval

A feature is an input to a model computed from data: the customer's number of earlier claims, the days between loss and report, the policy's age. A feature store keeps feature definitions once and serves them twice: an offline store of history for building training sets, and an online store of the latest values for scoring in milliseconds. One definition for both is the cure for training/serving skew: a model trained on features computed one way and served features computed another, which degrades silently (chapter 32).

A training set must be point-in-time correct: each row's features are the values known at the moment the label's event happened, never later. Joining on "the latest value" lets future information leak into the past. The correct join is an as-of join: for each event, the feature value with the latest timestamp at or before the event's time.

A customer's earlier claims as known at each loss date, against the count as known today (PostgreSQL)
WITH claim_customer AS (
    SELECT c.claim_id, c.loss_date, p.customer_id
    FROM claims c
    JOIN policies p ON p.policy_id = c.policy_id
)
SELECT
    COUNT(*) AS claims,
    SUM(then_known.n) AS earlier_claims_at_loss_date,
    SUM(known_today.n) AS other_claims_known_today,
    COUNT(*) FILTER (WHERE then_known.n <> known_today.n) AS rows_that_leak
FROM claim_customer cc
CROSS JOIN LATERAL (SELECT COUNT(*) AS n FROM claim_customer o
                    WHERE o.customer_id = cc.customer_id AND o.loss_date < cc.loss_date) then_known
CROSS JOIN LATERAL (SELECT COUNT(*) AS n FROM claim_customer o
                    WHERE o.customer_id = cc.customer_id AND o.claim_id <> cc.claim_id) known_today

On the seed, 35 of the 60 claims would get a different value from the leaky version: summed over all claims, the point-in-time feature counts 66 earlier claims, the leaky one 134. A model trained on the second learns from claims that had not happened yet.

An embedding represents a piece of text (or an image, or a customer) as a vector of numbers, learnt so that things with similar meaning sit close together. Closeness is usually measured by cosine similarity: the cosine of the angle between two vectors, 1 when they point the same way, near 0 when unrelated. Real embeddings have hundreds or thousands of dimensions; the table uses three made-up ones (water damage, motor, money) to show the idea.

Cosine similarity between the question and four passages, in a toy three-dimensional space
PassageVectorSimilarity to the question
A burst pipe flooded the kitchen(0.90, 0.10, 0.20)0.88
Escape of water from a washing machine(0.80, 0.00, 0.30)0.91
A van reversed into the client's car(0.10, 0.90, 0.20)0.45
The excess on the motor policy(0.00, 0.70, 0.70)0.61

Vector search finds the passages nearest to a question's vector. Comparing with every vector is exact and too slow for millions, so indexes trade a little accuracy for speed: approximate nearest neighbour (ANN) search. HNSW, the most widely used, builds a graph in layers: sparse long links at the top for big jumps, dense short links at the bottom for fine steps; a search descends greedily and returns neighbours that are very likely, not certainly, the nearest. Other indexes cluster the vectors and search only the nearest clusters. Lexical search (BM25, as in chapter 33's workbench) still wins on exact codes and names, so many systems combine both (hybrid search).

Retrieval-augmented generation, over governed data Lewis and colleagues' retrieval-augmented generation (RAG) pairs a generator with a retriever: the question retrieves passages from an index, and the model generates its answer conditioned on them. The answer can cite its sources and stays current when the index does. The index is a copy of the source documents, so it must carry their governance: each chunk keeps its source, owner, classification and access rights as metadata; the retriever filters by the asker's rights before ranking, so nothing they may not read reaches the prompt; and lineage links each answer back to the passages and the documents' versions. A RAG system without that is a way around every access control of page 16.

Chunking (splitting documents into passages) decides what can be found: too small and a passage loses its context, too large and it dilutes the match. Evaluating retrieval needs a set of questions with known good passages, measured by whether those passages are retrieved (recall at k) before the generation is judged at all.

Why it matters

The combined company's AI pilot (chapter 44) fails three ways, and all three are data: a leaky feature, a stale index, and a retriever that ignores access rights. Each fix is on this page: point-in-time features, an index refreshed with lineage, and filters applied before ranking.

Chapter 34 turns the same ideas into foundations: a glossary the model can read, governed indexes and evaluation sets, so the next AI use starts from trusted data.

Where it goes wrong

  • Features joined on the latest value. The future leaks into training, and the model is worse in production than in testing.
  • Two implementations of one feature. A SQL version for training and an application version for serving drift apart.
  • Filtering after ranking. Removing forbidden passages from the top results can leave none, and retrieving them at all is already a leak.
  • An index nobody refreshes. The assistant cites last year's policy wording.
  • Embeddings for exact matches. A claim number or a policy code is found better by lexical search.

Check yourself

Choose an answer to each question, then press Check.

1. What makes a training set point-in-time correct?
2. What does approximate nearest neighbour search trade away for speed?
3. In retrieval over governed data, when are the asker's access rights applied?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • P. Lewis et al., "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks", Advances in Neural Information Processing Systems (NeurIPS), 2020A generator conditioned on passages retrieved from an index, with answers that can be traced to sources.
  • D. Sculley et al., "Hidden Technical Debt in Machine Learning Systems", NeurIPS, 2015Data dependencies, feedback loops and training/serving skew as the hidden costs of machine learning.
  • Y. A. Malkov and D. A. Yashunin, "Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs", IEEE Transactions on Pattern Analysis and Machine Intelligence 42(4), 2020The HNSW index: layered graphs searched greedily from coarse to fine.
  • T. Mikolov et al., "Efficient Estimation of Word Representations in Vector Space" (2013)Learnt embeddings in which similar meanings sit close together.
  • S. Robertson and H. Zaragoza, "The Probabilistic Relevance Framework: BM25 and Beyond", Foundations and Trends in Information Retrieval 3(4), 2009BM25 lexical ranking, the other half of hybrid search.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Retrieval workbench · Chunk and index Harbour Mutual's documents with their access rights, and ask questions as different roles.

Terms taught here: FeatureFeature storePoint-in-time correctnessTraining/serving skewChunkEmbeddingVector indexRetrieval-augmented generation

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.