langchain-ai / langchainjs

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

Could not import faiss-node. Please install as a dependency with npm install -S faiss-node #5164

Closed Huma10 closed 2 months ago

Huma10 commented 5 months ago

Checked other resources

Example Code

import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"; import { BedrockChat } from "@langchain/community/chat_models/bedrock"; import { PromptTemplate } from "@langchain/core/prompts"; import { RunnableLambda, RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables"; import { StringOutputParser } from "@langchain/core/output_parsers"; / eslint-disable @typescript-eslint/no-non-null-assertion / import { BedrockEmbeddings } from "@langchain/community/embeddings/bedrock"; import { FaissStore } from "@langchain/community/vectorstores/faiss"; import { formatDocumentsAsString } from "langchain/util/document"; import { TextLoader } from "langchain/document_loaders/fs/text"; import express = require("express"); import { Request, Response } from "express"; class TestService { async Test(searchParam:string): Promise { try { const result = await isPiiFunc(searchParam); console.log('========RESult=======', result); if (result === 'NO') { const result = await ragFunc(searchParam); console.log('=========result===========', result); } else { console.log("Your query should not contain any personal information such as name, address, or date. Please ensure your input adheres to this guideline."); } } catch (error) { console.log("Error >>>>>..", error); throw error; } } } const testService = new TestService(); console.log("====here2====="); const app = express(); console.log("====here3====="); app.use(express.json()); console.log("====here4====="); const PORT = 8000; console.log("====here5====="); app.post('/search', async (req : Request, res: Response) => { try { const { searchParam } = req.body; if(!searchParam) { res.status(400).json({error: 'Search parameter is required'}); } const result = await testService.Test(searchParam); res.status(200).json(result); } catch (error) { res.status(500).json({error: 'Internal Server Error'}); } }) // export const handler = async(event: APIGatewayProxyEvent) : Promise => { // try { // if(!event.body) { // return { // statusCode: 400, // headers: { // 'Content-type':'application/json' // }, // body: JSON.stringify({ // message:"Empty request body" // }) // } // } // const reqBody :any = JSON.parse(event.body); // const searchParam: string = reqBody.searchParam // console.log('Search>>>', searchParam); // const response = await Test(searchParam); // return { // statusCode: 200, // headers: { // "Access-Control-Allow-Headers" : "Content-Type", // "Access-Control-Allow-Origin": "", // "Access-Control-Allow-Methods": "OPTIONS,POST,GET" // }, // body: JSON.stringify(response) // } // } catch (error) { // return { // statusCode: 500, // headers: { // "Access-Control-Allow-Headers" : "Content-Type", // "Access-Control-Allow-Origin": "", // "Access-Control-Allow-Methods": "OPTIONS,POST,GET" // }, // body: JSON.stringify(error) // } // } // } // async function Test(searchParam:string) { // try { // const result = await isPiiFunc(searchParam); // console.log('========RESult=======', result); // if (result === 'NO') { // const result = await ragFunc(searchParam); // console.log('=========result===========', result); // } else { // console.log("Your query should not contain any personal information such as name, address, or date. Please ensure your input adheres to this guideline."); // } // } catch (error) { // console.log("Error >>>>>..", error); // throw error; // } // } async function isPiiFunc(question: string): Promise { console.log("Here"); try { const piiPromptTemplate = Human: Based on the provided user input, check if the user input contains personal information that includes name, address or date.Question: ${question}From the above sentence, identify if the above query contains a human's name or address or any date, then return 'YES', else return 'NO'.You are a bot that strictly answers in YES or NO. The only accepted response is YES/NO. Do not explain your answer. Do not answer the query.Assistant:; const model = new BedrockChat({ model: 'anthropic.claude-3-sonnet-20240229-v1:0', region: "us-east-1", modelKwargs: { temperature: 0, } }); const prompt = new PromptTemplate({ template: piiPromptTemplate, inputVariables: ['question'], }); const retrievalChain = RunnableSequence.from([ { question: new RunnablePassthrough() }, prompt, model, new StringOutputParser(), ]); const chain = retrievalChain.invoke(question); return chain; } catch (error: any) { console.log("=======Error=============", error.message); throw error; } } async function ragFunc(question: string): Promise { const embeddings = new BedrockEmbeddings({ model: "amazon.titan-embed-text-v1", // Default value }); console.log("==============embeddings==========", embeddings); console.log("Here"); const res = await embeddings.embedQuery(question); console.log("==============res==========", {res}); console.log("here2"); // Create docs with a loader const loader = new TextLoader("example.txt"); const docs = await loader.load(); const vectorStore = await FaissStore.fromDocuments( docs, embeddings ); console.log("=====Vectorstore==========", vectorStore); // const resultOne = await vectorStore.similaritySearch(question, 1); // console.log("==============resultOne==========", resultOne); // Initialize a retriever wrapper around the vector store const retriever = vectorStore.asRetriever({ k: 1, searchType: "similarity", }); console.log("==============retriever==========", retriever); const promptTemplate = Human:Context:{context}Relevant documents is provided above in the context. Please look into those and select the best answer for the question asked. DO NOT answer using information that is NOT present in the context.If you do not know the answer, just say 'I dont know'. Only provide relevant answers.Now here is the question for you to answer: {question}You will only return a JSON object containing the key answer.Pull relevant sentences from the documents that pertain to the answer and write them inside answer JSON key.Include guidelines, comparisons, challenges and examples pertaining to the question if the documents contains any of them without enclosing them in quotation marks. Ensure that you provide examples from the documents to every point you mention. Do not use any such similar phrases such as "According to the documents" or "The documents state that" because a human would not talk like that. Do not talk as if you are refering to any documents. Remember to rephrase your answer in such a way as if you are talking to a human. Elaborate every point in detail. Make use of second person pronouns or third person pronouns.The JSON object must strictly not contain any other keys. Please display only the answer and not the references from the douments.Assistant:; const model = new BedrockChat({ model: 'anthropic.claude-3-sonnet-20240229-v1:0', region: 'us-east-1', streaming: true, modelKwargs: { temperature: 0.1, //max_tokens_to_sample: 2048 }, }); const prompt = new PromptTemplate({ template: promptTemplate, inputVariables: ['context','question'], }); console.log('=============PROMPT==============', prompt); // const retrievalChain = RunnableSequence.from([ // { question: new RunnablePassthrough() }, // prompt, // model, // retriever, // new StringOutputParser(), // ]); const retrievalChain = RunnableSequence.from([ RunnableLambda.from((input: any) => input.question), { context: retriever.pipe(formatDocumentsAsString), question: new RunnablePassthrough(), }, prompt, model, new StringOutputParser(), ]); console.log("===========RETRIVAL============", retrievalChain); const chain = retrievalChain.invoke({question}); return chain; } //=================================Testing=============================================// // Test2(); // async function Test2() { // try { // const searchParam: string = "how many leaves are there" // console.log('Search>>>', searchParam); // const response = await Test(searchParam); // return { // statusCode: 200, // headers: { // "Access-Control-Allow-Headers" : "Content-Type", // "Access-Control-Allow-Origin": "", // "Access-Control-Allow-Methods": "OPTIONS,POST,GET" // }, // body: JSON.stringify(response) // } // } catch (error) { // return { // statusCode: 500, // headers: { // "Access-Control-Allow-Headers" : "Content-Type", // "Access-Control-Allow-Origin": "", // "Access-Control-Allow-Methods": "OPTIONS,POST,GET" // }, // body: JSON.stringify(error) // } // } // } app.listen(PORT, () => { console.log(Server is running on port ${PORT}); });

