langchain-ai / langchainjs

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

Can not import faiss-node from Electron.js 'main #5843

Open AlexandrosAnapolitanos opened 3 months ago

AlexandrosAnapolitanos commented 3 months ago

Checked other resources

Example Code

ipcMain.on('create-vector-store', async (event, pathName, folderNameA, folderNameB) => {
...
  const vectorstore = await FaissStore.fromDocuments(documents, embeddings, {})
  await vectorstore.save('/Users/alex/Desktop/ale')
}

Error Message and Stack Trace (if applicable)

(node:15641) UnhandledPromiseRejectionWarning: Error: Could not import faiss-node. Please install faiss-node as a dependency with, e.g. npm install -S faiss-node.

Error: Could not dynamically require "/Users/alexandrosanapolitanos/Desktop/ragmaster/ragmaster/build/faiss-node.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work. at FaissStore.importFaiss (/Users/alexandrosanapolitanos/Desktop/ragmaster/ragmaster/out/main/index.js:14851:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async FaissStore.addVectors (/Users/alexandrosanapolitanos/Desktop/ragmaster/ragmaster/out/main/index.js:14600:31) at async FaissStore.fromDocuments (/Users/alexandrosanapolitanos/Desktop/ragmaster/ragmaster/out/main/index.js:14827:5) at async IpcMainImpl. (/Users/alexandrosanapolitanos/Desktop/ragmaster/ragmaster/out/main/index.js:22090:23) (Use Electron --trace-warnings ... to show where the warning was created) (node:15641) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 977)

Description

Hi I am using electron js to create a langchain js application and while I am trying to add my vectors with FaissStore, I am getting this error:

(node:15641) UnhandledPromiseRejectionWarning: Error: Could not import faiss-node. Please install faiss-node as a dependency with, e.g. npm install -S faiss-node.

Error: Could not dynamically require "/Users/alex/Desktop/test/project/build/faiss-node.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work. at FaissStore.importFaiss (/Users/... See below for whole error

After trying to:

const vectorstore = await FaissStore.fromDocuments(documents, embeddings, {}) await vectorstore.save('/Users/alex/Desktop/ale')

Now this is very similar it seems to me to the https://github.com/langchain-ai/langchainjs/issues/4027 but unfortunately I am on electron and not next js in order to do the servercomponentsexternalpackages solution.

I am fairly new on the langchain environment so if I am missing something or you have any suggestions please let me know.

Thank you very much.

System Info

"dependencies": { "@chakra-ui/react": "^2.8.2", "@electron-toolkit/preload": "^3.0.0", "@electron-toolkit/utils": "^3.0.0", "@emotion/react": "^11.11.4", "@emotion/styled": "^11.11.5", "framer-motion": "^11.2.10", "mammoth": "^1.8.0", "mime": "^4.0.3" }, "devDependencies": { "@electron-toolkit/eslint-config": "^1.0.2", "@electron-toolkit/eslint-config-prettier": "^2.0.0", "@langchain/community": "0.2.12", "@langchain/core": "0.1.57", "@langchain/openai": "0.0.28", "@vitejs/plugin-react": "^4.2.1", "dotenv": "^16.4.5", "electron": "^28.2.0", "electron-builder": "^24.9.1", "electron-vite": "^2.0.0", "eslint": "^8.56.0", "eslint-plugin-react": "^7.33.2", "faiss-node": "^0.5.1", "langchain": "0.2.5", "openai": "^4.33.1", "pdf-parse": "^1.1.1", "prettier": "^3.2.4", "react": "^18.3.1", "react-dom": "^18.3.1", "vite": "^5.0.12" } }

dosubot[bot] commented 3 months ago

Hello, @AlexandrosAnapolitanos! I'm here to assist you with your LangChain.js issue. I can help you solve bugs, answer questions, and even become a contributor. Let's work together to resolve the issue you're facing.

To resolve the error related to faiss-node in your Electron.js application, ensure that faiss-node is properly installed and that your Electron.js environment supports dynamic imports. Here are the steps you can follow:

  1. Install faiss-node: Make sure faiss-node is installed as a dependency in your project:

    npm install -S faiss-node
  2. Adjust @rollup/plugin-commonjs Configuration: You need to configure @rollup/plugin-commonjs to handle dynamic requires for native modules. Add the dynamicRequireTargets option to your Rollup configuration to include the faiss-node module. Here is an example configuration:

    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: Ensure that the import of faiss-node is handled correctly in your code. Here is the relevant part of the code that handles the import of faiss-node:

    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. Check Electron.js Environment: Ensure that your Electron.js environment supports dynamic imports and that the module resolution is correctly configured. If you are still encountering issues, consider exploring alternative vector stores that do not rely on dynamic imports.

By following these steps, you should be able to resolve the error related to faiss-node in your Electron.js application [1].

To continue talking to Dosu, mention @dosu.

jacoblee93 commented 3 months ago

CC @ewfian but I'm not sure this will work in that environment - was the Dosu answer helpful?

AlexandrosAnapolitanos commented 3 months ago

I tried to implement the Dosu answer but I keep getting errors. It's midnight here so I ll try to persist with more effort tomorrow.

AlexandrosAnapolitanos commented 3 months ago

@dosu can you please elaborate on where to put the code you suggested?

dosubot[bot] commented 3 months ago

To integrate the suggested code into your Electron.js project, 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 Electron.js 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. Use the Configuration in Your Project: Ensure that your Electron.js environment supports dynamic imports and that the module resolution is correctly configured. If you encounter further issues, consider exploring alternative vector stores that do not rely on dynamic imports.

By following these steps, you should be able to resolve the error related to faiss-node in your Electron.js application. If you need further assistance, feel free to ask!

To continue talking to Dosu, mention @dosu.

Aadelrag commented 3 months ago

Were you able to resolve this? I'm not quite getting the instructions.

AlexandrosAnapolitanos commented 3 months ago

No I wasnt able to resolve this. Any help would be very appreciated