Xenova HF staff commited on
Commit
21e49f4
1 Parent(s): e2d28eb

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +72 -0
README.md CHANGED
@@ -4,4 +4,76 @@ library_name: transformers.js
4
 
5
  https://huggingface.co/BAAI/bge-base-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-base-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-base-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, 768 ],
28
+ // type: 'float32',
29
+ // data: Float32Array(1536) [ 0.019079938530921936, 0.041718777269124985, ... ],
30
+ // size: 1536
31
+ // }
32
+
33
+ console.log(embeddings.tolist()); // Convert embeddings to a JavaScript list
34
+ // [
35
+ // [ 0.019079938530921936, 0.041718777269124985, 0.037672195583581924, ... ],
36
+ // [ 0.020936904475092888, 0.020080938935279846, -0.00787576474249363, ... ]
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.7787772374597298, 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.7071589521880506, text: 'I love pandas so much!' },
74
+ // { id: 0, score: 0.4252782730390429, 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`).