Xenova HF staff commited on
Commit
00fc3ae
·
verified ·
1 Parent(s): a19b072

Update README.md

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