Skip to content

Commit

Permalink
feat: Add Zep Cloud Memory component (#9657)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-paliychuk authored Jul 1, 2024
1 parent 55cbc90 commit 41c47a2
Show file tree
Hide file tree
Showing 7 changed files with 1,169 additions and 146 deletions.
33 changes: 23 additions & 10 deletions packages/@n8n/nodes-langchain/credentials/ZepApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ export class ZepApi implements ICredentialType {
documentationUrl = 'zep';

properties: INodeProperties[] = [
{
displayName: 'API URL',
name: 'apiUrl',
required: true,
type: 'string',
default: 'http://localhost:8000',
},
{
displayName: 'API Key',
name: 'apiKey',
Expand All @@ -28,21 +21,41 @@ export class ZepApi implements ICredentialType {
required: false,
default: '',
},
{
displayName: 'Cloud',
description: 'Whether you are adding credentials for Zep Cloud instead of Zep Open Source',
name: 'cloud',
type: 'boolean',
default: false,
},
{
displayName: 'API URL',
name: 'apiUrl',
required: false,
type: 'string',
default: 'http://localhost:8000',
displayOptions: {
show: {
cloud: [false],
},
},
},
];

authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '={{$credentials.apiKey ? "Bearer " + $credentials.apiKey : undefined }}',
Authorization:
'={{$credentials.apiKey && !$credentials.cloud ? "Bearer " + $credentials.apiKey : "Api-Key " + $credentials.apiKey }}',
},
},
};

test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.apiUrl}}',
url: '/api/v1/collection',
baseURL: '={{!$credentials.cloud ? $credentials.apiUrl : "https://api.getzep.com"}}',
url: '={{!$credentials.cloud ? "/api/v1/collection" : "/api/v2/collections"}}',
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand All @@ -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),
Expand Down
Binary file modified packages/@n8n/nodes-langchain/nodes/memory/MemoryZep/zep.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions packages/@n8n/nodes-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,14 @@
"@aws-sdk/client-bedrock-runtime": "3.535.0",
"@aws-sdk/credential-provider-node": "3.535.0",
"@getzep/zep-js": "0.9.0",
"@getzep/zep-cloud": "1.0.6",
"@google-ai/generativelanguage": "2.5.0",
"@google/generative-ai": "0.11.4",
"@huggingface/inference": "2.7.0",
"@langchain/anthropic": "0.1.21",
"@langchain/cohere": "0.0.10",
"@langchain/community": "0.2.2",
"@langchain/core": "0.2.0",
"@langchain/community": "0.2.13",
"@langchain/core": "0.2.9",
"@langchain/google-genai": "0.0.16",
"@langchain/groq": "0.0.12",
"@langchain/mistralai": "0.0.22",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
},
"dependencies": {
"@langchain/community": "0.2.2",
"@langchain/core": "0.2.0",
"@langchain/core": "0.2.9",
"@langchain/openai": "0.0.33",
"@langchain/pinecone": "0.0.6",
"@n8n/client-oauth2": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"dist/**/*"
],
"devDependencies": {
"@langchain/core": "^0.2.0",
"@langchain/core": "^0.2.9",
"@types/deep-equal": "^1.0.1",
"@types/express": "^4.17.21",
"@types/jmespath": "^0.15.0",
Expand Down
Loading

0 comments on commit 41c47a2

Please sign in to comment.