We use cookies to improve your experience and measure traffic. Cookie policy

    Scalor
    CONVINCE US →
    Glossary/Retrieval & RAG

    Cosine Similarity

    similaridade do cosseno

    A mathematical metric that measures the degree of similarity between two items, such as documents or products, based on the orientation of their numerical meaning rather than their length.

    What it is

    Cosine Similarity is a statistical technique used to determine how similar two objects are in a multidimensional space. In the context of modern Artificial Intelligence and natural language processing, it is the tool that allows a computer to state that the sentence "How can I export invoices to Excel?" is semantically close to "Financial data extraction guide", even if the exact words are different.

    For an SME, this metric is the invisible engine behind recommendation systems and intelligent search engines. Instead of searching for exact keywords (like the 'Ctrl+F' command), cosine similarity looks at the "context" and "intent" encoded into numerical vectors (embeddings).

    How it works

    Imagine that each concept or document is transformed into an arrow (vector) pointing in a specific direction on a graph.

    1. Direction is Meaning: If two arrows point in very close directions, the angle between them is small. This means the topics are very similar.
    2. The Calculation: The metric calculates the cosine of the angle between these two vectors.
      • If the value is 1, the angle is zero: the items are identical in meaning.
      • If the value is 0, the items are orthogonal: they have no relationship to each other.
      • If the value is -1, they are opposites (although in text AI, values are rarely negative).

    The great advantage of this approach over other metrics (such as Euclidean Distance) is that it ignores magnitude. If we have a 50-page technical manual about "Heat Pumps" and a small 1-page brochure on the same topic, cosine similarity will recognize that both speak about the same subject, while other metrics might think they are different just because one is much longer than the other.

    When to use

    Cosine Similarity is the standard choice in several practical AI consulting scenarios:

    • RAG (Retrieval-Augmented Generation) Systems: When a Chatbot needs to read your company's PDF manuals to answer a customer, it uses this metric to find the exact paragraph containing the answer.
    • Inventory Classification: To group products from an e-commerce catalog that do not have standardized descriptions, facilitating user navigation.
    • Duplicate Detection: Identifying if a support ticket that just came in is identical to one that was resolved yesterday, allowing for response automation.
    • B2B Recommendation Systems: Suggesting replacement parts based on order history or technical compatibility described in free text.

    Common errors

    • Ignoring Embedding quality: Cosine similarity is just a mathematical calculation. If the AI model that transformed text into numbers (the embedding model) is weak or does not understand technical language, the result will be poor, regardless of the mathematical precision.
    • Confusing similarity with truth: Just because two documents are semantically close does not mean one is the correct answer to the other. It is merely a measure of thematic kinship.
    • Not considering negation context: Sometimes, two vectors can be close even if one says "you can do X" and the other "you cannot do X", if the base model is not robust enough to capture negation.
    • Wasting resources on small databases: If your company only has 50 documents, a simple keyword search may be sufficient. Cosine similarity shines when we have thousands of data points to cross-reference.

    Practical example for an SME

    Imagine an electrical equipment distribution company with a catalog of 20,000 references. A customer writes in the portal chat: "I need something to protect circuits against voltage spikes in industrial environments".

    A traditional database system would look for the exact words. If the product in the catalog is registered as "Type 2 Surge Protector", the common search would fail.

    With Cosine Similarity:

    1. The customer's phrase is converted into a vector.
    2. The system compares that vector with the vectors of all products in the catalog.
    3. The calculation identifies that "protect against voltage spikes" and "Surge Protector" point to the same conceptual direction.
    4. The system returns the correct product at the top of the list, converting customer frustration into an immediate sale.

    Frequently Asked Questions

    Q: What is the difference between Cosine Similarity and keyword search? A: Keyword search requires an exact letter match. Cosine Similarity looks for conceptual proximity, allowing results to be found even when using synonyms or different technical terms.

    Q: Do I need a supercomputer to calculate this? A: No. Although calculating thousands of vectors sounds heavy, there are technologies called Vector Databases (like Pinecone or Weaviate) that perform millions of these comparisons in milliseconds on an ordinary computer.

    Q: Does this metric work for images? A: Yes, provided the images have been converted into vectors by a visual AI model. This is how "search for similar images" features work.

    Q: Can the similarity value be used as a confidence score? A: Yes, many companies set a threshold (e.g., 0.85). If the similarity is lower than that value, the system assumes it found no relevant information and prefers to say "I don't know" rather than inventing an answer.

    Practical examples

    • 01Comparing a customer's question with an FAQ database to find the most likely answer.
    • 02Grouping invoices from different suppliers that use distinct descriptions for the same service.
    • 03Recommending similar blog articles based on what the user has just read.
    • 04Identifying resumes that most closely match a job description at a consultancy firm.

    Want to use Cosine Similarity in your company?

    30 minutes, free, no commitment. We map where it fits.

    Free AI diagnosis