LLM Application Development
Intermediate
4.5

An Intro to Embeddings

Understand embeddings and use them for semantic similarity.

1h 30m
1 lesson
1.2K students

What You'll Learn

Learning objectives will be added soon.

Tutorial Content

What embeddings are

An embedding turns a piece of text into a list of numbers — a vector — that captures its meaning. The magic property: texts with similar meaning end up with similar vectors, landing near each other in space, even when they share no words. "How do I reset my password?" sits close to "recovering account access," while "banana bread recipe" sits far away. This is what lets computers compare text by meaning instead of exact keywords, and it's the foundation of semantic search, RAG, and recommendations.

Measure similarity in code

To compare two texts, embed both and measure the angle between their vectors with cosine similarity (1.0 = identical meaning, 0 = unrelated):

import numpy as np
from openai import OpenAI
client = OpenAI()

def embed(t):
    return client.embeddings.create(model="text-embedding-3-small", input=t).data[0].embedding

a, b = embed("dog"), embed("puppy")
cos = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print("similarity:", round(cos, 3))

"dog" and "puppy" will score high; swap one for "tax return" and the score plummets. That single number is the engine behind semantic search.

What you can build

Once text is a vector, a lot of problems become "find the nearby vectors":

  • Semantic search over your documents — match by meaning, not keywords.
  • Clustering and topic discovery — group similar items automatically.
  • Recommendation — "more like this" for articles, products, or songs.
  • Deduplication — catch near-duplicate text that isn't an exact match.

Practical tips

  • Use a retrieval-tuned model (OpenAI, Cohere, Voyage, or open options) — they separate meanings better than generic ones.
  • *Embed queries and documents with the same model* — vectors from different models aren't comparable.
  • Store the vectors once, then reuse them; re-embedding on every query is wasteful.

The takeaway

An embedding is meaning turned into geometry. Once your text lives as points in space, "find me something relevant" becomes "find me something nearby" — a problem computers solve in milliseconds. Embeddings are the quiet workhorse behind most RAG and search systems.

Try it now: Embed three short phrases — two related, one unrelated — and print the pairwise similarities. Watching the related pair score high and the odd one out score low makes the whole idea concrete.

Your Progress

Sign in to track your progress

Tags

Embeddings
Python
RAG