LlamaIndexllama-index-core 0.14 · Python 3.10+
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
30 small wins to finish your pathNext lesson →

Embeddings: text as numbers that carry meaning

An embedding is a list of numbers that stands for the meaning of a text, so two texts that mean the same thing get vectors that point almost the same way, even with no word in common.

Last updated: 28 Sep, 2026 · LlamaIndex 0.14

The last lesson showed keyword search failing on "How do I get my money back?". Embeddings are the fix: they place the meaning of a text as a point in space, and a question near a passage means the same thing as it.

Loading the embedding model

Settings holds the defaults LlamaIndex uses everywhere. Set embed_model once to the local model, and every later step uses it:

python
from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")

Turning a question into 384 numbers

Example
vector = Settings.embed_model.get_text_embedding("How do I get my money back?")
print(len(vector))
print([round(x, 3) for x in vector[:5]])

Reading the vector

  • 384 numbers come back for any text: this model always returns a vector of that length.
  • Each number on its own means nothing readable; the meaning is in the whole vector's direction.
  • The same text always gives the same vector, so results are repeatable.

Measuring closeness with cosine

Two vectors are close in meaning when they point the same way. Cosine similarity measures that: multiply the vectors together and divide by their lengths, which gives a number near 1 for the same direction and near 0 for unrelated text.

python
import numpy as np


def similarity(a, b):
    va = np.array(Settings.embed_model.get_text_embedding(a))
    vb = np.array(Settings.embed_model.get_text_embedding(b))
    return float(va @ vb / (np.linalg.norm(va) * np.linalg.norm(vb)))

Comparing the money-back question to three sentences

Example
question = "How do I get my money back?"
for text in ["You can get a full refund within 30 days of delivery.", "Standard delivery takes 3 to 5 working days.", "The LMP-204 desk lamp has a known cable fault."]:
    print(f"{similarity(question, text):.3f}  {text}")

Reading the similarity scores

  • The refund sentence scores highest for the money-back question, with no word in common, which is the case keyword search could not handle.
  • The delivery and lamp sentences score near zero, so meaning tells the relevant sentence from the rest.
  • The scores are cosine values, so they are comparable across questions of any length.
Keyword searchEmbeddings
ComparesWords presentDirection of two vectors
"money back" and refundNo matchHigh similarity
Score meaningCount of shared wordsCosine, near 1 is close
CostNoneOne local model, run once per text

When embeddings help most

  • Questions asked in the reader's own words, where the document uses different terms.
  • Search over prose, where meaning matters more than the exact string.
  • As the backbone of the vector index built in the next lesson.
Local model, real results
Watch out. Two sentences that mean the opposite, such as "refunds are allowed" and "refunds are not allowed", still score high together, because they are about the same topic. Similarity finds the relevant passage; deciding whether it says yes or no is the answering step's job, later in the course.
Try it yourself
  • Compare "My parcel is late" with the three sentences.
  • Compare "refunds are allowed" with "refunds are not allowed". What does the score tell you?
  • Embed the same sentence twice and check the similarity.

Slow is fine. Stopping is the only problem.