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,133 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import type { BufferWindowMemoryInput } from 'langchain/memory';
import { BufferWindowMemory } from 'langchain/memory';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
class MemoryChatBufferSingleton {
private static instance: MemoryChatBufferSingleton;
private memoryBuffer: Map<
string,
{ buffer: BufferWindowMemory; created: Date; last_accessed: Date }
>;
private constructor() {
this.memoryBuffer = new Map();
}
public static getInstance(): MemoryChatBufferSingleton {
if (!MemoryChatBufferSingleton.instance) {
MemoryChatBufferSingleton.instance = new MemoryChatBufferSingleton();
}
return MemoryChatBufferSingleton.instance;
}
public async getMemory(
sessionKey: string,
memoryParams: BufferWindowMemoryInput,
): Promise<BufferWindowMemory> {
await this.cleanupStaleBuffers();
let memoryInstance = this.memoryBuffer.get(sessionKey);
if (memoryInstance) {
memoryInstance.last_accessed = new Date();
} else {
const newMemory = new BufferWindowMemory(memoryParams);
memoryInstance = {
buffer: newMemory,
created: new Date(),
last_accessed: new Date(),
};
this.memoryBuffer.set(sessionKey, memoryInstance);
}
return memoryInstance.buffer;
}
private async cleanupStaleBuffers(): Promise<void> {
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
for (const [key, memoryInstance] of this.memoryBuffer.entries()) {
if (memoryInstance.last_accessed < oneHourAgo) {
await this.memoryBuffer.get(key)?.buffer.clear();
this.memoryBuffer.delete(key);
}
}
}
}
export class MemoryBufferWindow implements INodeType {
description: INodeTypeDescription = {
displayName: 'Window Buffer Memory (easiest)',
name: 'memoryBufferWindow',
icon: 'fa:database',
group: ['transform'],
version: 1,
description: 'Stores in n8n memory, so no credentials required',
defaults: {
name: 'Window Buffer Memory',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Memory'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorybufferwindow/',
},
],
},
},
// 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.AiMemory],
outputNames: ['Memory'],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiAgent]),
{
displayName: 'Session Key',
name: 'sessionKey',
type: 'string',
default: 'chat_history',
description: 'The key to use to store the memory in the workflow data',
},
{
displayName: 'Context Window Length',
name: 'contextWindowLength',
type: 'number',
default: 5,
description: 'The number of previous messages to consider for context',
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const sessionKey = this.getNodeParameter('sessionKey', itemIndex) as string;
const contextWindowLength = this.getNodeParameter('contextWindowLength', itemIndex) as number;
const workflowId = this.getWorkflow().id;
const memoryInstance = MemoryChatBufferSingleton.getInstance();
const memory = await memoryInstance.getMemory(`${workflowId}__${sessionKey}`, {
k: contextWindowLength,
inputKey: 'input',
memoryKey: 'chat_history',
outputKey: 'output',
returnMessages: true,
});
return {
response: logWrapper(memory, this),
};
}
}

View File

@@ -0,0 +1,104 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IDataObject,
type IExecuteFunctions,
type INodeExecutionData,
type INodeType,
type INodeTypeDescription,
} from 'n8n-workflow';
import type { BaseChatMemory } from 'langchain/memory';
import type { BaseMessage } from 'langchain/schema';
function simplifyMessages(messages: BaseMessage[]) {
const chunkedMessages = [];
for (let i = 0; i < messages.length; i += 2) {
chunkedMessages.push([messages[i], messages[i + 1]]);
}
const transformedMessages = chunkedMessages.map((exchange) => {
const simplified = {
[exchange[0]._getType()]: exchange[0].content,
};
if (exchange[1]) {
simplified[exchange[1]._getType()] = exchange[1].content;
}
return {
json: simplified,
};
});
return transformedMessages;
}
export class MemoryChatRetriever implements INodeType {
description: INodeTypeDescription = {
displayName: 'Chat Messages Retriever',
name: 'memoryChatRetriever',
icon: 'fa:database',
group: ['transform'],
version: 1,
description: 'Retrieve chat messages from memory and use them in the workflow',
defaults: {
name: 'Chat Messages Retriever',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Miscellaneous'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorychatretriever/',
},
],
},
},
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
inputs: [
NodeConnectionType.Main,
{
displayName: 'Memory',
maxConnections: 1,
type: NodeConnectionType.AiMemory,
required: true,
},
],
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
outputs: [NodeConnectionType.Main],
properties: [
{
displayName: 'Simplify Output',
name: 'simplifyOutput',
type: 'boolean',
description: 'Whether to simplify the output to only include the sender and the text',
default: true,
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
this.logger.verbose('Executing Chat Memory Retriever');
const memory = (await this.getInputConnectionData(NodeConnectionType.AiMemory, 0)) as
| BaseChatMemory
| undefined;
const simplifyOutput = this.getNodeParameter('simplifyOutput', 0) as boolean;
const messages = await memory?.chatHistory.getMessages();
if (simplifyOutput && messages) {
return this.prepareOutputData(simplifyMessages(messages));
}
const serializedMessages =
messages?.map((message) => {
const serializedMessage = message.toJSON();
return { json: serializedMessage as unknown as IDataObject };
}) ?? [];
return this.prepareOutputData(serializedMessages);
}
}

