mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 21:00:58 +03:00
MongoDB Atlas Integration: Adding MongoDB Memory
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
import { INodeParams, INodeCredential } from '../src/Interface'
|
||||||
|
|
||||||
|
class MongoDBUrlApi implements INodeCredential {
|
||||||
|
label: string
|
||||||
|
name: string
|
||||||
|
version: number
|
||||||
|
description: string
|
||||||
|
inputs: INodeParams[]
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.label = 'MongoDB ATLAS'
|
||||||
|
this.name = 'mongoDBUrlApi'
|
||||||
|
this.version = 1.0
|
||||||
|
this.inputs = [
|
||||||
|
{
|
||||||
|
label: 'ATLAS Connection URL',
|
||||||
|
name: 'mongoDBConnectUrl',
|
||||||
|
type: 'string',
|
||||||
|
placeholder: 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { credClass: MongoDBUrlApi }
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
import { getBaseClasses, getCredentialData, getCredentialParam, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
|
||||||
|
import { MongoDBChatMessageHistory } from 'langchain/stores/message/mongodb'
|
||||||
|
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
||||||
|
import { MongoClient } from 'mongodb'
|
||||||
|
|
||||||
|
class MongoDB_Memory implements INode {
|
||||||
|
label: string
|
||||||
|
name: string
|
||||||
|
version: number
|
||||||
|
description: string
|
||||||
|
type: string
|
||||||
|
icon: string
|
||||||
|
category: string
|
||||||
|
baseClasses: string[]
|
||||||
|
credential: INodeParams
|
||||||
|
inputs: INodeParams[]
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.label = 'MongoDB Atlas Chat Memory'
|
||||||
|
this.name = 'MongoDBAtlasChatMemory'
|
||||||
|
this.version = 1.0
|
||||||
|
this.type = 'MongoDBAtlasChatMemory'
|
||||||
|
this.icon = 'mongodb.png'
|
||||||
|
this.category = 'Memory'
|
||||||
|
this.description = 'Stores the conversation in MongoDB Atlas'
|
||||||
|
this.baseClasses = [this.type, ...getBaseClasses(BufferMemory)]
|
||||||
|
this.credential = {
|
||||||
|
label: 'Connect Credential',
|
||||||
|
name: 'credential',
|
||||||
|
type: 'credential',
|
||||||
|
credentialNames: ['mongoDBUrlApi']
|
||||||
|
}
|
||||||
|
this.inputs = [
|
||||||
|
{
|
||||||
|
label: 'Database',
|
||||||
|
name: 'databaseName',
|
||||||
|
placeholder: '<DB_NAME>',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Collection Name',
|
||||||
|
name: 'collectionName',
|
||||||
|
placeholder: '<COLLECTION_NAME>',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Session ID',
|
||||||
|
name: 'sessionId',
|
||||||
|
type: 'string',
|
||||||
|
default: '5f9cf7c08d5b1a06b80fae61',
|
||||||
|
description: 'Must be an Hex String of 24 chars. This will be the objectId of the document in MongoDB Atlas'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Memory Key',
|
||||||
|
name: 'memoryKey',
|
||||||
|
type: 'string',
|
||||||
|
default: 'chat_history',
|
||||||
|
additionalParams: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||||
|
return initializeMongoDB(nodeData, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||||
|
const mongodbMemory = await initializeMongoDB(nodeData, options)
|
||||||
|
const sessionId = nodeData.inputs?.sessionId as string
|
||||||
|
options.logger.info(`Clearing MongoDB memory session ${sessionId}`)
|
||||||
|
await mongodbMemory.clear()
|
||||||
|
options.logger.info(`Successfully cleared MongoDB memory session ${sessionId}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): Promise<BufferMemory> => {
|
||||||
|
const databaseName = nodeData.inputs?.databaseName as string
|
||||||
|
const collectionName = nodeData.inputs?.collectionName as string
|
||||||
|
const sessionId = nodeData.inputs?.sessionId as string
|
||||||
|
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||||
|
|
||||||
|
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||||
|
let mongoDBConnectUrl = getCredentialParam('mongoDBConnectUrl', credentialData, nodeData)
|
||||||
|
|
||||||
|
const client = new MongoClient(mongoDBConnectUrl)
|
||||||
|
await client.connect()
|
||||||
|
const collection = client.db(databaseName).collection(collectionName)
|
||||||
|
|
||||||
|
const mongoDBChatMessageHistory = new MongoDBChatMessageHistory({
|
||||||
|
collection,
|
||||||
|
sessionId: sessionId
|
||||||
|
})
|
||||||
|
|
||||||
|
return new BufferMemoryExtended({
|
||||||
|
memoryKey,
|
||||||
|
chatHistory: mongoDBChatMessageHistory,
|
||||||
|
returnMessages: true,
|
||||||
|
isSessionIdUsingChatMessageId: false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BufferMemoryExtendedInput {
|
||||||
|
isSessionIdUsingChatMessageId: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
class BufferMemoryExtended extends BufferMemory {
|
||||||
|
isSessionIdUsingChatMessageId? = false
|
||||||
|
|
||||||
|
constructor(fields: BufferMemoryInput & Partial<BufferMemoryExtendedInput>) {
|
||||||
|
super(fields)
|
||||||
|
this.isSessionIdUsingChatMessageId = fields.isSessionIdUsingChatMessageId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { nodeClass: MongoDB_Memory }
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 3.7 KiB |
@@ -55,6 +55,7 @@
|
|||||||
"llmonitor": "^0.5.5",
|
"llmonitor": "^0.5.5",
|
||||||
"mammoth": "^1.5.1",
|
"mammoth": "^1.5.1",
|
||||||
"moment": "^2.29.3",
|
"moment": "^2.29.3",
|
||||||
|
"mongodb": "^6.2.0",
|
||||||
"mysql2": "^3.5.1",
|
"mysql2": "^3.5.1",
|
||||||
"node-fetch": "^2.6.11",
|
"node-fetch": "^2.6.11",
|
||||||
"node-html-markdown": "^1.3.0",
|
"node-html-markdown": "^1.3.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user