Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Generative AI›Embedding Models — OpenAI, Sentence Transformers

Embeddings and Semantic Search

Embedding Models — OpenAI, Sentence Transformers

Embedding models are mathematical tools that transform unstructured text into high-dimensional vectors, capturing deep semantic relationships rather than literal keyword matches. This capability is the cornerstone of modern retrieval-augmented generation, enabling systems to understand the 'meaning' behind user queries in massive datasets. You reach for these models whenever you need to implement semantic search, document clustering, or context retrieval for large language models.

The Geometry of Meaning

To understand embeddings, visualize a high-dimensional space where every possible concept has a unique set of coordinates. When we pass text through an embedding model, we map linguistic tokens to a point in this space such that semantically similar ideas occupy nearby locations. This happens because the model is trained on vast amounts of data to predict context, forcing words that appear in similar environments to have similar vector representations. Unlike traditional keyword search, which relies on literal string matching and often fails to handle synonyms or linguistic nuance, vector representations treat language as a continuous field. By measuring the distance—typically using cosine similarity—between two vectors, we quantify how related two pieces of text are. This geometric approach is powerful because it allows the machine to infer relationships that were never explicitly defined in the training data, providing a robust foundation for identifying intent beyond the surface-level vocabulary used by the user.

import numpy as np

# Simulating two vectors in a 3D space for clarity
vec_cat = np.array([0.1, 0.9, 0.2])
vec_dog = np.array([0.15, 0.85, 0.25])
vec_car = np.array([0.9, 0.1, 0.0])

# A simple dot product calculates similarity; closer to 1 means more alike
similarity = np.dot(vec_cat, vec_dog) / (np.linalg.norm(vec_cat) * np.linalg.norm(vec_dog))
print(f"Semantic similarity between cat and dog: {similarity:.2f}")

Sentence Transformers: Local Processing

Sentence Transformers, or SBERT, represent a significant advancement because they fine-tune language models specifically for sentence-level embeddings using a Siamese network architecture. In standard models, you might treat each word individually, losing the holistic meaning of a sentence; SBERT, however, uses pooling layers to aggregate token-level outputs into a fixed-size vector representing the entire input string. Because these models are typically small enough to run on local hardware, they provide a private, cost-effective solution for applications where data residency is a concern. The reasoning behind their success lies in the contrastive learning objective, where the model is explicitly taught to push unrelated sentences apart while pulling related ones together in vector space. When you need to embed large internal document corpora without incurring per-token costs or network latency, Sentence Transformers are the industry standard for high-performance, local text representation tasks.

from sentence_transformers import SentenceTransformer

# Load a pre-trained model optimized for speed and quality
model = SentenceTransformer('all-MiniLM-L6-v2')

# Encode sentences into their geometric representation
embeddings = model.encode(['The sky is blue.', 'The weather is sunny.'])
print(f"Vector shape: {embeddings.shape}") # Outputs (2, 384)

OpenAI Embeddings: Scalable Abstractions

OpenAI provides an API-based embedding service that offloads the heavy computational requirements of large-scale vector generation to their infrastructure. These models are trained on an unprecedented scale, allowing them to capture subtle nuances, domain-specific jargon, and multi-lingual relationships that smaller models might miss. When you call an embedding endpoint, you receive a dense vector of high dimensionality—often 1536 or more—which packs significant semantic density into a compact representation. The benefit of this abstraction is twofold: first, you do not need to manage the underlying compute or hardware optimization; second, you benefit from the massive, diverse training set that allows the models to generalize across vastly different topics. In production environments, this becomes a strategic advantage when your application must remain indifferent to the specific subject matter of the input text, as the model essentially acts as a generalized semantic engine for any language input provided.

import openai

# Example of generating embeddings using a managed API
client = openai.OpenAI(api_key='your-key-here')
response = client.embeddings.create(
    input="Generative AI is transforming software engineering.",
    model="text-embedding-3-small"
)
embedding = response.data[0].embedding
print(f"First 5 dimensions: {embedding[:5]}")

