Using Sentence-Transformers-All-MiniLM-L6-V2 in Transformers.js

Oct 28, 2024 | Educational

Welcome to an exciting journey where we unlock the power of natural language processing with the sentence-transformers-all-MiniLM-L6-v2 model! In this article, we will guide you through the steps to use this model in the JavaScript library, Transformers.js, ensuring you can compute sentence embeddings with ease.

What You Need to Get Started

  • Basic knowledge of JavaScript.
  • Node.js installed on your machine.
  • Some familiarity with sentence embeddings and transformer models.

Step-by-Step Guide to Setup

To kick off, you need to install the Transformers.js library. Follow the steps below:

npm i @xenovatransformers

Creating the Feature Extraction Pipeline

Now that you have installed the library, you can create a feature-extraction pipeline. Consider the following analogy: think of this pipeline as a conveyor belt that takes raw materials (sentences) and processes them to produce high-quality output (embeddings).


import pipeline from '@xenovatransformers';
// Create a feature-extraction pipeline
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');

Computing Sentence Embeddings

Next, let’s take our sentences and run them through the conveyor belt:


// Define your sentences
const sentences = ["This is an example sentence", "Each sentence is converted"];
// Compute sentence embeddings
const output = await extractor(sentences, { pooling: 'mean', normalize: true });
console.log(output);

Here, the output variable contains tensor data that represents the embeddings of the input sentences. It’s a bit like receiving packages containing compressed information about the sentences!

Understanding the Output

The output has tensor dimensions of [2, 384], meaning we have embeddings for two sentences, each with a vector size of 384.


Tensor    dims: [ 2, 384 ],
type: float32,
data: Float32Array(768) [ 0.04592696577310562, 0.07328180968761444, ... ],
size: 768

You can even convert this tensor to a more manageable format:


console.log(output.tolist());

This will give you a nested JavaScript array that is easy to work with!

Troubleshooting Tips

As you embark on your journey with Transformers.js and sentence embeddings, you may encounter a few bumps along the way. Here are some troubleshooting ideas:

  • Issue with Installation: Ensure you have the latest version of Node.js and NPM.
  • Pipeline Not Working: Double-check the pipeline creation code; syntax errors frequently occur here.
  • Output Issues: If the tensor output is not as expected, verify that you are sending well-structured sentences to the extractor.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Temporary Solution with ONNX Weights

It’s important to note that having a separate repository for ONNX weights is a temporary solution. If you want to make your models web-ready, consider converting them to ONNX using Optimum, and structure your repository so that ONNX weights are in a subfolder named onnx.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox