LLM engineering · Medium · 12 min

Retrieve the k most similar documents

The retrieval step of RAG: rank documents by similarity to the query and keep the top k, with stable tie-breaking.

Write topK(query, docs, k) that returns an array with the id of the k documents most similar to query, most similar first. Each document is { id, vector }.

Use cosine(a, b), which is already defined. When two documents tie on similarity, the one with the alphabetically smaller id comes first. Do not modify docs. If k is larger than the number of documents, return all of them; if it is 0, return [].

DOCS and QUERY hold an example index with 3-dimensional embeddings.

Challenges 0/4

  • Returns the 3 most similar in order
  • Breaks ties by id (guia-ollama and ollama-api have the same similarity)
  • Does not change the order of DOCS
  • With k above the index size returns everything; with k = 0, nothing

function topK(query, docs, k) {
  // score each doc with cosine(query, doc.vector), sort high to low, ties by id, take k
  return docs.slice(0, k).map((d) => d.id);
}

console.log(topK(QUERY, DOCS, 3));

Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.

Console output appears here (console.log).

Why this matters

This is the step that decides what the model reads. If the right chunk is not among the first k, no prompt will fix it. Stable tie-breaking looks like a detail, but without it the same question returns different chunks on two runs and you cannot compare one version of the system with the previous one. Choosing k is a trade-off: more chunks raise the chance of a hit, and also the noise and the context spent. How to choose it with data is in how to test your RAG before production.

Go deeper: the related guide →

This in production, with your data? Let's talk for 15 minutes →