Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.3 — Embeddings: Meaning as Numbers
LLM Engineering · Example 1 of 1

Dot product and cosine similarity on 3 numbers

Real embeddings come from an embedding model and have far more numbers, but the maths is identical.

An embedding turns text into a list of numbers so that similar meanings get nearby lists. Cosine similarity measures how close two lists point, which is the base of semantic search and RAG.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write cosine(a, b) for two lists and print cosine([1, 0], [0, 1]) and cosine([1, 2], [2, 4]) rounded to 4 decimals.

02

Write nearest(query, vectors) that returns the name with the highest cosine similarity to query. Use {"a": [1, 0], "b": [0, 1], "c": [1, 1]} and query [0.9, 0.2].

Explain it without notes

01

What is an embedding, and what problem does it solve?

02

Explain the difference between the dot product and cosine similarity. When do they give different rankings?

03

What is the difference between a model's internal token embeddings and an embedding model?

04

Name two mistakes that silently break semantic search.

Dot product and cosine similarity on 3 numbers
Sign in to run this example in your browser.

Expected output

dot(cat, dog)    = 1.3850
dot(cat, car)    = 0.3450
closest to cat by cosine:
  dog    0.9958
  kitten 0.9923
  banana 0.7503
  car    0.2926
  truck  0.2750
dot(cat, doubled cat)    = 2.9200
cosine(cat, doubled cat) = 1.0000