langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.24k stars 2.07k forks source link

MS Azure - Could not import faiss-node ... faiss-node.node is not a valid Win32 application #6148

Open jacknkandy opened 1 month ago

jacknkandy commented 1 month ago

Checked other resources

Example Code

import statement:

import { FaissStore } from '@langchain/community/vectorstores/faiss';

package.json

{
    "name": "pdf-chat-prototype",
    "version": "0.1.0",
    "description": "",
    "scripts": {
        "ingest": "tsx -r dotenv/config scripts/ingest.ts",
        "start": "nodemon --exec ts-node ./src/index.ts",
        "build": "npx tsc"
    },
    "dependencies": {
        "@fastify/cors": "^9.0.1",
        "@langchain/community": "^0.2.19",
        "@langchain/core": "^0.2.17",
        "@langchain/openai": "^0.2.4",
        "ajv-errors": "^3.0.0",
        "ajv-formats": "^3.0.1",
        "dotenv": "^16.4.5",
        "faiss-node": "^0.5.1",
        "fastify": "^4.28.1",
        "langchain": "^0.2.10",
        "moment": "^2.30.1",
        "pdf-parse": "^1.1.1"
    },
    "devDependencies": {
        "@types/pdf-parse": "^1.1.4",
        "nodemon": "^3.1.4",
        "ts-node": "^10.9.2",
        "tsx": "^4.16.2",
        "typescript": "^5.5.3"
    },
    "engines": {
        "node": ">=18"
    }
}

GitHub Actions:

name: Build and deploy Node.js app to Azure Web App - mecone-chat-bot

on:
  push:
    branches:
      - azure-prod
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js version
        uses: actions/setup-node@v3
        with:
          node-version: '18.x'

      - name: npm install
        run: |
          npm install

      - name: npm build
        run: |
          npm run build --if-present

      - name: Compress artifact before upload
        uses: a7ul/tar-action@v1.1.0
        id: compress
        with:
          command: c
          cwd: ./
          files: |
            ./dist
            ./node_modules
            ./vectors
            ./web.config
          outPath: release.tar.gz

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: node-app
          path: ./release.tar.gz

  deploy:
    runs-on: windows-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    permissions:
      id-token: write #This is required for requesting the JWT

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: node-app

      - name: Extract artifact for before deployment
        uses: a7ul/tar-action@v1.1.0
        id: extract
        with:
          command: x
          cwd: ./
          files: release.tar.gz

      - name: Remove archive before deployment
        run: |
          rm release.tar.gz

      - name: Login to Azure
        uses: azure/login@v2
        with:
          client-id: ###
          tenant-id: ###
          subscription-id: ###

      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v3
        id: deploy-to-webapp
        with:
          app-name: 'mecone-chat-bot'
          slot-name: 'Production'
          package: .

Error Message and Stack Trace (if applicable)

Could not import faiss-node. Please install faiss-node as a dependency with, e.g. npm install -S faiss-node.\n\nError: \\?\C:\home\site\wwwroot\node_modules\faiss-node\build\Release\faiss-node.node is not a valid Win32 application.\r\n\\?\C:\home\site\wwwroot\node_modules\faiss-node\build\Release\faiss-node.node

Description

Hi there,

I am having issues getting a sample app to run on an Azure Web App service. I can confirm that my app is working locally fine (MacOS) but am getting the following error after having built and deployed to a Windows-based Azure Web App service.

Could not import faiss-node. Please install faiss-node as a dependency with, e.g. `npm install -S faiss-node`.\n\nError: \\\\?\\C:\\home\\site\\wwwroot\\node_modules\\faiss-node\\build\\Release\\faiss-node.node is not a valid Win32 application.\r\n\\\\?\\C:\\home\\site\\wwwroot\\node_modules\\faiss-node\\build\\Release\\faiss-node.node

I am deploying via a GitHub actions deployment script, and have made sure that it is being built and deployed for using a Windows-based build server.

I can see a variety of similar issues reported for AWS etc. but cannot find a solution that works for me.

https://github.com/langchain-ai/langchainjs/issues/5843 https://github.com/langchain-ai/langchainjs/issues/5164 https://github.com/langchain-ai/langchainjs/issues/4027 https://github.com/langchain-ai/langchainjs/issues/3941

My error is slightly different as it is complaining that the faiss-node.node file is not a valid Win32 application.

I can locate the file mentioned in the path, running Node v18 on the Web App.

image

I have tried installing the package locally using npm i faiss-node via my Azure Web app, this didn't work. Additionally I tried dropping in the files from the pre-built release from faiss-node-v0.5.1-napi-v6-win32-x64.tar.gz again this didn't change anything.

Any help or guidance would be appreciated!

System Info

platform: Windows langchain 0.2.10 node 18.19.1

dosubot[bot] commented 1 month ago

Hey there, @jacknkandy! I'm here to help you with your technical questions and bug fixes. Let's tackle this issue together while we wait for a human maintainer to jump in.

To resolve the error related to faiss-node in your Azure Web App service, follow these steps:

  1. Install faiss-node: Ensure faiss-node is installed as a dependency:

    npm install -S faiss-node
  2. Adjust @rollup/plugin-commonjs Configuration: Create or update your Rollup configuration file (e.g., rollup.config.js) to include the dynamicRequireTargets option for @rollup/plugin-commonjs:

    import commonjs from '@rollup/plugin-commonjs';
    
    export default {
     // other Rollup configuration options
     plugins: [
       commonjs({
         dynamicRequireTargets: [
           'node_modules/faiss-node/**/*'
         ],
         ignoreDynamicRequires: true
       })
     ]
    };
  3. Ensure Proper Import in Code: In your main process file (e.g., main.js or index.js), ensure the import of faiss-node is handled correctly. Add the following code snippet to handle the import:

    static async importFaiss(): Promise<{ IndexFlatL2: typeof IndexFlatL2 }> {
     try {
       const {
         default: { IndexFlatL2 },
       } = await import("faiss-node");
    
       return { IndexFlatL2 };
     } catch (err: any) {
       throw new Error(
         `Could not import faiss-node. Please install faiss-node as a dependency with, e.g. \`npm install -S faiss-node\`.\n\nError: ${err?.message}`
       );
     }
    }
  4. Configure Azure Web App for Native Modules: Create a custom startup script (e.g., startup.sh) to install any additional dependencies and set up the environment. This script can be used to install system-level dependencies that faiss-node might require.

    Example startup.sh script:

    #!/bin/bash
    
    # Install system-level dependencies required by faiss-node
    apt-get update && apt-get install -y build-essential
    
    # Navigate to the app directory
    cd /home/site/wwwroot
    
    # Install npm dependencies
    npm install
    
    # Start the application
    npm start

    Ensure the script is executable:

    chmod +x startup.sh

    Add the custom startup script to your Azure Web App configuration:

    • Name: WEBSITE_RUN_FROM_PACKAGE
    • Value: 1
    • Name: STARTUP_COMMAND
    • Value: bash startup.sh

By following these steps, you can ensure that the Azure Web App service is configured to support native modules like faiss-node [1][2].

To continue talking to Dosu, mention @dosu.

jacknkandy commented 1 month ago

Just an update - I switched out and set up a new Linux-based Web App and was able to get my app to run without any hiccups. I believe there is some compatibility issue with the Windows-based build of faiss-node in Azure (potentially along the lines of https://github.com/ewfian/faiss-node/issues/25) but was unable to investigate any further.