@n8n/n8n-nodes-langchain
Version:

132 lines • 6.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChainLlm = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const N8nOutputParser_1 = require("../../../utils/output_parsers/N8nOutputParser");
const methods_1 = require("./methods");
const processItem_1 = require("./methods/processItem");
const error_handling_1 = require("../../vendors/OpenAi/helpers/error-handling");
class ChainLlm {
constructor() {
this.description = {
displayName: 'Basic LLM Chain',
name: 'chainLlm',
icon: 'node:basic-llm-chain',
iconColor: 'black',
group: ['transform'],
version: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9],
description: 'A simple chain to prompt a large language model',
defaults: {
name: 'Basic LLM Chain',
},
codex: {
alias: ['LangChain'],
categories: ['AI'],
subcategories: {
AI: ['Chains', 'Root Nodes'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.chainllm/',
},
],
},
},
inputs: `={{ ((parameter) => { ${methods_1.getInputs.toString()}; return getInputs(parameter) })($parameter) }}`,
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
builderHint: {
inputs: {
ai_languageModel: { required: true },
ai_outputParser: {
required: false,
displayOptions: { show: { hasOutputParser: [true] } },
},
},
},
credentials: [],
properties: methods_1.nodeProperties,
};
}
async execute() {
this.logger.debug('Executing Basic LLM Chain');
const items = this.getInputData();
const returnData = [];
const outputParser = await (0, N8nOutputParser_1.getOptionalOutputParser)(this);
const shouldUnwrapObjects = this.getNode().typeVersion >= 1.6 || !!outputParser;
const batchSize = this.getNodeParameter('batching.batchSize', 0, 5);
const delayBetweenBatches = this.getNodeParameter('batching.delayBetweenBatches', 0, 0);
if (this.getNode().typeVersion >= 1.7 && batchSize > 1) {
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize);
const batchPromises = batch.map(async (_item, batchItemIndex) => {
return await (0, processItem_1.processItem)(this, i + batchItemIndex);
});
const batchResults = await Promise.allSettled(batchPromises);
batchResults.forEach((promiseResult, batchItemIndex) => {
const itemIndex = i + batchItemIndex;
if (promiseResult.status === 'rejected') {
const error = promiseResult.reason;
if (error instanceof n8n_workflow_1.NodeApiError && (0, error_handling_1.isOpenAiError)(error.cause)) {
const openAiErrorCode = error.cause.error?.code;
if (openAiErrorCode) {
const customMessage = (0, error_handling_1.getCustomErrorMessage)(openAiErrorCode);
if (customMessage) {
error.message = customMessage;
}
}
}
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: itemIndex },
});
return;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error);
}
const responses = promiseResult.value;
responses.forEach((response) => {
returnData.push({
json: (0, methods_1.formatResponse)(response, shouldUnwrapObjects),
});
});
});
if (i + batchSize < items.length && delayBetweenBatches > 0) {
await (0, n8n_workflow_1.sleep)(delayBetweenBatches);
}
}
}
else {
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const responses = await (0, processItem_1.processItem)(this, itemIndex);
responses.forEach((response) => {
returnData.push({
json: (0, methods_1.formatResponse)(response, shouldUnwrapObjects),
});
});
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError && (0, error_handling_1.isOpenAiError)(error.cause)) {
const openAiErrorCode = error.cause.error?.code;
if (openAiErrorCode) {
const customMessage = (0, error_handling_1.getCustomErrorMessage)(openAiErrorCode);
if (customMessage) {
error.message = customMessage;
}
}
}
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
continue;
}
throw error;
}
}
}
return [returnData];
}
}
exports.ChainLlm = ChainLlm;
//# sourceMappingURL=ChainLlm.node.js.map