RAG: search that answers · Chapter 7 of 37
Brute-force vector search
By the end of this chapter you can search Paisa Setu's 866 chunk embeddings by meaning, in memory: every vector stored in one compact array, a score for every chunk, the best k kept without sorting them all, and real timings at 866 and at 100,000 vectors. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Embeddings
- 5 tasks
- In the all labs plan
The first 2 chapters of RAG: search that answers are free. This one comes with the all labs plan.
Why you need this
Chapter 6 turned every chunk into 384 numbers. Kabir now wants the search itself: a merchant asks a question, and the five closest chunks come back while the merchant is still on the page, 600 times a day. Arjun adds a number: with the developer docs and two years of community posts, Paisa Setu expects 100,000 chunks next year. He wants to know whether that needs a vector database.
Everyone reaches for one, but under every vector database is the loop you write in this chapter, and the exact answer it gives is what their fast, approximate searches are measured against (chapter 16). You cannot judge the trade-off without knowing what the simple version costs.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
pack: one Float32Array for every vector
Store the vectors row after row in one Float32Array: vector i fills positions i * dim up to i * dim + dim - 1, where dim is the first vector's length. Return { dim, count, data }.
pack([[1, 2, 3], [4, 5, 6]]) is { dim: 3, count: 2, data: Float32Array [1, 2, 3, 4, 5, 6] }, 24 bytes. 866 vectors of 384 numbers are 1,330,176 bytes. A vector with a different length throws an Error 'Vector 1 has 2 numbers, expected 3'.
- Task 2
scoreAll: a score for every row
Return a Float32Array with one number per row: the dot product of that row of index.data with query. The vectors are normalised, so each score is a cosine. The query can be a normal array or a Float32Array.
With rows [1, 0], [0, 1] and [0.6, 0.8], scoreAll(index, [0.6, 0.8]) is [0.6, 0.8, 1]. A query whose length is not index.dim throws an Error.
- Task 3
topK: the best k, without sorting everything
Return the k best scores as { index, score }, best first, where index is the score's position. Equal scores put the earlier index first. k bigger than the list returns all of them; k of 0 returns []. Scores can all be negative.
topK([0.1, 0.9, 0.3, 0.7], 2) is [{ index: 1, score: 0.9 }, { index: 3, score: 0.7 }], and topK([0.5, 0.9, 0.5, 0.9], 3) has the indexes 1, 3, 0. Keep only the best k as you go, in a small min-heap or a short sorted list: the Check watches every sort and fails if you sort more than k items at once.
- Task 4
search: the closest chunks to a question
Embed the question with EMBED_MODEL, score it against every chunk with scoreAll, keep the best k with topK, and return them as { id, score } (the chunk's id), best first. embedChunks, given, returns every chunk's id and vector in order.
search('I just started a week ago and not a single rupee has reached my bank', 5) starts with help-where-is-my-money#3, score 0.566. Build the index once and keep it for every later search: the Check makes 3 searches and fails if more than 866 + 3 texts were embedded.
- Task 5 · interview question
Interview: randomIndex, vectors for a benchmark
Make count random normalised vectors of dim numbers as a VectorIndex. Take every number from one generator, mulberry32(seed), row by row: each number is random() * 2 - 1, and then the row is divided by its length. The same seed gives the same vectors; another seed gives others.
randomIndex(100_000, 384, 42) holds 153,600,000 bytes of data. Time scoreAll and topK over it with node exercise.ts.
Arjun will ask: Paisa Setu will have 100,000 chunks next year. Is a loop over every vector still fast enough, how much memory does it take, and when do we need an index?
What you will learn
- Flat storage: all vectors in one Float32Array, 1,536 bytes each.
- Brute force: one dot product per stored vector, exact every time.
- Top k: a small min-heap keeps the best k without sorting everything.
- Measuring: milliseconds and megabytes, at 866 and 100,000 vectors.
More in Embeddings
Build "Brute-force vector search" yourself
Paisa Setu, a payments company, needs its help centre to answer merchants correctly. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of RAG: search that answers are free. This one comes with the all labs plan.