I’m building a custom LLM Chain node, but I cannot access the document from the document loader, nor can I see any docs on how to correctly do this. Can anyone help with this? Here is my code
const { PromptTemplate } = require('langchain/prompts');
// This function is designed to be run inside the Custom - LLM Chain Node
async function processDocumentWithLLM() {
try {
// Retrieve the document data from the Default Data Loader1 node
const documentData = await this.getInputConnectionData('ai_document', 0);
console.log('Document Data:', documentData);
// Assuming the document data has a property 'text' that contains the document content
const documentText = documentData.text || '';
console.log('Document Text:', documentText);
// Define your prompt more dynamically, integrating the document text
const promptText = `Given the following document content: "${documentText}"\n\nProvide a detailed summary and analysis.`;
console.log('Prompt Text:', promptText);
// Using the dynamic prompt with the LangChain PromptTemplate
const prompt = PromptTemplate.fromTemplate(promptText);
// Log to check the prompt structure
console.log('Prompt Structure:', prompt);
// Obtain the LLM model connection
const llm = await this.getInputConnectionData('ai_languageModel', 0);
// Ensure that you have the LLM model connected to the ai_languageModel input
if (!llm) {
throw new Error('LLM (Language Model) connection data not found.');
}
console.log('LLM Connection Data:', llm);
// Create the chain with the LLM
let chain = prompt.pipe(llm);
console.log('Chain:', chain);
// Invoke the chain to process the prompt with the document text
const output = await chain.invoke();
console.log('Chain Output:', output);
// Return the processed output
return [{ json: { output } }];
} catch (error) {
console.error('Error processing document with LLM:', error);
return [{ json: { error: error.message } }];
}
}
// Execute the function to process the document
return processDocumentWithLLM();
[
{
"error": "The document data is not in the expected format or is empty."
}
]