Views
No views yet
npm i @huggingface/transformers1import { pipeline } from '@huggingface/transformers';
2
3// Create a feature-extraction pipeline
4const extractor = await pipeline('feature-extraction', 'Xenova/gte-small');
5
6// Compute sentence embeddings
7const sentences = ['That is a happy person', 'That is a very happy person'];
8const output = await extractor(sentences, { pooling: 'mean', normalize: true });
9console.log(output);
10// Tensor {
11// dims: [ 2, 384 ],
12// type: 'float32',
13// data: Float32Array(768) [ -0.053555335849523544, 0.00843878649175167, ... ],
14// size: 768
15// }
16
17// Compute cosine similarity
18import { cos_sim } from '@huggingface/transformers';
19console.log(cos_sim(output[0].data, output[1].data))
20// 0.9798319649182318.tolist():1console.log(output.tolist());
2// [
3// [ -0.053555335849523544, 0.00843878649175167, 0.06234041228890419, ... ],
4// [ -0.049980051815509796, 0.03879701718688011, 0.07510733604431152, ... ]
5// ]{ dtype: 'fp32' } in the pipeline function:1const extractor = await pipeline('feature-extraction', 'Xenova/gte-small', {
2 dtype: 'fp32' // Options: "fp32", "fp16", "q8", "q4"
3});onnx).