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

@@ -2,11 +2,11 @@
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';
import { sessionIdOption, sessionKeyProperty } from '../descriptions';
import { sessionIdOption, sessionKeyProperty, contextWindowLengthProperty } from '../descriptions';
import { getSessionId } from '../../../utils/helpers';
export class MemoryXata implements INodeType {
@@ -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,10 @@ export class MemoryXata implements INodeType {
},
},
sessionKeyProperty,
{
...contextWindowLengthProperty,
displayOptions: { hide: { '@version': [{ _cnd: { lt: 1.3 } }] } },
},
],
};
@@ -120,12 +124,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 {