feat: Add Zep Cloud Memory component (#9657)

This commit is contained in:
Pavlo Paliychuk
2024-07-01 08:32:26 -04:00
committed by GitHub
parent 55cbc900a4
commit 41c47a28a9
7 changed files with 1119 additions and 96 deletions

View File

@@ -5,12 +5,29 @@ import {
type INodeType,
type INodeTypeDescription,
type SupplyData,
NodeOperationError,
} from 'n8n-workflow';
import { ZepMemory } from '@langchain/community/memory/zep';
import { ZepCloudMemory } from '@langchain/community/memory/zep_cloud';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
import { sessionIdOption, sessionKeyProperty } from '../descriptions';
import { getSessionId } from '../../../utils/helpers';
import type { BaseChatMemory } from '@langchain/community/dist/memory/chat_memory';
import type { InputValues, MemoryVariables } from '@langchain/core/memory';
import type { BaseMessage } from '@langchain/core/messages';
// Extend ZepCloudMemory to trim white space in messages.
class WhiteSpaceTrimmedZepCloudMemory extends ZepCloudMemory {
override async loadMemoryVariables(values: InputValues): Promise<MemoryVariables> {
const memoryVariables = await super.loadMemoryVariables(values);
memoryVariables.chat_history = memoryVariables.chat_history.filter((m: BaseMessage) =>
m.content.toString().trim(),
);
return memoryVariables;
}
}
export class MemoryZep implements INodeType {
description: INodeTypeDescription = {
@@ -89,7 +106,8 @@ export class MemoryZep implements INodeType {
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = (await this.getCredentials('zepApi')) as {
apiKey?: string;
apiUrl: string;
apiUrl?: string;
cloud?: boolean;
};
const nodeVersion = this.getNode().typeVersion;
@@ -102,15 +120,36 @@ export class MemoryZep implements INodeType {
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',
});
let memory: BaseChatMemory;
if (credentials.cloud) {
if (!credentials.apiKey) {
throw new NodeOperationError(this.getNode(), 'API key is required to use Zep Cloud');
}
memory = new WhiteSpaceTrimmedZepCloudMemory({
sessionId,
apiKey: credentials.apiKey,
memoryType: 'perpetual',
memoryKey: 'chat_history',
returnMessages: true,
inputKey: 'input',
outputKey: 'output',
separateMessages: false,
});
} else {
if (!credentials.apiUrl) {
throw new NodeOperationError(this.getNode(), 'API url is required to use Zep Open Source');
}
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.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB