Update Transformers.js sample code
Browse files
README.md
CHANGED
|
@@ -103,27 +103,30 @@ print(similarities)
|
|
| 103 |
Use with `transformers.js`:
|
| 104 |
|
| 105 |
```js
|
| 106 |
-
// npm i @
|
| 107 |
-
import { pipeline,
|
| 108 |
-
|
| 109 |
-
// Create feature extraction pipeline
|
| 110 |
-
const extractor = await pipeline(
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
"what is the capital of China?",
|
| 117 |
"how to implement quick sort in python?",
|
| 118 |
"Beijing",
|
| 119 |
-
"sorting algorithms"
|
| 120 |
-
]
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
// Compute similarity scores
|
| 124 |
-
const [
|
| 125 |
-
|
| 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
|