Update README.md
Browse files
README.md
CHANGED
@@ -8,6 +8,79 @@ tags:
|
|
8 |
- sentence-transformers
|
9 |
- feature-extraction
|
10 |
- sentence-similarity
|
11 |
-
- mteb
|
12 |
library_name: transformers.js
|
13 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
- sentence-transformers
|
9 |
- feature-extraction
|
10 |
- sentence-similarity
|
|
|
11 |
library_name: transformers.js
|
12 |
+
---
|
13 |
+
|
14 |
+
|
15 |
+
## Usage (Transformers.js)
|
16 |
+
|
17 |
+
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/@huggingface/transformers) using:
|
18 |
+
```bash
|
19 |
+
npm i @huggingface/transformers
|
20 |
+
```
|
21 |
+
|
22 |
+
You can then use the model to compute embeddings, as follows:
|
23 |
+
|
24 |
+
```js
|
25 |
+
import { pipeline } from '@huggingface/transformers';
|
26 |
+
|
27 |
+
// Create a feature-extraction pipeline
|
28 |
+
const extractor = await pipeline('feature-extraction', 'onnx-community/bge-base-en-v1.5-ONNX');
|
29 |
+
|
30 |
+
// Compute sentence embeddings
|
31 |
+
const texts = ['Hello world.', 'Example sentence.'];
|
32 |
+
const embeddings = await extractor(texts, { pooling: 'mean', normalize: true });
|
33 |
+
console.log(embeddings);
|
34 |
+
// Tensor {
|
35 |
+
// dims: [ 2, 768 ],
|
36 |
+
// type: 'float32',
|
37 |
+
// data: Float32Array(1536) [ 0.019079938530921936, 0.041718777269124985, ... ],
|
38 |
+
// size: 1536
|
39 |
+
// }
|
40 |
+
|
41 |
+
console.log(embeddings.tolist()); // Convert embeddings to a JavaScript list
|
42 |
+
// [
|
43 |
+
// [ 0.019079938530921936, 0.041718777269124985, 0.037672195583581924, ... ],
|
44 |
+
// [ 0.020936904475092888, 0.020080938935279846, -0.00787576474249363, ... ]
|
45 |
+
// ]
|
46 |
+
```
|
47 |
+
|
48 |
+
You can also use the model for retrieval. For example:
|
49 |
+
```js
|
50 |
+
import { pipeline, cos_sim } from '@huggingface/transformers';
|
51 |
+
|
52 |
+
// Create a feature-extraction pipeline
|
53 |
+
const extractor = await pipeline('feature-extraction', 'onnx-community/bge-base-en-v1.5-ONNX');
|
54 |
+
|
55 |
+
// List of documents you want to embed
|
56 |
+
const texts = [
|
57 |
+
'Hello world.',
|
58 |
+
'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.',
|
59 |
+
'I love pandas so much!',
|
60 |
+
];
|
61 |
+
|
62 |
+
// Compute sentence embeddings
|
63 |
+
const embeddings = await extractor(texts, { pooling: 'mean', normalize: true });
|
64 |
+
|
65 |
+
// Prepend recommended query instruction for retrieval.
|
66 |
+
const query_prefix = 'Represent this sentence for searching relevant passages: '
|
67 |
+
const query = query_prefix + 'What is a panda?';
|
68 |
+
const query_embeddings = await extractor(query, { pooling: 'mean', normalize: true });
|
69 |
+
|
70 |
+
// Sort by cosine similarity score
|
71 |
+
const scores = embeddings.tolist().map(
|
72 |
+
(embedding, i) => ({
|
73 |
+
id: i,
|
74 |
+
score: cos_sim(query_embeddings.data, embedding),
|
75 |
+
text: texts[i],
|
76 |
+
})
|
77 |
+
).sort((a, b) => b.score - a.score);
|
78 |
+
console.log(scores);
|
79 |
+
// [
|
80 |
+
// { id: 1, score: 0.7787772374597298, text: 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.' },
|
81 |
+
// { id: 2, score: 0.7071589521880506, text: 'I love pandas so much!' },
|
82 |
+
// { id: 0, score: 0.4252782730390429, text: 'Hello world.' }
|
83 |
+
// ]
|
84 |
+
```
|
85 |
+
|
86 |
+
---
|