Blog

How ChatGPT Ranks Results

Understanding How AI Delivers Relevant Product Suggestions

Dec 28, 2024
5 min read
ChatGPT
AI Learning
Joe Delgado
Co-Founder

How ChatGPT Ranks Results: A Technical Look at Tokens, Embeddings, and Similarity

When you search for something like "carpenters in Los Angeles," you may wonder how tools like ChatGPT understand your request and deliver accurate results. At its core, this process relies on key concepts like tokens, embeddings, and a mathematical technique called Embedding Cosine Similarity (ECS). Let’s break this down in an approachable way.


Tokens: Breaking Down the Input

When you type a query, ChatGPT doesn’t see it as a single chunk of text. Instead, it breaks the input into smaller units called tokens.

  • A token can be a word, part of a word, or even punctuation.
  • For example, “carpenters in Los Angeles” might be tokenized as:
    [“carp”, “ent”, “ers”, “in”, “Los”, “Angeles”]

Tokenization ensures that the model processes even complex or unfamiliar terms by splitting them into manageable pieces.


Embeddings: Giving Meaning to Tokens

Once the input is tokenized, the next step is to convert these tokens into embeddings.

  • An embedding is a numerical representation of a token’s meaning in a multi-dimensional space.

  • Think of it like placing words on a giant map where similar words are close to each other. For instance:

  • “Carpenters” might be near “craftsmen” or “woodworkers.”

  • “Los Angeles” might cluster with other geographic terms like “California” or “San Francisco.”

These embeddings are the foundation for understanding relationships between words.


ECS: Measuring Similarity Between Terms

To determine how well a result matches your query, ChatGPT calculates Embedding Cosine Similarity (ECS) between the embeddings of your query and potential responses.

  • Cosine similarity measures the angle between two vectors (embeddings) in the multi-dimensional space.
  • A similarity score of 1.0 means two terms are identical in meaning, while a score closer to 0 indicates little to no similarity.

For example:

Term 1Term 2ECS Score
"carpenters""woodworkers"0.95
"Los Angeles""San Diego"0.80
"carpenters""bakers"0.10

Using ECS, the model can determine that a response containing “woodworkers in Los Angeles” is a close match to your query for “carpenters in Los Angeles.”


Ranking Results

With tokens, embeddings, and ECS scores in hand, ChatGPT ranks potential responses by considering:

  • Relevance: How closely the ECS score matches the query.
  • Context: If prior queries suggest additional preferences (e.g., eco-friendly materials).
  • Diversity: Offering varied responses, such as carpenters specializing in furniture, general repairs, or sustainable practices.

For example, here’s how results for "carpenters in Los Angeles" might be ranked:

  1. Smith’s Custom Carpentry (ECS: 0.96)
  • Specializes in bespoke furniture.
  • Highly relevant based on similarity to “carpenters.”
  1. GreenBuild Carpentry (ECS: 0.89)
  • Focuses on sustainable materials.
  • Matches a broader context if “eco-friendly” was discussed.
  1. Affordable Handyman Services (ECS: 0.75)
  • Offers general repairs, slightly less specific but still relevant.

Tokens, Embeddings, and the Big Picture

The key strength of ChatGPT lies in its ability to:

  • Break down complex queries into meaningful tokens.
  • Represent words as embeddings that capture their nuanced relationships.
  • Use ECS to measure how well a potential result matches your input.

This approach allows ChatGPT to not only rank results but also adapt to the context of your queries.


Self-Attention: How ChatGPT Understands Relationships Between Words

A key technology powering ChatGPT is self-attention, which allows the model to understand the relationships between words in your query. Unlike simpler methods that look at each word individually, self-attention helps ChatGPT analyze how every word in a sentence interacts with the others.

What Is Self-Attention?

Self-attention is a mechanism that assigns weights to different words in a sentence based on their importance to each other. These weights help the model focus on the parts of the input that are most relevant for understanding meaning.

For example, in the query:

"Find carpenters in Los Angeles who specialize in custom furniture."

  • The model learns that:
    • "carpenters" is closely linked to "custom furniture."
    • "Los Angeles" provides geographical context for the search.

By calculating these relationships, the model understands that you’re looking for carpenters in Los Angeles who specifically deal with custom furniture—not just any carpenter or location.


How Does Self-Attention Work?

Here’s a simplified step-by-step explanation:

  1. Input Tokens: The query is tokenized into smaller units:
    [“Find”, “carpenters”, “in”, “Los”, “Angeles”, “who”, “specialize”, “in”, “custom”, “furniture”]

  2. Weight Calculation: For each token, the model calculates how much it should "pay attention" to every other token. For instance:

  • "carpenters" heavily weights "custom" and "furniture."
  • "Los" and "Angeles" are closely linked.
  1. Relevance Scores: Using these weights, the model builds an understanding of how tokens relate to one another in the context of the query.

  2. Contextual Understanding: Self-attention enables the model to represent each token not just as an individual unit, but as part of a larger, meaningful context.


Why Is Self-Attention Important?

Self-attention is what makes transformer models like ChatGPT so powerful. Unlike older models that processed sentences sequentially, self-attention enables ChatGPT to:

  • Handle Long Sentences: By capturing relationships between words no matter how far apart they are, the model can better understand complex queries.
  • Adapt to Context: Self-attention helps the model interpret context dynamically. For instance, in “custom furniture,” the word “custom” alters the meaning of “furniture.”
  • Deliver Relevant Results: By weighting important terms more heavily, the model ensures that your query produces the most relevant and accurate responses.

Self-Attention in Action

Let’s go back to our example query:

"Find carpenters in Los Angeles who specialize in custom furniture."

Through self-attention, the model understands that:

  • "carpenters" is the key term defining the service.
  • "Los Angeles" refines the search geographically.
  • "specialize in custom furniture" further narrows down the type of carpenter.

This understanding is what allows ChatGPT to generate results like:

  1. Smith’s Custom Carpentry
  • A carpenter specializing in custom furniture.
  • Serves Los Angeles.
  1. GreenBuild Carpentry
  • Focuses on sustainable materials and custom projects.

Self-attention is the backbone of how ChatGPT processes language. By considering how every part of your query relates to every other part, the model achieves a nuanced understanding that delivers precise and context-aware results.

Conclusion

Next time you search for something like "carpenters in Los Angeles," remember that behind the scenes, a lot of sophisticated math and linguistics are at work. From tokenization to embeddings and ECS, tools like ChatGPT combine these technical aspects to deliver fast, relevant results—all in a way that feels natural and intuitive.
When you go to chatrank.ai and enter your brand, you will see all of the relevant words with high ECS to your prompt, helping you understand what types of words should be added to your content for ChatGPT to rank you higher.