🧠 AI with Python – 🤖 Sentence Embeddings with Sentence Transformers
Posted on: July 28, 2026
Description:
If you've ever searched for something online, you've probably noticed that traditional search engines work best when your search query contains the exact words you're looking for. Search for "Python AI library" and you'll likely get results containing those exact keywords. But what if a document says "Python is widely used for Artificial Intelligence" instead? Even though both phrases mean nearly the same thing, a simple keyword search may not rank that document highly.
Modern AI solves this problem using Sentence Embeddings.
Instead of treating text as a collection of individual words, sentence embeddings represent the entire meaning of a sentence as a numerical vector. Once sentences are converted into vectors, AI can compare their meanings mathematically instead of relying on exact word matches. This is one of the fundamental ideas behind semantic search, Retrieval-Augmented Generation (RAG), recommendation systems, enterprise search, and many other Large Language Model applications.
In this article, we'll explore how Sentence Transformers make generating sentence embeddings remarkably simple and why they have become one of the most important tools in modern Natural Language Processing.
Why Do We Need Sentence Embeddings?
Imagine these two sentences:
Python is excellent for AI development.
Artificial Intelligence projects commonly use Python.
A human immediately understands that both sentences convey almost the same idea.
However, a traditional keyword-based algorithm focuses primarily on matching individual words. Since the wording differs, it may not recognize that the two sentences are closely related.
Sentence embeddings solve this challenge by converting both sentences into vectors that capture their semantic meaning. Because both vectors represent similar ideas, they end up being very close together in the vector space.
This allows AI to retrieve information based on meaning, not simply matching words.
What Exactly Is a Sentence Embedding?
Think of an embedding as a mathematical fingerprint of a sentence.
Instead of storing text as plain words, a neural network converts every sentence into hundreds of numerical values.
For example:
"Python is great for AI"
↓
[0.12, -0.44, 0.91, ..., 0.27]
Of course, real embeddings contain hundreds of dimensions, making them impossible for humans to interpret directly. What matters is that sentences with similar meanings produce embeddings that are located close to each other.
This simple idea is the foundation of semantic AI.
Meet Sentence Transformers
Creating high-quality sentence embeddings used to require building complex deep learning architectures from scratch.
Fortunately, the Sentence Transformers library makes this incredibly easy.
With just a single line of code, we can load a pre-trained model:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
The all-MiniLM-L6-v2 model is one of the most widely used embedding models because it offers an excellent balance between speed, accuracy, and model size.
Instead of training a neural network ourselves, we simply use a model that has already learned to represent the semantic meaning of millions of sentences.
Converting Sentences into Embeddings
Let's create a small collection of sentences:
sentences = [
"Python is widely used for Artificial Intelligence.",
"Machine learning allows computers to learn from data.",
"Large Language Models understand natural language.",
"Football is played by millions of people worldwide.",
"Deep learning uses neural networks."
]
Generating embeddings is surprisingly simple:
embeddings = model.encode(
sentences,
convert_to_tensor=True
)
Each sentence is now represented by a dense numerical vector.
Although we can't directly understand the numbers, the model has already encoded the semantic meaning of every sentence into those vectors.
Searching by Meaning Instead of Keywords
Suppose a user searches for:
Artificial Intelligence with Python
Notice that none of our stored sentences contain this exact phrase.
We first convert the query into an embedding:
query_embedding = model.encode(
"Artificial Intelligence with Python",
convert_to_tensor=True
)
Now both the stored sentences and the search query exist in the same semantic space.
The next step is comparing them.
Measuring Semantic Similarity
To compare embeddings, we use Cosine Similarity.
from sentence_transformers import util
similarities = util.cos_sim(
query_embedding,
embeddings
)
Cosine Similarity measures how close two vectors are.
- 1.0 → Almost identical meaning
- 0.8 → Highly related
- 0.5 → Moderately related
- 0.0 → Completely unrelated
Rather than checking whether words match, AI simply asks:
"How close are these meanings?"
This is what makes semantic search so powerful.
Ranking the Best Matches
Once similarity scores are calculated, we simply sort them.
scores = similarities[0]
ranked_results = sorted(
zip(sentences, scores),
key=lambda x: x[1],
reverse=True
)
The sentence with the highest score becomes our best result.
Instead of returning documents that merely contain matching keywords, we're now returning documents that are semantically related.
This is precisely how modern intelligent search engines work.
Why This Is So Important for Large Language Models
Sentence embeddings aren't just useful for search engines.
They're one of the core building blocks of today's AI ecosystem.
Whenever an LLM needs external knowledge, the first challenge is identifying which documents are actually relevant.
This is exactly where sentence embeddings come into play.
For example:
- AI chatbots search company knowledge bases.
- Customer support systems retrieve similar support tickets.
- Enterprise search finds documents across thousands of files.
- Recommendation systems identify similar products.
- Duplicate detection identifies repeated questions.
- Document clustering groups related articles automatically.
In almost every case, embeddings are responsible for retrieving the most relevant information before an LLM generates its final response.
From Sentence Embeddings to RAG
This concept becomes even more powerful when combined with vector databases.
Instead of comparing every document one by one, embeddings are stored inside databases such as FAISS, ChromaDB, Pinecone, or Weaviate.
When a user asks a question, the application converts the query into an embedding and retrieves the most semantically similar documents within milliseconds.
Those retrieved documents are then passed to a Large Language Model, allowing it to answer questions using external knowledge instead of relying only on what it learned during training.
This entire workflow is known as Retrieval-Augmented Generation (RAG), one of the most influential techniques in modern AI development.
Final Thoughts
Sentence Embeddings are one of the biggest reasons modern AI feels intelligent. They enable applications to understand meaning rather than matching words, allowing systems to retrieve relevant information even when the wording is completely different.
With the Sentence Transformers library, generating high-quality embeddings requires only a few lines of Python code, making sophisticated semantic AI accessible to every developer. Whether you're building chatbots, recommendation engines, enterprise search platforms, or document retrieval systems, sentence embeddings form the foundation that makes these applications possible.
In this article, we explored how sentence embeddings work, learned how to generate them using Sentence Transformers, and saw how cosine similarity enables semantic search. These concepts are not only valuable on their own but also serve as the stepping stone toward one of the most important LLM architectures used today.
Code Snippet:
from sentence_transformers import SentenceTransformer
from sentence_transformers import util
# =========================================================
# Load Pretrained Sentence Transformer
# =========================================================
print("=" * 70)
print("Loading Sentence Transformer Model...")
print("=" * 70)
model = SentenceTransformer("all-MiniLM-L6-v2")
# =========================================================
# Create Sample Knowledge Base
# =========================================================
sentences = [
"Python is widely used for Artificial Intelligence.",
"Machine learning allows computers to learn from data.",
"Large Language Models understand natural language.",
"Football is played by millions of people worldwide.",
"Deep learning uses neural networks.",
"Pandas is a powerful Python library for data analysis."
]
print("\nKnowledge Base:\n")
for index, sentence in enumerate(sentences, start=1):
print(f"{index}. {sentence}")
# =========================================================
# Generate Sentence Embeddings
# =========================================================
print("\nGenerating Sentence Embeddings...\n")
embeddings = model.encode(
sentences,
convert_to_tensor=True
)
print(f"Generated {len(embeddings)} sentence embeddings.")
# =========================================================
# Encode Search Query
# =========================================================
query = "Artificial Intelligence with Python"
print("\nSearch Query:")
print(query)
query_embedding = model.encode(
query,
convert_to_tensor=True
)
# =========================================================
# Compute Semantic Similarity
# =========================================================
similarities = util.cos_sim(
query_embedding,
embeddings
)
# =========================================================
# Rank Results
# =========================================================
scores = similarities[0]
ranked_results = sorted(
zip(sentences, scores),
key=lambda x: x[1],
reverse=True
)
# =========================================================
# Display Search Results
# =========================================================
print("\n" + "=" * 70)
print("Most Similar Sentences")
print("=" * 70)
for rank, (sentence, score) in enumerate(ranked_results, start=1):
print(f"\nRank #{rank}")
print("-" * 60)
print(sentence)
print(f"Similarity Score : {score:.4f}")
# =========================================================
# Compare Multiple Queries
# =========================================================
queries = [
"Neural Networks",
"Artificial Intelligence",
"Sports",
"Python Programming",
"Data Analysis"
]
print("\n" + "=" * 70)
print("Multiple Query Search")
print("=" * 70)
for query in queries:
query_embedding = model.encode(
query,
convert_to_tensor=True
)
similarities = util.cos_sim(
query_embedding,
embeddings
)[0]
best_match = similarities.argmax()
print(f"\nQuery : {query}")
print(f"Best Match : {sentences[best_match]}")
print(f"Score : {similarities[best_match]:.4f}")
# =========================================================
# Compare Two Individual Sentences
# =========================================================
sentence1 = "Python is excellent for AI."
sentence2 = "Artificial Intelligence projects commonly use Python."
embedding1 = model.encode(
sentence1,
convert_to_tensor=True
)
embedding2 = model.encode(
sentence2,
convert_to_tensor=True
)
score = util.cos_sim(
embedding1,
embedding2
)
print("\n" + "=" * 70)
print("Sentence-to-Sentence Similarity")
print("=" * 70)
print(f"\nSentence 1 : {sentence1}")
print(f"Sentence 2 : {sentence2}")
print(f"\nSimilarity Score : {score.item():.4f}")
# =========================================================
# Semantic Similarity Matrix
# =========================================================
print("\n" + "=" * 70)
print("Similarity Matrix")
print("=" * 70)
matrix = util.cos_sim(
embeddings,
embeddings
)
for i, sentence in enumerate(sentences):
print(f"\n{sentence}")
for j, similarity in enumerate(matrix[i]):
print(f" -> Sentence {j + 1}: {similarity:.4f}")
# =========================================================
# Program Completed
# =========================================================
print("\n" + "=" * 70)
print("Sentence Embedding Demonstration Completed Successfully!")
print("=" * 70)
No comments yet. Be the first to comment!