Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Still having an issue using FaissStore in AWS lambda #3941

Closed
PierrickLozach opened this issue Jan 7, 2024 · 4 comments
Closed

Still having an issue using FaissStore in AWS lambda #3941

PierrickLozach opened this issue Jan 7, 2024 · 4 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@PierrickLozach
Copy link

PierrickLozach commented Jan 7, 2024

I have tried the layer as proposed here: #1930 (comment)

However, I get a different error message (Invalid ELF header):

2024-01-07T13:48:06.953Z	6fc99d7c-f00d-443d-9090-6d156308cc46	ERROR	Error: Could not import faiss-node. Please install faiss-node as a dependency with, e.g. `npm install -S faiss-node`.

Error: /var/task/node_modules/faiss-node/build/Release/faiss-node.node: invalid ELF header    

at FaissStore.importFaiss (/var/task/node_modules/@langchain/community/dist/vectorstores/faiss.cjs:378:19)    
at async readIndex (/var/task/node_modules/@langchain/community/dist/vectorstores/faiss.cjs:248:37)    
at async Promise.all (index 1)    
at async FaissStore.load (/var/task/node_modules/@langchain/community/dist/vectorstores/faiss.cjs:251:51)    
at async module.exports.generateResponse [as handler] (/var/task/ai/handler.js:203:25) | 

Any help would be appreciated.

Copy link

dosubot bot commented Jan 7, 2024

🤖

Hello @PierrickI3! I'm Dosu, a friendly bot here to lend a hand with your LangChainJS journey. Think of me as your virtual sidekick while you're waiting for a real-life human maintainer. From solving bugs to answering your questions or even helping you become a contributor, I'm here to assist. Let's dive into your issue, shall we?

The error message you're seeing is typically due to a mismatch between the environment where the faiss-node package was built and where it's being run. AWS Lambda uses a specific Amazon Linux environment, and if the faiss-node package was built in a different environment (like your local machine), it may not work correctly when deployed to Lambda.

To resolve this, you can try building your faiss-node package directly in an Amazon Linux environment. Here's a general approach:

  1. Set up an EC2 instance using the Amazon Linux AMI, which matches the Lambda environment.
  2. Install Node.js and npm on the EC2 instance.
  3. Clone your project repository to the EC2 instance.
  4. Run npm install in your project directory on the EC2 instance. This will build faiss-node in the Amazon Linux environment.
  5. Package your application code and node_modules directory into a .zip file.
  6. Upload this .zip file to Lambda as your deployment package.

This should ensure that faiss-node is built in an environment compatible with AWS Lambda.

If you're using Docker, you can use the lambci/lambda:build-nodejs12.x image, which closely mimics the AWS Lambda environment, to build your faiss-node package.

Here's a Dockerfile example:

FROM lambci/lambda:build-nodejs12.x

WORKDIR /app
COPY . .
RUN npm install

CMD ["zip", "-r", "package.zip", "."]

You can build and run this Dockerfile to produce a package.zip file, which you can then upload to AWS Lambda.

Please let me know if this helps or if you need further assistance.

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.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jan 7, 2024
@PierrickLozach
Copy link
Author

Well, I can't get it to work because I keep on getting this error now:

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

Error: /var/task/node_modules/faiss-node/build/Release/faiss-node.node: cannot open shared object file: No such file or directory
    at FaissStore.importFaiss (/var/task/node_modules/@langchain/community/dist/vectorstores/faiss.cjs:378:19)
    at async readIndex (/var/task/node_modules/@langchain/community/dist/vectorstores/faiss.cjs:248:37)
    at async Promise.all (index 1)
    at async FaissStore.load (/var/task/node_modules/@langchain/community/dist/vectorstores/faiss.cjs:251:51)
    at async module.exports.generateResponse [as handler] (/var/task/aihandler.js:203:25)

Here is my Dockerfile:

FROM amazonlinux:latest

RUN yum install -y nodejs gzip tar zip

WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY . .

RUN zip -r package.zip .

package.json

{
  "name": "securityqa-conversations",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "docker:build": "docker build -t dev-green-teacket-securityqa-package-faiss-node .",
    "docker:create": "docker create -ti --name dev-green-teacket-securityqa-package-faiss-node-temp dev-green-teacket-securityqa-package-faiss-node bash",
    "docker:copy": "docker cp dev-green-teacket-securityqa-package-faiss-node-temp:/app/package.zip ./out/",
    "docker:clean": "docker rm -f dev-green-teacket-securityqa-package-faiss-node-temp",
    "package": "npm run docker:build && npm run docker:create && npm run docker:copy && npm run docker:clean",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "faiss-node": "^0.5.1",
    "serverless-iam-roles-per-function": "^3.2.0",
    "serverless-offline": "^13.3.2",
    "serverless-offline-local-authorizers-plugin": "^1.2.0",
    "serverless-openapi-documenter": "^0.0.93",
    "serverless-package-external": "^1.3.5",
    "serverless-plugin-common-excludes": "^4.0.0",
    "serverless-prune-plugin": "^2.0.2"
  },
  "dependencies": {
    "@aws-sdk/client-bedrock-runtime": "^3.484.0",
    "@langchain/community": "^0.0.13",
    "langchain": "^0.0.212",
    "uuid": "^9.0.1"
  }
}

I also have a build script:

#!/bin/bash
clear

# Clean up initial files/directories/docker containers
docker rm -f dev-green-teacket-securityqa-package-faiss-node

# Run sls package to make sure the .serverless folder is present with some required files
sls package

# Create "out" directory
mkdir -p out

# Build package in docker
npm run package

if [ $? -eq 0 ]
then
    sleep 3 # Wait until the zip file is created
    cp ./.serverless/cloudformation-template-update-stack.json out/
    cp ./.serverless/custom-resources.zip out/
    cp ./.serverless/serverless-state.json out/
    
    # Deploy serverless package
    sls deploy --package ./out
fi

The entire process basically copies a few dependency files, starts a docker container with the lambda base image, runs npm ci, zip everything up and copies the zipped file to the host (out directory).
Then my script takes the output, adds a few required serverless files (generated by running serverless package) and deploys it to AWS.

However, I still get an error: cannot open shared object file: No such file or directory

How can I find out what the shared object file is?

@PierrickLozach
Copy link
Author

Any news about this? Would love to make it work in my lambda

@PierrickLozach
Copy link
Author

I ended up using memoryStore instead just like this: #943 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant