| 1 | --- |
| 2 | library_name: "transformers.js" |
| 3 | --- |
| 4 | |
| 5 | https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 with ONNX weights to be compatible with Transformers.js. |
| 6 | |
| 7 | ## Usage (Transformers.js) |
| 8 | |
| 9 | If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using: |
| 10 | |
| 11 | ```bash |
| 12 | npm i @xenova/transformers |
| 13 | ``` |
| 14 | |
| 15 | You can then use the model to compute embeddings like this: |
| 16 | |
| 17 | ```js |
| 18 | import { pipeline } from "@xenova/transformers"; |
| 19 | |
| 20 | // Create a feature-extraction pipeline |
| 21 | const extractor = await pipeline( |
| 22 | "feature-extraction", |
| 23 | "Xenova/all-MiniLM-L6-v2", |
| 24 | ); |
| 25 | |
| 26 | // Compute sentence embeddings |
| 27 | const sentences = ["This is an example sentence", "Each sentence is converted"]; |
| 28 | const output = await extractor(sentences, { pooling: "mean", normalize: true }); |
| 29 | console.log(output); |
| 30 | // Tensor { |
| 31 | // dims: [ 2, 384 ], |
| 32 | // type: 'float32', |
| 33 | // data: Float32Array(768) [ 0.04592696577310562, 0.07328180968761444, ... ], |
| 34 | // size: 768 |
| 35 | // } |
| 36 | ``` |
| 37 | |
| 38 | You can convert this Tensor to a nested JavaScript array using `.tolist()`: |
| 39 | |
| 40 | ```js |
| 41 | console.log(output.tolist()); |
| 42 | // [ |
| 43 | // [ 0.04592696577310562, 0.07328180968761444, 0.05400655046105385, ... ], |
| 44 | // [ 0.08188057690858841, 0.10760223120450974, -0.013241755776107311, ... ] |
| 45 | // ] |
| 46 | ``` |
| 47 | |
| 48 | Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [馃 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`). |