liveBook · Manning

Similarity search

As explained in Grokking AI Applications

A search method that finds stored vectors whose representations are most similar to a query vector.

Conceptual search finds information by meaning rather than by exact word overlap. It represents both stored text chunks and a user’s question as vectors—lists of numbers called embeddings—and looks for chunks whose vectors lie closest to the question vector. This lets a search connect differently worded texts that express the same idea. section 4.1

Think of a vector database as a map of ideas. During setup, documents are split into chunks, each chunk is embedded, and those embeddings are stored. At question time, the same embedding process turns the question into a point on that map. Comparing that point with stored points identifies the chunks most likely to help answer it. section 4.1 section 4.2

A practical retrieval flow has three stages:

  • Prepare knowledge: read documents, split their text into manageable chunks, create an embedding for each chunk, and store the vectors in a vector database. section 4.2
  • Represent the question: convert the user’s query into an embedding using the same kind of representation used for the chunks. section 4.1
  • Rank and return matches: calculate each chunk’s distance from the query vector, then select the closest chunks as retrieval results for answer generation. section 4.1 section 4.2

Finding a “nasty cat”

Suppose six pet-related chunks are placed on a simple two-dimensional embedding map. A question such as “Who is a nasty cat?” should land near the chunks about nasty cats. Measuring the distance from the question point to every stored point ranks the results: the closest chunk is returned first, even if its wording does not exactly repeat the question. section 4.1

Figure 4.5 Euclidean distances between our question and each embedding in the vector database

This distance diagram makes the central mental model visible: retrieval ranks stored embeddings by their closeness to the query embedding. section 4.1

Retrieval quality depends on what is stored and how many results are returned. Very small chunks may omit needed context, while very large chunks can be poorly represented by a fixed-size embedding and can make retrieval less effective. The number of returned results, often called top k, also needs balance: too many chunks can confuse an LLM, while too few can leave out critical information. section 4.3 section 4.3

Conceptual search is commonly used as the retrieval step in retrieval-augmented generation (RAG). The selected chunks become outside context for an LLM, helping it answer questions using a particular PDF, cookbook, or other prepared collection instead of relying only on pretrained knowledge. It can also serve as a tool available to a specialized agent. section 4.2 section 6.3

How Grokking AI Applications covers it

How deep it goes
Explains embeddings, distance ranking, chunking, and top-k tuning without model-internals detail.
Examples
Uses pet embeddings, a pizza PDF assistant, and cookbook recipe retrieval.
What it assumes
Introduces retrieval through intuitive vector maps and does not assume AI engineering expertise.
What you can do after
Apply it via Langflow to retrieve relevant chunks from a vector database for an LLM.
How it connects
Connects embeddings, vector databases, chunking, retrieval-augmented generation, and LLMs.

Also explained in

Compare how 22 books explain similarity search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Applications. The section links point to the parts of the book it draws on.