first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
import type { RedisChatMessageHistoryInput } from '@langchain/redis';
|
||||
import { RedisChatMessageHistory } from '@langchain/redis';
|
||||
import { BufferMemory, BufferWindowMemory } from '@langchain/classic/memory';
|
||||
import {
|
||||
NodeOperationError,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
NodeConnectionTypes,
|
||||
} from 'n8n-workflow';
|
||||
import type { RedisClientOptions } from 'redis';
|
||||
import { createClient } from 'redis';
|
||||
|
||||
import { getSessionId } from '@utils/helpers';
|
||||
import { logWrapper, getConnectionHintNoticeField } from '@n8n/ai-utilities';
|
||||
|
||||
import {
|
||||
sessionIdOption,
|
||||
sessionKeyProperty,
|
||||
contextWindowLengthProperty,
|
||||
expressionSessionKeyProperty,
|
||||
} from '../descriptions';
|
||||
|
||||
export class MemoryRedisChat implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Redis Chat Memory',
|
||||
name: 'memoryRedisChat',
|
||||
icon: 'file:redis.svg',
|
||||
group: ['transform'],
|
||||
version: [1, 1.1, 1.2, 1.3, 1.4, 1.5],
|
||||
description: 'Stores the chat history in Redis.',
|
||||
defaults: {
|
||||
name: 'Redis Chat Memory',
|
||||
},
|
||||
credentials: [
|
||||
{
|
||||
name: 'redis',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Memory'],
|
||||
Memory: ['Other memories'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryredischat/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiMemory],
|
||||
outputNames: ['Memory'],
|
||||
properties: [
|
||||
getConnectionHintNoticeField([NodeConnectionTypes.AiAgent]),
|
||||
{
|
||||
displayName: 'Session Key',
|
||||
name: 'sessionKey',
|
||||
type: 'string',
|
||||
default: 'chat_history',
|
||||
description: 'The key to use to store the memory in the workflow data',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [1],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Session ID',
|
||||
name: 'sessionKey',
|
||||
type: 'string',
|
||||
default: '={{ $json.sessionId }}',
|
||||
description: 'The key to use to store the memory',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [1.1],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
...sessionIdOption,
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [{ _cnd: { gte: 1.2 } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
expressionSessionKeyProperty(1.4),
|
||||
sessionKeyProperty,
|
||||
{
|
||||
displayName: 'Session Time To Live',
|
||||
name: 'sessionTTL',
|
||||
type: 'number',
|
||||
default: 0,
|
||||
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 } }] } },
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
const credentials = await this.getCredentials('redis');
|
||||
const nodeVersion = this.getNode().typeVersion;
|
||||
|
||||
const sessionTTL = this.getNodeParameter('sessionTTL', itemIndex, 0) as number;
|
||||
|
||||
let sessionId;
|
||||
|
||||
if (nodeVersion >= 1.2) {
|
||||
sessionId = getSessionId(this, itemIndex);
|
||||
} else {
|
||||
sessionId = this.getNodeParameter('sessionKey', itemIndex) as string;
|
||||
}
|
||||
|
||||
const redisOptions: RedisClientOptions = {
|
||||
socket: {
|
||||
host: credentials.host as string,
|
||||
port: credentials.port as number,
|
||||
tls: credentials.ssl === true,
|
||||
},
|
||||
database: credentials.database as number,
|
||||
};
|
||||
|
||||
if (credentials.user && nodeVersion >= 1.5) {
|
||||
redisOptions.username = credentials.user as string;
|
||||
}
|
||||
if (credentials.password) {
|
||||
redisOptions.password = credentials.password as string;
|
||||
}
|
||||
|
||||
const client = createClient({
|
||||
...redisOptions,
|
||||
});
|
||||
|
||||
client.on('error', async (error: Error) => {
|
||||
await client.quit();
|
||||
throw new NodeOperationError(this.getNode(), 'Redis Error: ' + error.message);
|
||||
});
|
||||
|
||||
const redisChatConfig: RedisChatMessageHistoryInput = {
|
||||
client,
|
||||
sessionId,
|
||||
};
|
||||
|
||||
if (sessionTTL > 0) {
|
||||
redisChatConfig.sessionTTL = sessionTTL;
|
||||
}
|
||||
const redisChatHistory = new RedisChatMessageHistory(redisChatConfig);
|
||||
|
||||
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() {
|
||||
void client.disconnect();
|
||||
}
|
||||
|
||||
return {
|
||||
closeFunction,
|
||||
response: logWrapper(memory, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="60" height="60"><g fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"><path fill="#A41E11" d="M57.656 43.99c-3.201 1.683-19.787 8.561-23.318 10.417s-5.494 1.838-8.283.494c-2.79-1.343-20.449-8.535-23.629-10.067C.834 44.066.002 43.422.002 42.811v-6.117s22.98-5.045 26.69-6.388 4.995-1.39 8.154-.225c3.16 1.165 22.035 4.603 25.154 5.756v6.032c0 .605-.72 1.283-2.35 2.124z"/><path fill="#D82C20" d="M57.656 37.872c-3.201 1.685-19.787 8.56-23.318 10.417s-5.494 1.838-8.283.494c-2.79-1.343-20.449-8.534-23.63-10.068s-3.243-2.588-.122-3.82l24.388-9.52c3.71-1.34 4.994-1.39 8.153-.225s19.643 7.78 22.747 8.951c3.103 1.17 3.24 2.086.037 3.786z"/><path fill="#A41E11" d="M57.656 34.015c-3.201 1.683-19.787 8.561-23.318 10.417s-5.494 1.838-8.283.495c-2.79-1.344-20.449-8.536-23.629-10.067C.834 34.092.002 33.447.002 32.836V26.72s22.98-5.045 26.69-6.387c3.711-1.343 4.995-1.39 8.154-.225 3.16 1.165 22.035 4.602 25.154 5.756v6.032c0 .605-.72 1.283-2.35 2.123z"/><path fill="#D82C20" d="M57.656 27.898c-3.201 1.685-19.787 8.561-23.318 10.417s-5.494 1.838-8.283.495c-2.79-1.344-20.449-8.534-23.63-10.067-3.18-1.534-3.243-2.588-.122-3.82l24.388-9.52c3.71-1.343 4.994-1.39 8.153-.225 3.16 1.166 19.644 7.785 22.765 8.935s3.24 2.085.038 3.785z"/><path fill="#A41E11" d="M57.656 23.671c-3.201 1.683-19.787 8.561-23.318 10.419s-5.494 1.838-8.283.495c-2.79-1.344-20.449-8.535-23.629-10.069-1.592-.765-2.424-1.411-2.424-2.02v-6.11s22.98-5.045 26.69-6.388 4.995-1.39 8.154-.225c3.16 1.165 22.035 4.591 25.154 5.745v6.032c0 .605-.72 1.283-2.35 2.123z"/><path fill="#D82C20" d="M57.656 17.553c-3.201 1.685-19.787 8.561-23.318 10.417s-5.494 1.838-8.283.495c-2.79-1.344-20.449-8.534-23.63-10.068s-3.243-2.587-.122-3.82l24.388-9.52c3.71-1.343 4.994-1.39 8.153-.226 3.16 1.165 19.643 7.785 22.765 8.936s3.24 2.085.038 3.785z"/><path fill="#FFF" d="m31.497 15.032-1.88-3.153-6.002-.545 4.48-1.63L26.75 7.2l4.192 1.653 3.955-1.305-1.07 2.586 4.032 1.524-5.198.546zm-10.014 6.275 13.903-2.153-4.2 6.211zm-11.17-5.167c0-1.61 3.314-2.906 7.431-2.906 4.118 0 7.432 1.296 7.432 2.906s-3.314 2.905-7.432 2.905c-4.117 0-7.431-1.295-7.431-2.905"/><path fill="#7A0C00" d="m52.233 15.714-8.224 3.276-.007-6.556z"/><path fill="#AD2115" d="m44.01 18.991-.89.353-8.217-3.276 9.094-3.63z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 2.3 KiB |
Reference in New Issue
Block a user