From bc71e57834c2419e4e96f3017dbb5911bea15e10 Mon Sep 17 00:00:00 2001 From: Govind Kumar Date: Fri, 30 Jun 2023 18:58:15 +0530 Subject: [PATCH 1/2] added initial code --- .../nodes/memory/DynamoDb/DynamoDb.ts | 90 +++++++++++++++++++ .../nodes/memory/DynamoDb/dynamodb.svg | 18 ++++ packages/components/package.json | 1 + 3 files changed, 109 insertions(+) create mode 100644 packages/components/nodes/memory/DynamoDb/DynamoDb.ts create mode 100644 packages/components/nodes/memory/DynamoDb/dynamodb.svg diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts new file mode 100644 index 00000000000..d0845d96896 --- /dev/null +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -0,0 +1,90 @@ +import { ICommonObject, INode, INodeData, INodeParams, getBaseClasses } from '../../../src' +import { DynamoDBChatMessageHistory } from 'langchain/stores/message/dynamodb' +import { BufferMemory } from 'langchain/memory' + +class DynamoDb_Memory implements INode { + label: string + name: string + description: string + type: string + icon: string + category: string + baseClasses: string[] + inputs: INodeParams[] + + constructor() { + this.label = 'DynamoDB Memory' + this.name = 'DynamoDbMemory' + this.icon = 'dynamodb.svg' + this.category = 'Memory' + this.description = 'Stores the conversation in dynamo db table' + this.baseClasses = [this.type, ...getBaseClasses(DynamoDBChatMessageHistory)] + this.inputs = [ + { + label: 'Table Name', + name: 'tableName', + type: 'string' + }, + { + label: 'Partition Key', + name: 'partitionKey', + type: 'string' + }, + { + label: 'Session ID', + name: 'sessionId', + type: 'string', + description: 'if empty, chatId will be used automatically', + default: '', + additionalParams: true + }, + { + label: 'Region', + name: 'region', + type: 'string', + description: 'The aws region in which table is located', + placeholder: 'us-east-1' + }, + { + label: 'Access Key', + name: 'accessKey', + type: 'password' + }, + { + label: 'Secret Access Key', + name: 'secretAccessKey', + type: 'password' + } + ] + } + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { + const tableName = nodeData.inputs?.tableName as string + const partitionKey = nodeData.inputs?.partitionKey as string + const sessionId = nodeData.inputs?.sessionId as string + const region = nodeData.inputs?.region as string + const accessKey = nodeData.inputs?.accessKey as string + const secretAccessKey = nodeData.inputs?.secretAccessKey as string + + const chatId = options.chatId + + const dynamoDb = new DynamoDBChatMessageHistory({ + tableName, + partitionKey, + sessionId: sessionId ? sessionId : chatId, + config: { + region, + credentials: { + accessKeyId: accessKey, + secretAccessKey + } + } + }) + + const memory = new BufferMemory({ + chatHistory: dynamoDb + }) + return memory + } +} + +module.exports = { nodeClass: DynamoDb_Memory } diff --git a/packages/components/nodes/memory/DynamoDb/dynamodb.svg b/packages/components/nodes/memory/DynamoDb/dynamodb.svg new file mode 100644 index 00000000000..f2798350a93 --- /dev/null +++ b/packages/components/nodes/memory/DynamoDb/dynamodb.svg @@ -0,0 +1,18 @@ + + + + Icon-Architecture/16/Arch_Amazon-DynamoDB_16 + Created with Sketch. + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/components/package.json b/packages/components/package.json index f5b67cc7612..5ecd12e4ec4 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -16,6 +16,7 @@ }, "license": "SEE LICENSE IN LICENSE.md", "dependencies": { + "@aws-sdk/client-dynamodb": "^3.360.0", "@dqbd/tiktoken": "^1.0.7", "@getzep/zep-js": "^0.3.1", "@huggingface/inference": "1", From 0efed9d7d43bf66a8e10155bfeb865300984925d Mon Sep 17 00:00:00 2001 From: Govind Kumar Date: Sun, 2 Jul 2023 23:45:42 +0530 Subject: [PATCH 2/2] added dynamo db backed memory --- .../components/nodes/memory/DynamoDb/DynamoDb.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts index d0845d96896..8b4cd69dd83 100644 --- a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -18,7 +18,7 @@ class DynamoDb_Memory implements INode { this.icon = 'dynamodb.svg' this.category = 'Memory' this.description = 'Stores the conversation in dynamo db table' - this.baseClasses = [this.type, ...getBaseClasses(DynamoDBChatMessageHistory)] + this.baseClasses = [this.type, ...getBaseClasses(BufferMemory)] this.inputs = [ { label: 'Table Name', @@ -54,6 +54,12 @@ class DynamoDb_Memory implements INode { label: 'Secret Access Key', name: 'secretAccessKey', type: 'password' + }, + { + label: 'Memory Key', + name: 'memoryKey', + type: 'string', + default: 'chat_history' } ] } @@ -64,6 +70,7 @@ class DynamoDb_Memory implements INode { const region = nodeData.inputs?.region as string const accessKey = nodeData.inputs?.accessKey as string const secretAccessKey = nodeData.inputs?.secretAccessKey as string + const memoryKey = nodeData.inputs?.memoryKey as string const chatId = options.chatId @@ -81,7 +88,9 @@ class DynamoDb_Memory implements INode { }) const memory = new BufferMemory({ - chatHistory: dynamoDb + memoryKey, + chatHistory: dynamoDb, + returnMessages: true }) return memory }