README.md
| 1 | --- |
| 2 | base_model: BAAI/bge-base-en-v1.5 |
| 3 | library_name: transformers.js |
| 4 | license: mit |
| 5 | --- |
| 6 | |
| 7 | https://huggingface.co/BAAI/bge-base-en-v1.5 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 use the model to compute embeddings, as follows: |
| 17 | |
| 18 | ```js |
| 19 | import { pipeline } from '@huggingface/transformers'; |
| 20 | |
| 21 | // Create a feature-extraction pipeline |
| 22 | const extractor = await pipeline('feature-extraction', 'Xenova/bge-base-en-v1.5'); |
| 23 | |
| 24 | // Compute sentence embeddings |
| 25 | const texts = ['Hello world.', 'Example sentence.']; |
| 26 | const embeddings = await extractor(texts, { pooling: 'mean', normalize: true }); |
| 27 | console.log(embeddings); |
| 28 | // Tensor { |
| 29 | // dims: [ 2, 768 ], |
| 30 | // type: 'float32', |
| 31 | // data: Float32Array(1536) [ 0.019079938530921936, 0.041718777269124985, ... ], |
| 32 | // size: 1536 |
| 33 | // } |
| 34 | |
| 35 | console.log(embeddings.tolist()); // Convert embeddings to a JavaScript list |
| 36 | // [ |
| 37 | // [ 0.019079938530921936, 0.041718777269124985, 0.037672195583581924, ... ], |
| 38 | // [ 0.020936904475092888, 0.020080938935279846, -0.00787576474249363, ... ] |
| 39 | // ] |
| 40 | ``` |
| 41 | |
| 42 | You can also use the model for retrieval. For example: |
| 43 | ```js |
| 44 | import { pipeline, cos_sim } from '@huggingface/transformers'; |
| 45 | |
| 46 | // Create a feature-extraction pipeline |
| 47 | const extractor = await pipeline('feature-extraction', 'Xenova/bge-base-en-v1.5'); |
| 48 | |
| 49 | // List of documents you want to embed |
| 50 | const texts = [ |
| 51 | 'Hello world.', |
| 52 | 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.', |
| 53 | 'I love pandas so much!', |
| 54 | ]; |
| 55 | |
| 56 | // Compute sentence embeddings |
| 57 | const embeddings = await extractor(texts, { pooling: 'mean', normalize: true }); |
| 58 | |
| 59 | // Prepend recommended query instruction for retrieval. |
| 60 | const query_prefix = 'Represent this sentence for searching relevant passages: ' |
| 61 | const query = query_prefix + 'What is a panda?'; |
| 62 | const query_embeddings = await extractor(query, { pooling: 'mean', normalize: true }); |
| 63 | |
| 64 | // Sort by cosine similarity score |
| 65 | const scores = embeddings.tolist().map( |
| 66 | (embedding, i) => ({ |
| 67 | id: i, |
| 68 | score: cos_sim(query_embeddings.data, embedding), |
| 69 | text: texts[i], |
| 70 | }) |
| 71 | ).sort((a, b) => b.score - a.score); |
| 72 | console.log(scores); |
| 73 | // [ |
| 74 | // { id: 1, score: 0.7787772374597298, text: 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.' }, |
| 75 | // { id: 2, score: 0.7071589521880506, text: 'I love pandas so much!' }, |
| 76 | // { id: 0, score: 0.4252782730390429, text: 'Hello world.' } |
| 77 | // ] |
| 78 | ``` |
| 79 | |
| 80 | --- |
| 81 | |
| 82 | 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`). |