Feature/Add dedicated agent memory nodes (#3649)

add dedicated agent memory nodes
This commit is contained in:
Henry Heng
2024-12-06 22:16:01 +00:00
committed by GitHub
parent fe2ed26999
commit cadc3b8fb3
12 changed files with 330 additions and 15 deletions
@@ -2,10 +2,10 @@ import path from 'path'
import { getBaseClasses, getCredentialData, getCredentialParam, getUserHome } from '../../../src/utils'
import { SaverOptions } from './interface'
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../src/Interface'
import { SqliteSaver } from './sqliteSaver'
import { SqliteSaver } from './SQLiteAgentMemory/sqliteSaver'
import { DataSource } from 'typeorm'
import { PostgresSaver } from './pgSaver'
import { MySQLSaver } from './mysqlSaver'
import { PostgresSaver } from './PostgresAgentMemory/pgSaver'
import { MySQLSaver } from './MySQLAgentMemory/mysqlSaver'
class AgentMemory_Memory implements INode {
label: string
@@ -29,6 +29,7 @@ class AgentMemory_Memory implements INode {
this.category = 'Memory'
this.description = 'Memory for agentflow to remember the state of the conversation'
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
this.badge = 'DEPRECATING'
this.credential = {
label: 'Connect Credential',
name: 'credential',
@@ -0,0 +1,112 @@
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../../src/utils'
import { SaverOptions } from '../interface'
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../../src/Interface'
import { DataSource } from 'typeorm'
import { MySQLSaver } from './mysqlSaver'
class MySQLAgentMemory_Memory implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
badge: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'MySQL Agent Memory'
this.name = 'mySQLAgentMemory'
this.version = 1.0
this.type = 'AgentMemory'
this.icon = 'mysql.png'
this.category = 'Memory'
this.description = 'Memory for agentflow to remember the state of the conversation using MySQL database'
this.baseClasses = [this.type, ...getBaseClasses(MySQLSaver)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['MySQLApi'],
optional: true
}
this.inputs = [
{
label: 'Host',
name: 'host',
type: 'string'
},
{
label: 'Database',
name: 'database',
type: 'string'
},
{
label: 'Port',
name: 'port',
type: 'number',
default: '3306'
},
{
label: 'Additional Connection Configuration',
name: 'additionalConfig',
type: 'json',
additionalParams: true,
optional: true
}
]
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const additionalConfig = nodeData.inputs?.additionalConfig as string
const databaseEntities = options.databaseEntities as IDatabaseEntity
const chatflowid = options.chatflowid as string
const appDataSource = options.appDataSource as DataSource
let additionalConfiguration = {}
if (additionalConfig) {
try {
additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig)
} catch (exception) {
throw new Error('Invalid JSON in the Additional Configuration: ' + exception)
}
}
const threadId = options.sessionId || options.chatId
let datasourceOptions: ICommonObject = {
...additionalConfiguration,
type: 'mysql'
}
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const user = getCredentialParam('user', credentialData, nodeData)
const password = getCredentialParam('password', credentialData, nodeData)
const _port = (nodeData.inputs?.port as string) || '3306'
const port = parseInt(_port)
datasourceOptions = {
...datasourceOptions,
host: nodeData.inputs?.host as string,
port,
database: nodeData.inputs?.database as string,
username: user,
user: user,
password: password,
charset: 'utf8mb4'
}
const args: SaverOptions = {
datasourceOptions,
threadId,
appDataSource,
databaseEntities,
chatflowid
}
const recordManager = new MySQLSaver(args)
return recordManager
}
}
module.exports = { nodeClass: MySQLAgentMemory_Memory }
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
import { RunnableConfig } from '@langchain/core/runnables'
import { BaseMessage } from '@langchain/core/messages'
import { DataSource } from 'typeorm'
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
import { IMessage, MemoryMethods } from '../../../src/Interface'
import { mapChatMessageToBaseMessage } from '../../../src/utils'
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
import { IMessage, MemoryMethods } from '../../../../src/Interface'
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
export class MySQLSaver extends BaseCheckpointSaver implements MemoryMethods {
protected isSetup: boolean
@@ -0,0 +1,111 @@
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../../src/utils'
import { SaverOptions } from '../interface'
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../../src/Interface'
import { DataSource } from 'typeorm'
import { PostgresSaver } from './pgSaver'
class PostgresAgentMemory_Memory implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
badge: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'Postgres Agent Memory'
this.name = 'postgresAgentMemory'
this.version = 1.0
this.type = 'AgentMemory'
this.icon = 'postgres.svg'
this.category = 'Memory'
this.description = 'Memory for agentflow to remember the state of the conversation using Postgres database'
this.baseClasses = [this.type, ...getBaseClasses(PostgresSaver)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['PostgresApi'],
optional: true
}
this.inputs = [
{
label: 'Host',
name: 'host',
type: 'string'
},
{
label: 'Database',
name: 'database',
type: 'string'
},
{
label: 'Port',
name: 'port',
type: 'number',
default: '5432'
},
{
label: 'Additional Connection Configuration',
name: 'additionalConfig',
type: 'json',
additionalParams: true,
optional: true
}
]
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const additionalConfig = nodeData.inputs?.additionalConfig as string
const databaseEntities = options.databaseEntities as IDatabaseEntity
const chatflowid = options.chatflowid as string
const appDataSource = options.appDataSource as DataSource
let additionalConfiguration = {}
if (additionalConfig) {
try {
additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig)
} catch (exception) {
throw new Error('Invalid JSON in the Additional Configuration: ' + exception)
}
}
const threadId = options.sessionId || options.chatId
let datasourceOptions: ICommonObject = {
...additionalConfiguration,
type: 'postgres'
}
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const user = getCredentialParam('user', credentialData, nodeData)
const password = getCredentialParam('password', credentialData, nodeData)
const _port = (nodeData.inputs?.port as string) || '5432'
const port = parseInt(_port)
datasourceOptions = {
...datasourceOptions,
host: nodeData.inputs?.host as string,
port,
database: nodeData.inputs?.database as string,
username: user,
user: user,
password: password
}
const args: SaverOptions = {
datasourceOptions,
threadId,
appDataSource,
databaseEntities,
chatflowid
}
const recordManager = new PostgresSaver(args)
return recordManager
}
}
module.exports = { nodeClass: PostgresAgentMemory_Memory }
@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
import { RunnableConfig } from '@langchain/core/runnables'
import { BaseMessage } from '@langchain/core/messages'
import { DataSource } from 'typeorm'
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
import { IMessage, MemoryMethods } from '../../../src/Interface'
import { mapChatMessageToBaseMessage } from '../../../src/utils'
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
import { IMessage, MemoryMethods } from '../../../../src/Interface'
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
export class PostgresSaver extends BaseCheckpointSaver implements MemoryMethods {
protected isSetup: boolean
File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 6.8 KiB

@@ -0,0 +1,87 @@
import path from 'path'
import { getBaseClasses, getUserHome } from '../../../../src/utils'
import { SaverOptions } from '../interface'
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../../src/Interface'
import { SqliteSaver } from './sqliteSaver'
import { DataSource } from 'typeorm'
class SQLiteAgentMemory_Memory implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
badge: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'SQLite Agent Memory'
this.name = 'sqliteAgentMemory'
this.version = 1.0
this.type = 'SQLiteAgentMemory'
this.icon = 'sqlite.png'
this.category = 'Memory'
this.description = 'Memory for agentflow to remember the state of the conversation using SQLite database'
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
this.inputs = [
/*{
label: 'Database File Path',
name: 'databaseFilePath',
type: 'string',
placeholder: 'C:\\Users\\User\\.flowise\\database.sqlite',
description: 'Path to the SQLite database file. Leave empty to use default application database',
optional: true
},*/
{
label: 'Additional Connection Configuration',
name: 'additionalConfig',
type: 'json',
additionalParams: true,
optional: true
}
]
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const additionalConfig = nodeData.inputs?.additionalConfig as string
const databaseEntities = options.databaseEntities as IDatabaseEntity
const chatflowid = options.chatflowid as string
const appDataSource = options.appDataSource as DataSource
let additionalConfiguration = {}
if (additionalConfig) {
try {
additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig)
} catch (exception) {
throw new Error('Invalid JSON in the Additional Configuration: ' + exception)
}
}
const threadId = options.sessionId || options.chatId
const database = path.join(process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise'), 'database.sqlite')
let datasourceOptions: ICommonObject = {
database,
...additionalConfiguration,
type: 'sqlite'
}
const args: SaverOptions = {
datasourceOptions,
threadId,
appDataSource,
databaseEntities,
chatflowid
}
const recordManager = new SqliteSaver(args)
return recordManager
}
}
module.exports = { nodeClass: SQLiteAgentMemory_Memory }
Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
import { RunnableConfig } from '@langchain/core/runnables'
import { BaseMessage } from '@langchain/core/messages'
import { DataSource, QueryRunner } from 'typeorm'
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
import { IMessage, MemoryMethods } from '../../../src/Interface'
import { mapChatMessageToBaseMessage } from '../../../src/utils'
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
import { IMessage, MemoryMethods } from '../../../../src/Interface'
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
export class SqliteSaver extends BaseCheckpointSaver implements MemoryMethods {
protected isSetup: boolean
+2 -1
View File
@@ -98,7 +98,8 @@ export const buildAgentGraph = async (
/*** Get Memory Node for Chat History ***/
let chatHistory: IMessage[] = []
const memoryNode = nodes.find((node) => node.data.name === 'agentMemory')
const agentMemoryList = ['agentMemory', 'sqliteAgentMemory', 'postgresAgentMemory', 'mySQLAgentMemory']
const memoryNode = nodes.find((node) => agentMemoryList.includes(node.data.name))
if (memoryNode) {
chatHistory = await getSessionChatHistory(
chatflowid,
+4 -2
View File
@@ -56,15 +56,17 @@ function a11yProps(index) {
const blacklistCategoriesForAgentCanvas = ['Agents', 'Memory', 'Record Manager', 'Utilities']
const agentMemoryNodes = ['agentMemory', 'sqliteAgentMemory', 'postgresAgentMemory', 'mySQLAgentMemory']
// Show blacklisted nodes (exceptions) for agent canvas
const exceptionsForAgentCanvas = {
Memory: ['agentMemory'],
Memory: agentMemoryNodes,
Utilities: ['getVariable', 'setVariable', 'stickyNote']
}
// Hide some nodes from the chatflow canvas
const blacklistForChatflowCanvas = {
Memory: ['agentMemory']
Memory: agentMemoryNodes
}
const AddNodes = ({ nodesData, node, isAgentCanvas }) => {