Update README.md
Browse files
README.md
CHANGED
@@ -4,4 +4,76 @@ library_name: transformers.js
|
|
4 |
|
5 |
https://huggingface.co/BAAI/bge-large-en-v1.5 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-large-en-v1.5 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-large-en-v1.5');
|
21 |
+
|
22 |
+
// Compute sentence embeddings
|
23 |
+
const texts = [ 'Hello world.', 'Example sentence.'];
|
24 |
+
const embeddings = await extractor(texts, { pooling: 'mean', normalize: true });
|
25 |
+
console.log(embeddings);
|
26 |
+
// Tensor {
|
27 |
+
// dims: [ 2, 1024 ],
|
28 |
+
// type: 'float32',
|
29 |
+
// data: Float32Array(2048) [ 0.03169844672083855, 0.011085662990808487, ... ],
|
30 |
+
// size: 2048
|
31 |
+
// }
|
32 |
+
|
33 |
+
console.log(embeddings.tolist()); // Convert embeddings to a JavaScript list
|
34 |
+
// [
|
35 |
+
// [ 0.03169844672083855, 0.011085662990808487, 0.030054178088903427, ... ],
|
36 |
+
// [ 0.009418969973921776, -0.024539148434996605, 0.036459196358919144, ... ]
|
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-large-en-v1.5');
|
46 |
+
|
47 |
+
// List of documents you want to embed
|
48 |
+
const texts = [
|
49 |
+
'Hello world.',
|
50 |
+
'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.',
|
51 |
+
'I love pandas so much!',
|
52 |
+
];
|
53 |
+
|
54 |
+
// Compute sentence embeddings
|
55 |
+
const embeddings = await extractor(texts, { pooling: 'mean', normalize: true });
|
56 |
+
|
57 |
+
// Prepend recommended query instruction for retrieval.
|
58 |
+
const query_prefix = 'Represent this sentence for searching relevant passages: '
|
59 |
+
const query = query_prefix + 'What is a panda?';
|
60 |
+
const query_embeddings = await extractor(query, { pooling: 'mean', 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: 1, score: 0.7671812872502833, text: 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.' },
|
73 |
+
// { id: 2, score: 0.7219157959783322, text: 'I love pandas so much!' },
|
74 |
+
// { id: 0, score: 0.5109676329796601, text: 'Hello world.' }
|
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`).
|