feat(Text Classifier Node): Change fallback option to a drop-down (no-changelog) (#10039)

This commit is contained in:
jeanpaul 2024-07-12 17:11:08 +02:00 committed by GitHub
parent 27410ab2af
commit fd833ec079
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,11 +21,11 @@ const SYSTEM_PROMPT_TEMPLATE =
const configuredOutputs = (parameters: INodeParameters) => { const configuredOutputs = (parameters: INodeParameters) => {
const categories = ((parameters.categories as IDataObject)?.categories as IDataObject[]) ?? []; const categories = ((parameters.categories as IDataObject)?.categories as IDataObject[]) ?? [];
const fallback = (parameters.options as IDataObject)?.fallback as boolean; const fallback = (parameters.options as IDataObject)?.fallback as string;
const ret = categories.map((cat) => { const ret = categories.map((cat) => {
return { type: NodeConnectionType.Main, displayName: cat.category }; return { type: NodeConnectionType.Main, displayName: cat.category };
}); });
if (fallback) ret.push({ type: NodeConnectionType.Main, displayName: 'Other' }); if (fallback === 'other') ret.push({ type: NodeConnectionType.Main, displayName: 'Other' });
return ret; return ret;
}; };
@ -122,11 +122,23 @@ export class TextClassifier implements INodeType {
default: false, default: false,
}, },
{ {
displayName: 'Add Fallback Option', displayName: 'When No Clear Match',
name: 'fallback', name: 'fallback',
type: 'boolean', type: 'options',
default: false, default: 'discard',
description: 'Whether to add a "fallback" option if no other categories match', description: 'What to do with items that dont match the categories exactly',
options: [
{
name: 'Discard Item',
value: 'discard',
description: 'Ignore the item and drop it from the output',
},
{
name: "Output on Extra, 'Other' Branch",
value: 'other',
description: "Create a separate output branch called 'Other'",
},
],
}, },
{ {
displayName: 'System Prompt Template', displayName: 'System Prompt Template',
@ -158,11 +170,11 @@ export class TextClassifier implements INodeType {
const options = this.getNodeParameter('options', 0, {}) as { const options = this.getNodeParameter('options', 0, {}) as {
multiClass: boolean; multiClass: boolean;
fallback: boolean; fallback?: string;
systemPromptTemplate?: string; systemPromptTemplate?: string;
}; };
const multiClass = options?.multiClass ?? false; const multiClass = options?.multiClass ?? false;
const fallback = options?.fallback ?? false; const fallback = options?.fallback ?? 'discard';
const schemaEntries = categories.map((cat) => [ const schemaEntries = categories.map((cat) => [
cat.category, cat.category,
@ -172,7 +184,7 @@ export class TextClassifier implements INodeType {
`Should be true if the input has category "${cat.category}" (description: ${cat.description})`, `Should be true if the input has category "${cat.category}" (description: ${cat.description})`,
), ),
]); ]);
if (fallback) if (fallback === 'other')
schemaEntries.push([ schemaEntries.push([
'fallback', 'fallback',
z.boolean().describe('Should be true if none of the other categories apply'), z.boolean().describe('Should be true if none of the other categories apply'),
@ -184,9 +196,11 @@ export class TextClassifier implements INodeType {
const multiClassPrompt = multiClass const multiClassPrompt = multiClass
? 'Categories are not mutually exclusive, and multiple can be true' ? 'Categories are not mutually exclusive, and multiple can be true'
: 'Categories are mutually exclusive, and only one can be true'; : 'Categories are mutually exclusive, and only one can be true';
const fallbackPrompt = fallback
? 'If no categories apply, select the "fallback" option.' const fallbackPrompt = {
: 'One of the options must always be true.'; other: 'If no categories apply, select the "fallback" option.',
discard: 'If there is not a very fitting category, select none of the categories.',
}[fallback];
const systemPromptTemplate = SystemMessagePromptTemplate.fromTemplate( const systemPromptTemplate = SystemMessagePromptTemplate.fromTemplate(
`${options.systemPromptTemplate ?? SYSTEM_PROMPT_TEMPLATE} `${options.systemPromptTemplate ?? SYSTEM_PROMPT_TEMPLATE}
@ -196,7 +210,7 @@ ${fallbackPrompt}`,
); );
const returnData: INodeExecutionData[][] = Array.from( const returnData: INodeExecutionData[][] = Array.from(
{ length: categories.length + (fallback ? 1 : 0) }, { length: categories.length + (fallback === 'other' ? 1 : 0) },
(_) => [], (_) => [],
); );
for (let itemIdx = 0; itemIdx < items.length; itemIdx++) { for (let itemIdx = 0; itemIdx < items.length; itemIdx++) {
@ -216,7 +230,8 @@ ${fallbackPrompt}`,
categories.forEach((cat, idx) => { categories.forEach((cat, idx) => {
if (output[cat.category]) returnData[idx].push(items[itemIdx]); if (output[cat.category]) returnData[idx].push(items[itemIdx]);
}); });
if (fallback && output.fallback) returnData[returnData.length - 1].push(items[itemIdx]); if (fallback === 'other' && output.fallback)
returnData[returnData.length - 1].push(items[itemIdx]);
} }
return returnData; return returnData;
} }