thenlper Xenova HF staff commited on
Commit
75cd141
·
verified ·
1 Parent(s): 586ae96

Update Transformers.js sample code (#6)

Browse files

- Update Transformers.js sample code (26dda71fa433170ff379aaa64b6ad0500a81a415)
- Update README.md (3d26940620003c46d06bfa7663552f42e00b65bc)


Co-authored-by: Joshua <Xenova@users.noreply.huggingface.co>

Files changed (1) hide show
  1. README.md +20 -16
README.md CHANGED
@@ -10,6 +10,7 @@ tags:
10
  - sentence-transformers
11
  - mteb
12
  - embedding
 
13
  ---
14
 
15
  # gte-modernbert-base
@@ -103,27 +104,30 @@ print(similarities)
103
  Use with `transformers.js`:
104
 
105
  ```js
106
- // npm i @xenova/transformers
107
- import { pipeline, dot } from '@xenova/transformers';
108
-
109
- // Create feature extraction pipeline
110
- const extractor = await pipeline('feature-extraction', 'Alibaba-NLP/gte-modernbert-base', {
111
- quantized: false, // Comment out this line to use the quantized version
112
- });
113
-
114
- // Generate sentence embeddings
115
- const sentences = [
 
 
 
116
  "what is the capital of China?",
117
  "how to implement quick sort in python?",
118
  "Beijing",
119
- "sorting algorithms"
120
- ]
121
- const output = await extractor(sentences, { normalize: true, pooling: 'cls' });
 
122
 
123
  // Compute similarity scores
124
- const [source_embeddings, ...document_embeddings ] = output.tolist();
125
- const similarities = document_embeddings.map(x => 100 * dot(source_embeddings, x));
126
- console.log(similarities);
127
  ```
128
 
129
  ## Training Details
 
10
  - sentence-transformers
11
  - mteb
12
  - embedding
13
+ - transformers.js
14
  ---
15
 
16
  # gte-modernbert-base
 
104
  Use with `transformers.js`:
105
 
106
  ```js
107
+ // npm i @huggingface/transformers
108
+ import { pipeline, matmul } from "@huggingface/transformers";
109
+
110
+ // Create a feature extraction pipeline
111
+ const extractor = await pipeline(
112
+ "feature-extraction",
113
+ "Alibaba-NLP/gte-modernbert-base",
114
+ { dtype: "fp32" }, // Supported options: "fp32", "fp16", "q8", "q4", "q4f16"
115
+ );
116
+
117
+ // Embed queries and documents
118
+ const embeddings = await extractor(
119
+ [
120
  "what is the capital of China?",
121
  "how to implement quick sort in python?",
122
  "Beijing",
123
+ "sorting algorithms",
124
+ ],
125
+ { pooling: "cls", normalize: true },
126
+ );
127
 
128
  // Compute similarity scores
129
+ const similarities = (await matmul(embeddings.slice([0, 1]), embeddings.slice([1, null]).transpose(1, 0))).mul(100);
130
+ console.log(similarities.tolist()); // [[42.89077377319336, 71.30916595458984, 33.66455841064453]]
 
131
  ```
132
 
133
  ## Training Details