feat: Add AI tool building capabilities (#7336)

Github issue / Community forum post (link here to close automatically):
https://community.n8n.io/t/langchain-memory-chat/23733

---------

Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Val <68596159+valya@users.noreply.github.com>
Co-authored-by: Alex Grozav <alex@grozav.com>
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: Deborah <deborah@starfallprojects.co.uk>
Co-authored-by: Jesper Bylund <mail@jesperbylund.com>
Co-authored-by: Jon <jonathan.bennetts@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: Giulio Andreini <andreini@netseven.it>
Co-authored-by: Mason Geloso <Mason.geloso@gmail.com>
Co-authored-by: Mason Geloso <hone@Masons-Mac-mini.local>
Co-authored-by: Mutasem Aldmour <mutasem@n8n.io>
This commit is contained in:
Jan Oberhauser
2023-11-29 12:13:55 +01:00
committed by GitHub
parent dbfd617ace
commit 87def60979
243 changed files with 21526 additions and 321 deletions

View File

@@ -0,0 +1,51 @@
import { BaseOutputParser, OutputParserException } from 'langchain/schema/output_parser';
export class ItemListOutputParser extends BaseOutputParser<string[]> {
lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items'];
private numberOfItems: number | undefined;
private separator: string;
constructor(options: { numberOfItems?: number; separator?: string }) {
super();
if (options.numberOfItems && options.numberOfItems > 0) {
this.numberOfItems = options.numberOfItems;
}
this.separator = options.separator ?? '\\n';
if (this.separator === '\\n') {
this.separator = '\n';
}
}
async parse(text: string): Promise<string[]> {
const response = text
.split(this.separator)
.map((item) => item.trim())
.filter((item) => item);
if (this.numberOfItems && response.length < this.numberOfItems) {
// Only error if to few items got returned, if there are to many we can autofix it
throw new OutputParserException(
`Wrong number of items returned. Expected ${this.numberOfItems} items but got ${response.length} items instead.`,
);
}
return response.slice(0, this.numberOfItems);
}
getFormatInstructions(): string {
const instructions = `Your response should be a list of ${
this.numberOfItems ? this.numberOfItems + ' ' : ''
}items separated by`;
const numberOfExamples = this.numberOfItems ?? 3;
const examples: string[] = [];
for (let i = 1; i <= numberOfExamples; i++) {
examples.push(`item${i}`);
}
return `${instructions} "${this.separator}" (for example: "${examples.join(this.separator)}")`;
}
}

View File

@@ -0,0 +1,95 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
import { ItemListOutputParser } from './ItemListOutputParser';
export class OutputParserItemList implements INodeType {
description: INodeTypeDescription = {
displayName: 'Item List Output Parser',
name: 'outputParserItemList',
icon: 'fa:bars',
group: ['transform'],
version: 1,
description: 'Return the results as separate items',
defaults: {
name: 'Item List Output Parser',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Output Parsers'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.outputparseritemlist/',
},
],
},
},
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
inputs: [],
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
outputs: [NodeConnectionType.AiOutputParser],
outputNames: ['Output Parser'],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiChain, NodeConnectionType.AiAgent]),
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Number Of Items',
name: 'numberOfItems',
type: 'number',
default: -1,
description:
'Defines many many items should be returned maximally. If set to -1, there is no limit.',
},
// For that to be easily possible the metadata would have to be returned and be able to be read.
// Would also be possible with a wrapper but that would be even more hacky and the output types
// would not be correct anymore.
// {
// displayName: 'Parse Output',
// name: 'parseOutput',
// type: 'boolean',
// default: true,
// description: 'Whether the output should be automatically be parsed or left RAW',
// },
{
displayName: 'Separator',
name: 'separator',
type: 'string',
default: '\\n',
description:
'Defines the separator that should be used to split the results into separate items. Defaults to a new line but can be changed depending on the data that should be returned.',
},
],
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const options = this.getNodeParameter('options', itemIndex, {}) as {
numberOfItems?: number;
separator?: string;
};
const parser = new ItemListOutputParser(options);
return {
response: logWrapper(parser, this),
};
}
}