Using Embeddings in Practice: Generating Them and Computing Cosine Similarity

~13 min read

Turning the theory into code: call an embedding API or a local Hugging Face model to get vectors, then use cosine similarity — the standard metric — to measure how close two pieces of text are in meaning.

Using Embeddings in Practice: Generating Them and Computing Cosine Similarity is a Pro topic

Sign in, then upgrade to Pro or Power to unlock this topic and the full AI Engineering curriculum.

Key points

  • Getting an embedding means calling a hosted API (OpenAI, no local setup, per-call cost) or running a local model (Hugging Face sentence-transformers, no per-call cost, you host it)
  • Both paths return the same shape of thing — a fixed-length vector — so downstream comparison code works the same regardless of source
  • Cosine similarity measures the ANGLE between two vectors, ignoring their length — ranges from -1 (opposite) to 0 (unrelated) to 1 (identical direction)
  • For well-trained text embeddings, similar meanings reliably score high cosine similarity (often 0.7+), unrelated text scores much lower
  • The full semantic-search recipe: embed documents once, embed the query the same way, rank documents by cosine similarity to the query — brute-force fine at small scale