Update README.md
Browse files
README.md
CHANGED
@@ -4,4 +4,76 @@ library_name: transformers.js
|
|
4 |
|
5 |
https://huggingface.co/BAAI/bge-m3 with ONNX weights to be compatible with Transformers.js.
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
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`).
|
|
|
4 |
|
5 |
https://huggingface.co/BAAI/bge-m3 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 |
+
```bash
|
11 |
+
npm i @xenova/transformers
|
12 |
+
```
|
13 |
+
|
14 |
+
You can then use the model to compute embeddings, as follows:
|
15 |
+
|
16 |
+
```js
|
17 |
+
import { pipeline } from '@xenova/transformers';
|
18 |
+
|
19 |
+
// Create a feature-extraction pipeline
|
20 |
+
const extractor = await pipeline('feature-extraction', 'Xenova/bge-m3');
|
21 |
+
|
22 |
+
// Compute sentence embeddings
|
23 |
+
const texts = ["What is BGE M3?", "Defination of BM25"]
|
24 |
+
const embeddings = await extractor(texts, { pooling: 'cls', normalize: true });
|
25 |
+
console.log(embeddings);
|
26 |
+
// Tensor {
|
27 |
+
// dims: [ 2, 1024 ],
|
28 |
+
// type: 'float32',
|
29 |
+
// data: Float32Array(2048) [ -0.0340719036757946, -0.04478546231985092, ... ],
|
30 |
+
// size: 2048
|
31 |
+
// }
|
32 |
+
|
33 |
+
console.log(embeddings.tolist()); // Convert embeddings to a JavaScript list
|
34 |
+
// [
|
35 |
+
// [ -0.0340719036757946, -0.04478546231985092, -0.004497686866670847, ... ],
|
36 |
+
// [ -0.015383965335786343, -0.041989751160144806, -0.025820579379796982, ... ]
|
37 |
+
// ]
|
38 |
+
```
|
39 |
+
|
40 |
+
You can also use the model for retrieval. For example:
|
41 |
+
```js
|
42 |
+
import { pipeline, cos_sim } from '@xenova/transformers';
|
43 |
+
|
44 |
+
// Create a feature-extraction pipeline
|
45 |
+
const extractor = await pipeline('feature-extraction', 'Xenova/bge-m3');
|
46 |
+
|
47 |
+
// Define query to use for retrieval
|
48 |
+
const query = 'What is BGE M3?';
|
49 |
+
|
50 |
+
// List of documents you want to embed
|
51 |
+
const texts = [
|
52 |
+
'BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.',
|
53 |
+
'BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document',
|
54 |
+
];
|
55 |
+
|
56 |
+
// Compute sentence embeddings
|
57 |
+
const embeddings = await extractor(texts, { pooling: 'cls', normalize: true });
|
58 |
+
|
59 |
+
// Compute query embeddings
|
60 |
+
const query_embeddings = await extractor(query, { pooling: 'cls', normalize: true });
|
61 |
+
|
62 |
+
// Sort by cosine similarity score
|
63 |
+
const scores = embeddings.tolist().map(
|
64 |
+
(embedding, i) => ({
|
65 |
+
id: i,
|
66 |
+
score: cos_sim(query_embeddings.data, embedding),
|
67 |
+
text: texts[i],
|
68 |
+
})
|
69 |
+
).sort((a, b) => b.score - a.score);
|
70 |
+
console.log(scores);
|
71 |
+
// [
|
72 |
+
// { id: 0, score: 0.62532672968664, text: 'BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.' },
|
73 |
+
// { id: 1, score: 0.33111060648806, text: 'BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document' },
|
74 |
+
// ]
|
75 |
+
```
|
76 |
+
|
77 |
+
---
|
78 |
+
|
79 |
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`).
|