Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    AI Embeddings and Vector Similarity

    Artificial Intelligence Tutorials


    An embedding represents an item as a list of numbers called a vector. Embedding models place related items near one another in a mathematical space. This can help an application find relevant text even when a question does not repeat the document's exact words.

    A close vector is a clue about similarity, not proof that a document answers the question. Always inspect the retrieved content before using it as evidence.

    From Text to Vectors

    Imagine a help centre with articles about refunds, delivery, and account access. An embedding model converts each article into a vector such as [0.12, -0.38, ...]. The numbers are learned by the model; they are not hand-written labels. Real vectors can have many dimensions, and individual values are usually difficult to interpret on their own.

    1. Convert each article or passage into a vector and store it with its source text.
    2. Convert the customer's question using the same compatible embedding model.
    3. Compare the question vector with stored vectors and rank the closest candidates.
    4. Read the candidate passages to decide whether they actually answer the question.
    Illustrative query and document vectors with the refund-policy vector closest to the query
    In this toy diagram, the refund-policy vector is closer to the query than the delivery and account vectors.

    How Is Similarity Measured?

    Cosine similarity compares the direction of two non-zero vectors. Its score is 1 for vectors pointing in the same direction, 0 for perpendicular vectors, and -1 for opposite directions. A high score does not mean there is that probability of a correct answer.

    Suppose a toy query vector is [1, 0]. The document vector [0.8, 0.2] points in a similar direction, while [0, 1] is perpendicular. The first document ranks closer by cosine similarity. Real text vectors are learned and much larger; these two-number vectors only make the geometry easy to see.

    Comparison What it measures
    Cosine similarity Angle or direction between vectors
    Euclidean distance Straight-line distance between points
    Dot product Direction and magnitude together

    Use a metric compatible with the embedding model and search index. Evaluate the ranking on real questions instead of assuming one metric works best for every task.

    Try a Small Python Comparison

    The example uses hand-chosen vectors to show the calculation. It does not generate embeddings or perform semantic search. A real application would obtain vectors from an embedding model.

    Example:

    from math import sqrt
    
    
    def cosine_similarity(left, right):
        if len(left) != len(right):
            raise ValueError("Vectors must have the same size")
    
        dot = sum(a * b for a, b in zip(left, right))
        left_length = sqrt(sum(value * value for value in left))
        right_length = sqrt(sum(value * value for value in right))
        if left_length == 0 or right_length == 0:
            raise ValueError("Zero vectors have no direction")
        return dot / (left_length * right_length)
    
    
    query = [1.0, 0.0]
    documents = {
        "Refund policy": [0.8, 0.2],
        "Delivery guide": [0.1, 0.9],
        "Account help": [0.0, 1.0],
    }
    
    for title, vector in documents.items():
        print(f"{title}: {cosine_similarity(query, vector):.3f}")

    Output:

    Refund policy: 0.970
    Delivery guide: 0.110
    Account help: 0.000

    The refund vector ranks first because it points closest to the query vector. The article titles do not affect the calculation. In a real system, inspect the retrieved text rather than trusting the score alone.

    Where Can Embedding Search Fail?

    • Wrong intent: “Cancel an order” and “cancel a subscription” may be close in wording but need different policies.
    • No good match: A simple nearest-neighbor search can still return a top result when none is useful. Add a no-match path and test it.
    • Changed content: Re-create a stored vector when its source passage changes.
    • Incompatible vectors: Do not compare vectors from unrelated models as though their coordinates meant the same thing.
    • Private content: Control what text is sent to an embedding service and who may search the stored data.

    Embeddings work well when related ideas use different wording, such as “money back” and “refund.” Exact names, codes, and dates may be better served by keyword search. Some applications combine both methods.

    Note: A similarity score is a ranking signal, not a factual source or a guarantee of relevance.

    Conclusion

    Embeddings turn content into vectors that can help find related items. Compare compatible vectors, test retrieval on real questions, and verify the original passages before using them in an answer.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.