fix(Item List Output Parser Node): Fix number of items parameter issue (#11696)

This commit is contained in:
Eugene
2024-11-12 14:40:46 +01:00
committed by GitHub
parent a025848ec4
commit 01ebe9dd38
2 changed files with 25 additions and 5 deletions

View File

@@ -3,16 +3,21 @@ import { BaseOutputParser, OutputParserException } from '@langchain/core/output_
export class N8nItemListOutputParser extends BaseOutputParser<string[]> {
lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items'];
private numberOfItems: number = 3;
private numberOfItems: number | undefined;
private separator: string;
constructor(options: { numberOfItems?: number; separator?: string }) {
super();
if (options.numberOfItems && options.numberOfItems > 0) {
this.numberOfItems = options.numberOfItems;
const { numberOfItems = 3, separator = '\n' } = options;
if (numberOfItems && numberOfItems > 0) {
this.numberOfItems = numberOfItems;
}
this.separator = options.separator ?? '\\n';
this.separator = separator;
if (this.separator === '\\n') {
this.separator = '\n';
}
@@ -39,7 +44,7 @@ export class N8nItemListOutputParser extends BaseOutputParser<string[]> {
this.numberOfItems ? this.numberOfItems + ' ' : ''
}items separated by`;
const numberOfExamples = this.numberOfItems;
const numberOfExamples = this.numberOfItems ?? 3; // Default number of examples in case numberOfItems is not set
const examples: string[] = [];
for (let i = 1; i <= numberOfExamples; i++) {