feat(Text Classifier Node): Add output fixing parser (#10667)

This commit is contained in:
Eugene 2024-09-05 09:39:44 +02:00 committed by GitHub
parent 81540318b4
commit aa37c32f26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View file

@ -129,7 +129,8 @@ export class SentimentAnalysis implements INodeType {
name: 'enableAutoFixing',
type: 'boolean',
default: true,
description: 'Whether to enable auto-fixing for the output parser',
description:
'Whether to enable auto-fixing (may trigger an additional LLM call if output is broken)',
},
],
},

View file

@ -12,7 +12,7 @@ import { NodeConnectionType } from 'n8n-workflow';
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { HumanMessage } from '@langchain/core/messages';
import { SystemMessagePromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';
import { StructuredOutputParser } from 'langchain/output_parsers';
import { OutputFixingParser, StructuredOutputParser } from 'langchain/output_parsers';
import { z } from 'zod';
import { getTracingConfig } from '../../../utils/tracing';
@ -151,6 +151,14 @@ export class TextClassifier implements INodeType {
rows: 6,
},
},
{
displayName: 'Enable Auto-Fixing',
name: 'enableAutoFixing',
type: 'boolean',
default: true,
description:
'Whether to enable auto-fixing (may trigger an additional LLM call if output is broken)',
},
],
},
],
@ -173,6 +181,7 @@ export class TextClassifier implements INodeType {
multiClass: boolean;
fallback?: string;
systemPromptTemplate?: string;
enableAutoFixing: boolean;
};
const multiClass = options?.multiClass ?? false;
const fallback = options?.fallback ?? 'discard';
@ -192,7 +201,11 @@ export class TextClassifier implements INodeType {
]);
const schema = z.object(Object.fromEntries(schemaEntries));
const parser = StructuredOutputParser.fromZodSchema(schema);
const structuredParser = StructuredOutputParser.fromZodSchema(schema);
const parser = options.enableAutoFixing
? OutputFixingParser.fromLLM(llm, structuredParser)
: structuredParser;
const multiClassPrompt = multiClass
? 'Categories are not mutually exclusive, and multiple can be true'