AI support copilot project (TypeScript) · Chapter 4 of 12

Vector search

By the end of this chapter you can measure how alike two embeddings are with a plain loop, ask Postgres for the chunks nearest a question with pgvector's cosine distance, from Drizzle, in a stable order, and say what an HNSW index does, what it gives up, and when Parda needs one. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Search
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of AI support copilot project (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Postgres now holds Parda's 179 chunks, each with the 384 numbers of its meaning. Neha asks on WhatsApp: "Can I bring my wheelchair to Kala Talkies?" Somewhere in those 179 rows is the chunk that answers (wheelchair-access-03: "Kala Talkies has a ramp at its side entrance"), and the copilot must find it among the five it hands the model, for every one of Datta's 200 questions a day, in milliseconds.

Chapter 3 stored the arrows, but nothing compares them. There is no way yet to say how near two arrows are, no query that asks Postgres for the nearest ones, and no route a member's question can reach.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    vector.ts: cosine similarity with a loop

    Write cosineSimilarity(a, b) in src/search/vector.ts: the dot product of a and b divided by the length of a times the length of b, as a number (1 the same direction, 0 unrelated, -1 opposite). The dot product multiplies a[i] by b[i] for every i and adds it all up; a vector's length is the square root of its dot product with itself. Use a loop over the indexes: JavaScript has no maths for whole arrays.

    The Check runs six small pairs ([3, 4] and [6, 8] point the same way, so 1), then embeds the question "Can I pay cash at the box office?" and two chunks and wants the cash chunk nearer than the pets chunk.

  2. Task 2

    vector.ts: the nearest chunks, in Postgres

    Write vectorSearch(queryVector, k = 5) in src/search/vector.ts. Return the k chunks nearest the query vector, nearest first, as Hits: { chunkId, pageId, title, heading, text, score }. With db, select the chunk's id, pageId, heading and text, the page's title (an inner join of pages on pages.id equal to chunks.pageId) and the cosine distance, cosineDistance(chunks.embedding, queryVector) (pgvector's <=>); order by the distance, then the chunk's id; limit k. score is 1 - distance, as a number.

    The Check compares your 5 hits for "Can I bring my wheelchair to Kala Talkies?" with its own SQL, adds five chunks at the same distance (they must come back in id order), and calls GET /search?q=...&k=3.

  3. Task 3 · interview question

    Interview: the HNSW index migration

    Write the migration drizzle/0001_chunks_embedding_hnsw.sql: one SQL statement that creates an index called chunks_embedding_hnsw on the table chunks, the column embedding, using the method hnsw, with the operator class vector_cosine_ops after the column.

    The Check applies the chapter's migrations, reads the index's definition from pg_indexes (its name, USING hnsw, embedding vector_cosine_ops), and wants EXPLAIN of a nearest-5 query (with sequential scans turned off) to use it.

    Nikhil will ask: HNSW is an approximate index. Why would Parda accept a search that may miss the true nearest chunk, and when would you not add the index at all?

What you will learn

  • Cosine similarity: the dot product and the lengths, and why only the direction counts; one loop does it in TypeScript.
  • Cosine distance in pgvector: the <=> operator, and its cousins <-> and <#>.
  • The nearest k: ORDER BY a distance and LIMIT, and why a second sort key makes the order stable.
  • A Drizzle select: cosineDistance, innerJoin, orderBy, limit, and the rows it gives back.
  • HNSW: what an approximate index does inside, what it trades away, how a migration adds it, and how EXPLAIN shows whether Postgres uses it.

More in Search

Build "Vector search" yourself

Parda answers the same audience questions all day. You build the copilot that answers them from its own help pages, with sources, and says so when it does not know. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI support copilot project (TypeScript) are free. This one comes with the all labs plan.