0

I need to create embeddings in NextJS 15, so I started looking into @xenova/transformers and @huggingface/transformers. It was too simple to implement in a Node script (without NextJS), but I am struggling to make this work.

After the project creation, I created an API route as usual (/app/api/route.ts) and proceeded with the given example:

import { pipeline } from '@huggingface/transformers'

export async function GET() {
    const extractor = await pipeline(
        'feature-extraction',
        'Xenova/all-MiniLM-L6-v2'
    )

    const sentences = ['This is an example sentence', 'Each sentence is converted'];
    const output = await extractor(sentences, { pooling: 'mean', normalize: true });

    console.log(output)
}

But when I visit the API URL I get this error:

./node_modules/@huggingface/transformers/node_modules/onnxruntime-node/bin/napi-v3/darwin/arm64/onnxruntime_binding.node Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)

I have tried also to update the Next configuration file next.config.ts with:

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  output: 'standalone',
  serverExternalPackages: ['sharp', 'onnxruntime-node'] 
};

export default nextConfig;

I don't really know how to proceed and implement this in the server side (API routes). If I need a loader, I don't know how to implement it.

1
  • Looks like file's encoding is changed, change encoding to utf-8 in vs code and then try. Commented Dec 29, 2024 at 14:11

1 Answer 1

0

This may help you

  1. Update Next.js Configuration
    Adjust your next.config.js to exclude onnxruntime-node from Webpack bundling. This ensures the binary file is treated as a native Node.js module:

    const nextConfig = {
      output: 'standalone',
      webpack: (config, { isServer }) => {
        if (isServer) {
          config.externals = {
            ...config.externals,
            'onnxruntime-node': 'commonjs onnxruntime-node',
          };
        }
        return config;
      },
    };
    
    export default nextConfig;
    
  2. Server-Side Execution
    Ensure the embedding code runs on the server side. Update your API route to use onnxruntime-node properly in the Node.js environment:

    import { pipeline } from '@huggingface/transformers';
    
    export async function GET() {
      try {
        const extractor = await pipeline(
          'feature-extraction',
          'Xenova/all-MiniLM-L6-v2'
        );
    
        const sentences = ['This is an example sentence', 'Each sentence is converted'];
        const output = await extractor(sentences, { pooling: 'mean', normalize: true });
    
        return new Response(JSON.stringify(output), { status: 200 });
      } catch (error) {
        console.error('Error:', error);
        return new Response('Error generating embeddings', { status: 500 });
      }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering. Unfortunately, I get this error and I can't run the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.