Help with a Custom LLM Node

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();

image

[
  {
    "error": "The document data is not in the expected format or is empty."
  }
]

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Wayne :wave: Thanks for sharing those details!

Someone like @Jon , @marcus , or @oleg might be the best bet with node creation on this one :bowing_man:

Hey @Wayne,

Have you tried following what an existing node does and work from there? Which node is saying the document data is wrong is it your node or the default loader? Is the data empty as well as the erorr suggests?

Hey @Jon, I’m tyring too, is there a way to view that javascript that has been used on the preprogrammed langchain nodes?

Hey @Wayne,

Of course you can find the nodes in our GitHub repo here: n8n/packages/@n8n/nodes-langchain/nodes at master · n8n-io/n8n · GitHub