What are Embeddings?

Embeddings turn text into lists of numbers that capture meaning.

An embedding is a list of numbers (a vector) that represents the meaning of a piece of text. A dedicated embedding model converts text into this vector.

Text is converted into vectors so that similar meanings sit close together in spacecatkittencarembedVector spacecatkittencar
Similar meanings land close together; unrelated ones land far apart.

A vector might have hundreds or thousands of numbers. You do not read them — you compare them mathematically to find related text.

Example

Example · json
{
  "input": "cat",
  "embedding": [0.021, -0.184, 0.077, 0.512, -0.093, "...", 0.006],
  "dimensions": 1024
}

When to use it

  • A search engine embeds every article headline so users can find relevant results by meaning rather than exact keyword match.
  • A job board embeds candidate resumes and job descriptions to surface the closest skill matches even when wording differs.
  • An e-commerce site embeds product descriptions so users who search 'cosy winter shoes' find boots even if the listing says 'thermal ankle boots'.

More examples

Generate an embedding for a string

Calls the embeddings endpoint and inspects the resulting numeric vector and its dimensionality.

Example · python
from openai import OpenAI

client = OpenAI()
response = client.embeddings.create(
    model='text-embedding-3-small',
    input='The quick brown fox jumps over the lazy dog'
)
vector = response.data[0].embedding
print(f'Dimensions: {len(vector)}')
print('First 5 values:', vector[:5])

Embed a batch of sentences

Embeds multiple sentences in one API call, producing one vector per input string.

Example · python
from openai import OpenAI

client = OpenAI()
sentences = [
    'Cats are independent animals.',
    'Dogs are loyal companions.',
    'Python is a programming language.',
]
response = client.embeddings.create(
    model='text-embedding-3-small',
    input=sentences
)
for i, item in enumerate(response.data):
    print(f'Sentence {i}: {len(item.embedding)}-dim vector')

Compare vector magnitudes

Shows that embeddings are numeric vectors with measurable magnitude, one per input word.

Example · python
import math
from openai import OpenAI

client = OpenAI()
words = ['king', 'queen', 'apple']
vectors = {
    w: client.embeddings.create(model='text-embedding-3-small', input=w).data[0].embedding
    for w in words
}
for name, vec in vectors.items():
    magnitude = math.sqrt(sum(x**2 for x in vec))
    print(f'{name}: magnitude = {magnitude:.4f}')

Discussion

  • Be the first to comment on this lesson.