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

Cannot read properties of undefined (reading 'replace') at OpenAIEmbeddings.embedQuery #5143

Open
5 tasks done
bhargavAtgithub opened this issue Apr 18, 2024 · 3 comments
Open
5 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@bhargavAtgithub
Copy link

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

Generating Embeddings

const generateEmbeddings = async () => {
    const embeddingsModel = new OpenAIEmbeddings();

    const data = fs.readFileSync(profileFilePath);

    const textSplitterChat = new RecursiveCharacterTextSplitter({
        chunkSize: 1000,
        chunkOverlap: 200,
        separators: ["\n"],
    });

    const docs = await textSplitterChat.createDocuments(
        profile.toString()
    );

    // Create a vector store from the documents.
    const vectorStore = await HNSWLib.fromDocuments(
        docs,
        embeddingsModel
    );

    // Save the vector store to a directory
    await vectorStore.save(embeddingsDir);
    console.log("Embeddings saved");
};

Initiating conversation

export const initiateConversation = async () => {
    await generateEmbeddings();

    const model = new WatsonxAI({
        modelId: "ibm/granite-13b-instruct-v2",
    });

    const embeddingsModel = new OpenAIEmbeddings();

    const loadedVectorStore = await HNSWLib.load(
        embeddingsDir,
        embeddingsModel
    );

    let vectorStoreRetriever = loadedVectorStore.asRetriever();


    let SYSTEM_TEMPLATE = `
            Your task is to perform the following actions: 
            1....
            2...
            3...
            
            """{context}"""

          `;

    const messages = [
        SystemMessagePromptTemplate.fromTemplate(SYSTEM_TEMPLATE),
        HumanMessagePromptTemplate.fromTemplate(
            "...?"
        ),
    ];

    const chatHistoryPrompt = ChatPromptTemplate.fromMessages(messages);

    const chatHistoryChain = RunnableSequence.from([
        {
           context: vectorStoreRetriever.pipe(formatDocumentsAsString),
        },
        chatHistoryPrompt,
        model,
        new StringOutputParser(),
    ]);

    const response = await chatHistoryChain.invoke(question);
    console.log({ response });
    return response;
};

Error Message and Stack Trace (if applicable)

TypeError: Cannot read properties of undefined (reading 'replace')
at OpenAIEmbeddings.embedQuery (file:///.../node_modules/@langchain/openai/dist/embeddings.js:212:46)
at HNSWLib.similaritySearch (file:///.../node_modules/@langchain/core/dist/vectorstores.js:104:90)
at VectorStoreRetriever._getRelevantDocuments (file:///.../node_modules/@langchain/core/dist/vectorstores.js:68:33)
at VectorStoreRetriever.getRelevantDocuments (file:///.../node_modules/@langchain/core/dist/retrievers/index.js:67:40)
at async RunnableSequence.invoke (file:///.../node_modules/@langchain/core/dist/runnables/base.js:1055:33)
at async file:///.../node_modules/@langchain/core/dist/runnables/base.js:1264:31
at async Promise.all (index 0)
at async RunnableMap.invoke (file:///.../node_modules/@langchain/core/dist/runnables/base.js:1263:13)
at async RunnableSequence.invoke (file:///...node_modules/@langchain/core/dist/runnables/base.js:1055:33)
at async initiateConversation (file:///.../src/conversation.js:124:26)

Description

Embeddings are being saved successfully.
But the problem is in initiateConversation.

System Info

{
"type": "module",
"dependencies": {
"@langchain/community": "0.0.49",
"@langchain/openai": "0.0.28",
"dotenv": "16.4.5",
"hnswlib-node": "1.4.2",
"langchain": "0.1.33"
},
"engines": {
"node": ">=18"
}
}

Copy link

dosubot bot commented Apr 18, 2024

The error you're encountering, "TypeError: Cannot read properties of undefined (reading 'replace')", occurs because the embedQuery method in OpenAIEmbeddings expects a string as input, but is receiving undefined. This method attempts to call replace on the input text, which fails if the input is not a valid string.

To resolve this issue, ensure that the text passed to embedQuery is always a string and is not undefined. You can add a check before calling embedQuery to verify that the text is defined and is a string. If you're directly modifying the library's code, you could also update the embedQuery method to include a validation check for the text parameter, throwing a clear error if the input is not a string.

This problem is likely happening because the text parameter being passed to embedQuery is not properly initialized or is missing in the part of your code where embedQuery is called. Double-check the data flow in your application to ensure that the text parameter is correctly passed to embedQuery.

Additionally, reviewing similar issues in the LangChain.js repository might provide further insights or solutions that are specific to your use case.

Sources

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.

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

I can see that embeddings are being created and are saved in the folder.
I don't understand why input is undefined when I'm providing the same path to load embeddings from.

@jeasonnow
Copy link
Contributor

const data = fs.readFileSync(profileFilePath);

const textSplitterChat = new RecursiveCharacterTextSplitter({
    chunkSize: 1000,
    chunkOverlap: 200,
    separators: ["\n"],
});

const docs = await textSplitterChat.createDocuments(
    profile.toString()
);

Maybe the data should be profile?

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

2 participants