Typorithm · medium

Embeddings

embeddingsllmvectorssimilarity
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Embeddings

0%

RAG & Embeddings

An embedding maps text to a dense vector where distance encodes semantic meaning. Cosine similarity ranks how related two pieces of text are, and these vectors are the foundation of semantic search and RAG.

An embedding model turns text into a dense vector of floats — often hundreds or thousands of dimensions. Unlike keyword matching, the vector captures meaning, so paraphrases land near each other even with no shared words.

To compare two pieces of text you compare their vectors. Cosine similarity looks at the angle between them: a score near 1 means very similar meaning, near 0 means unrelated. This turns 'how related are these?' into simple arithmetic.

Because semantically close text produces nearby vectors, embeddings power semantic search: embed a query, find the nearest stored vectors, and you retrieve the most relevant passages. That retrieval step is the heart of RAG.

01

An embedding is a dense vector encoding text meaning.

02

Cosine similarity scores how related two vectors are.

03

Embeddings underpin semantic search and RAG.

const res = await embedder.embed("the quick brown fox");
// vector: [0.12, -0.04, ...] e.g. 1536 dims
const sim = cosineSimilarity(vecA, vecB);
// near vectors mean similar meaning

Read the implementation

Connect each part of the code to the concept before you type.

1. Embed text

An embedding model maps a piece of text to a fixed-length numeric vector.

const res = await embedder.embed("the quick brown fox");

2. Dense vector

The result is a dense vector of floats (here 1536 dimensions) that encodes the text's meaning.

// vector: [0.12, -0.04, ...] e.g. 1536 dims

3. Cosine similarity

Cosine similarity measures the angle between two vectors to score how related their texts are.

const sim = cosineSimilarity(vecA, vecB);

4. Distance = meaning

Vectors that sit close together represent text with similar semantic meaning.

// near vectors mean similar meaning

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Embeddings: Lesson & Practice · Typorithm | ApplyDjinn