Views
No views yet
npm i @huggingface/transformers1import { pipeline, cos_sim } from '@huggingface/transformers';
2
3// Create a feature extraction pipeline
4const extractor = await pipeline('feature-extraction', 'Xenova/jina-embeddings-v2-base-zh', {
5 dtype: "fp32" // Options: "fp32", "fp16", "q8", "q4"
6});
7
8// Compute sentence embeddings
9const texts = ['How is the weather today?', '今天天气怎么样?'];
10const output = await extractor(texts, { pooling: 'mean', normalize: true });
11// Tensor {
12// dims: [2, 768],
13// type: 'float32',
14// data: Float32Array(1536)[...],
15// size: 1536
16// }
17
18// Compute cosine similarity between the two embeddings
19const score = cos_sim(output[0].data, output[1].data);
20console.log(score);
21// 0.7860610759096025onnx).