Update README.md
Browse files
README.md
CHANGED
@@ -6,4 +6,61 @@ pipeline_tag: feature-extraction
|
|
6 |
|
7 |
https://huggingface.co/Qwen/Qwen3-Embedding-0.6B with ONNX weights to be compatible with Transformers.js.
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
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`).
|
|
|
6 |
|
7 |
https://huggingface.co/Qwen/Qwen3-Embedding-0.6B with ONNX weights to be compatible with Transformers.js.
|
8 |
|
9 |
+
## Usage (Transformers.js)
|
10 |
+
|
11 |
+
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:
|
12 |
+
```bash
|
13 |
+
npm i @huggingface/transformers
|
14 |
+
```
|
15 |
+
|
16 |
+
You can then compute embeddings as follows:
|
17 |
+
```js
|
18 |
+
import { pipeline, matmul } from "@huggingface/transformers";
|
19 |
+
|
20 |
+
// Create a feature extraction pipeline
|
21 |
+
const extractor = await pipeline(
|
22 |
+
"feature-extraction",
|
23 |
+
"onnx-community/Qwen3-Embedding-0.6B-ONNX",
|
24 |
+
{
|
25 |
+
dtype: "fp32", // Options: "fp32", "fp16", "q8"
|
26 |
+
// device: "webgpu",
|
27 |
+
},
|
28 |
+
);
|
29 |
+
|
30 |
+
function get_detailed_instruct(task_description, query) {
|
31 |
+
return `Instruct: ${task_description}\nQuery:${query}`;
|
32 |
+
}
|
33 |
+
|
34 |
+
// Each query must come with a one-sentence instruction that describes the task
|
35 |
+
const task = "Given a web search query, retrieve relevant passages that answer the query";
|
36 |
+
const queries = [
|
37 |
+
get_detailed_instruct(task, "What is the capital of China?"),
|
38 |
+
get_detailed_instruct(task, "Explain gravity"),
|
39 |
+
];
|
40 |
+
|
41 |
+
// No need to add instruction for retrieval documents
|
42 |
+
const documents = [
|
43 |
+
"The capital of China is Beijing.",
|
44 |
+
"Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
|
45 |
+
];
|
46 |
+
const input_texts = [...queries, ...documents];
|
47 |
+
|
48 |
+
// Extract embeddings for queries and documents
|
49 |
+
const output = await extractor(input_texts, {
|
50 |
+
pooling: "last_token",
|
51 |
+
normalize: true,
|
52 |
+
});
|
53 |
+
const scores = await matmul(
|
54 |
+
output.slice([0, queries.length]), // Query embeddings
|
55 |
+
output.slice([queries.length, null]).transpose(1, 0), // Document embeddings
|
56 |
+
);
|
57 |
+
console.log(scores.tolist());
|
58 |
+
// [
|
59 |
+
// [ 0.7645590305328369, 0.14142560958862305 ],
|
60 |
+
// [ 0.13549776375293732, 0.599955141544342 ]
|
61 |
+
// ]
|
62 |
+
```
|
63 |
+
|
64 |
+
---
|
65 |
+
|
66 |
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`).
|