GenAI Lesson 14 – OpenAI Embeddings | Dataplexa

OpenAI Embeddings

So far, you have learned what embeddings are and why they are critical for modern GenAI systems.

In this lesson, we move from concept to execution: how embeddings are actually generated in real systems.

This is where GenAI stops being theoretical and starts becoming an engineering workflow.

Why Use OpenAI Embeddings

Training your own embedding model is expensive, time-consuming, and rarely necessary.

Most production systems use pretrained embedding models that already capture rich semantic structure.

OpenAI provides high-quality embedding models optimized for search, clustering, and retrieval.

Thinking Before Writing Code

Before touching any API, ask:

What problem are we trying to solve with embeddings?

Common goals include:

  • Semantic search
  • Similarity matching
  • RAG pipelines

The code you write depends on this goal.

What an Embedding API Actually Does

When you send text to an embedding API:

  • The text is tokenized
  • The model processes semantic meaning
  • A fixed-length vector is returned

That vector is what you store and compare later.

Basic OpenAI Embedding Flow

At a high level, the steps are:

  • Send text to the API
  • Receive a vector
  • Store the vector for later use

Let’s see how this looks in code.

Generating an Embedding (Python)

This is the simplest possible embedding request.


from openai import OpenAI

client = OpenAI()

response = client.embeddings.create(
    model="text-embedding-3-small",
    input="Generative AI changes how software is built"
)

embedding = response.data[0].embedding
print(len(embedding))
  

Before running this code, understand what each part does:

  • The model name selects the embedding space
  • The input is the text you want to represent
  • The output is a numerical vector
1536

The output length is fixed, which makes comparisons efficient and predictable.

What This Vector Represents

Each number in the vector represents a learned semantic feature.

Individually, the numbers mean nothing.

Together, they encode meaning.

Embedding Multiple Texts

In real applications, you rarely embed a single sentence.

Most systems embed batches of text.

Batch Embedding Example


texts = [
    "Generative AI powers chatbots",
    "Embeddings enable semantic search",
    "Vector databases store embeddings"
]

response = client.embeddings.create(
    model="text-embedding-3-small",
    input=texts
)

vectors = [item.embedding for item in response.data]
print(len(vectors), len(vectors[0]))
  

This approach is more efficient and cost-effective.

3 1536

How Learners Should Practice This

To truly learn embeddings:

  • Embed different types of text
  • Compare similar vs dissimilar sentences
  • Store vectors locally and experiment

Do not memorize the API. Understand the flow.

Similarity Search with OpenAI Embeddings

Once embeddings exist, the next step is comparison.

Cosine Similarity in Practice


import numpy as np

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

query = "How do embeddings work?"

query_embedding = client.embeddings.create(
    model="text-embedding-3-small",
    input=query
).data[0].embedding

score = cosine_similarity(query_embedding, vectors[1])
print(score)
  

This score tells us how close two pieces of text are in meaning.

0.82

Higher scores mean stronger semantic similarity.

Where OpenAI Embeddings Are Used

OpenAI embeddings are commonly used in:

  • Vector databases (Pinecone, Chroma, FAISS)
  • RAG pipelines
  • Enterprise search systems
  • Recommendation engines

This lesson is the foundation for all of those.

Common Mistakes to Avoid

  • Embedding entire documents without chunking
  • Mixing different embedding models
  • Comparing raw text instead of vectors

These mistakes lead to poor retrieval quality.

Practice

What does the OpenAI embedding API return?



What operation is used to compare embeddings?



What technique improves efficiency when embedding many texts?



Quick Quiz

Which provider offers ready-to-use embedding models?





What is the typical dimensionality of OpenAI embeddings?





Which system heavily relies on embeddings for grounding?





Recap: OpenAI embeddings convert text into vectors that power semantic search, similarity, and retrieval systems.

Next up: We use these embeddings for similarity search — the core of intelligent retrieval.