Update Transformers.js code snippets to V3
Browse files
README.md
CHANGED
|
@@ -2726,31 +2726,31 @@ print('similarities:', similarities)
|
|
| 2726 |
|
| 2727 |
### Transformers.js
|
| 2728 |
|
| 2729 |
-
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/@
|
| 2730 |
|
| 2731 |
-
```
|
| 2732 |
-
npm i @
|
| 2733 |
```
|
| 2734 |
|
| 2735 |
You can then use the model to compute embeddings like this:
|
| 2736 |
|
| 2737 |
```javascript
|
| 2738 |
-
import { pipeline, cos_sim } from
|
| 2739 |
|
| 2740 |
// Create a feature extraction pipeline
|
| 2741 |
-
const extractor = await pipeline(
|
| 2742 |
-
|
| 2743 |
});
|
| 2744 |
|
| 2745 |
// Generate sentence embeddings
|
| 2746 |
const docs = [
|
| 2747 |
-
|
| 2748 |
-
|
| 2749 |
-
|
| 2750 |
-
|
| 2751 |
-
|
| 2752 |
]
|
| 2753 |
-
const output = await extractor(docs, { pooling:
|
| 2754 |
|
| 2755 |
// Compute similarity scores
|
| 2756 |
const [source_embeddings, ...document_embeddings ] = output.tolist();
|
|
|
|
| 2726 |
|
| 2727 |
### Transformers.js
|
| 2728 |
|
| 2729 |
+
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:
|
| 2730 |
|
| 2731 |
+
```sh
|
| 2732 |
+
npm i @huggingface/transformers
|
| 2733 |
```
|
| 2734 |
|
| 2735 |
You can then use the model to compute embeddings like this:
|
| 2736 |
|
| 2737 |
```javascript
|
| 2738 |
+
import { pipeline, cos_sim } from "@huggingface/transformers";
|
| 2739 |
|
| 2740 |
// Create a feature extraction pipeline
|
| 2741 |
+
const extractor = await pipeline("feature-extraction", "mixedbread-ai/mxbai-embed-large-v1", {
|
| 2742 |
+
dtype: "fp32", // Options: "fp32", "fp16", "q8"
|
| 2743 |
});
|
| 2744 |
|
| 2745 |
// Generate sentence embeddings
|
| 2746 |
const docs = [
|
| 2747 |
+
"Represent this sentence for searching relevant passages: A man is eating a piece of bread",
|
| 2748 |
+
"A man is eating food.",
|
| 2749 |
+
"A man is eating pasta.",
|
| 2750 |
+
"The girl is carrying a baby.",
|
| 2751 |
+
"A man is riding a horse.",
|
| 2752 |
]
|
| 2753 |
+
const output = await extractor(docs, { pooling: "cls" });
|
| 2754 |
|
| 2755 |
// Compute similarity scores
|
| 2756 |
const [source_embeddings, ...document_embeddings ] = output.tolist();
|