From 1a8b4a1a5cc13410cf946319bb68d975f432a50c Mon Sep 17 00:00:00 2001 From: JP van Oosten Date: Mon, 22 Jul 2024 10:22:11 +0200 Subject: [PATCH 1/5] Convert description to hint for memory buffer window sub-node --- .../nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts index 2b7e205de63fb..409cb6c839dbb 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts @@ -135,7 +135,7 @@ export class MemoryBufferWindow implements INodeType { name: 'contextWindowLength', type: 'number', default: 5, - description: 'The number of previous messages to consider for context', + hint: 'How many past interactions the model receives as context', }, ], }; From 65a70c61579c2969a05b605235c76a2494b64f5d Mon Sep 17 00:00:00 2001 From: JP van Oosten Date: Thu, 25 Jul 2024 17:38:30 +0200 Subject: [PATCH 2/5] Add context window length options to redis, postgres and xata memory --- .../MemoryPostgresChat.node.ts | 25 ++++++++++++++++--- .../MemoryRedisChat/MemoryRedisChat.node.ts | 25 ++++++++++++++++--- .../memory/MemoryXata/MemoryXata.node.ts | 25 ++++++++++++++++--- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts index ea3ed3c33ea6b..ee5170a1de43e 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts @@ -1,7 +1,7 @@ /* eslint-disable n8n-nodes-base/node-dirname-against-convention */ import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow'; import { NodeConnectionType } from 'n8n-workflow'; -import { BufferMemory } from 'langchain/memory'; +import { BufferMemory, BufferWindowMemory } from 'langchain/memory'; import { PostgresChatMessageHistory } from '@langchain/community/stores/message/postgres'; import type pg from 'pg'; import { configurePostgres } from 'n8n-nodes-base/dist/nodes/Postgres/v2/transport'; @@ -18,7 +18,7 @@ export class MemoryPostgresChat implements INodeType { name: 'memoryPostgresChat', icon: 'file:postgres.svg', group: ['transform'], - version: [1], + version: [1, 1.1], description: 'Stores the chat history in Postgres table.', defaults: { name: 'Postgres Chat Memory', @@ -60,6 +60,18 @@ export class MemoryPostgresChat implements INodeType { description: 'The table name to store the chat history in. If table does not exist, it will be created.', }, + { + displayName: 'Context Window Length', + name: 'contextWindowLength', + type: 'number', + default: 5, + hint: 'How many past interactions the model receives as context', + displayOptions: { + hide: { + '@version': [{ _cnd: { lt: 1.1 } }], + }, + }, + }, ], }; @@ -83,12 +95,19 @@ export class MemoryPostgresChat implements INodeType { tableName, }); - const memory = new BufferMemory({ + const memClass = this.getNode().typeVersion < 1.1 ? BufferMemory : BufferWindowMemory; + const kOptions = + this.getNode().typeVersion < 1.1 + ? {} + : { k: this.getNodeParameter('contextWindowLength', itemIndex) }; + + const memory = new memClass({ memoryKey: 'chat_history', chatHistory: pgChatHistory, returnMessages: true, inputKey: 'input', outputKey: 'output', + ...kOptions, }); async function closeFunction() { diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts index d139bd31e365a..e8b8894633678 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts @@ -7,7 +7,7 @@ import { type SupplyData, NodeConnectionType, } from 'n8n-workflow'; -import { BufferMemory } from 'langchain/memory'; +import { BufferMemory, BufferWindowMemory } from 'langchain/memory'; import type { RedisChatMessageHistoryInput } from '@langchain/redis'; import { RedisChatMessageHistory } from '@langchain/redis'; import type { RedisClientOptions } from 'redis'; @@ -23,7 +23,7 @@ export class MemoryRedisChat implements INodeType { name: 'memoryRedisChat', icon: 'file:redis.svg', group: ['transform'], - version: [1, 1.1, 1.2], + version: [1, 1.1, 1.2, 1.3], description: 'Stores the chat history in Redis.', defaults: { name: 'Redis Chat Memory', @@ -95,6 +95,18 @@ export class MemoryRedisChat implements INodeType { description: 'For how long the session should be stored in seconds. If set to 0 it will not expire.', }, + { + displayName: 'Context Window Length', + name: 'contextWindowLength', + type: 'number', + default: 5, + hint: 'How many past interactions the model receives as context', + displayOptions: { + hide: { + '@version': [{ _cnd: { lt: 1.3 } }], + }, + }, + }, ], }; @@ -143,12 +155,19 @@ export class MemoryRedisChat implements INodeType { } const redisChatHistory = new RedisChatMessageHistory(redisChatConfig); - const memory = new BufferMemory({ + const memClass = this.getNode().typeVersion < 1.1 ? BufferMemory : BufferWindowMemory; + const kOptions = + this.getNode().typeVersion < 1.1 + ? {} + : { k: this.getNodeParameter('contextWindowLength', itemIndex) }; + + const memory = new memClass({ memoryKey: 'chat_history', chatHistory: redisChatHistory, returnMessages: true, inputKey: 'input', outputKey: 'output', + ...kOptions, }); async function closeFunction() { diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts index e5c9dc4c35611..eecb239226a14 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts @@ -2,7 +2,7 @@ import { NodeConnectionType, NodeOperationError } from 'n8n-workflow'; import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow'; import { XataChatMessageHistory } from '@langchain/community/stores/message/xata'; -import { BufferMemory } from 'langchain/memory'; +import { BufferMemory, BufferWindowMemory } from 'langchain/memory'; import { BaseClient } from '@xata.io/client'; import { logWrapper } from '../../../utils/logWrapper'; import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; @@ -15,7 +15,7 @@ export class MemoryXata implements INodeType { name: 'memoryXata', icon: 'file:xata.svg', group: ['transform'], - version: [1, 1.1, 1.2], + version: [1, 1.1, 1.2, 1.3], description: 'Use Xata Memory', defaults: { name: 'Xata', @@ -81,6 +81,18 @@ export class MemoryXata implements INodeType { }, }, sessionKeyProperty, + { + displayName: 'Context Window Length', + name: 'contextWindowLength', + type: 'number', + default: 5, + hint: 'How many past interactions the model receives as context', + displayOptions: { + hide: { + '@version': [{ _cnd: { lt: 1.3 } }], + }, + }, + } ], }; @@ -120,12 +132,19 @@ export class MemoryXata implements INodeType { apiKey: credentials.apiKey as string, }); - const memory = new BufferMemory({ + const memClass = this.getNode().typeVersion < 1.3 ? BufferMemory : BufferWindowMemory; + const kOptions = + this.getNode().typeVersion < 1.3 + ? {} + : { k: this.getNodeParameter('contextWindowLength', itemIndex) }; + + const memory = new memClass({ chatHistory, memoryKey: 'chat_history', returnMessages: true, inputKey: 'input', outputKey: 'output', + ...kOptions, }); return { From d9ae913629d90e27077102e2e4ac442f31e2b19d Mon Sep 17 00:00:00 2001 From: JP van Oosten Date: Thu, 25 Jul 2024 18:36:11 +0200 Subject: [PATCH 3/5] Move context window length property to a shared function --- .../MemoryBufferWindow/MemoryBufferWindow.node.ts | 10 ++-------- .../MemoryPostgresChat/MemoryPostgresChat.node.ts | 15 ++------------- .../MemoryRedisChat/MemoryRedisChat.node.ts | 15 ++------------- .../nodes/memory/MemoryXata/MemoryXata.node.ts | 15 ++------------- .../nodes-langchain/nodes/memory/descriptions.ts | 13 ++++++++++++- 5 files changed, 20 insertions(+), 48 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts index 409cb6c839dbb..58495353de169 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts @@ -10,7 +10,7 @@ import type { BufferWindowMemoryInput } from 'langchain/memory'; import { BufferWindowMemory } from 'langchain/memory'; import { logWrapper } from '../../../utils/logWrapper'; import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; -import { sessionIdOption, sessionKeyProperty } from '../descriptions'; +import { sessionIdOption, sessionKeyProperty, contextWindowLengthProperty } from '../descriptions'; import { getSessionId } from '../../../utils/helpers'; class MemoryChatBufferSingleton { @@ -130,13 +130,7 @@ export class MemoryBufferWindow implements INodeType { }, }, sessionKeyProperty, - { - displayName: 'Context Window Length', - name: 'contextWindowLength', - type: 'number', - default: 5, - hint: 'How many past interactions the model receives as context', - }, + contextWindowLengthProperty(), ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts index ee5170a1de43e..9f3626e392cd1 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts @@ -9,7 +9,7 @@ import type { PostgresNodeCredentials } from 'n8n-nodes-base/dist/nodes/Postgres import { postgresConnectionTest } from 'n8n-nodes-base/dist/nodes/Postgres/v2/methods/credentialTest'; import { logWrapper } from '../../../utils/logWrapper'; import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; -import { sessionIdOption, sessionKeyProperty } from '../descriptions'; +import { sessionIdOption, sessionKeyProperty, contextWindowLengthProperty } from '../descriptions'; import { getSessionId } from '../../../utils/helpers'; export class MemoryPostgresChat implements INodeType { @@ -60,18 +60,7 @@ export class MemoryPostgresChat implements INodeType { description: 'The table name to store the chat history in. If table does not exist, it will be created.', }, - { - displayName: 'Context Window Length', - name: 'contextWindowLength', - type: 'number', - default: 5, - hint: 'How many past interactions the model receives as context', - displayOptions: { - hide: { - '@version': [{ _cnd: { lt: 1.1 } }], - }, - }, - }, + contextWindowLengthProperty({ hide: { '@version': [{ _cnd: { lt: 1.1 } }] } }), ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts index e8b8894633678..e4a435cf08729 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts @@ -14,7 +14,7 @@ import type { RedisClientOptions } from 'redis'; import { createClient } from 'redis'; import { logWrapper } from '../../../utils/logWrapper'; import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; -import { sessionIdOption, sessionKeyProperty } from '../descriptions'; +import { sessionIdOption, sessionKeyProperty, contextWindowLengthProperty } from '../descriptions'; import { getSessionId } from '../../../utils/helpers'; export class MemoryRedisChat implements INodeType { @@ -95,18 +95,7 @@ export class MemoryRedisChat implements INodeType { description: 'For how long the session should be stored in seconds. If set to 0 it will not expire.', }, - { - displayName: 'Context Window Length', - name: 'contextWindowLength', - type: 'number', - default: 5, - hint: 'How many past interactions the model receives as context', - displayOptions: { - hide: { - '@version': [{ _cnd: { lt: 1.3 } }], - }, - }, - }, + contextWindowLengthProperty({ hide: { '@version': [{ _cnd: { lt: 1.3 } }] } }), ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts index eecb239226a14..dc8702854c645 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts @@ -6,7 +6,7 @@ import { BufferMemory, BufferWindowMemory } from 'langchain/memory'; import { BaseClient } from '@xata.io/client'; import { logWrapper } from '../../../utils/logWrapper'; import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; -import { sessionIdOption, sessionKeyProperty } from '../descriptions'; +import { sessionIdOption, sessionKeyProperty, contextWindowLengthProperty } from '../descriptions'; import { getSessionId } from '../../../utils/helpers'; export class MemoryXata implements INodeType { @@ -81,18 +81,7 @@ export class MemoryXata implements INodeType { }, }, sessionKeyProperty, - { - displayName: 'Context Window Length', - name: 'contextWindowLength', - type: 'number', - default: 5, - hint: 'How many past interactions the model receives as context', - displayOptions: { - hide: { - '@version': [{ _cnd: { lt: 1.3 } }], - }, - }, - } + contextWindowLengthProperty({ hide: { '@version': [{ _cnd: { lt: 1.3 } }] } }), ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts b/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts index 5f722c4647828..28f7296006f13 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts @@ -1,4 +1,4 @@ -import type { INodeProperties } from 'n8n-workflow'; +import type { INodeProperties, IDisplayOptions } from 'n8n-workflow'; export const sessionIdOption: INodeProperties = { displayName: 'Session ID', @@ -33,3 +33,14 @@ export const sessionKeyProperty: INodeProperties = { }, }, }; + +export function contextWindowLengthProperty(displayOptions?: IDisplayOptions): INodeProperties { + return { + displayName: 'Context Window Length', + name: 'contextWindowLength', + type: 'number', + default: 5, + hint: 'How many past interactions the model receives as context', + displayOptions: displayOptions ?? {}, + }; +} From 68a51a6cd9df392f81e6b8cf51a54465a04d346e Mon Sep 17 00:00:00 2001 From: JP van Oosten Date: Mon, 5 Aug 2024 17:03:57 +0200 Subject: [PATCH 4/5] Use simple object instead of function; use correct version in redis node --- .../MemoryBufferWindow.node.ts | 2 +- .../MemoryPostgresChat.node.ts | 5 ++++- .../MemoryRedisChat/MemoryRedisChat.node.ts | 9 ++++++--- .../nodes/memory/MemoryXata/MemoryXata.node.ts | 5 ++++- .../nodes/memory/descriptions.ts | 17 +++++++---------- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts index 58495353de169..b8eea7a5e2c82 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryBufferWindow/MemoryBufferWindow.node.ts @@ -130,7 +130,7 @@ export class MemoryBufferWindow implements INodeType { }, }, sessionKeyProperty, - contextWindowLengthProperty(), + contextWindowLengthProperty, ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts index 9f3626e392cd1..b1a9cd7aeaec5 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts @@ -60,7 +60,10 @@ export class MemoryPostgresChat implements INodeType { description: 'The table name to store the chat history in. If table does not exist, it will be created.', }, - contextWindowLengthProperty({ hide: { '@version': [{ _cnd: { lt: 1.1 } }] } }), + { + ...contextWindowLengthProperty, + displayOptions: { hide: { '@version': [{ _cnd: { lt: 1.1 } }] } }, + }, ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts index e4a435cf08729..da57ede1d2315 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts @@ -95,7 +95,10 @@ export class MemoryRedisChat implements INodeType { description: 'For how long the session should be stored in seconds. If set to 0 it will not expire.', }, - contextWindowLengthProperty({ hide: { '@version': [{ _cnd: { lt: 1.3 } }] } }), + { + ...contextWindowLengthProperty, + displayOptions: { hide: { '@version': [{ _cnd: { lt: 1.3 } }] } }, + }, ], }; @@ -144,9 +147,9 @@ export class MemoryRedisChat implements INodeType { } const redisChatHistory = new RedisChatMessageHistory(redisChatConfig); - const memClass = this.getNode().typeVersion < 1.1 ? BufferMemory : BufferWindowMemory; + const memClass = this.getNode().typeVersion < 1.3 ? BufferMemory : BufferWindowMemory; const kOptions = - this.getNode().typeVersion < 1.1 + this.getNode().typeVersion < 1.3 ? {} : { k: this.getNodeParameter('contextWindowLength', itemIndex) }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts b/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts index dc8702854c645..f0177d9e75e8b 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/MemoryXata/MemoryXata.node.ts @@ -81,7 +81,10 @@ export class MemoryXata implements INodeType { }, }, sessionKeyProperty, - contextWindowLengthProperty({ hide: { '@version': [{ _cnd: { lt: 1.3 } }] } }), + { + ...contextWindowLengthProperty, + displayOptions: { hide: { '@version': [{ _cnd: { lt: 1.3 } }] } }, + }, ], }; diff --git a/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts b/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts index 28f7296006f13..6a5f7268511fa 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts @@ -34,13 +34,10 @@ export const sessionKeyProperty: INodeProperties = { }, }; -export function contextWindowLengthProperty(displayOptions?: IDisplayOptions): INodeProperties { - return { - displayName: 'Context Window Length', - name: 'contextWindowLength', - type: 'number', - default: 5, - hint: 'How many past interactions the model receives as context', - displayOptions: displayOptions ?? {}, - }; -} +export const contextWindowLengthProperty: INodeProperties = { + displayName: 'Context Window Length', + name: 'contextWindowLength', + type: 'number', + default: 5, + hint: 'How many past interactions the model receives as context', +}; From d97a03669ad758147dac4774a6426b220e2440c1 Mon Sep 17 00:00:00 2001 From: JP van Oosten Date: Mon, 5 Aug 2024 17:11:16 +0200 Subject: [PATCH 5/5] Remove the import of IDisplayOptions --- packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts b/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts index 6a5f7268511fa..354d134fb7c42 100644 --- a/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts +++ b/packages/@n8n/nodes-langchain/nodes/memory/descriptions.ts @@ -1,4 +1,4 @@ -import type { INodeProperties, IDisplayOptions } from 'n8n-workflow'; +import type { INodeProperties } from 'n8n-workflow'; export const sessionIdOption: INodeProperties = { displayName: 'Session ID',