Error Message and Stack Trace (if applicable)

2024-04-21T16:13:20.512+05:30Error >>>>>.. Error: Could not import faiss-node. Please install faiss-node as a dependency with, e.g. npm install -S faiss-node. Error >>>>>.. Error: Could not import faiss-node. Please install faiss-node as a dependency with, e.g. npm install -S faiss-node.
  2024-04-21T16:13:20.512+05:30Error: Could not locate the bindings file. Tried:

 

Description

I am trying to use faiss-node library

System Info

System is linux When I run it on AWS lambda or when I creat ECS task there I am getting this error

dosubot[bot] commented 5 months ago

The error you're encountering with faiss-node not being found in your AWS Lambda or ECS environment can be addressed through several steps:

  1. Ensure faiss-node is installed: Run npm install -S faiss-node in your project directory to add it as a dependency.

  2. Check Environment Compatibility: Verify that faiss-node is compatible with the Node.js runtime version in AWS Lambda or ECS. Compatibility details can usually be found in the faiss-node documentation.

  3. Native Dependencies: faiss-node may require native libraries. For AWS Lambda, these dependencies need to be compiled to match the Lambda execution environment. This might involve using a Docker image that mimics the AWS environment for compiling these dependencies.

  4. Docker Images for ECS: If using Docker, ensure your Dockerfile includes the necessary dependencies for faiss-node. This may require customizing the Dockerfile to install specific libraries or tools.

If these steps do not resolve the issue, consider building the faiss-node package in an Amazon Linux environment or using a Docker image like lambci/lambda:build-nodejs12.x for compatibility with AWS Lambda. Alternatively, switching to memoryStore could serve as a temporary workaround, though it doesn't directly solve the faiss-node issue.

These steps are derived from resolutions to similar issues within the LangChain JS repository, indicating that such problems can often be resolved by ensuring proper installation and environment setup.

#### Details **Sources** - [Could not import faiss-node (NextJS) when loading from python](https://github.com/langchain-ai/langchainjs/issues/4027) - [Still having an issue using FaissStore in AWS lambda](https://github.com/langchain-ai/langchainjs/issues/3941) - [libs/langchain-community/src/vectorstores/faiss.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/src/vectorstores/faiss.ts)
**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.

To continue the conversation, mention @dosu.