Semantic Similarity
Learn how AI systems decide whether two pieces of text are similar in meaning, even when they use different words. You will connect embeddings, vector similarity, semantic search, thresholds, and practical Python evaluation.
By the end of this lesson, you will be able to:
- Explain semantic similarity in simple terms.
- Understand how embeddings turn meaning into comparable vectors.
- Distinguish semantic similarity from keyword matching.
- Calculate and interpret similarity scores with Python.
- Choose practical thresholds and evaluate similarity results.
- Apply semantic similarity to search, FAQs, recommendations, and RAG.
What Is Semantic Similarity?
Semantic similarity measures how closely two pieces of content are related in meaning. The wording does not have to be identical. Two sentences can use different vocabulary while expressing nearly the same idea.
Keyword Matching vs Semantic Similarity
Traditional keyword search can be excellent when users know the exact terms. Semantic similarity becomes useful when the query and the stored content use different words for the same or related idea.
Looks at words
Query: “cancel subscription”
Document: “How to cancel your subscription”
Looks at meaning
Query: “I don't want my plan anymore”
Document: “How to end your membership”
How Embeddings Enable Semantic Similarity
An embedding model converts text into a numerical vector. Once two texts have vectors, a similarity or distance function can compare those vectors.
The embedding model and similarity metric work together. A similarity score is only meaningful in the context of the model, metric, data, and task being used.
A Simple Semantic Similarity Example
Consider these three pairs. The first pair expresses almost the same request. The second pair is related but not identical. The third pair is unrelated.
Similarity Scores
A similarity function produces a numerical signal that can be used to rank candidate texts. With cosine similarity, for example, vectors with more aligned directions receive higher cosine values.
The numbers above are illustrative. Do not copy a score such as 0.80
as a universal definition of “similar.” Different embedding models and systems
can produce different score distributions.
Cosine Similarity and Semantic Similarity
A common approach is to represent each text as an embedding and calculate cosine similarity between the two vectors.
For embedding-based semantic comparison, cosine similarity is widely used, but it is not the only possible metric. The correct choice depends on the embedding system and application.
Python: Compare Two Simple Vectors
Before using a real embedding model, it helps to understand the comparison step with small vectors.
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (
np.linalg.norm(a) * np.linalg.norm(b)
)
a = np.array([0.8, 0.2, 0.1])
b = np.array([0.7, 0.3, 0.1])
score = cosine_similarity(a, b)
print("Similarity:", score)
Python: Compare Multiple Text Candidates
A semantic search workflow usually compares one query against many candidate vectors and then ranks the candidates by similarity.
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (
np.linalg.norm(a) * np.linalg.norm(b)
)
query = np.array([0.8, 0.2, 0.1])
documents = {
"password reset": np.array([0.78, 0.24, 0.12]),
"account login": np.array([0.70, 0.30, 0.15]),
"weather forecast": np.array([0.05, 0.90, 0.20]),
}
results = []
for name, vector in documents.items():
score = cosine_similarity(query, vector)
results.append((name, score))
results.sort(key=lambda item: item[1], reverse=True)
for name, score in results:
print(f"{name}: {score:.3f}")
In production, the document vectors would normally be generated by an embedding model and stored in an appropriate vector index or database.
Similarity Is Not the Same as Correctness
A high similarity score means that the vectors are close according to the selected comparison method. It does not prove that the retrieved text is the correct answer.
Choosing a Similarity Threshold
Sometimes an application needs a decision rather than a ranking: “Is this FAQ close enough to answer the user's question?” A threshold can help, but it must be selected from evaluation data rather than guessed.
For example, you might test thresholds such as 0.70, 0.75, 0.80, and 0.85 on a labeled set of real queries. Measure how many relevant results are retrieved and how many irrelevant results are incorrectly accepted.
Semantic Similarity in Semantic Search
Semantic search is one of the most common applications. The system embeds a user query, compares it with stored document embeddings, and returns the strongest candidates.
Semantic Similarity in RAG
In Retrieval-Augmented Generation, semantic similarity helps retrieve chunks that are relevant to a user's question before the language model generates an answer.
Retrieval quality also depends on chunk size, metadata, query formulation, embedding model, indexing strategy, reranking, and evaluation. Similarity is an important component, not the entire RAG system.
Real-World Applications
Match natural-language questions to help articles.
Find relevant passages even when wording differs.
Find products or content with related representations.
Identify records or messages that express similar ideas.
Compare a support request with known issue categories.
Retrieve context that can support an LLM answer.
Common Mistakes
Score distributions differ by model, data, metric, and task.
Two texts can be related but still answer different questions.
Vectors should come from a compatible embedding space and representation pipeline.
Use real queries and labeled examples to measure retrieval quality.
A similarity score is a ranking signal, not a guarantee of factual correctness.
Hybrid approaches can combine semantic and keyword signals when exact terms matter.
Build a Tiny Semantic Similarity Experiment
Create a small dataset of FAQ questions and use vectors to compare a new query against the existing questions.
- Write five FAQ questions about one topic.
- Create or obtain an embedding for each question.
- Create an embedding for a new user query.
- Calculate similarity between the query and every FAQ.
- Sort the results from highest to lowest similarity.
- Test several thresholds and inspect false matches.
Test Your Understanding
How closely two pieces of content are related in meaning according to a chosen representation and comparison method.
Embeddings represent learned relationships in a vector space, allowing related wording to be compared through their vectors.
No. Score interpretation depends on the model, metric, data, and application. Thresholds should be evaluated.
No. It is a useful retrieval or ranking signal, not proof of correctness.
Semantic Similarity in One Flow
Key Takeaways
- Semantic similarity compares content by meaning rather than only exact word overlap.
- Embeddings provide the vector representations that make numerical comparison possible.
- Cosine similarity is a common way to compare embedding vectors.
- Similarity scores are model- and task-dependent; there is no universal threshold.
- High similarity does not guarantee relevance, factual correctness, or the right answer.
- Semantic similarity is a core building block for semantic search, recommendations, duplicate detection, and RAG.
- Always evaluate similarity and retrieval behavior using real examples from your application.