cosine

← All adapters

import { cosine } from '@routecraft/routecraft'

Comparator that groups items by cosine similarity of a numeric vector field. Pass it to group({ comparator: cosine(options) }).

.transform(group({
  comparator: cosine({ field: 'embedding', threshold: 0.85 }),
  from: (body) => body.items,
}))

Options (CosineOptions):

OptionTypeRequiredDescription
fieldstringYesProperty on each item holding the embedding vector (number[])
thresholdnumberNoItems cluster when their cosine similarity is strictly greater than this value (default: 0.82)

Items whose field is not an array never match.