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,86 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { OutputFixingParser } from 'langchain/output_parsers';
import type { BaseOutputParser } from 'langchain/schema/output_parser';
import type { BaseLanguageModel } from 'langchain/base_language';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class OutputParserAutofixing implements INodeType {
description: INodeTypeDescription = {
displayName: 'Auto-fixing Output Parser',
name: 'outputParserAutofixing',
icon: 'fa:tools',
group: ['transform'],
version: 1,
description: 'Automatically fix the output if it is not in the correct format',
defaults: {
name: 'Auto-fixing 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.outputparserautofixing/',
},
],
},
},
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
inputs: [
{
displayName: 'Model',
maxConnections: 1,
type: NodeConnectionType.AiLanguageModel,
required: true,
},
{
displayName: 'Output Parser',
maxConnections: 1,
required: true,
type: NodeConnectionType.AiOutputParser,
},
],
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
outputs: [NodeConnectionType.AiOutputParser],
outputNames: ['Output Parser'],
properties: [
{
displayName:
'This node wraps another output parser. If the first one fails it calls an LLM to fix the format',
name: 'info',
type: 'notice',
default: '',
},
getConnectionHintNoticeField([NodeConnectionType.AiChain, NodeConnectionType.AiAgent]),
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const model = (await this.getInputConnectionData(
NodeConnectionType.AiLanguageModel,
itemIndex,
)) as BaseLanguageModel;
const outputParser = (await this.getInputConnectionData(
NodeConnectionType.AiOutputParser,
itemIndex,
)) as BaseOutputParser;
const parser = OutputFixingParser.fromLLM(model, outputParser);
return {
response: logWrapper(parser, this),
};
}
}

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),
};
}
}

View File

@@ -0,0 +1,167 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
jsonParse,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
NodeOperationError,
NodeConnectionType,
} from 'n8n-workflow';
import { parseSchema } from 'json-schema-to-zod';
import { z } from 'zod';
import type { JSONSchema7 } from 'json-schema';
import { StructuredOutputParser } from 'langchain/output_parsers';
import { OutputParserException } from 'langchain/schema/output_parser';
import get from 'lodash/get';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
const STRUCTURED_OUTPUT_KEY = '__structured__output';
const STRUCTURED_OUTPUT_OBJECT_KEY = '__structured__output__object';
const STRUCTURED_OUTPUT_ARRAY_KEY = '__structured__output__array';
class N8nStructuredOutputParser<T extends z.ZodTypeAny> extends StructuredOutputParser<T> {
async parse(text: string): Promise<z.infer<T>> {
try {
const parsed = (await super.parse(text)) as object;
return (
get(parsed, `${STRUCTURED_OUTPUT_KEY}.${STRUCTURED_OUTPUT_OBJECT_KEY}`) ??
get(parsed, `${STRUCTURED_OUTPUT_KEY}.${STRUCTURED_OUTPUT_ARRAY_KEY}`) ??
get(parsed, STRUCTURED_OUTPUT_KEY) ??
parsed
);
} catch (e) {
// eslint-disable-next-line n8n-nodes-base/node-execute-block-wrong-error-thrown
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
}
}
static fromZedJsonSchema(
schema: JSONSchema7,
): StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>> {
// Make sure to remove the description from root schema
const { description, ...restOfSchema } = schema;
const zodSchemaString = parseSchema(restOfSchema as JSONSchema7);
// TODO: This is obviously not great and should be replaced later!!!
// eslint-disable-next-line @typescript-eslint/no-implied-eval
const itemSchema = new Function('z', `return (${zodSchemaString})`)(z) as z.ZodSchema<object>;
const returnSchema = z.object({
[STRUCTURED_OUTPUT_KEY]: z
.object({
[STRUCTURED_OUTPUT_OBJECT_KEY]: itemSchema.optional(),
[STRUCTURED_OUTPUT_ARRAY_KEY]: z.array(itemSchema).optional(),
})
.describe(
`Wrapper around the output data. It can only contain ${STRUCTURED_OUTPUT_OBJECT_KEY} or ${STRUCTURED_OUTPUT_ARRAY_KEY} but never both.`,
)
.refine(
(data) => {
// Validate that one and only one of the properties exists
return (
Boolean(data[STRUCTURED_OUTPUT_OBJECT_KEY]) !==
Boolean(data[STRUCTURED_OUTPUT_ARRAY_KEY])
);
},
{
message:
'One and only one of __structured__output__object and __structured__output__array should be present.',
path: [STRUCTURED_OUTPUT_KEY],
},
),
});
return N8nStructuredOutputParser.fromZodSchema(returnSchema);
}
}
export class OutputParserStructured implements INodeType {
description: INodeTypeDescription = {
displayName: 'Structured Output Parser',
name: 'outputParserStructured',
icon: 'fa:code',
group: ['transform'],
version: 1,
description: 'Return data in a defined JSON format',
defaults: {
name: 'Structured Output Parser',
},
codex: {
alias: ['json', 'zod'],
categories: ['AI'],
subcategories: {
AI: ['Output Parsers'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.outputparserstructured/',
},
],
},
},
// 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: 'JSON Schema',
name: 'jsonSchema',
type: 'json',
description: 'JSON Schema to structure and validate the output against',
default: `{
"type": "object",
"properties": {
"state": {
"type": "string"
},
"cities": {
"type": "array",
"items": {
"type": "string"
}
}
}
}`,
typeOptions: {
rows: 10,
editor: 'json',
editorLanguage: 'json',
},
required: true,
},
{
displayName:
'The schema has to be defined in the <a target="_blank" href="https://json-schema.org/">JSON Schema</a> format. Look at <a target="_blank" href="https://json-schema.org/learn/miscellaneous-examples.html">this</a> page for examples.',
name: 'notice',
type: 'notice',
default: '',
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const schema = this.getNodeParameter('jsonSchema', itemIndex) as string;
let itemSchema: JSONSchema7;
try {
itemSchema = jsonParse<JSONSchema7>(schema);
} catch (error) {
throw new NodeOperationError(this.getNode(), 'Error during parsing of JSON Schema.');
}
const parser = N8nStructuredOutputParser.fromZedJsonSchema(itemSchema);
return {
response: logWrapper(parser, this),
};
}
}