langchainjs: HNSWLib-node not found when using in a AWS Lambda function
I recently made a simple Typescript function to create a VectorStore using HNSWLib-node. It saves the vector store in a folder and then, in another script file, I load and execute a RetrievalQAChain using OpenAI.
Everything was working fine until I decided to put that in a AWS Lambda Function.
My package.json has the following dependencies:
"hnswlib-node": "^1.4.2",
"langchain": "^0.0.59",
Also, I double checked and the hnswlib-node folder is inside “node_modules” folder in my lambda function folder.
However, I keep getting the following error (from CloudWatch Logs):
ERROR Invoke Error {"errorType":"Error","errorMessage":"Please install hnswlib-node as a dependency with,
e.g. `npm install -S hnswlib-node`",
"stack":["Error: Please install hnswlib-node as a dependency with, e.g. `npm install -S hnswlib-node`","
at Function.imports (/var/task/node_modules/langchain/dist/vectorstores/hnswlib.cjs:161:19)","
at async Function.getHierarchicalNSW (/var/task/node_modules/langchain/dist/vectorstores/hnswlib.cjs:38:37)","
at async Function.load (/var/task/node_modules/langchain/dist/vectorstores/hnswlib.cjs:123:23)","
at async AMCompanion (/var/task/index.js:18:29)"," at async Runtime.exports.handler (/var/task/index.js:39:22)"]}
Also, this error is not thrown on importing HNSWLib, but only in the following line of code:
const vectorStore = await HNSWLib.load("data", new OpenAIEmbeddings(
{
openAIApiKey: process.env.OPENAI_API_KEY,
}
))
This is my import:
const { HNSWLib } = require("langchain/vectorstores/hnswlib")
It seems I’m not the only one with this problem. See this post
Tried this and didn’t work.
Expeted behavior: code would be executed properly, just like when executed on my local machine.
Actual behavior: the error pasted above.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 26 (3 by maintainers)
I found the solution to this on Nextjs, user the following
I’m using Vercel serverless functions, which under the hood utilize AWS Lambda. Interestingly this used to work for me fine, but it stopped working when I switched over to NextJS 13 Route Handlers, which is currently in beta. I’m pretty sure the new NextJS 13 app router also utilizes Lambda, so I don’t know what changed compared to the old NextJS implementation specifically that is causing this to stop working.
If anyone else has this working with NextJS Route handlers, let me know.
Hi @joaotextor,
Thank you for your response. We appreciate your input. Since you’re no longer using it, we’ll go ahead and close the issue. If you ever encounter similar issues in the future, feel free to reach out. Thank you again!
About Dosu
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.
Hi, @joaotextor,
I’m helping the langchainjs team manage their backlog and am marking this issue as stale. It looks like the issue involves the HNSWLib-node library not being found when the code is executed in an AWS Lambda function, despite being present in the node_modules folder. There were discussions about potential solutions, including the use of Docker to produce node_modules containing all the required libs, but it seems the error persisted for you and other users even after trying the suggested command. Some users found workarounds, while others are still seeking a resolution.
Could you please confirm if this issue is still relevant to the latest version of the langchainjs repository? If it is, please let the langchainjs team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!
@joaotextor try this this one works for me
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
const embeddings = new OpenAIEmbeddings();
const store = await MemoryVectorStore.fromDocuments(docs, embeddings);
This is the alternate I have been using and it works fine for my use case.
Any chance of some more detail on how to do this for a docker newbie? Essentially this doesn’t work for building my lambda layer because most of the node modules require 18.x.
So, can I “docker pull node” to get the latest version, then do some “docker run docker run --rm -v “$PWD”:/var/task nodes npm install langchain hnswlib-node my_other_modules” magic to create a node_modules that I can zip up and use as a lambda layer?
Thanks for any help,
David
@manduks Cheers mate! After a few days of headache with this issue, your solution worked for me as charm. Awesome stuff! Thank you again!