View File

@@ -0,0 +1,81 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { MotorheadMemory } from 'langchain/memory';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class MemoryMotorhead implements INodeType {
description: INodeTypeDescription = {
displayName: 'Motorhead',
name: 'memoryMotorhead',
icon: 'fa:file-export',
group: ['transform'],
version: 1,
description: 'Use Motorhead Memory',
defaults: {
name: 'Motorhead',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Memory'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorymotorhead/',
},
],
},
},
// 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.AiMemory],
outputNames: ['Memory'],
credentials: [
{
name: 'motorheadApi',
required: true,
},
],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiAgent]),
{
displayName: 'Session ID',
name: 'sessionId',
type: 'string',
required: true,
default: '',
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('motorheadApi');
const sessionId = this.getNodeParameter('sessionId', itemIndex) as string;
const memory = new MotorheadMemory({
sessionId,
url: `${credentials.host as string}/motorhead`,
clientId: credentials.clientId as string,
apiKey: credentials.apiKey as string,
memoryKey: 'chat_history',
returnMessages: true,
});
await memory.init();
return {
response: logWrapper(memory, this),
};
}
}

View File

@@ -0,0 +1,121 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeOperationError,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
NodeConnectionType,
} from 'n8n-workflow';
import { BufferMemory } from 'langchain/memory';
import type { RedisChatMessageHistoryInput } from 'langchain/stores/message/redis';
import { RedisChatMessageHistory } from 'langchain/stores/message/redis';
import type { RedisClientOptions } from 'redis';
import { createClient } from 'redis';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class MemoryRedisChat implements INodeType {
description: INodeTypeDescription = {
displayName: 'Redis Chat Memory',
name: 'memoryRedisChat',
icon: 'file:redis.svg',
group: ['transform'],
version: 1,
description: 'Stores the chat history in Redis.',
defaults: {
name: 'Redis Chat Memory',
},
credentials: [
{
name: 'redis',
required: true,
},
],
codex: {
categories: ['AI'],
subcategories: {
AI: ['Memory'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryredischat/',
},
],
},
},
// 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.AiMemory],
outputNames: ['Memory'],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiAgent]),
{
displayName: 'Session Key',
name: 'sessionKey',
type: 'string',
default: 'chat_history',
description: 'The key to use to store the memory in the workflow data',
},
{
displayName: 'Session Time To Live',
name: 'sessionTTL',
type: 'number',
default: 0,
description:
'For how long the session should be stored in seconds. If set to 0 it will not expire.',
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('redis');
const sessionKey = this.getNodeParameter('sessionKey', itemIndex) as string;
const sessionTTL = this.getNodeParameter('sessionTTL', itemIndex, 0) as number;
const redisOptions: RedisClientOptions = {
socket: {
host: credentials.host as string,
port: credentials.port as number,
},
database: credentials.database as number,
};
if (credentials.password) {
redisOptions.password = credentials.password as string;
}
const client = createClient({
...redisOptions,
});
client.on('error', async (error: Error) => {
await client.quit();
throw new NodeOperationError(this.getNode(), 'Redis Error: ' + error.message);
});
const redisChatConfig: RedisChatMessageHistoryInput = {
client,
sessionId: sessionKey,
};
if (sessionTTL > 0) {
redisChatConfig.sessionTTL = sessionTTL;
}
const redisChatHistory = new RedisChatMessageHistory(redisChatConfig);
const memory = new BufferMemory({
memoryKey: 'chat_history',
chatHistory: redisChatHistory,
returnMessages: true,
inputKey: 'input',
outputKey: 'output',
});
return {
response: logWrapper(memory, this),
};
}
}

