Skip to main content
πŸ€– Learn AI: Build with LLMs

Embeddings & Similarity

LLMs turn text into embeddings β€” vectors of numbers. Similar meanings have vectors pointing the same way, measured by cosine similarity:

cosine(a, b) = dot(a, b) / (|a| Γ— |b|)

Result ranges from 1 (identical direction) to -1 (opposite direction), with 0 meaning unrelated. In practice, text embedding vectors are non-negative, so similarity scores land in [0, 1]. This is the math behind semantic search and RAG.

Your task

Write cosine(a, b) for two equal-length vectors, rounded to 2 decimals.

⚑ 45 XP
solution.jsRuns in your browser
Loading editor…
Tests3 to pass
identical direction β†’ 1
orthogonal β†’ 0
[3,4] vs [4,3] β†’ 0.96

Run your code to check these.