Sentence Transformers Library
You'll learn how to generate embeddings locally using the sentence-transformers library and pre-trained models.
What Is Sentence Transformers?
Sentence Transformers is a Python library that lets you create embeddings on your own machine without calling external APIs. Instead of sending text to OpenAI or Cohere, you download a pre-trained model once and run it locally. Think of it like owning a pocket calculator instead of calling a mathematician every time you need to add numbers—you have the tool right there.
The library specializes in sentence and document embeddings, making it perfect for semantic search, clustering, and similarity tasks. It's built on top of transformer models (like BERT) but optimized specifically for creating meaningful vector representations.
Why Use It?
- No API costs: After downloading the model, generating embeddings is free
- Privacy: Your data never leaves your infrastructure
- Speed: No network latency for each request
- Offline capability: Works without internet once models are cached
Basic Usage Pattern
from sentence_transformers import SentenceTransformer
# Load a pre-trained model (downloads once, then cached)
model = SentenceTransformer('all-MiniLM-L6-v2')
# Generate embeddings
sentences = ["AI is fascinating", "Machine learning powers recommendations"]
embeddings = model.encode(sentences)
# embeddings is now a numpy array of vectors
print(embeddings.shape) # (2, 384) - two sentences, 384 dimensions
Popular models like all-MiniLM-L6-v2 balance quality and speed, while larger models like all-mpnet-base-v2 offer higher accuracy at the cost of computation time.
Key Takeaway: Sentence Transformers gives you local, cost-free embedding generation using pre-trained models—ideal when privacy, cost, or offline operation matter more than cutting-edge API performance.