View File

@@ -0,0 +1 @@
<svg width="60" height="60" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"><path d="M57.656 43.99c-3.201 1.683-19.787 8.561-23.318 10.417-3.532 1.856-5.494 1.838-8.283.494-2.79-1.343-20.449-8.535-23.629-10.067C.834 44.066.002 43.422.002 42.811v-6.117s22.98-5.045 26.69-6.388c3.711-1.342 4.995-1.39 8.154-.225 3.16 1.165 22.035 4.603 25.154 5.756v6.032c0 .605-.72 1.283-2.35 2.124l.006-.003z" fill="#A41E11"/><path d="M57.656 37.872c-3.201 1.685-19.787 8.56-23.318 10.417-3.532 1.856-5.494 1.838-8.283.494-2.79-1.343-20.449-8.534-23.63-10.068-3.18-1.533-3.243-2.588-.122-3.82l24.388-9.52c3.71-1.34 4.994-1.39 8.153-.225 3.16 1.165 19.643 7.78 22.747 8.951 3.103 1.17 3.24 2.086.037 3.786l.028-.015z" fill="#D82C20"/><path d="M57.656 34.015c-3.201 1.683-19.787 8.561-23.318 10.417-3.532 1.856-5.494 1.838-8.283.495-2.79-1.344-20.449-8.536-23.629-10.067C.834 34.092.002 33.447.002 32.836V26.72s22.98-5.045 26.69-6.387c3.711-1.343 4.995-1.39 8.154-.225 3.16 1.165 22.035 4.602 25.154 5.756v6.032c0 .605-.72 1.283-2.35 2.123l.006-.003z" fill="#A41E11"/><path d="M57.656 27.898c-3.201 1.685-19.787 8.561-23.318 10.417-3.532 1.856-5.494 1.838-8.283.495-2.79-1.344-20.449-8.534-23.63-10.067-3.18-1.534-3.243-2.588-.122-3.82l24.388-9.52c3.71-1.343 4.994-1.39 8.153-.225 3.16 1.166 19.644 7.785 22.765 8.935 3.121 1.15 3.24 2.085.038 3.785h.01z" fill="#D82C20"/><path d="M57.656 23.671c-3.201 1.683-19.787 8.561-23.318 10.419-3.532 1.858-5.494 1.838-8.283.495-2.79-1.344-20.449-8.535-23.629-10.069-1.592-.765-2.424-1.411-2.424-2.02v-6.11s22.98-5.045 26.69-6.388c3.711-1.343 4.995-1.39 8.154-.225 3.16 1.165 22.035 4.591 25.154 5.745v6.032c0 .605-.72 1.283-2.35 2.123l.006-.002z" fill="#A41E11"/><path d="M57.656 17.553c-3.201 1.685-19.787 8.561-23.318 10.417-3.532 1.856-5.494 1.838-8.283.495-2.79-1.344-20.449-8.534-23.63-10.068-3.18-1.533-3.243-2.587-.122-3.82l24.388-9.52c3.71-1.343 4.994-1.39 8.153-.226 3.16 1.165 19.643 7.785 22.765 8.936 3.122 1.15 3.24 2.085.038 3.785l.01.001z" fill="#D82C20"/><path d="M31.497 15.032l-1.88-3.153-6.002-.545 4.48-1.63L26.75 7.2l4.192 1.653 3.955-1.305-1.07 2.586 4.032 1.524-5.198.546-1.164 2.827zm-10.014 6.275l13.903-2.153-4.2 6.211-9.703-4.058zm-11.17-5.167c0-1.61 3.314-2.906 7.431-2.906 4.118 0 7.432 1.296 7.432 2.906s-3.314 2.905-7.432 2.905c-4.117 0-7.431-1.295-7.431-2.905z" fill="#FFF"/><path fill="#7A0C00" d="M52.233 15.714l-8.224 3.276-.007-6.556z"/><path fill="#AD2115" d="M44.01 18.991l-.89.353-8.217-3.276 9.094-3.63z"/></g></svg>

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@@ -0,0 +1,94 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow';
import { XataChatMessageHistory } from 'langchain/stores/message/xata';
import { BufferMemory } from 'langchain/memory';
import { BaseClient } from '@xata.io/client';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class MemoryXata implements INodeType {
description: INodeTypeDescription = {
displayName: 'Xata',
name: 'memoryXata',
icon: 'file:xata.svg',
group: ['transform'],
version: 1,
description: 'Use Xata Memory',
defaults: {
name: 'Xata',
// eslint-disable-next-line n8n-nodes-base/node-class-description-non-core-color-present
color: '#1321A7',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Memory'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryxata/',
},
],
},
},
// 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.AiMemory],
outputNames: ['Memory'],
credentials: [
{
name: 'xataApi',
required: true,
},
],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiAgent]),
{
displayName: 'Session ID',
name: 'sessionId',
type: 'string',
required: true,
default: '',
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('xataApi');
const xataClient = new BaseClient({
apiKey: credentials.apiKey as string,
branch: (credentials.branch as string) || 'main',
databaseURL: credentials.databaseEndpoint as string,
});
const sessionId = this.getNodeParameter('sessionId', itemIndex) as string;
const table = (credentials.databaseEndpoint as string).match(
/https:\/\/[^.]+\.[^.]+\.xata\.sh\/db\/([^\/:]+)/,
);
if (table === null) {
throw new NodeOperationError(
this.getNode(),
'It was not possible to extract the table from the Database Endpoint.',
);
}
const memory = new BufferMemory({
chatHistory: new XataChatMessageHistory({
table: table[1],
sessionId,
client: xataClient,
apiKey: credentials.apiKey as string,
}),
memoryKey: 'chat_history',
returnMessages: true,
});
return {
response: logWrapper(memory, this),
};
}
}

