CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
From Text to Vectors: Compute Your First Text Embeddings with sentence-transformers

From Text to Vectors: Compute Your First Text Embeddings with sentence-transformers

Chris Harper

3 min read

Aug 8, 2026 · 12:03 UTC

AI
Tutorial
Embeddings
HuggingFace

Three lines of Python turn any text into a 384-dimensional vector — and cosine similarity tells you exactly how closely two sentences mean the same thing, even when they share no words.

What you'll be able to do after this:

  • Turn raw text into dense vector representations using a pre-trained model — no training required, runs on CPU
  • Measure semantic similarity between any two strings using cosine similarity, even when they share no vocabulary
  • Understand what an embedding actually is and why the geometric distance between vectors captures meaning — the foundation for every RAG pipeline, semantic search engine, and recommendation system

Install

pip install sentence-transformers

The model used here is all-MiniLM-L6-v2: 22 million parameters, 384-dimensional output, roughly 14,000 sentences per second on a standard CPU. It's the standard starting point — small enough to run anywhere, good enough for most retrieval and similarity tasks.

Compute your first embeddings

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

sentences = [
    "The weather is lovely today.",
    "It's so sunny outside!",
    "He drove to the store yesterday.",
]

embeddings = model.encode(sentences)
print(embeddings.shape)  # (3, 384)

Each sentence becomes a 384-dimensional float array. That's it. The model has already learned — from over a billion training pairs — how to map semantically similar text to nearby regions of the vector space.

Measure similarity

similarities = model.similarity(embeddings, embeddings)
print(similarities)

Output (approximate):

tensor([[1.00, 0.67, 0.10],
        [0.67, 1.00, 0.09],
        [0.10, 0.09, 1.00]])

The two weather sentences score 0.67 — semantically close, despite sharing no words. "He drove to the store" scores ~0.10 against both — unrelated. A cosine score of 1.0 means identical direction in vector space; 0.0 means orthogonal (unrelated); negative means opposite.

Encode a query against a corpus

The most common real-world use: given a user query, find the most relevant passages from a set of documents.

corpus = [
    "Puppies are adorable little dogs.",
    "The stock market fell sharply today.",
    "Kittens are playful young cats.",
    "Interest rates affect bond prices.",
]
query = "cute baby animals"

corpus_embeddings = model.encode(corpus)
query_embedding = model.encode([query])

scores = model.similarity(query_embedding, corpus_embeddings)[0]
ranked = sorted(zip(scores, corpus), reverse=True)

for score, sentence in ranked:
    print(f"{score:.2f}  {sentence}")

Output:

0.71  Puppies are adorable little dogs.
0.68  Kittens are playful young cats.
0.04  The stock market fell sharply today.
0.02  Interest rates affect bond prices.

The model ranked "cute baby animals" correctly against passages it has never seen, with no keyword overlap required.

What comes next

This is the foundation. The next step in the curriculum is storing these vectors in a vector database — Chroma, FAISS, or pgvector — so you can search across thousands or millions of them in milliseconds. The encode() + similarity pattern you just ran is exactly what happens inside every RAG retrieval step; knowing it hands-on makes the rest of the stack legible.

Over 10,000 pre-trained Sentence Transformer models are available on Hugging Face for specialized domains (code, scientific text, multilingual) — all use the same API.

Sources: Quickstart — sbert.net · all-MiniLM-L6-v2 — Hugging Face · Semantic Textual Similarity — sbert.net