Vector Search: Finding the Needle

Once you have your vectors, you need a mechanism to query them effectively. A vector database or search index acts as a specialized storage engine that organizes embeddings for rapid similarity searching. Rather than checking every single vector in your database—which would be computationally prohibitive at scale—these systems utilize algorithms like HNSW (Hierarchical Navigable Small Worlds) to create a graph-like structure that allows for approximate nearest neighbor searches. The search process performs a traversal through this graph, quickly narrowing down the candidate space to find vectors that are geometrically nearest to your query vector. This is essential for retrieval-augmented generation, as you must retrieve the most relevant pieces of context from thousands of documents in milliseconds. By choosing a vector index that balances search speed with recall precision, you ensure that your application provides relevant information without introducing unacceptable latency into the end-user experience.

from sklearn.neighbors import NearestNeighbors

# Create a simple index using a Nearest Neighbor search
data = np.random.rand(100, 384) # 100 documents of 384 dims
index = NearestNeighbors(n_neighbors=3, metric='cosine').fit(data)

# Find the 3 most relevant documents to a new query
query_vec = np.random.rand(1, 384)
distances, indices = index.kneighbors(query_vec)
print(f"Nearest document indices: {indices}")

Context Windows and Chunking

The effectiveness of any embedding strategy is strictly limited by the input data quality and granularity. Because models have a finite context window, you cannot simply dump an entire encyclopedia into a single embedding request. Instead, you must implement a chunking strategy that breaks long-form text into semantically coherent segments, such as paragraphs or sections of 500 tokens. This partitioning ensures that the model can focus on the core meaning of specific sections rather than being diluted by extraneous information. Furthermore, overlapping chunks can help preserve context at the edges of segments, ensuring that a concept isn't lost if it happens to be split across the cut point. When reasoning about your implementation, always prioritize a strategy that aligns the size of your chunks with the typical length of the queries you expect to receive, ensuring that the retrieved vectors are tightly focused on relevant domain knowledge.

def chunk_text(text, chunk_size=500):
    # Simple split based on fixed length with overlap
    return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size - 50)]

document = "Large body of text..." * 100
chunks = chunk_text(document)
print(f"Generated {len(chunks)} chunks for retrieval.")

Key points

  • Embeddings convert human language into high-dimensional numerical vectors for machine processing.
  • Semantic similarity is measured by calculating the geometric distance between two vector points.
  • Sentence Transformers allow for efficient, private, and local embedding generation.
  • OpenAI embedding models offer superior generalization through massive, diverse training datasets.
  • Cosine similarity is the standard metric used to compare the relationship between two vectors.
  • Vector indices use graph-based traversal algorithms to enable fast searching in large datasets.
  • Proper chunking of text is critical to ensure that retrieved context remains semantically relevant.
  • Embedding models represent a shift from literal keyword search to contextual meaning understanding.

Common mistakes

  • Mistake: Expecting embeddings to be human-readable vectors. Why it's wrong: Embeddings are high-dimensional numerical representations, not interpretative text. Fix: View embeddings as coordinates in semantic space used for distance calculations, not for direct inspection.
  • Mistake: Normalizing vectors when using Dot Product similarity. Why it's wrong: Dot product is only equivalent to Cosine similarity if vectors are unit-length. Fix: Either use Cosine similarity or manually normalize your embeddings before performing a Dot Product calculation.
  • Mistake: Assuming that a single embedding model is optimal for all domains. Why it's wrong: Models are trained on specific corpora and may struggle with specialized technical or domain-specific jargon. Fix: Evaluate domain-specific performance or fine-tune models on your specific dataset.
  • Mistake: Ignoring context window limits during embedding generation. Why it's wrong: Most embedding models truncate text that exceeds their token limit, leading to loss of information. Fix: Implement proper chunking strategies that respect the model's maximum sequence length.
  • Mistake: Mixing embedding models within the same vector database. Why it's wrong: Vector representations from different models exist in incompatible semantic spaces. Fix: Re-embed all existing data if switching to a new embedding model architecture.

