sentence-similarity Β· text Β· WASM Β· ~34 MB
A stronger retrieval embedder than MiniLM: BAAI's BGE turns each sentence into a 384-number vector tuned for search and similarity. Give it a query and a handful of candidates and it ranks them by cosine similarity β and it knows a trick MiniLM doesn't: a query instruction that sharpens short-query-to-passage search. Everything runs locally; nothing leaves your device.
| Model | Xenova/bge-small-en-v1.5 (ONNX build of BAAI/bge-small-en-v1.5) |
|---|---|
| Task / pipeline | sentence-similarity, via feature-extraction |
| Architecture | BERT-style encoder (12 layers, 33M params) β 384-dimensional sentence embedding |
| Pooling | CLS pooling (the [CLS] token vector β not mean pooling), then L2-normalize |
| Query instruction | Represent this sentence for searching relevant passages: β prepended to the query for asymmetric search |
| Quantization | q8 (8-bit) ONNX |
| Backend | WebAssembly (runs anywhere; no WebGPU needed) |
| Download | ~34 MB, cached after first load |
| License | MIT (BAAI FlagEmbedding) |
| Web APIs | WebAssembly, Web Workers β all Baseline widely available |
Type a query and one candidate per line. BGE embeds all of them and ranks the candidates by cosine similarity to the query. Toggle the query instruction to see BGE's asymmetric-search trick: it's prepended to the query only, never to the candidates.
BGE emits a 384-number vector for the whole sentence by taking the transformer's first [CLS] token (not an average of all tokens like MiniLM), then L2-normalizing it to unit length. Below left: the real vector for a chosen sentence. Below right: the same ranking run with and without the query instruction β proof the prefix actually moves ranks.
First 96 of 384 dimensions (blue = positive, orange = negative, brighter = larger):
| candidate | with | without | Ξ |
|---|
Cosine to the query, ranked, both ways. For short-query β passage search the instruction usually lifts the truly relevant lines and widens the gap to the distractors.
Embeddings are the workhorse behind search, recommendations, dedup, clustering, and RAG. BGE is a retrieval-tuned embedder β on the MTEB benchmark it out-ranks general models like MiniLM for finding the right passage β yet at 34 MB it still runs in a tab. So you can ship meaning-based search with no vector database, no API bill, and no user text leaving the device.
Pair similarity felt as one number, plus a demo of why meaning beats keyword overlap.
Semantic search that beats keywords, and near-duplicate clustering over a pasted corpus.
An STS "how similar, 0β5?" gauge, paraphrase mining across a pile, and odd-one-out.
BGE retrieves a shortlist, then hands it to the MS-MARCO cross-encoder to rerank.
Feature-extraction with CLS pooling is the whole recipe β plus the query instruction for asymmetric search:
import { pipeline } from "@huggingface/transformers";
const extract = await pipeline(
"feature-extraction",
"Xenova/bge-small-en-v1.5",
); // downloads + caches ~34 MB once
// BGE v1.5 is trained with CLS pooling (NOT mean pooling):
const embed = (texts) => extract(texts, { pooling: "cls", normalize: true });
// Asymmetric search: prefix the QUERY only, leave passages bare.
const INSTR = "Represent this sentence for searching relevant passages:";
const q = await embed([`${INSTR} how to keep a battery healthy`]);
const passages = await embed([
"Keep lithium-ion charge between 20 and 80 percent.",
"The best hiking trails in the Lake District.",
]);
// unit vectors β cosine similarity is just the dot product:
const score = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0);
const q0 = q.tolist()[0], ps = passages.tolist();
ps.map((p) => score(q0, p)); // β [0.71, 0.28] β battery line wins
This page asks the worker for the un-normalized CLS vectors so "See inside" can show the real pre-normalization magnitude, then normalizes in JS. Inference runs in a Web Worker so ranking never janks the page. Use the instruction for short-query search; for symmetric similarity (STS, dedup, paraphrase) prefix nothing on either side.