UNPKG

@n8n/n8n-nodes-langchain

Version:

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

223 lines 7.99 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var process_exports = {}; __export(process_exports, { process: () => process }); module.exports = __toCommonJS(process_exports); var import_n8n_workflow = require("n8n-workflow"); var import_base = require("../helpers/base"); var import_common = require("../helpers/common"); var import_mappers = require("../helpers/mappers"); var import_model = require("../helpers/model"); var import_preflight = require("../helpers/preflight"); var import_jailbreak = require("./checks/jailbreak"); var import_keywords = require("./checks/keywords"); var import_nsfw = require("./checks/nsfw"); var import_pii = require("./checks/pii"); var import_secretKeys = require("./checks/secretKeys"); var import_topicalAlignment = require("./checks/topicalAlignment"); var import_urls = require("./checks/urls"); async function process(itemIndex, model) { const inputText = this.getNodeParameter("text", itemIndex); const operation = this.getNodeParameter("operation", 0); const guardrails = this.getNodeParameter("guardrails", itemIndex); const customizeSystemMessage = operation === "classify" && this.getNodeParameter("customizeSystemMessage", itemIndex, false); const systemMessage = customizeSystemMessage ? this.getNodeParameter("systemMessage", itemIndex) : void 0; const failedChecks = []; const passedChecks = []; const handleFailedResults = (results) => { const unexpectedError = results.failed.find( (result) => result.status === "rejected" || result.status === "fulfilled" && result.value.executionFailed ); if (results.failed.length && operation === "sanitize") { throw new import_n8n_workflow.NodeOperationError(this.getNode(), "Failed to sanitize text", { description: (0, import_mappers.mapGuardrailErrorsToMessage)(results.failed), itemIndex }); } if (unexpectedError && !this.continueOnFail()) { const error = unexpectedError.status === "rejected" ? unexpectedError.reason : unexpectedError.value.originalException; throw new import_n8n_workflow.NodeOperationError(this.getNode(), error, { description: error?.description || error?.message, itemIndex }); } return results.failed.map(import_mappers.mapGuardrailResultToUserResult); }; const stageGuardrails = { preflight: [], input: [] }; const checkModelAvailable = (model2) => { if (!model2) { throw new import_n8n_workflow.NodeOperationError(this.getNode(), "Chat Model is required"); } return true; }; if (guardrails.pii?.value) { const { entities } = guardrails.pii.value; stageGuardrails.preflight.push({ name: "personalData", check: (0, import_pii.createPiiCheckFn)({ entities }) }); } if (guardrails.customRegex?.regex) { stageGuardrails.preflight.push({ name: "customRegex", check: (0, import_pii.createCustomRegexCheckFn)({ customRegex: guardrails.customRegex.regex }) }); } if (guardrails.secretKeys?.value) { const { permissiveness } = guardrails.secretKeys.value; stageGuardrails.preflight.push({ name: "secretKeys", check: (0, import_secretKeys.createSecretKeysCheckFn)({ threshold: permissiveness }) }); } if (guardrails.urls?.value) { const { allowedUrls, allowedSchemes, blockUserinfo, allowSubdomains } = guardrails.urls.value; stageGuardrails.preflight.push({ name: "urls", check: (0, import_urls.createUrlsCheckFn)({ allowedUrls: (0, import_common.splitByComma)(allowedUrls), allowedSchemes, blockUserinfo, allowSubdomains }) }); } if (operation === "classify") { if (guardrails.keywords) { stageGuardrails.input.push({ name: "keywords", check: (0, import_keywords.createKeywordsCheckFn)({ keywords: (0, import_common.splitByComma)(guardrails.keywords) }) }); } if (guardrails.jailbreak?.value && checkModelAvailable(model)) { const { prompt, threshold } = guardrails.jailbreak.value; stageGuardrails.input.push({ name: "jailbreak", check: (0, import_jailbreak.createJailbreakCheckFn)({ model, prompt: prompt?.trim() || import_jailbreak.JAILBREAK_PROMPT, threshold, systemMessage }) }); } if (guardrails.nsfw?.value && checkModelAvailable(model)) { const { prompt, threshold } = guardrails.nsfw.value; stageGuardrails.input.push({ name: "nsfw", check: (0, import_nsfw.createNSFWCheckFn)({ model, prompt: prompt?.trim() || import_nsfw.NSFW_SYSTEM_PROMPT, threshold, systemMessage }) }); } if (guardrails.topicalAlignment?.value && checkModelAvailable(model)) { const { prompt, threshold } = guardrails.topicalAlignment.value; stageGuardrails.input.push({ name: "topicalAlignment", check: (0, import_topicalAlignment.createTopicalAlignmentCheckFn)({ model, prompt: prompt?.trim() || import_topicalAlignment.TOPICAL_ALIGNMENT_SYSTEM_PROMPT, systemMessage, threshold }) }); } if (guardrails.custom?.guardrail && checkModelAvailable(model)) { for (const customGuardrail of guardrails.custom.guardrail) { const { prompt, threshold, name } = customGuardrail; stageGuardrails.input.push({ name, check: (0, import_model.createLLMCheckFn)(name, { model, prompt, threshold, systemMessage }) }); } } } const preflightResults = await (0, import_base.runStageGuardrails)({ inputText, stageGuardrails, stage: "preflight", failOnlyOnErrors: operation === "sanitize" }); if (preflightResults.failed.length > 0) { failedChecks.push.apply(failedChecks, handleFailedResults(preflightResults)); return { guardrailsInput: inputText, passed: null, failed: { checks: failedChecks } }; } else { passedChecks.push.apply( passedChecks, preflightResults.passed.map(import_mappers.mapGuardrailResultToUserResult) ); } const modifiedInputText = (0, import_preflight.applyPreflightModifications)( inputText, preflightResults.passed.map((result) => result.value) ); const inputResults = await (0, import_base.runStageGuardrails)({ inputText: modifiedInputText, stageGuardrails, stage: "input", failOnlyOnErrors: operation === "sanitize" }); if (inputResults.failed.length > 0) { failedChecks.push.apply(failedChecks, handleFailedResults(inputResults)); return { guardrailsInput: modifiedInputText, passed: null, failed: { checks: failedChecks } }; } else { passedChecks.push.apply(passedChecks, inputResults.passed.map(import_mappers.mapGuardrailResultToUserResult)); } return { guardrailsInput: modifiedInputText, passed: { checks: passedChecks }, failed: null }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { process }); //# sourceMappingURL=process.js.map