UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

163 lines 6.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelSelector = void 0; const n8n_workflow_1 = require("n8n-workflow"); const helpers_1 = require("./helpers"); const ai_utilities_1 = require("@n8n/ai-utilities"); const N8nNonEstimatingTracing_1 = require("../llms/N8nNonEstimatingTracing"); function getCallbacksArray(callbacks) { if (!callbacks) return []; if (Array.isArray(callbacks)) { return callbacks; } return callbacks.handlers || []; } class ModelSelector { constructor() { this.description = { displayName: 'Model Selector', name: 'modelSelector', icon: 'node:model-selector', iconColor: 'black', defaults: { name: 'Model Selector', }, version: 1, group: ['transform'], description: 'Use this node to select one of the connected models to this node based on workflow data', inputs: `={{ ((parameters) => { ${helpers_1.configuredInputs.toString()}; return configuredInputs(parameters) })($parameter) }}`, codex: { categories: ['AI'], subcategories: { AI: ['Language Models'], }, resources: { primaryDocumentation: [ { url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.modelselector/', }, ], }, }, outputs: [n8n_workflow_1.NodeConnectionTypes.AiLanguageModel], requiredInputs: 1, properties: [ helpers_1.numberInputsProperty, { displayName: 'Rules', name: 'rules', placeholder: 'Add Rule', type: 'fixedCollection', typeOptions: { multipleValues: true, sortable: true, }, description: 'Rules to map workflow data to specific models', default: {}, options: [ { displayName: 'Rule', name: 'rule', values: [ { displayName: 'Model', name: 'modelIndex', type: 'options', description: 'Choose model input from the list', default: 1, required: true, placeholder: 'Choose model input from the list', typeOptions: { loadOptionsMethod: 'getModels', }, }, { displayName: 'Conditions', name: 'conditions', placeholder: 'Add Condition', type: 'filter', default: {}, typeOptions: { filter: { caseSensitive: true, typeValidation: 'strict', version: 2, }, }, description: 'Conditions that must be met to select this model', }, ], }, ], }, ], }; this.methods = { loadOptions: { async getModels() { const numberInputs = this.getCurrentNodeParameter('numberInputs'); return Array.from({ length: numberInputs ?? 2 }, (_, i) => ({ value: i + 1, name: `Model ${(i + 1).toString()}`, })); }, }, }; } async supplyData(itemIndex) { const models = (await this.getInputConnectionData(n8n_workflow_1.NodeConnectionTypes.AiLanguageModel, itemIndex)); if (!models || models.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No models connected', { itemIndex, description: 'No models found in input connections', }); } models.reverse(); const rules = this.getNodeParameter('rules.rule', itemIndex, []); if (!rules || rules.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No rules defined', { itemIndex, description: 'At least one rule must be defined to select a model', }); } for (let i = 0; i < rules.length; i++) { const rule = rules[i]; const modelIndex = rule.modelIndex; if (modelIndex <= 0 || modelIndex > models.length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid model index ${modelIndex}`, { itemIndex, description: `Model index must be between 1 and ${models.length}`, }); } const conditionsMet = this.getNodeParameter(`rules.rule[${i}].conditions`, itemIndex, false, { extractValue: true, }); if (conditionsMet) { const selectedModel = models[modelIndex - 1]; const originalCallbacks = getCallbacksArray(selectedModel.callbacks); for (const currentCallback of originalCallbacks) { if (currentCallback instanceof ai_utilities_1.N8nLlmTracing) { currentCallback.setParentRunIndex(this.getNextRunIndex()); } } const modelSelectorTracing = new N8nNonEstimatingTracing_1.N8nNonEstimatingTracing(this); selectedModel.callbacks = [...originalCallbacks, modelSelectorTracing]; return { response: selectedModel, }; } } throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No matching rule found', { itemIndex, description: 'None of the defined rules matched the workflow data', }); } } exports.ModelSelector = ModelSelector; //# sourceMappingURL=ModelSelector.node.js.map