Interview questions

What is an embedding model and why is it foundational to Generative AI applications?

An embedding model is a type of machine learning model that translates unstructured data, such as text, into a high-dimensional vector space where numerical values represent semantic meaning. This is foundational to Generative AI because it allows models to perform mathematical operations, like cosine similarity, to determine how related two pieces of information are. Without embeddings, AI systems would treat words as isolated tokens without any understanding of context or synonymy. By mapping text into vectors, we enable efficient semantic search and retrieval-augmented generation, allowing models to ground their responses in specific, relevant knowledge rather than relying solely on their pre-trained weights.

How does the Sentence Transformers framework differ from the standard OpenAI embedding API in terms of deployment and usage?

The primary difference lies in the deployment environment and control. OpenAI provides embeddings as a managed API service, meaning you simply send a text string and receive a vector back; this requires no hardware management but incurs costs and data privacy considerations. Conversely, Sentence Transformers is an open-source library that allows you to run state-of-the-art embedding models locally on your own infrastructure, such as using `from sentence_transformers import SentenceTransformer; model = SentenceTransformer('all-MiniLM-L6-v2'); embeddings = model.encode(['Generative AI is transformative'])`. This approach offers full data sovereignty, zero latency overhead from network calls, and the ability to fine-tune models on proprietary datasets without relying on external third-party providers.

Compare the 'text-embedding-3-small' approach by OpenAI to a custom-trained Sentence Transformer model. When would you choose one over the other?

The 'text-embedding-3-small' model is a highly optimized, general-purpose embedding engine trained on massive, diverse datasets, making it excellent for out-of-the-box performance across various languages and domains without any configuration. I would choose this when I need quick integration, scalability, and high performance without the burden of infrastructure maintenance. In contrast, a custom-trained Sentence Transformer is superior when your domain has highly specialized jargon, such as medical or legal documents, where general models fail to capture specific semantic nuances. I would choose the custom approach if I have labeled data, need to comply with strict regulatory data residency requirements, or want to minimize long-term API dependency costs.

What is the importance of 'embedding dimensions' when configuring your retrieval system, and how does selecting a dimension size impact performance?

Embedding dimensions represent the number of features or coordinates in the vector space used to represent a piece of text. OpenAI’s newer models allow you to truncate these dimensions without losing much semantic information. Choosing a smaller dimension size, such as 256 or 512, significantly reduces the memory footprint and increases the speed of similarity searches in your vector database, which is critical for real-time Generative AI applications. However, if your application requires extreme precision across highly complex or multi-faceted topics, you might need a higher dimension count like 1536. Essentially, you balance computational efficiency and latency against the theoretical accuracy limits of your vector representation.

Explain the concept of 'chunking' in the context of embedding generation and why it is necessary for large documents.

Embedding models have strict context window limitations; if you attempt to embed a massive document as a single vector, the model will essentially compress too much information, leading to the loss of granular semantic detail. Chunking involves breaking large documents into smaller, meaningful segments—often with overlapping windows to preserve context between chunks. By generating individual embeddings for these chunks, we enable a retrieval system to pinpoint the exact paragraph or sentence that answers a user's prompt. This is crucial for Generative AI, as it provides the model with highly specific 'context snippets' to use in the generation phase, preventing the model from hallucinating by grounding it in precise, localized information.

How do you handle 'semantic drift' when using pre-trained embedding models for dynamic, evolving datasets?

