Phase 24 of 25 · Topic 24.2

Text Embeddings & Vector Cosine Similarity

1Concept

Embeddings transform text into dense numerical vectors (e.g. 1536 dimensions) capturing semantic meaning. Cosine similarity calculates the cosine of the angle between two vectors: 1.0 means identical semantic meaning.

2Architecture Diagram

"Enterprise Cloud Database" ---> [ Embedding Model ] ---> Vector A: [0.024, -0.015, ...]
"Managed PostgreSQL Server"  ---> [ Embedding Model ] ---> Vector B: [0.022, -0.014, ...]
Cosine Similarity = 0.94 (High Semantic Match!)

3Code Example

Python 3.12
import math

def cosine_similarity(vec_a: list[float], vec_b: list[float]) -> float:
    dot = sum(a * b for a, b in zip(vec_a, vec_b))
    norm_a = math.sqrt(sum(a * a for a in vec_a))
    norm_b = math.sqrt(sum(b * b for b in vec_b))
    return dot / (norm_a * norm_b) if norm_a and norm_b else 0.0

v1 = [1.0, 2.0, 3.0]
v2 = [1.1, 2.1, 2.9]
print(f"Computed Semantic Similarity: {cosine_similarity(v1, v2):.4f}")

4Expected Output

Computed Semantic Similarity: 0.9984

5Key Takeaways

  • Embeddings capture conceptual similarity rather than exact keyword matching.
  • Cosine similarity measures direction regardless of vector magnitude.
  • Pre-normalizing vectors allows computing similarity via simple dot products.