View File

@@ -0,0 +1 @@
<svg width="1600" height="1600" viewBox="0 0 1600 1600" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1250.12 576.498c-.11 89.997-36 176.267-99.79 239.83l-.01-.007-226.282 225.489c-7.841 7.82-20.58 7.84-27.927-.44-55.015-61.995-85.587-142.175-85.49-225.478.106-89.997 36-176.267 99.787-239.83l.007.007 206.745-206.014c18.63-18.569 49.12-18.702 64.92 2.324 43.99 58.525 68.12 130.089 68.04 204.119zM440.552 817.702c-63.787-63.563-99.682-149.833-99.787-239.83-.087-74.03 24.048-145.594 68.035-204.119 15.803-21.026 46.294-20.893 64.928-2.324l206.741 206.016.006-.007c63.787 63.564 99.681 149.833 99.787 239.831.097 83.302-30.475 163.483-85.49 225.471-7.347 8.28-20.086 8.26-27.927.45L440.558 817.696l-.006.006zM1141.82 1221.19c-16.63 20.39-47.04 20.21-65.63 1.59l-127.698-127.84c-7.836-7.85-7.821-20.56.033-28.39l212.095-211.345c7.84-7.813 20.62-7.859 27.54.784 36.81 45.996 51.29 109.566 40.34 179.551-10.01 64.06-40.65 129.19-86.68 185.65zM514.696 1224.16c-18.594 18.61-49.002 18.79-65.626-1.6-46.036-56.46-76.672-121.58-86.687-185.64-10.943-69.992 3.531-133.562 40.342-179.558 6.916-8.642 19.703-8.597 27.544-.784l212.092 211.352c7.854 7.82 7.868 20.54.033 28.38l-127.698 127.85z" fill="#7D7D87"/></svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@@ -0,0 +1,84 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { ZepMemory } from 'langchain/memory/zep';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class MemoryZep implements INodeType {
description: INodeTypeDescription = {
displayName: 'Zep',
name: 'memoryZep',
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
icon: 'file:zep.png',
group: ['transform'],
version: 1,
description: 'Use Zep Memory',
defaults: {
name: 'Zep',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Memory'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryzep/',
},
],
},
},
// 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.AiMemory],
outputNames: ['Memory'],
credentials: [
{
name: 'zepApi',
required: true,
},
],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiAgent]),
{
displayName: 'Session ID',
name: 'sessionId',
type: 'string',
required: true,
default: '',
},
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = (await this.getCredentials('zepApi')) as {
apiKey?: string;
apiUrl: string;
};
// TODO: Should it get executed once per item or not?
const sessionId = this.getNodeParameter('sessionId', itemIndex) as string;
const memory = new ZepMemory({
sessionId,
baseURL: credentials.apiUrl,
apiKey: credentials.apiKey,
memoryKey: 'chat_history',
returnMessages: true,
inputKey: 'input',
outputKey: 'output',
});
return {
response: logWrapper(memory, this),
};
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB