New Tool Radar

AI Concepts

How to Embed Notes and Rank Them by Cosine Similarity

Embed your notes once, embed the question the same way, and rank the notes by cosine similarity.

Schematic illustration of embed notes and rank them by cosine similarity: step one embed the notes, step two embed the question, step three rank by similarity.

An embedding is a vector, a list of floating point numbers, and the distance between two vectors measures how related the texts are: small distances suggest high relatedness. To search notes, embed each note once, embed the question with the same model, and rank the notes by cosine similarity to the question. OpenAI recommends cosine similarity for its embeddings.

Prerequisites

  • An embedding model and API access.
  • A set of notes to search, as plain text.
  • Code that can compute cosine similarity, or a vector database that does it.

Steps

Step 1: Embed the notes

Send each note to the embeddings endpoint with one model, and store each returned vector next to its note. Use the same model for every note; vectors from different models cannot be compared.

Step 2: Embed the question

Send the question to the same endpoint with the same model. One question gives one vector, in the same space as the notes.

Step 3: Rank by similarity

Compute the cosine similarity between the question vector and each note vector, and sort the notes from highest to lowest. Return the top few, not the whole list, to the model or the user.

Worked example: Maple Corner bakery recipe notes

The fictional Maple Corner bakery embeds 3 recipe notes and asks which note covers sourdough starter care.

Note Cosine similarity to the question Rank
Feeding a sourdough starter 0.87 1
Baking rye bread 0.64 2
Cleaning the ovens 0.31 3

The sourdough starter note scores 0.87, the highest, so it ranks first, and the oven-cleaning note scores 0.31 and ranks last.

Expected result. The notes rank in similarity order, with the most relevant note first, and the ranking comes from one model used for both notes and question. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • Similarity measures relatedness of wording, not truth; a wrong note can still rank first.
  • Embeddings from different models, or different dimension counts, cannot be compared directly.
  • This guide does not cover storing vectors in a database.

Sources

See all