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

Data going undefined #5684

Closed
5 tasks done
lakshya29154 opened this issue Jun 6, 2024 · 3 comments
Closed
5 tasks done

Data going undefined #5684

lakshya29154 opened this issue Jun 6, 2024 · 3 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@lakshya29154
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

/eslint-disable/
import fs from 'fs';
import { OpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
const OPENAI_API_KEY = "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"
export async function handlePdfUpload(pdf,query)
{
let bufferData;
console.log("This is pdf",pdf.path)
console.log("This is query",query)
fs.readFile(pdf.path,(err,data)=>{
if(err)
{
console.log(err)
}
else
{
bufferData="My name is Lakshya and I am of 23 age";
console.log(bufferData.toString())

}

})
const model = new OpenAI({ temperature: 0, apiKey: OPENAI_API_KEY });
const API_URL_PROMPT_TEMPLATE1 = `Given the following information, please answer the question:

Information:
${bufferData}

Question:
${query}
`;
const API_URL_PROMPT_TEMPLATE = /* #PURE */ new PromptTemplate({
inputVariables: ["bufferData", "query"],
template: API_URL_PROMPT_TEMPLATE1,
});

const apiUrlPrompt = await API_URL_PROMPT_TEMPLATE.format({
bufferData: "My name is lakshya and age is 23",
query:query,
});

const apiUrlResponse = await model.generate([apiUrlPrompt]);
console.log("OPEN API RESPONSE:", JSON.stringify(apiUrlResponse, null, 2));
return apiUrlResponse

}

Error Message and Stack Trace (if applicable)

[
  {
    "text": "\nNo, I cannot tell you what the information is because it is undefined.",
    "generationInfo": {
      "finishReason": "stop",
      "logprobs": null
    }
  }
]

Description

I mught think buffer Data is undefined so I purt explicitally data but still information going undefined

System Info

all good

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jun 6, 2024
Copy link

dosubot bot commented Jun 6, 2024

The issue with bufferData being undefined in your code is due to the asynchronous nature of fs.readFile. The bufferData variable is being set inside the callback of fs.readFile, but the rest of your code continues executing before the callback completes, leading to bufferData being undefined when it is used.

To fix this, you should use the promise-based version of fs.readFile to ensure that bufferData is set before it is used. Here is an updated version of your code:

/*eslint-disable*/
import fs from 'fs/promises';
import { OpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";

const OPENAI_API_KEY = "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";

export async function handlePdfUpload(pdf, query) {
  console.log("This is pdf", pdf.path);
  console.log("This is query", query);

  let bufferData;
  try {
    const data = await fs.readFile(pdf.path);
    bufferData = "My name is Lakshya and I am of 23 age";
    console.log(bufferData.toString());
  } catch (err) {
    console.log(err);
    return;
  }

  const model = new OpenAI({ temperature: 0, apiKey: OPENAI_API_KEY });
  const API_URL_PROMPT_TEMPLATE1 = `Given the following information, please answer the question:
        
  Information:
  ${bufferData}
  
  Question:
  ${query}
  `;

  const API_URL_PROMPT_TEMPLATE = new PromptTemplate({
    inputVariables: ["bufferData", "query"],
    template: API_URL_PROMPT_TEMPLATE1,
  });

  const apiUrlPrompt = await API_URL_PROMPT_TEMPLATE.format({
    bufferData: "My name is lakshya and age is 23",
    query: query,
  });

  const apiUrlResponse = await model.generate([apiUrlPrompt]);
  console.log("OPEN API RESPONSE:", JSON.stringify(apiUrlResponse, null, 2));
  return apiUrlResponse;
}

By using fs.promises.readFile, you can await the file read operation, ensuring that bufferData is set before it is used in the prompt template and API call. This should resolve the issue of bufferData being undefined.

To continue talking to Dosu, mention @dosu.

@jeasonnow
Copy link
Contributor

jeasonnow commented Jun 7, 2024

Be careful, your OPENAI_API_KEY has been leaked!!! @lakshya29154

@jeasonnow
Copy link
Contributor

Currently, langchainjs supports two types of template syntax: mustache and f-string.
Perhaps you should follow the default f-string syntax and replace the ${} in your template with {}. Here is an official example of how to use it.

const oneInputPrompt = new PromptTemplate({
inputVariables: ["adjective"],
template: "Tell me a {adjective} joke.",
});

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 6, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 13, 2024
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