GENERATIVE AI • LESSON 7

Semantic Similarity

The simplest definition is:

CORE IDEA

Semantic similarity measures how similar two pieces of text are in meaning, even when they use different words.

01

Simple Example

Consider these two sentences:

Sentence A:

"I love learning Python."

Sentence B:

"Python programming is something I really enjoy."

The words are different, but the meaning is very similar.

Semantic similarity → HIGH

Now compare:

Sentence C:

"The car needs new tires."

That's about something completely different.

A ↔ B → High similarity

A ↔ C → Low similarity

That's semantic similarity.

02

Example With Three Sentences

Suppose we have:

A = "I love Python programming."

B = "I enjoy coding with Python."

C = "The weather is very cold today."

After generating embeddings:

A → Vector A
B → Vector B
C → Vector C

We compare them.

A ↔ B
HIGH similarity

A ↔ C
LOW similarity

B ↔ C
LOW similarity

Why? Because A and B are about Python programming. C is about weather.

03

What Is a Similarity Score?

A similarity algorithm compares two vectors and produces a number.

For example, conceptually:

Sentence A:
"I love Python."

Sentence B:
"I enjoy Python programming."

Similarity:
0.91

And:

Sentence A:
"I love Python."

Sentence C:
"I bought a new car."

Similarity:
0.12

The exact score depends on the embedding model and similarity method.

IMPORTANT

Don't teach students that 0.9 always means "similar" and 0.2 always means "different."

There is no universal threshold.

04

Practical OpenAI Python Example

Using an embedding model:

</>   Python
from openai import OpenAI

client = OpenAI()

texts = [
    "I love learning Python.",
    "Python programming is very interesting."
]

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

vector_a = response.data[0].embedding
vector_b = response.data[1].embedding

Now calculate similarity:

</>   Python
import math


def cosine_similarity(a, b):

    dot_product = sum(
        x * y for x, y in zip(a, b)
    )

    magnitude_a = math.sqrt(
        sum(x * x for x in a)
    )

    magnitude_b = math.sqrt(
        sum(x * x for x in b)
    )

    return dot_product / (
        magnitude_a * magnitude_b
    )


score = cosine_similarity(
    vector_a,
    vector_b
)

print("Similarity:", score)
05 • WHOLE IDEA

Whole Idea

Semantic similarity allows an AI application to find information that is similar in meaning, not just information containing the same words.