CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Turn Any Text Into Numbers Your Code Can Search: Sentence Transformers in 10 Lines of Python

Turn Any Text Into Numbers Your Code Can Search: Sentence Transformers in 10 Lines of Python

Chris Harper

2 min read

Aug 23, 2026 · 12:15 UTC

AI
Tutorial
Embeddings
HuggingFace

TL;DR: The sentence-transformers library converts text into dense vectors that encode meaning — load a model, call .encode(), compare with .similarity(). No API key, no cloud call, runs on a laptop CPU.

What you'll be able to do after this:

  • Convert sentences into 384-dimensional float arrays where similar text ends up close together in vector space
  • Find the semantically closest match to a query in a list of documents without writing a single rule
  • Understand what every RAG pipeline's embedding step is actually doing under the hood

The model has one job: given a sentence, return a fixed-length vector where meaning is encoded in direction and magnitude. Sentences about similar topics cluster together; unrelated sentences don't. That geometry is what makes semantic search, clustering, and retrieval work.

pip install sentence-transformers
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")  # 384-dim, ~80MB, CPU-friendly

corpus = [
    "Claude Code can spin up subagents for parallel work",
    "Autonomous agents complete tasks without step-by-step supervision",
    "The weather in San Francisco is mild year-round",
]
corpus_embeddings = model.encode(corpus)

query_embedding = model.encode(["agents that work unattended"])
scores = model.similarity(query_embedding, corpus_embeddings)
print(scores)  # tensor([[0.88, 0.83, 0.10]])

The first two sentences are semantically close to the query; the weather one is not. You wrote no rules — the similarity falls out of the geometry of the embedding space.

Where it breaks: all-MiniLM-L6-v2 is fast and small (80 MB) but loses precision on domain-specific jargon and proprietary terms that were not in its training data. For production over technical documentation, compare candidates on the MTEB leaderboard before committing. Also: cosine similarity scores are not calibrated — 0.6 does not mean "60% similar." Compare scores against each other, not against an absolute cutoff.

Sources: Sentence Transformers quickstart — sbert.net · HuggingFace sentence-transformers collection · MTEB Leaderboard