Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Embedding
- A list of numbers a model produces for a piece of text, so that similar texts get similar lists.
- Length Also norm
- The square root of the sum of squared components, ‖a‖ = √(a · a).
- Dot product
- The sum of products of matching components; it equals ‖a‖ ‖b‖ cos θ, so it mixes length with direction.
- Cosine similarity
- The dot product divided by both lengths: the cosine of the angle between two vectors, from −1 to 1.
- Centroid
- The average of a set of vectors, here the embeddings of a team’s past tickets.
- Projection
- The part of a vector that lies along another direction, (v · u / u · u) u; subtracting it leaves the rest.
Why this matters
Search, recommendations, duplicate detection and routing all turn text into vectors and compare them. The comparison decides what users see. A dot product looks like a similarity score, but it also measures length, and length often tracks something else entirely, such as the embedding model’s scale and how consistently a team’s examples point in one direction. An average does not become longer merely because it includes more tickets.
This lesson assumes squares, square roots and sums.
What an embedding vector represents
An embedding is a list of numbers. Read it as an arrow: its direction says what the text is about, and its length is √(a₁² + a₂² + …). A centroid is the component-by-component average of several embeddings, here the past tickets for one team.
The dot product adds products of matching components. Cosine similarity divides that sum by both vector lengths to compare directions; it requires nonzero vectors. These comparisons describe the chosen embedding model, rather than a probability that a routing decision is correct.
01Direction and length
Direction and length
A helpdesk turns each incoming support ticket into an embedding and sends it to the team whose past tickets it most resembles. Since launch, Billing gets most of the queue, including password and delivery questions. The router scores teams with a dot product, and an export inspection shows that Billing’s centroid has the greatest length.
Real embeddings have hundreds of components; the examples here have four so we can inspect the scoring rule before changing the router. Components have no physical units, and every vector must use the same model and component order.
const ticket = [1, 3, 0, 1]; // "I can't sign in after resetting my password"
const billing = [6, 3, 1, 2]; // average embedding of Billing tickets
const auth = [0, 2, 0, 1]; // average of Auth tickets
const dot = (a: number[], b: number[]) => a.reduce((sum, x, i) => sum + x * b[i]!, 0);
const cosine = (a: number[], b: number[]) =>
dot(a, b) / Math.sqrt(dot(a, a) * dot(b, b));
console.log(dot(ticket, billing), dot(ticket, auth)); // raw scores
console.log(cosine(ticket, billing).toFixed(3), cosine(ticket, auth).toFixed(3)); // directions only
There is no built-in vector type; plain arrays with reduce are fine for a few hundred components. Vector databases and ML runtimes do this work in native code.
02What a dot product measures
What a dot product measures
The dot product equals ‖a‖ ‖b‖ cos θ. Two arrows pointing the same way score high, and, with positive alignment held fixed, making either arrow longer increases the score. Perpendicular vectors still score zero; opposite directions score negatively. The exported Billing centroid is longer than the others. That is an observed property of these vectors, not a consequence of how many tickets Billing handles.
| Team | Centroid length | Dot product | Cosine |
|---|---|---|---|
| Billing (6, 3, 1, 2) | 7.071 | 17 | 0.725 |
| Auth (0, 2, 0, 1) | 2.236 | 7 | 0.944 |
| Shipping (1, 0, 4, 0) | 4.123 | 1 | 0.073 |
03Compare directions with cosine
Compare directions with cosine
For the sign-in ticket, q · q = 1 + 9 + 0 + 1 = 11, while Billing · Billing = 50 and Auth · Auth = 5. Its scores are therefore 17 ÷ √550 = 0.725 and 7 ÷ √55 = 0.944. The units cancel: cosine is dimensionless. Dividing by both lengths leaves cos θ: 1 for the same direction, 0 for perpendicular directions, −1 for opposite. The ticket points almost exactly where Auth points, 0.944, while Billing scores 0.725 once its length is divided out.
04Remove a shared direction
Remove a shared direction
Cosine fixes the length bias, but manual review still finds tickets matching on greetings and signatures. Suppose this model’s inspection identifies a shared component: those words pull every embedding a little in the same direction u, here (1, 1, 1, 1). The part of a vector v that lies along u is (v · u / u · u) u; subtracting it leaves the rest. Do it to the ticket and to every centroid, then compare.
For the ticket (2, 1, 2, 1), v · u = 6 and u · u = 4, so subtract 1.5 from every component. Billing wins at 0.850 before the cleanup. After it, the ticket is (0.5, −0.5, 0.5, −0.5) and Shipping wins at 0.762. If nothing is left, the ticket has no mathematical direction left. The router also refuses a negligible residual: ‖cleaned ticket‖ ≤ 10⁻⁹ × ‖original ticket‖. This is an application cutoff, not a definition of zero: a smaller nonzero residual still has a direction, but this router does not trust it. Otherwise route when the best cosine reaches the minimum, or send a lower score to human triage.
05What cosine does not tell you
What cosine does not tell you
06Route the support queue
Route the support queue
Start with lengths and the dot product, then route by direction, then strip the boilerplate first. Each level keeps the earlier working helpers, and the complete program and its reader are visible.
A helpdesk ticket router
Length and dot product, route by direction, then strip the boilerplate.
In the frontend Suggested help articles in Next.js and Svelte Optional
A help centre can suggest articles as the user types: embed the draft question, compare it with each article’s precomputed embedding by cosine, and show nothing when no score passes the threshold.
"use client";
// Suggest help articles as the user types, by cosine similarity of precomputed embeddings.
import { useMemo } from "react";
type Article = { title: string; embedding: number[] };
const dot = (a: number[], b: number[]) => a.reduce((sum, x, i) => sum + x * b[i]!, 0);
const cosine = (a: number[], b: number[]) =>
dot(a, b) / Math.sqrt(dot(a, a) * dot(b, b));
export function Suggestions({
query,
articles
}: {
query: number[] | null;
articles: Article[];
}) {
const ranked = useMemo(
() =>
query === null || dot(query, query) === 0
? []
: articles
.map((article) => ({ article, score: cosine(query, article.embedding) }))
.filter(({ score }) => score >= 0.75) // below this, show nothing rather than a weak guess
.sort((a, b) => b.score - a.score)
.slice(0, 3),
[query, articles]
);
return (
<ul>
{ranked.map(({ article }) => (
<li key={article.title}>{article.title}</li>
))}
</ul>
);
}
Check this idea later
Sources
- Google: measuring similarity from embeddings
- Arora et al. (2017): a simple but tough-to-beat baseline for sentence embeddings (common component removal)
- C++: std::inner_product
Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The helpdesk and its embeddings are a worked model.
Sign in to keep your read marks on every device. Sign in
Was this helpful?