Xenova HF staff commited on
Commit
26dda71
·
verified ·
1 Parent(s): 586ae96

Update Transformers.js sample code

Browse files
Files changed (1) hide show
  1. README.md +19 -16
README.md CHANGED
@@ -103,27 +103,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
 
103
  Use with `transformers.js`:
104
 
105
  ```js
106
+ // npm i @huggingface/transformers
107
+ import { pipeline, matmul } from "@huggingface/transformers";
108
+
109
+ // Create a feature extraction pipeline
110
+ const extractor = await pipeline(
111
+ "feature-extraction",
112
+ "Alibaba-NLP/gte-modernbert-base",
113
+ { dtype: "fp32" }, // Supported options: "fp32", "fp16", "q8", "q4", "q4f16"
114
+ );
115
+
116
+ // Embed queries and documents
117
+ const embeddings = await extractor(
118
+ [
119
  "what is the capital of China?",
120
  "how to implement quick sort in python?",
121
  "Beijing",
122
+ "sorting algorithms",
123
+ ],
124
+ { pooling: "cls", normalize: true },
125
+ );
126
 
127
  // Compute similarity scores
128
+ const similarities = (await matmul(embeddings.slice([0, 1]), embeddings.slice([1, null]).transpose(1, 0))).mul(100);
129
+ console.log(similarities.tolist()); // [[42.89077377319336, 71.30916595458984, 33.66455841064453]]
 
130
  ```
131
 
132
  ## Training Details