-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ Implement Redis Chat Memory, update LC
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
- Loading branch information
1 parent
9a343f9
commit 2df34fb
Showing
8 changed files
with
1,172 additions
and
871 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/@n8n/nodes-langchain/nodes/llms/LMOpenAi/LmOpenAi.node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/MemoryRedisChat.node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */ | ||
import { | ||
NodeOperationError, | ||
type IExecuteFunctions, | ||
type INodeType, | ||
type INodeTypeDescription, | ||
type SupplyData, | ||
} from 'n8n-workflow'; | ||
import { BufferMemory } from 'langchain/memory'; | ||
import type { RedisChatMessageHistoryInput } from 'langchain/stores/message/redis'; | ||
import { RedisChatMessageHistory } from 'langchain/stores/message/redis'; | ||
import type { RedisClientOptions } from 'redis'; | ||
import { createClient } from 'redis'; | ||
import { logWrapper } from '../../../utils/logWrapper'; | ||
|
||
export class MemoryRedisChat implements INodeType { | ||
description: INodeTypeDescription = { | ||
displayName: 'Redis Chat Memory', | ||
name: 'memoryRedisChat', | ||
icon: 'file:redis.svg', | ||
group: ['transform'], | ||
version: 1, | ||
description: 'Stores the chat history in Redis.', | ||
defaults: { | ||
name: 'Redis Chat Memory', | ||
}, | ||
credentials: [ | ||
{ | ||
name: 'redis', | ||
required: true, | ||
}, | ||
], | ||
codex: { | ||
categories: ['AI'], | ||
subcategories: { | ||
AI: ['Memory'], | ||
}, | ||
}, | ||
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node | ||
inputs: [], | ||
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong | ||
outputs: ['memory'], | ||
outputNames: ['Memory'], | ||
properties: [ | ||
{ | ||
displayName: 'Session Key', | ||
name: 'sessionKey', | ||
type: 'string', | ||
default: 'chat_history', | ||
description: 'The key to use to store the memory in the workflow data', | ||
}, | ||
{ | ||
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.', | ||
}, | ||
], | ||
}; | ||
|
||
async supplyData(this: IExecuteFunctions): Promise<SupplyData> { | ||
const credentials = await this.getCredentials('redis'); | ||
const sessionKey = this.getNodeParameter('sessionKey', 0) as string; | ||
const sessionTTL = this.getNodeParameter('sessionTTL', 0, 0) as number; | ||
|
||
const redisOptions: RedisClientOptions = { | ||
socket: { | ||
host: credentials.host as string, | ||
port: credentials.port as number, | ||
}, | ||
database: credentials.database as number, | ||
}; | ||
|
||
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: sessionKey, | ||
}; | ||
|
||
if (sessionTTL > 0) { | ||
redisChatConfig.sessionTTL = sessionTTL; | ||
} | ||
const redisChatHistory = new RedisChatMessageHistory(redisChatConfig); | ||
|
||
const memory = new BufferMemory({ | ||
memoryKey: 'chat_history', | ||
chatHistory: redisChatHistory, | ||
returnMessages: true, | ||
inputKey: 'input', | ||
outputKey: 'output', | ||
}); | ||
|
||
return { | ||
response: logWrapper(memory, this), | ||
}; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/@n8n/nodes-langchain/nodes/memory/MemoryRedisChat/redis.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.