Cosine Similarity Meter
Shows a cosine similarity (0–1) as a large numeric value with a human-readable bucket label and a proportional arc gauge.
Cosine Similarity Meter
The Cosine Similarity Meter displays a cosine similarity score (0–1) as a large numeric value with a human-readable bucket label (e.g. "Very similar", "Unrelated") using configurable thresholds, plus a proportional half-ring arc gauge.
When to use it: when you compute the cosine similarity of two embeddings yourself (from useEmbed / useEmbedImage) and want to present it — semantic search relevance, duplicate detection, or cross-modal matching.
Preview
Installation
pnpm dlx shadcn@latest add @localmode/ui/results/cosine-similarity-meternpx shadcn@latest add @localmode/ui/results/cosine-similarity-meteryarn dlx shadcn@latest add @localmode/ui/results/cosine-similarity-meterbunx --bun shadcn@latest add @localmode/ui/results/cosine-similarity-meterDependencies
-
Data source: renders a single
similarity(0–1) you pass — works with any backend. Recommended producer: compute cosine yourself from embeddings (e.g. viauseEmbed/useEmbedImagefrom@localmode/react, optional). See Bring your own data. -
clsx+tailwind-merge— via the sharedcn()util
Files installed
cosine-similarity-meter.tsx— the componentlib/utils.ts— thecn()helper (if not already present)
Props
CosineSimilarityMeter
Prop
Type
Examples
From two embeddings
import { CosineSimilarityMeter } from '@/components/cosine-similarity-meter';
function cosine(a: Float32Array, b: Float32Array) {
let dot = 0, na = 0, nb = 0;
for (let i = 0; i < a.length; i++) { dot += a[i] * b[i]; na += a[i] ** 2; nb += b[i] ** 2; }
return dot / (Math.sqrt(na) * Math.sqrt(nb));
}
export function Example({ queryVec, docVec }) {
return <CosineSimilarityMeter similarity={cosine(queryVec, docVec)} caption="query ↔ document" />;
}Custom buckets
<CosineSimilarityMeter
similarity={0.74}
buckets={[
{ min: 0.7, label: 'Match', color: 'var(--color-emerald-500)' },
{ min: 0, label: 'No match', color: 'var(--color-muted-foreground)' },
]}
/>Customization
The arc gauge is a two-path SVG semicircle; the active arc's color comes from the resolved bucket and its length from the score. Bucket colors are wired to CSS variables so the meter themes via your tokens. The exported resolveBucket() helper lets you reuse the exact bucketing logic elsewhere.