langchain-ai / langchainjs

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

HNSW Lib does not work even after installing hnswlib-node #3782

Closed adityadafe closed 8 months ago

adityadafe commented 9 months ago

After Installing hnswlib-node the issue persists- image

dosubot[bot] commented 9 months ago

🤖

Hey @KaKashi1210! I'm Dosu, a friendly bot here to lend a hand with bugs, answer questions, and guide you to be an awesome contributor. While we're waiting for a human maintainer, I'm here to help. Let's dive into this together. 😊

The error you're encountering might be due to the way hnswlib-node is being imported in the HNSWLib class. The error message suggests that the hnswlib-node library is not found, which could be due to a number of reasons:

  1. The hnswlib-node library is not installed. You can install it using npm install hnswlib-node.

  2. The hnswlib-node library is installed, but it's not in the correct location where the code is looking for it. Make sure that the hnswlib-node library is in the node_modules directory of your project.

  3. There might be an issue with the way the hnswlib-node library is being imported. The hnswlib-node library is being imported using a dynamic import statement, which might not be supported in your environment. You can try changing the dynamic import statement to a static import statement at the top of your file.

Here is the part of the code that might be causing the issue:

static async imports(): Promise<{
    HierarchicalNSW: typeof HierarchicalNSWT;
  }> {
    try {
      const {
        default: { HierarchicalNSW },
      } = await import("hnswlib-node");

      return { HierarchicalNSW };
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    } catch (err: any) {
      throw new Error(
        `Could not import hnswlib-node. Please install hnswlib-node as a dependency with, e.g. \`npm install -S hnswlib-node\`.\n\nError: ${err?.message}`
      );
    }
  }

You can try changing the dynamic import statement to a static import statement like this:

import { HierarchicalNSW } from "hnswlib-node";

And then use HierarchicalNSW directly in your code without the need to import it dynamically.

You can find this code in the libs/langchain-community/src/vectorstores/hnswlib.ts file.

If you're still encountering issues, could you please provide more details about the error message you're seeing and the steps you're taking when the error occurs? This will help me better understand the issue and provide a more accurate solution.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

mo29cg commented 8 months ago

same here

jacoblee93 commented 8 months ago

It looks like you're using bun - can you try with just base node?

It might be worth opening an issue/PR on the HNSW package for bun support.

adityadafe commented 8 months ago

@jacoblee93 thanks for the idea much appreciated <3

ddosakura commented 5 months ago

use bun 1.1.0+

cshaxu commented 3 months ago

same here, but I am using Node 18.18.0 - any recommendations please?