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,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