Joel Stevick

Chapter 5 · section 1 of 7

Finding It by Meaning

This chapter builds the second half of retrieval: a search that finds a law by what it means rather than by the words it uses, and a ten-line function that merges the two searches into one list.

Chapter 4 ended with two questions word matching cannot answer. Can I leave my car right next to a fireplug shares no word with the hydrant law and does not reach it. ¿A qué distancia de un hidrante puedo estacionar? reaches nothing at all. Both ask the question the hydrant law answers, and a reader who knew both languages would see that at once. The stage this chapter builds is a model that sees it the same way.

Text as a point in space

An embedding is a list of numbers that a model produces from a piece of text, built so that two texts with similar meaning produce lists that point in similar directions. The list is a vector, and the model that makes it is fitted for that one job: not to continue text, as the models in the earlier chapters do, but to place it. The model this pipeline uses produces 1,024 numbers for any text, in any of a hundred languages, and the Spanish question lands near the English law that answers it because the model was fitted on both.

Two vectors are compared by the angle between them. Cosine similarity is the cosine of that angle: 1 for two vectors pointing the same way, 0 for two at right angles, and in practice a number between about 0.3 and 0.8 for any pair of English sentences, because every sentence shares something with every other. The function is ten lines, and it is in the listing below.

A vector index is a store of vectors, each under an id, that answers one question: which of these are nearest to the vector I am holding? Yours is an array and a loop. The deployed one is a service holding 172,000 vectors, and the chapter’s last section shows how it is filled and what can go wrong when it is.