issue is not getting data #5700
Replies: 2 comments
-
The issue with To fix this, you should use the promise-based version of /*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 = "Hello My name is Lakshya and I am 23 years old";
console.log(bufferData.toString());
} catch (err) {
console.log(err);
return;
}
const model = new OpenAI({ temperature: 0.7, apiKey: OPENAI_API_KEY });
const API_URL_PROMPT_TEMPLATE1 = `You have given the information ${bufferData} give me any data contained in file in response:
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: bufferData,
query: query,
});
const apiUrlResponse = await model.generate([apiUrlPrompt]);
console.log("OPEN API RESPONSE:", JSON.stringify(apiUrlResponse, null, 2));
return apiUrlResponse;
} By using |
Beta Was this translation helpful? Give feedback.
-
The bots answer is probably not really helpful. `Text ${identifier} Text` is javascript syntax for string interpolation. This const API_URL_PROMPT_TEMPLATE1 = `You have given the information ${bufferData} give me any data contained in file in response:
Information:
${bufferData}
Question:
${query}
`; is already a string containing all the values, not placeholders. For prompt placeholders you need to remove the const API_URL_PROMPT_TEMPLATE1 = `You have given the information {bufferData} give me any data contained in file in response:
Information:
{bufferData}
Question:
{query}
`; Note that reading from a pdf with fs is probably not what you want to do here. PDF-files do not contain raw text. What you read is binary data, which can be interpreted by a pdf interpreter such as PDF.js. Reading the raw data such as with fs will result in gibberish. For what it's worth, when I properly create the prompt and use static bufferData as a string, I get the correct from the api. Did this code, as you posted it, not work for you or was it just not working when actually reading the pdf? You can try checking the value of |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
This is the code actually I am passing the buffer Data like My name is lakshya and I am 23 years of age now also I have passed user query like what is the age of lakshya still it is not able to give me complete answer SO is there anything error in getting prompt data or anythinhg
System Info
all good
Beta Was this translation helpful? Give feedback.
All reactions