feat(Postgres Chat Memory, Redis Chat Memory, Xata): Add support for context window length (#10203)

This commit is contained in:
jeanpaul
2024-08-06 10:42:18 +02:00
committed by GitHub
parent 1eba7c3c76
commit e3edeaa035
5 changed files with 55 additions and 20 deletions

View File

@@ -7,14 +7,14 @@ 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';
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 {
@@ -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,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,
displayOptions: { hide: { '@version': [{ _cnd: { lt: 1.3 } }] } },
},
],
};
@@ -143,12 +147,19 @@ export class MemoryRedisChat implements INodeType {
}
const redisChatHistory = new RedisChatMessageHistory(redisChatConfig);
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({
memoryKey: 'chat_history',
chatHistory: redisChatHistory,
returnMessages: true,
inputKey: 'input',
outputKey: 'output',
...kOptions,
});
async function closeFunction() {