DEV Community

Anushka Gupta
Anushka Gupta

Posted on

Vectors and Embeddings in Generative AI🤖(How AI Understands Meaning Instead of Just Words)

One of the most confusing concepts in Generative AI is the difference between embeddings and vectors.

Many people use these terms interchangeably, but are they actually the same?

Not exactly.

Understanding this difference is essential if you're working with RAG (Retrieval-Augmented Generation), semantic search, recommendation systems, or AI chatbots.

Let's start with a simple example :

Imagine You're Organizing a Library 📚

Suppose your library contains these books:

Book 1
"Introduction to Machine Learning"

Book 2
"How to Bake a Chocolate Cake"

Book 3
"Deep Learning with Python"

Book 4
"Indian Desert Recipes"

Now someone asks:

"I want books about Artificial Intelligence."

Would you search for the exact keyword Artificial Intelligence?

No.

Book 3 doesn't even contain those exact words. Yet you know it's related.

Humans understand meaning. Computers don't...unless we convert meaning into numbers.

That's where embeddings come in.

Why Can't AI Understand Words Directly?

Computers only understand numbers.

They don't know what

Dog
Cat
Car
Love
Pizza

mean.

To a computer they're simply characters.

D
o
g

Nothing more.

So before AI can compare meanings, it converts words into numbers.

What is an Embedding?

An embedding is a numerical representation of data that captures its meaning and context.

Think of it as translating human language into a mathematical language that AI can understand.

Instead of storing

Machine Learning

AI stores something like

[0.42, -0.18, 0.91, 0.13, ...]

This list of numbers represents the meaning of the text.

Notice something...That list itself is actually a vector.

Wait... Then What's a Vector?

This is where most people get confused.

A vector is simply an ordered list of numbers.

Example:

[1, 5, 9]
or
[0.27, -0.83, 0.15, 0.92]

A vector has no inherent meaning. It's just numbers.

An embedding is a special type of vector whose numbers have been learned by an AI model to represent semantic meaning.

In other words:

Every embedding is a vector, but not every vector is an embedding.

Think of it like this:

  • Every mango is a fruit.
  • But not every fruit is a mango.

A Real-Life Analogy :

Imagine Google Maps.

Every city has coordinates.

Mumbai
(19.0760, 72.8777)

Delhi
(28.6139, 77.2090)

Coordinates don't describe the city. They simply tell you where it is.

Embeddings work similarly.

Instead of physical location, they represent semantic location.

Words with similar meanings end up close together.

Example : Suppose we have four words.

King
Queen
Apple
Banana

Their embeddings might conceptually look like this:

King → [0.91, 0.77]
Queen → [0.90, 0.79]
Apple → [-0.83, 0.21]
Banana → [-0.79, 0.25]

Notice:

  • King and Queen are close.
  • Apple and Banana are close.

The AI has learned relationships without anyone explicitly programming them.

How Are Embeddings Created?

This is where the embedding model comes in.

Popular embedding models include:

  1. OpenAI text-embedding models
  2. BAAI BGE
  3. E5
  4. Sentence Transformers
  5. Cohere Embed

Suppose your document says:

Employees receive 20 annual leaves.

The embedding model reads the sentence and produces something like:

[0.22,
-0.63,
0.89,
0.11,
...1536 numbers...]

Those numbers encode meaning.

Where Are Embeddings Stored?

Embeddings are stored inside a Vector Database.

Popular vector databases include:

  1. Pinecone
  2. Chroma
  3. FAISS
  4. Milvus
  5. Weaviate

How Does the Vector Database Know Which Documents Are Similar?

Imagine this 2D space.

                 AI

                  ● Machine Learning

             ● Deep Learning

--------------------------------------------

      ● Pizza

             ● Burger
Enter fullscreen mode Exit fullscreen mode

When someone asks:

"Tell me about AI."

The query embedding lands near:

  • Machine Learning
  • Deep Learning

Not near

  • Pizza

The vector database doesn't search words. It searches distances between vectors.

Measuring Similarity?

The database compares vectors using mathematical distance metrics.

Common ones include:

  1. Cosine Similarity (most common for text)
  2. Euclidean Distance
  3. Dot Product

Imagine throwing a dart onto a map, the database returns the nearest neighbors to where your query landed.

The closer two vectors are, the more similar their meanings are.

Why Are Embeddings So Powerful?

They enable:

  • Semantic search instead of keyword search.
  • Better document retrieval for RAG.
  • Product recommendations.
  • Similarity search.
  • Duplicate detection.
  • Personalized content.
  • Question answering.
  • Document clustering.

Keyword Search vs Semantic Search

Suppose your document says:

"Employees are entitled to 20 days of annual leave."

You ask:

"How many vacation days do I get?"

A keyword search might fail because the document says annual leave, not vacation days.

A vector search succeeds because the embeddings for "vacation days" and "annual leave" are close in semantic space.

This is one of the biggest advantages of embeddings.

Vector Vs Embedding

Final Takeaway

Vectors are simply numerical arrays. Embeddings are vectors that have been learned by AI models to capture semantic meaning. In a Generative AI application, documents and user queries are converted into embeddings and stored in a vector database. When a user asks a question, the query embedding is compared with stored embeddings to find the most semantically relevant information, which is then passed to the LLM to generate an accurate response.

In short:

Vectors are the language of mathematics. Embeddings are the language of meaning.

That's what makes modern AI systems capable of understanding concepts rather than just matching keywords.

Top comments (0)