UNPKG

@n8n/n8n-nodes-langchain

Version:
73 lines • 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CONFIDENCE_KEY = exports.FALLBACK_KEY = void 0; exports.findReservedCategory = findReservedCategory; exports.buildClassificationSchema = buildClassificationSchema; exports.toClassificationResult = toClassificationResult; const is_record_1 = require("@n8n/utils/is-record"); const zod_1 = require("zod"); exports.FALLBACK_KEY = 'fallback'; exports.CONFIDENCE_KEY = 'confidence'; const CONFIDENCE_MAP_DESCRIPTION = 'A score from 0.0 to 1.0 for every key above, including the categories you set to false. Score 1.0 when the text says so outright, 0.5 when the text is genuinely ambiguous, and 0.0 when nothing in the text fits. Use the whole range and rank the categories against each other: a category you set to false scores well under 0.5, and the scores must never all be high or all be the same.'; function findReservedCategory(categories) { return categories.find((cat) => cat.category === exports.CONFIDENCE_KEY)?.category; } function clampToScore(value) { if (typeof value !== 'number' || !Number.isFinite(value)) return undefined; return Math.min(1, Math.max(0, value)); } const numericStringToNumber = (value) => typeof value === 'string' && value.trim() !== '' ? Number(value) : value; function scoreField(decisionKey) { return zod_1.z .preprocess(numericStringToNumber, zod_1.z.number()) .nullish() .catch(undefined) .describe(decisionKey === exports.FALLBACK_KEY ? 'How poorly the text fits every category above: 1.0 when no category applies at all, 0.0 when one category clearly applies' : `How much of the text supports category "${decisionKey}"`); } function buildClassificationSchema(categories, withFallback, withConfidence = false) { const entries = categories.map((cat) => [ cat.category, zod_1.z .boolean() .describe(`Should be true if the input has category "${cat.category}" (description: ${cat.description})`), ]); if (withFallback) { entries.push([ exports.FALLBACK_KEY, zod_1.z.boolean().describe('Should be true if none of the other categories apply'), ]); } if (withConfidence) { const decisionKeys = entries.map(([key]) => key); entries.push([ exports.CONFIDENCE_KEY, zod_1.z .object(Object.fromEntries(decisionKeys.map((key) => [key, scoreField(key)]))) .nullish() .catch(undefined) .describe(CONFIDENCE_MAP_DESCRIPTION), ]); } return zod_1.z.object(Object.fromEntries(entries)); } function toClassificationResult(raw, categories) { const answer = (0, is_record_1.isRecord)(raw) ? raw : {}; const reportedScores = answer[exports.CONFIDENCE_KEY]; const scores = {}; if ((0, is_record_1.isRecord)(reportedScores)) { for (const [key, value] of Object.entries(reportedScores)) { const score = clampToScore(value); if (score !== undefined) scores[key] = score; } } return { matched: categories.filter((cat) => answer[cat.category] === true).map((cat) => cat.category), fallback: answer[exports.FALLBACK_KEY] === true, ...(Object.keys(scores).length > 0 && { scores }), }; } //# sourceMappingURL=classification.js.map