Xenova HF staff commited on
Commit
cfc4d51
1 Parent(s): 50d3402

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md CHANGED
@@ -4,4 +4,77 @@ library_name: transformers.js
4
 
5
  https://huggingface.co/BAAI/bge-small-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-small-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-small-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, 384 ],
28
+ // type: 'float32',
29
+ // data: Float32Array(768) [ -0.04314826801419258, -0.029488801956176758, ... ],
30
+ // size: 768
31
+ // }
32
+
33
+ console.log(embeddings.tolist()); // Convert embeddings to a JavaScript list
34
+ // [
35
+ // [ -0.04314826801419258, -0.029488801956176758, 0.027080481871962547, ... ],
36
+ // [ -0.03605496883392334, 0.01643390767276287, 0.008982205763459206, ... ]
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-small-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.7995888037433755, 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.6911046766159414, text: 'I love pandas so much!' },
74
+ // { id: 0, score: 0.39066192695524765, text: 'Hello world.' }
75
+ // ]
76
+
77
+ ```
78
+
79
+
80
  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`).