mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 11:00:55 +03:00
Merge branch 'main' into FEATURE/Vision
# Conflicts: # packages/server/src/index.ts
This commit is contained in:
@@ -3,7 +3,7 @@ import { initializeAgentExecutorWithOptions, AgentExecutor, InitializeAgentExecu
|
||||
import { Tool } from 'langchain/tools'
|
||||
import { BaseChatMemory } from 'langchain/memory'
|
||||
import { getBaseClasses, mapChatHistory } from '../../../src/utils'
|
||||
import { BaseLanguageModel } from 'langchain/base_language'
|
||||
import { BaseChatModel } from 'langchain/chat_models/base'
|
||||
import { flatten } from 'lodash'
|
||||
import { additionalCallbacks } from '../../../src/handler'
|
||||
|
||||
@@ -29,7 +29,7 @@ class ConversationalAgent_Agents implements INode {
|
||||
constructor() {
|
||||
this.label = 'Conversational Agent'
|
||||
this.name = 'conversationalAgent'
|
||||
this.version = 1.0
|
||||
this.version = 2.0
|
||||
this.type = 'AgentExecutor'
|
||||
this.category = 'Agents'
|
||||
this.icon = 'agent.svg'
|
||||
@@ -45,7 +45,7 @@ class ConversationalAgent_Agents implements INode {
|
||||
{
|
||||
label: 'Language Model',
|
||||
name: 'model',
|
||||
type: 'BaseLanguageModel'
|
||||
type: 'BaseChatModel'
|
||||
},
|
||||
{
|
||||
label: 'Memory',
|
||||
@@ -65,7 +65,7 @@ class ConversationalAgent_Agents implements INode {
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
const model = nodeData.inputs?.model as BaseLanguageModel
|
||||
const model = nodeData.inputs?.model as BaseChatModel
|
||||
let tools = nodeData.inputs?.tools as Tool[]
|
||||
tools = flatten(tools)
|
||||
const memory = nodeData.inputs?.memory as BaseChatMemory
|
||||
@@ -92,8 +92,6 @@ class ConversationalAgent_Agents implements INode {
|
||||
const executor = nodeData.instance as AgentExecutor
|
||||
const memory = nodeData.inputs?.memory as BaseChatMemory
|
||||
|
||||
const callbacks = await additionalCallbacks(nodeData, options)
|
||||
|
||||
if (options && options.chatHistory) {
|
||||
const chatHistoryClassName = memory.chatHistory.constructor.name
|
||||
// Only replace when its In-Memory
|
||||
@@ -103,6 +101,10 @@ class ConversationalAgent_Agents implements INode {
|
||||
}
|
||||
}
|
||||
|
||||
;(executor.memory as any).returnMessages = true // Return true for BaseChatModel
|
||||
|
||||
const callbacks = await additionalCallbacks(nodeData, options)
|
||||
|
||||
const result = await executor.call({ input }, [...callbacks])
|
||||
return result?.output
|
||||
}
|
||||
|
||||
+4
-2
@@ -21,7 +21,7 @@ class ConversationalRetrievalAgent_Agents implements INode {
|
||||
constructor() {
|
||||
this.label = 'Conversational Retrieval Agent'
|
||||
this.name = 'conversationalRetrievalAgent'
|
||||
this.version = 2.0
|
||||
this.version = 3.0
|
||||
this.type = 'AgentExecutor'
|
||||
this.category = 'Agents'
|
||||
this.icon = 'agent.svg'
|
||||
@@ -42,7 +42,7 @@ class ConversationalRetrievalAgent_Agents implements INode {
|
||||
{
|
||||
label: 'OpenAI/Azure Chat Model',
|
||||
name: 'model',
|
||||
type: 'ChatOpenAI | AzureChatOpenAI'
|
||||
type: 'BaseChatModel'
|
||||
},
|
||||
{
|
||||
label: 'System Message',
|
||||
@@ -82,6 +82,8 @@ class ConversationalRetrievalAgent_Agents implements INode {
|
||||
if (executor.memory) {
|
||||
;(executor.memory as any).memoryKey = 'chat_history'
|
||||
;(executor.memory as any).outputKey = 'output'
|
||||
;(executor.memory as any).returnMessages = true
|
||||
|
||||
const chatHistoryClassName = (executor.memory as any).chatHistory.constructor.name
|
||||
// Only replace when its In-Memory
|
||||
if (chatHistoryClassName && chatHistoryClassName === 'ChatMessageHistory') {
|
||||
|
||||
@@ -23,7 +23,7 @@ class OpenAIAssistant_Agents implements INode {
|
||||
constructor() {
|
||||
this.label = 'OpenAI Assistant'
|
||||
this.name = 'openAIAssistant'
|
||||
this.version = 1.0
|
||||
this.version = 2.0
|
||||
this.type = 'OpenAIAssistant'
|
||||
this.category = 'Agents'
|
||||
this.icon = 'openai.png'
|
||||
@@ -41,6 +41,15 @@ class OpenAIAssistant_Agents implements INode {
|
||||
name: 'tools',
|
||||
type: 'Tool',
|
||||
list: true
|
||||
},
|
||||
{
|
||||
label: 'Disable File Download',
|
||||
name: 'disableFileDownload',
|
||||
type: 'boolean',
|
||||
description:
|
||||
'Messages can contain text, images, or files. In some cases, you may want to prevent others from downloading the files. Learn more from OpenAI File Annotation <a target="_blank" href="https://platform.openai.com/docs/assistants/how-it-works/managing-threads-and-messages">docs</a>',
|
||||
optional: true,
|
||||
additionalParams: true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -76,49 +85,54 @@ class OpenAIAssistant_Agents implements INode {
|
||||
return null
|
||||
}
|
||||
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const selectedAssistantId = nodeData.inputs?.selectedAssistant as string
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
let sessionId = nodeData.inputs?.sessionId as string
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const selectedAssistantId = nodeData.inputs?.selectedAssistant as string
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
let sessionId = nodeData.inputs?.sessionId as string
|
||||
|
||||
const assistant = await appDataSource.getRepository(databaseEntities['Assistant']).findOneBy({
|
||||
id: selectedAssistantId
|
||||
})
|
||||
|
||||
if (!assistant) {
|
||||
options.logger.error(`Assistant ${selectedAssistantId} not found`)
|
||||
return
|
||||
}
|
||||
|
||||
if (!sessionId && options.chatId) {
|
||||
const chatmsg = await appDataSource.getRepository(databaseEntities['ChatMessage']).findOneBy({
|
||||
chatId: options.chatId
|
||||
const assistant = await appDataSource.getRepository(databaseEntities['Assistant']).findOneBy({
|
||||
id: selectedAssistantId
|
||||
})
|
||||
if (!chatmsg) {
|
||||
options.logger.error(`Chat Message with Chat Id: ${options.chatId} not found`)
|
||||
|
||||
if (!assistant) {
|
||||
options.logger.error(`Assistant ${selectedAssistantId} not found`)
|
||||
return
|
||||
}
|
||||
sessionId = chatmsg.sessionId
|
||||
}
|
||||
|
||||
const credentialData = await getCredentialData(assistant.credential ?? '', options)
|
||||
const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, nodeData)
|
||||
if (!openAIApiKey) {
|
||||
options.logger.error(`OpenAI ApiKey not found`)
|
||||
return
|
||||
}
|
||||
if (!sessionId && options.chatId) {
|
||||
const chatmsg = await appDataSource.getRepository(databaseEntities['ChatMessage']).findOneBy({
|
||||
chatId: options.chatId
|
||||
})
|
||||
if (!chatmsg) {
|
||||
options.logger.error(`Chat Message with Chat Id: ${options.chatId} not found`)
|
||||
return
|
||||
}
|
||||
sessionId = chatmsg.sessionId
|
||||
}
|
||||
|
||||
const openai = new OpenAI({ apiKey: openAIApiKey })
|
||||
options.logger.info(`Clearing OpenAI Thread ${sessionId}`)
|
||||
if (sessionId) await openai.beta.threads.del(sessionId)
|
||||
options.logger.info(`Successfully cleared OpenAI Thread ${sessionId}`)
|
||||
const credentialData = await getCredentialData(assistant.credential ?? '', options)
|
||||
const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, nodeData)
|
||||
if (!openAIApiKey) {
|
||||
options.logger.error(`OpenAI ApiKey not found`)
|
||||
return
|
||||
}
|
||||
|
||||
const openai = new OpenAI({ apiKey: openAIApiKey })
|
||||
options.logger.info(`Clearing OpenAI Thread ${sessionId}`)
|
||||
if (sessionId) await openai.beta.threads.del(sessionId)
|
||||
options.logger.info(`Successfully cleared OpenAI Thread ${sessionId}`)
|
||||
}
|
||||
}
|
||||
|
||||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string | object> {
|
||||
const selectedAssistantId = nodeData.inputs?.selectedAssistant as string
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
const disableFileDownload = nodeData.inputs?.disableFileDownload as boolean
|
||||
|
||||
let tools = nodeData.inputs?.tools
|
||||
tools = flatten(tools)
|
||||
const formattedTools = tools?.map((tool: any) => formatToOpenAIAssistantTool(tool)) ?? []
|
||||
@@ -310,7 +324,7 @@ class OpenAIAssistant_Agents implements INode {
|
||||
|
||||
const dirPath = path.join(getUserHome(), '.flowise', 'openai-assistant')
|
||||
|
||||
// Iterate over the annotations and add footnotes
|
||||
// Iterate over the annotations
|
||||
for (let index = 0; index < annotations.length; index++) {
|
||||
const annotation = annotations[index]
|
||||
let filePath = ''
|
||||
@@ -323,11 +337,13 @@ class OpenAIAssistant_Agents implements INode {
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename
|
||||
filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName)
|
||||
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
|
||||
fileAnnotations.push({
|
||||
filePath,
|
||||
fileName
|
||||
})
|
||||
if (!disableFileDownload) {
|
||||
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
|
||||
fileAnnotations.push({
|
||||
filePath,
|
||||
fileName
|
||||
})
|
||||
}
|
||||
} else {
|
||||
const file_path = (annotation as OpenAI.Beta.Threads.Messages.MessageContentText.Text.FilePath).file_path
|
||||
if (file_path) {
|
||||
@@ -335,22 +351,30 @@ class OpenAIAssistant_Agents implements INode {
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename
|
||||
filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName)
|
||||
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
|
||||
fileAnnotations.push({
|
||||
filePath,
|
||||
fileName
|
||||
})
|
||||
if (!disableFileDownload) {
|
||||
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
|
||||
fileAnnotations.push({
|
||||
filePath,
|
||||
fileName
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the text with a footnote
|
||||
message_content.value = message_content.value.replace(`${annotation.text}`, `${filePath}`)
|
||||
message_content.value = message_content.value.replace(
|
||||
`${annotation.text}`,
|
||||
`${disableFileDownload ? '' : filePath}`
|
||||
)
|
||||
}
|
||||
|
||||
returnVal += message_content.value
|
||||
} else {
|
||||
returnVal += content.text.value
|
||||
}
|
||||
|
||||
const lenticularBracketRegex = /【[^】]*】/g
|
||||
returnVal = returnVal.replace(lenticularBracketRegex, '')
|
||||
} else {
|
||||
const content = assistantMessages[0].content[i] as MessageContentImageFile
|
||||
const fileId = content.image_file.file_id
|
||||
|
||||
@@ -20,7 +20,7 @@ class OpenAIFunctionAgent_Agents implements INode {
|
||||
constructor() {
|
||||
this.label = 'OpenAI Function Agent'
|
||||
this.name = 'openAIFunctionAgent'
|
||||
this.version = 2.0
|
||||
this.version = 3.0
|
||||
this.type = 'AgentExecutor'
|
||||
this.category = 'Agents'
|
||||
this.icon = 'openai.png'
|
||||
@@ -41,7 +41,7 @@ class OpenAIFunctionAgent_Agents implements INode {
|
||||
{
|
||||
label: 'OpenAI/Azure Chat Model',
|
||||
name: 'model',
|
||||
type: 'ChatOpenAI | AzureChatOpenAI'
|
||||
type: 'BaseChatModel'
|
||||
},
|
||||
{
|
||||
label: 'System Message',
|
||||
@@ -87,6 +87,8 @@ class OpenAIFunctionAgent_Agents implements INode {
|
||||
}
|
||||
}
|
||||
|
||||
;(executor.memory as any).returnMessages = true // Return true for BaseChatModel
|
||||
|
||||
const loggerHandler = new ConsoleCallbackHandler(options.logger)
|
||||
const callbacks = await additionalCallbacks(nodeData, options)
|
||||
|
||||
|
||||
@@ -106,16 +106,18 @@ class ConversationChain_Chains implements INode {
|
||||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
|
||||
const chain = nodeData.instance as ConversationChain
|
||||
const memory = nodeData.inputs?.memory as BufferMemory
|
||||
memory.returnMessages = true // Return true for BaseChatModel
|
||||
|
||||
if (options && options.chatHistory) {
|
||||
const chatHistoryClassName = memory.chatHistory.constructor.name
|
||||
// Only replace when its In-Memory
|
||||
if (chatHistoryClassName && chatHistoryClassName === 'ChatMessageHistory') {
|
||||
memory.chatHistory = mapChatHistory(options)
|
||||
chain.memory = memory
|
||||
}
|
||||
}
|
||||
|
||||
chain.memory = memory
|
||||
|
||||
const loggerHandler = new ConsoleCallbackHandler(options.logger)
|
||||
const callbacks = await additionalCallbacks(nodeData, options)
|
||||
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src'
|
||||
import {
|
||||
ICommonObject,
|
||||
INode,
|
||||
INodeData,
|
||||
INodeParams,
|
||||
getBaseClasses,
|
||||
getCredentialData,
|
||||
getCredentialParam,
|
||||
serializeChatHistory
|
||||
} from '../../../src'
|
||||
import { DynamoDBChatMessageHistory } from 'langchain/stores/message/dynamodb'
|
||||
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
||||
|
||||
@@ -70,13 +79,23 @@ class DynamoDb_Memory implements INode {
|
||||
return initalizeDynamoDB(nodeData, options)
|
||||
}
|
||||
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const dynamodbMemory = await initalizeDynamoDB(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing DynamoDb memory session ${sessionId ? sessionId : chatId}`)
|
||||
await dynamodbMemory.clear()
|
||||
options.logger.info(`Successfully cleared DynamoDb memory session ${sessionId ? sessionId : chatId}`)
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const dynamodbMemory = await initalizeDynamoDB(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing DynamoDb memory session ${sessionId ? sessionId : chatId}`)
|
||||
await dynamodbMemory.clear()
|
||||
options.logger.info(`Successfully cleared DynamoDb memory session ${sessionId ? sessionId : chatId}`)
|
||||
},
|
||||
async getChatMessages(nodeData: INodeData, options: ICommonObject): Promise<string> {
|
||||
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||
const dynamodbMemory = await initalizeDynamoDB(nodeData, options)
|
||||
const key = memoryKey ?? 'chat_history'
|
||||
const memoryResult = await dynamodbMemory.loadMemoryVariables({})
|
||||
return serializeChatHistory(memoryResult[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,9 +128,8 @@ const initalizeDynamoDB = async (nodeData: INodeData, options: ICommonObject): P
|
||||
})
|
||||
|
||||
const memory = new BufferMemoryExtended({
|
||||
memoryKey,
|
||||
memoryKey: memoryKey ?? 'chat_history',
|
||||
chatHistory: dynamoDb,
|
||||
returnMessages: true,
|
||||
isSessionIdUsingChatMessageId
|
||||
})
|
||||
return memory
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
|
||||
import {
|
||||
getBaseClasses,
|
||||
getCredentialData,
|
||||
getCredentialParam,
|
||||
ICommonObject,
|
||||
INode,
|
||||
INodeData,
|
||||
INodeParams,
|
||||
serializeChatHistory
|
||||
} from '../../../src'
|
||||
import { MongoDBChatMessageHistory } from 'langchain/stores/message/mongodb'
|
||||
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
||||
import { BaseMessage, mapStoredMessageToChatMessage } from 'langchain/schema'
|
||||
@@ -67,13 +76,23 @@ class MongoDB_Memory implements INode {
|
||||
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
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing MongoDB memory session ${sessionId ? sessionId : chatId}`)
|
||||
await mongodbMemory.clear()
|
||||
options.logger.info(`Successfully cleared MongoDB memory session ${sessionId ? sessionId : chatId}`)
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const mongodbMemory = await initializeMongoDB(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing MongoDB memory session ${sessionId ? sessionId : chatId}`)
|
||||
await mongodbMemory.clear()
|
||||
options.logger.info(`Successfully cleared MongoDB memory session ${sessionId ? sessionId : chatId}`)
|
||||
},
|
||||
async getChatMessages(nodeData: INodeData, options: ICommonObject): Promise<string> {
|
||||
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||
const mongodbMemory = await initializeMongoDB(nodeData, options)
|
||||
const key = memoryKey ?? 'chat_history'
|
||||
const memoryResult = await mongodbMemory.loadMemoryVariables({})
|
||||
return serializeChatHistory(memoryResult[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,9 +142,8 @@ const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): P
|
||||
}
|
||||
|
||||
return new BufferMemoryExtended({
|
||||
memoryKey,
|
||||
memoryKey: memoryKey ?? 'chat_history',
|
||||
chatHistory: mongoDBChatMessageHistory,
|
||||
returnMessages: true,
|
||||
isSessionIdUsingChatMessageId
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../
|
||||
import { ICommonObject } from '../../../src'
|
||||
import { MotorheadMemory, MotorheadMemoryInput } from 'langchain/memory'
|
||||
import fetch from 'node-fetch'
|
||||
import { getBufferString } from 'langchain/memory'
|
||||
|
||||
class MotorMemory_Memory implements INode {
|
||||
label: string
|
||||
@@ -64,13 +65,23 @@ class MotorMemory_Memory implements INode {
|
||||
return initalizeMotorhead(nodeData, options)
|
||||
}
|
||||
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const motorhead = await initalizeMotorhead(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Motorhead memory session ${sessionId ? sessionId : chatId}`)
|
||||
await motorhead.clear()
|
||||
options.logger.info(`Successfully cleared Motorhead memory session ${sessionId ? sessionId : chatId}`)
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const motorhead = await initalizeMotorhead(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Motorhead memory session ${sessionId ? sessionId : chatId}`)
|
||||
await motorhead.clear()
|
||||
options.logger.info(`Successfully cleared Motorhead memory session ${sessionId ? sessionId : chatId}`)
|
||||
},
|
||||
async getChatMessages(nodeData: INodeData, options: ICommonObject): Promise<string> {
|
||||
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||
const motorhead = await initalizeMotorhead(nodeData, options)
|
||||
const key = memoryKey ?? 'chat_history'
|
||||
const memoryResult = await motorhead.loadMemoryVariables({})
|
||||
return getBufferString(memoryResult[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { INode, INodeData, INodeParams, ICommonObject } from '../../../src/Interface'
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam, serializeChatHistory } from '../../../src/utils'
|
||||
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
||||
import { RedisChatMessageHistory, RedisChatMessageHistoryInput } from 'langchain/stores/message/ioredis'
|
||||
import { mapStoredMessageToChatMessage, BaseMessage } from 'langchain/schema'
|
||||
@@ -65,13 +65,23 @@ class RedisBackedChatMemory_Memory implements INode {
|
||||
return await initalizeRedis(nodeData, options)
|
||||
}
|
||||
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const redis = await initalizeRedis(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
await redis.clear()
|
||||
options.logger.info(`Successfully cleared Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const redis = await initalizeRedis(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
await redis.clear()
|
||||
options.logger.info(`Successfully cleared Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
},
|
||||
async getChatMessages(nodeData: INodeData, options: ICommonObject): Promise<string> {
|
||||
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||
const redis = await initalizeRedis(nodeData, options)
|
||||
const key = memoryKey ?? 'chat_history'
|
||||
const memoryResult = await redis.loadMemoryVariables({})
|
||||
return serializeChatHistory(memoryResult[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +147,7 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom
|
||||
}
|
||||
|
||||
const memory = new BufferMemoryExtended({
|
||||
memoryKey,
|
||||
memoryKey: memoryKey ?? 'chat_history',
|
||||
chatHistory: redisChatMessageHistory,
|
||||
isSessionIdUsingChatMessageId
|
||||
})
|
||||
|
||||
+18
-8
@@ -1,5 +1,5 @@
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam, serializeChatHistory } from '../../../src/utils'
|
||||
import { ICommonObject } from '../../../src'
|
||||
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
||||
import { UpstashRedisChatMessageHistory } from 'langchain/stores/message/upstash_redis'
|
||||
@@ -63,13 +63,22 @@ class UpstashRedisBackedChatMemory_Memory implements INode {
|
||||
return initalizeUpstashRedis(nodeData, options)
|
||||
}
|
||||
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const redis = await initalizeUpstashRedis(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Upstash Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
await redis.clear()
|
||||
options.logger.info(`Successfully cleared Upstash Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const redis = await initalizeUpstashRedis(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Upstash Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
await redis.clear()
|
||||
options.logger.info(`Successfully cleared Upstash Redis memory session ${sessionId ? sessionId : chatId}`)
|
||||
},
|
||||
async getChatMessages(nodeData: INodeData, options: ICommonObject): Promise<string> {
|
||||
const redis = await initalizeUpstashRedis(nodeData, options)
|
||||
const key = 'chat_history'
|
||||
const memoryResult = await redis.loadMemoryVariables({})
|
||||
return serializeChatHistory(memoryResult[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +104,7 @@ const initalizeUpstashRedis = async (nodeData: INodeData, options: ICommonObject
|
||||
})
|
||||
|
||||
const memory = new BufferMemoryExtended({
|
||||
memoryKey: 'chat_history',
|
||||
chatHistory: redisChatMessageHistory,
|
||||
isSessionIdUsingChatMessageId
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { SystemMessage } from 'langchain/schema'
|
||||
import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
|
||||
import { getBufferString, InputValues, MemoryVariables, OutputValues } from 'langchain/memory'
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||
import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
|
||||
import { ICommonObject } from '../../../src'
|
||||
|
||||
class ZepMemory_Memory implements INode {
|
||||
@@ -19,7 +19,7 @@ class ZepMemory_Memory implements INode {
|
||||
constructor() {
|
||||
this.label = 'Zep Memory'
|
||||
this.name = 'ZepMemory'
|
||||
this.version = 1.0
|
||||
this.version = 2.0
|
||||
this.type = 'ZepMemory'
|
||||
this.icon = 'zep.png'
|
||||
this.category = 'Memory'
|
||||
@@ -40,17 +40,12 @@ class ZepMemory_Memory implements INode {
|
||||
type: 'string',
|
||||
default: 'http://127.0.0.1:8000'
|
||||
},
|
||||
{
|
||||
label: 'Auto Summary',
|
||||
name: 'autoSummary',
|
||||
type: 'boolean',
|
||||
default: true
|
||||
},
|
||||
{
|
||||
label: 'Session Id',
|
||||
name: 'sessionId',
|
||||
type: 'string',
|
||||
description: 'If not specified, the first CHAT_MESSAGE_ID will be used as sessionId',
|
||||
description:
|
||||
'If not specified, a random id will be used. Learn <a target="_blank" href="https://docs.flowiseai.com/memory/long-term-memory#ui-and-embedded-chat">more</a>',
|
||||
default: '',
|
||||
additionalParams: true,
|
||||
optional: true
|
||||
@@ -59,15 +54,10 @@ class ZepMemory_Memory implements INode {
|
||||
label: 'Size',
|
||||
name: 'k',
|
||||
type: 'number',
|
||||
default: '10',
|
||||
description: 'Window of size k to surface the last k back-and-forth to use as memory.'
|
||||
},
|
||||
{
|
||||
label: 'Auto Summary Template',
|
||||
name: 'autoSummaryTemplate',
|
||||
type: 'string',
|
||||
default: 'This is the summary of the following conversation:\n{summary}',
|
||||
additionalParams: true
|
||||
placeholder: '10',
|
||||
description: 'Window of size k to surface the last k back-and-forth to use as memory.',
|
||||
additionalParams: true,
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'AI Prefix',
|
||||
@@ -108,45 +98,28 @@ class ZepMemory_Memory implements INode {
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||
const autoSummaryTemplate = nodeData.inputs?.autoSummaryTemplate as string
|
||||
const autoSummary = nodeData.inputs?.autoSummary as boolean
|
||||
|
||||
const k = nodeData.inputs?.k as string
|
||||
|
||||
let zep = await initalizeZep(nodeData, options)
|
||||
|
||||
// hack to support summary
|
||||
let tmpFunc = zep.loadMemoryVariables
|
||||
zep.loadMemoryVariables = async (values) => {
|
||||
let data = await tmpFunc.bind(zep, values)()
|
||||
if (autoSummary && zep.returnMessages && data[zep.memoryKey] && data[zep.memoryKey].length) {
|
||||
const zepClient = await zep.zepClientPromise
|
||||
const memory = await zepClient.memory.getMemory(zep.sessionId, parseInt(k, 10) ?? 10)
|
||||
if (memory?.summary) {
|
||||
let summary = autoSummaryTemplate.replace(/{summary}/g, memory.summary.content)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[ZepMemory] auto summary:', summary)
|
||||
data[zep.memoryKey].unshift(new SystemMessage(summary))
|
||||
}
|
||||
}
|
||||
// for langchain zep memory compatibility, or we will get "Missing value for input variable chat_history"
|
||||
if (data instanceof Array) {
|
||||
data = {
|
||||
[zep.memoryKey]: data
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
return zep
|
||||
return await initalizeZep(nodeData, options)
|
||||
}
|
||||
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const zep = await initalizeZep(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Zep memory session ${sessionId ? sessionId : chatId}`)
|
||||
await zep.clear()
|
||||
options.logger.info(`Successfully cleared Zep memory session ${sessionId ? sessionId : chatId}`)
|
||||
//@ts-ignore
|
||||
memoryMethods = {
|
||||
async clearSessionMemory(nodeData: INodeData, options: ICommonObject): Promise<void> {
|
||||
const zep = await initalizeZep(nodeData, options)
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const chatId = options?.chatId as string
|
||||
options.logger.info(`Clearing Zep memory session ${sessionId ? sessionId : chatId}`)
|
||||
await zep.clear()
|
||||
options.logger.info(`Successfully cleared Zep memory session ${sessionId ? sessionId : chatId}`)
|
||||
},
|
||||
async getChatMessages(nodeData: INodeData, options: ICommonObject): Promise<string> {
|
||||
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||
const aiPrefix = nodeData.inputs?.aiPrefix as string
|
||||
const humanPrefix = nodeData.inputs?.humanPrefix as string
|
||||
const zep = await initalizeZep(nodeData, options)
|
||||
const key = memoryKey ?? 'chat_history'
|
||||
const memoryResult = await zep.loadMemoryVariables({})
|
||||
return getBufferString(memoryResult[key], humanPrefix, aiPrefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,40 +129,72 @@ const initalizeZep = async (nodeData: INodeData, options: ICommonObject): Promis
|
||||
const humanPrefix = nodeData.inputs?.humanPrefix as string
|
||||
const memoryKey = nodeData.inputs?.memoryKey as string
|
||||
const inputKey = nodeData.inputs?.inputKey as string
|
||||
const sessionId = nodeData.inputs?.sessionId as string
|
||||
const k = nodeData.inputs?.k as string
|
||||
const chatId = options?.chatId as string
|
||||
|
||||
let isSessionIdUsingChatMessageId = false
|
||||
if (!sessionId && chatId) isSessionIdUsingChatMessageId = true
|
||||
let sessionId = ''
|
||||
|
||||
if (!nodeData.inputs?.sessionId && chatId) {
|
||||
isSessionIdUsingChatMessageId = true
|
||||
sessionId = chatId
|
||||
} else {
|
||||
sessionId = nodeData.inputs?.sessionId
|
||||
}
|
||||
|
||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||
const apiKey = getCredentialParam('apiKey', credentialData, nodeData)
|
||||
|
||||
const obj: ZepMemoryInput & Partial<ZepMemoryExtendedInput> = {
|
||||
const obj: ZepMemoryInput & ZepMemoryExtendedInput = {
|
||||
baseURL,
|
||||
sessionId: sessionId ? sessionId : chatId,
|
||||
aiPrefix,
|
||||
humanPrefix,
|
||||
returnMessages: true,
|
||||
memoryKey,
|
||||
inputKey
|
||||
inputKey,
|
||||
isSessionIdUsingChatMessageId,
|
||||
k: k ? parseInt(k, 10) : undefined
|
||||
}
|
||||
if (apiKey) obj.apiKey = apiKey
|
||||
if (isSessionIdUsingChatMessageId) obj.isSessionIdUsingChatMessageId = true
|
||||
|
||||
return new ZepMemoryExtended(obj)
|
||||
}
|
||||
|
||||
interface ZepMemoryExtendedInput {
|
||||
isSessionIdUsingChatMessageId: boolean
|
||||
k?: number
|
||||
}
|
||||
|
||||
class ZepMemoryExtended extends ZepMemory {
|
||||
isSessionIdUsingChatMessageId? = false
|
||||
lastN?: number
|
||||
|
||||
constructor(fields: ZepMemoryInput & Partial<ZepMemoryExtendedInput>) {
|
||||
constructor(fields: ZepMemoryInput & ZepMemoryExtendedInput) {
|
||||
super(fields)
|
||||
this.isSessionIdUsingChatMessageId = fields.isSessionIdUsingChatMessageId
|
||||
this.lastN = fields.k
|
||||
}
|
||||
|
||||
async loadMemoryVariables(values: InputValues, overrideSessionId = ''): Promise<MemoryVariables> {
|
||||
if (overrideSessionId) {
|
||||
super.sessionId = overrideSessionId
|
||||
}
|
||||
return super.loadMemoryVariables({ ...values, lastN: this.lastN })
|
||||
}
|
||||
|
||||
async saveContext(inputValues: InputValues, outputValues: OutputValues, overrideSessionId = ''): Promise<void> {
|
||||
if (overrideSessionId) {
|
||||
super.sessionId = overrideSessionId
|
||||
}
|
||||
return super.saveContext(inputValues, outputValues)
|
||||
}
|
||||
|
||||
async clear(overrideSessionId = ''): Promise<void> {
|
||||
if (overrideSessionId) {
|
||||
super.sessionId = overrideSessionId
|
||||
}
|
||||
return super.clear()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class OpenAIModeration implements INode {
|
||||
this.name = 'inputModerationOpenAI'
|
||||
this.version = 1.0
|
||||
this.type = 'Moderation'
|
||||
this.icon = 'openai-moderation.png'
|
||||
this.icon = 'openai.png'
|
||||
this.category = 'Moderation'
|
||||
this.description = 'Check whether content complies with OpenAI usage policies.'
|
||||
this.baseClasses = [this.type, ...getBaseClasses(Moderation)]
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 47 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
+1
-1
@@ -14,7 +14,7 @@ export class SimplePromptModerationRunner implements Moderation {
|
||||
|
||||
async checkForViolations(input: string): Promise<string> {
|
||||
this.denyList.split('\n').forEach((denyListItem) => {
|
||||
if (denyListItem && denyListItem !== '' && input.includes(denyListItem)) {
|
||||
if (denyListItem && denyListItem !== '' && input.toLowerCase().includes(denyListItem.toLowerCase())) {
|
||||
throw Error(this.moderationErrorMessage)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { DynamicTool, DynamicToolInput } from 'langchain/tools'
|
||||
import { BaseChain } from 'langchain/chains'
|
||||
import { handleEscapeCharacters } from '../../../src/utils'
|
||||
|
||||
export interface ChainToolInput extends Omit<DynamicToolInput, 'func'> {
|
||||
chain: BaseChain
|
||||
@@ -14,7 +15,8 @@ export class ChainTool extends DynamicTool {
|
||||
func: async (input, runManager) => {
|
||||
// To enable LLM Chain which has promptValues
|
||||
if ((chain as any).prompt && (chain as any).prompt.promptValues) {
|
||||
const values = await chain.call((chain as any).prompt.promptValues, runManager?.getChild())
|
||||
const promptValues = handleEscapeCharacters((chain as any).prompt.promptValues, true)
|
||||
const values = await chain.call(promptValues, runManager?.getChild())
|
||||
return values?.text
|
||||
}
|
||||
return chain.run(input, runManager?.getChild())
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import { ZapierNLAWrapper, ZapierNLAWrapperParams } from 'langchain/tools'
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { ZapierToolKit } from 'langchain/agents'
|
||||
import { getCredentialData, getCredentialParam } from '../../../src'
|
||||
|
||||
class ZapierNLA_Tools 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 = 'Zapier NLA'
|
||||
this.name = 'zapierNLA'
|
||||
this.version = 1.0
|
||||
this.type = 'ZapierNLA'
|
||||
this.icon = 'zapier.svg'
|
||||
this.category = 'Tools'
|
||||
this.description = "Access to apps and actions on Zapier's platform through a natural language API interface"
|
||||
this.badge = 'DEPRECATING'
|
||||
this.inputs = []
|
||||
this.credential = {
|
||||
label: 'Connect Credential',
|
||||
name: 'credential',
|
||||
type: 'credential',
|
||||
credentialNames: ['zapierNLAApi']
|
||||
}
|
||||
this.baseClasses = [this.type, 'Tool']
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||
const zapierNLAApiKey = getCredentialParam('zapierNLAApiKey', credentialData, nodeData)
|
||||
|
||||
const obj: Partial<ZapierNLAWrapperParams> = {
|
||||
apiKey: zapierNLAApiKey
|
||||
}
|
||||
const zapier = new ZapierNLAWrapper(obj)
|
||||
const toolkit = await ZapierToolKit.fromZapierNLAWrapper(zapier)
|
||||
|
||||
return toolkit.tools
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: ZapierNLA_Tools }
|
||||
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="800px" height="800px" viewBox="0 0 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<g>
|
||||
<path d="M128.080089,-0.000183105 C135.311053,0.0131003068 142.422517,0.624138494 149.335663,1.77979593 L149.335663,1.77979593 L149.335663,76.2997796 L202.166953,23.6044907 C208.002065,27.7488446 213.460883,32.3582023 218.507811,37.3926715 C223.557281,42.4271407 228.192318,47.8867213 232.346817,53.7047992 L232.346817,53.7047992 L179.512985,106.400063 L254.227854,106.400063 C255.387249,113.29414 256,120.36111 256,127.587243 L256,127.587243 L256,127.759881 C256,134.986013 255.387249,142.066204 254.227854,148.960282 L254.227854,148.960282 L179.500273,148.960282 L232.346817,201.642324 C228.192318,207.460402 223.557281,212.919983 218.523066,217.954452 L218.523066,217.954452 L218.507811,217.954452 C213.460883,222.988921 208.002065,227.6115 202.182208,231.742607 L202.182208,231.742607 L149.335663,179.04709 L149.335663,253.5672 C142.435229,254.723036 135.323765,255.333244 128.092802,255.348499 L128.092802,255.348499 L127.907197,255.348499 C120.673691,255.333244 113.590195,254.723036 106.677048,253.5672 L106.677048,253.5672 L106.677048,179.04709 L53.8457596,231.742607 C42.1780766,223.466917 31.977435,213.278734 23.6658953,201.642324 L23.6658953,201.642324 L76.4997269,148.960282 L1.78485803,148.960282 C0.612750404,142.052729 0,134.946095 0,127.719963 L0,127.719963 L0,127.349037 C0.0121454869,125.473817 0.134939797,123.182933 0.311311815,120.812834 L0.36577283,120.099764 C0.887996182,113.428547 1.78485803,106.400063 1.78485803,106.400063 L1.78485803,106.400063 L76.4997269,106.400063 L23.6658953,53.7047992 C27.8076812,47.8867213 32.4300059,42.4403618 37.4769335,37.4193681 L37.4769335,37.4193681 L37.5023588,37.3926715 C42.5391163,32.3582023 48.0106469,27.7488446 53.8457596,23.6044907 L53.8457596,23.6044907 L106.677048,76.2997796 L106.677048,1.77979593 C113.590195,0.624138494 120.688946,0.0131003068 127.932622,-0.000183105 L127.932622,-0.000183105 L128.080089,-0.000183105 Z M128.067377,95.7600714 L127.945335,95.7600714 C118.436262,95.7600714 109.32891,97.5001809 100.910584,100.661566 C97.7553011,109.043534 96.0085811,118.129275 95.9958684,127.613685 L95.9958684,127.733184 C96.0085811,137.217594 97.7553011,146.303589 100.923296,154.685303 C109.32891,157.846943 118.436262,159.587052 127.945335,159.587052 L128.067377,159.587052 C137.576449,159.587052 146.683802,157.846943 155.089415,154.685303 C158.257411,146.290368 160.004131,137.217594 160.004131,127.733184 L160.004131,127.613685 C160.004131,118.129275 158.257411,109.043534 155.089415,100.661566 C146.683802,97.5001809 137.576449,95.7600714 128.067377,95.7600714 Z" fill="#FF4A00" fill-rule="nonzero">
|
||||
|
||||
</path>
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 2.7 KiB |
Reference in New Issue
Block a user