Build your own semantic search
Retrieval & RAG·Intro · 2–4h

Build your own semantic search

Embeddings, cosine similarity, and a brute-force index.

What you'll build

You'll build a complete semantic-search engine in TypeScript from the dot product up: write the vector math, a brute-force exact kNN index over 5,183 real scientific abstracts, and the retrieval metrics that actually matter — recall@k, MRR, and nDCG@k. No vector-DB API, no GPU, no Python — the embeddings ship precomputed as frozen Float32Array blobs, so every score is a deterministic function of bytes on disk. You finish with a CLI that takes a natural-language question and returns the abstracts that answer it, scoring the same nDCG@10 a published retrieval paper reports.

  • A vector-math kernel written by hand over Float32Array — dot product, L2 norm, and cosine similarity
  • A brute-force exact kNN index: score one query against all 5,183 docs and select the top-k with a deterministic tie-break
  • Hand-rolled retrieval metrics — recall@k, MRR, and nDCG@k — checked per-query against pytrec_eval, BEIR's evaluator
  • A search CLI that embeds your own query live on CPU with a 22M-param sentence model and returns ranked real abstracts

What you'll learn

  • What an embedding actually is — a point in a metric space — and why cosine collapses to a plain dot product on normalized vectors
  • How exact k-nearest-neighbor search works before anyone reaches for an approximate index, and why it's O(N·d) per query
  • How retrieval quality is really measured — recall@k, MRR, nDCG@k — the vocabulary of every embedding bake-off and RAG regression test
  • Why correctness (reproducing the exact nearest neighbors) and quality (matching human relevance judgments) are two different numbers you must never conflate

Prerequisites

  • Comfortable reading and running TypeScript
  • Basic vector arithmetic — adding and multiplying arrays of numbers (we derive the dot product, norm, and cosine from scratch)
  • No machine-learning, information-retrieval, or Python background required

Get started

Everything runs locally — no GPU, no API keys. Clear stage 1 in under ten minutes.

# 1. log in (approve in your browser — required)
npx theinference login

# 2. scaffold the course
npx theinference course start semantic-search ./semantic-search

# 3. install dependencies
cd semantic-search && npm install

# 4. grade the first stage
npx theinference test --stage run-the-grader --watch

Each lesson shows the exact --stage command for that stage — passing it marks the stage complete here automatically.