Semantic drift occurs when the meaning of domain-specific language evolves or changes in ways that the static, pre-trained embedding model cannot account for. To mitigate this, I implement a strategy involving periodic re-embedding of the knowledge base and, if necessary, Contrastive Fine-Tuning. By using a framework like Sentence Transformers, I can feed the model triplets of anchor, positive, and negative examples that reflect the new, domain-specific semantic relationships. This effectively updates the vector space alignment to ensure that the retrieval system remains accurate. I also monitor the 'hit rate' of my retrieval system; if the relevance of retrieved chunks declines over time, it is a clear indicator that the embedding space no longer aligns with the current data distribution.

All Generative AI interview questions →

Check yourself

1. When comparing two vectors generated by an OpenAI embedding model, why is Cosine Similarity generally preferred over Euclidean distance?

  • A.Cosine similarity measures the magnitude of the vectors while Euclidean ignores it.
  • B.Cosine similarity focuses on the angle between vectors, which better represents semantic orientation regardless of document length.
  • C.Euclidean distance is computationally impossible to calculate for vectors higher than 128 dimensions.
  • D.Cosine similarity is faster because it does not require vector normalization.
Show answer

B. Cosine similarity focuses on the angle between vectors, which better represents semantic orientation regardless of document length.
Cosine similarity identifies semantic alignment by angle, making it robust to variations in document length (magnitude). Euclidean distance is sensitive to magnitude, which can penalize longer documents even if they share the same topic. Option 0 is wrong because Cosine ignores magnitude; option 2 is false; option 3 is false as normalization is often recommended for Cosine.

2. What is the primary function of the 'chunking' step when preparing text for embedding models?

  • A.To increase the dimensionality of the resulting vectors for better accuracy.
  • B.To break down long documents into smaller segments that fit within the model's token limit.
  • C.To remove stop words so the model focuses only on keywords.
  • D.To allow the model to process multiple languages simultaneously.
Show answer

B. To break down long documents into smaller segments that fit within the model's token limit.
Embedding models have fixed input limits; chunking ensures content fits. Option 0 is wrong as chunking does not change dimensionality; option 2 is wrong because modern models need stop words for context; option 3 is irrelevant to chunking.

3. How does using a Sentence Transformer model differ from using a basic word-level embedding model?

  • A.Sentence Transformers generate a single vector representation for the entire input sequence rather than averaging word vectors.
  • B.Sentence Transformers only work with documents longer than 10,000 words.
  • C.Sentence Transformers are purely rule-based and do not use neural networks.
  • D.Sentence Transformers cannot be used for clustering tasks.
Show answer

A. Sentence Transformers generate a single vector representation for the entire input sequence rather than averaging word vectors.
Sentence Transformers are specifically designed to map entire sentences or paragraphs into a coherent semantic space. Option 1 is false; option 2 is false as they rely on deep neural networks; option 3 is false as they are excellent for clustering.

4. If you retrieve the wrong context in a RAG system, which part of the embedding pipeline should you primarily investigate?

  • A.The number of GPUs available for the inference task.
  • B.The semantic relevance of the embedding model's training data to your specific use case.
  • C.The color formatting of the input text.
  • D.The number of output dimensions of the vector.
Show answer

B. The semantic relevance of the embedding model's training data to your specific use case.
If retrieved context is irrelevant, the model may lack domain-specific training. Options 0, 2, and 3 have little impact on semantic search quality compared to the alignment of the embedding space.

5. Why is it critical to use the exact same model for both indexing your documents and querying your database?

  • A.Different models use different dimensions, making comparison mathematically undefined.
  • B.To ensure that the 'semantic coordinate system' remains consistent across search operations.
  • C.Because the API keys only allow for one specific model to be used at a time.
  • D.It is not necessary; most models are interoperable.
Show answer

B. To ensure that the 'semantic coordinate system' remains consistent across search operations.
Embeddings are coordinates in a specific semantic space. Using a different model creates a 'misaligned' map. Option 0 is sometimes true but not the primary issue; options 2 and 3 are conceptually incorrect.

Take the full Generative AI quiz →

← PreviousWhat are EmbeddingsNext →Cosine Similarity and Distance Metrics

Generative AI

41 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app