mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 03:01:10 +03:00
add missing methods, abstract classes
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
import { FlowiseMemory, ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||||
import { AgentExecutor as LCAgentExecutor, AgentExecutorInput } from 'langchain/agents'
|
import { AgentExecutor as LCAgentExecutor, AgentExecutorInput } from 'langchain/agents'
|
||||||
import { ChainValues, AgentStep, AgentFinish, AgentAction, BaseMessage, FunctionMessage, AIMessage } from 'langchain/schema'
|
import { ChainValues, AgentStep, AgentFinish, AgentAction, BaseMessage, FunctionMessage, AIMessage } from 'langchain/schema'
|
||||||
import { OutputParserException } from 'langchain/schema/output_parser'
|
import { OutputParserException } from 'langchain/schema/output_parser'
|
||||||
@@ -7,7 +7,6 @@ import { formatToOpenAIFunction } from 'langchain/tools'
|
|||||||
import { ToolInputParsingException, Tool } from '@langchain/core/tools'
|
import { ToolInputParsingException, Tool } from '@langchain/core/tools'
|
||||||
import { getBaseClasses } from '../../../src/utils'
|
import { getBaseClasses } from '../../../src/utils'
|
||||||
import { flatten } from 'lodash'
|
import { flatten } from 'lodash'
|
||||||
import { BaseChatMemory } from 'langchain/memory'
|
|
||||||
import { RunnableSequence } from 'langchain/schema/runnable'
|
import { RunnableSequence } from 'langchain/schema/runnable'
|
||||||
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
|
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
|
||||||
import { ChatPromptTemplate, MessagesPlaceholder } from 'langchain/prompts'
|
import { ChatPromptTemplate, MessagesPlaceholder } from 'langchain/prompts'
|
||||||
@@ -65,7 +64,7 @@ class OpenAIFunctionAgent_Agents implements INode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init(nodeData: INodeData): Promise<any> {
|
async init(nodeData: INodeData): Promise<any> {
|
||||||
const memory = nodeData.inputs?.memory as BaseChatMemory
|
const memory = nodeData.inputs?.memory as FlowiseMemory
|
||||||
|
|
||||||
const executor = prepareAgent(nodeData, this.sessionId)
|
const executor = prepareAgent(nodeData, this.sessionId)
|
||||||
if (memory) executor.memory = memory
|
if (memory) executor.memory = memory
|
||||||
@@ -74,7 +73,7 @@ class OpenAIFunctionAgent_Agents implements INode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
|
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
|
||||||
const memory = nodeData.inputs?.memory
|
const memory = nodeData.inputs?.memory as FlowiseMemory
|
||||||
|
|
||||||
const executor = prepareAgent(nodeData, this.sessionId)
|
const executor = prepareAgent(nodeData, this.sessionId)
|
||||||
|
|
||||||
@@ -120,7 +119,7 @@ const formatAgentSteps = (steps: AgentStep[]): BaseMessage[] =>
|
|||||||
|
|
||||||
const prepareAgent = (nodeData: INodeData, sessionId?: string) => {
|
const prepareAgent = (nodeData: INodeData, sessionId?: string) => {
|
||||||
const model = nodeData.inputs?.model as ChatOpenAI
|
const model = nodeData.inputs?.model as ChatOpenAI
|
||||||
const memory = nodeData.inputs?.memory
|
const memory = nodeData.inputs?.memory as FlowiseMemory
|
||||||
const systemMessage = nodeData.inputs?.systemMessage as string
|
const systemMessage = nodeData.inputs?.systemMessage as string
|
||||||
let tools = nodeData.inputs?.tools
|
let tools = nodeData.inputs?.tools
|
||||||
tools = flatten(tools)
|
tools = flatten(tools)
|
||||||
@@ -143,7 +142,7 @@ const prepareAgent = (nodeData: INodeData, sessionId?: string) => {
|
|||||||
[inputKey]: (i: { input: string; steps: AgentStep[] }) => i.input,
|
[inputKey]: (i: { input: string; steps: AgentStep[] }) => i.input,
|
||||||
agent_scratchpad: (i: { input: string; steps: AgentStep[] }) => formatAgentSteps(i.steps),
|
agent_scratchpad: (i: { input: string; steps: AgentStep[] }) => formatAgentSteps(i.steps),
|
||||||
[memoryKey]: async (_: { input: string; steps: AgentStep[] }) => {
|
[memoryKey]: async (_: { input: string; steps: AgentStep[] }) => {
|
||||||
const messages: BaseMessage[] = await memory.getChatMessages(sessionId, true)
|
const messages = (await memory.getChatMessages(sessionId, true)) as BaseMessage[]
|
||||||
return messages ?? []
|
return messages ?? []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IMessage, INode, INodeData, INodeParams, MessageType } from '../../../src/Interface'
|
import { IMessage, INode, INodeData, INodeParams, MemoryMethods, MessageType } from '../../../src/Interface'
|
||||||
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||||
import { ICommonObject } from '../../../src'
|
import { ICommonObject } from '../../../src'
|
||||||
import { MotorheadMemory, MotorheadMemoryInput, InputValues, MemoryVariables, OutputValues, getBufferString } from 'langchain/memory'
|
import { MotorheadMemory, MotorheadMemoryInput, InputValues, MemoryVariables, OutputValues, getBufferString } from 'langchain/memory'
|
||||||
@@ -136,7 +136,7 @@ interface MotorheadMemoryExtendedInput {
|
|||||||
isSessionIdUsingChatMessageId: boolean
|
isSessionIdUsingChatMessageId: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
class MotorheadMemoryExtended extends MotorheadMemory {
|
class MotorheadMemoryExtended extends MotorheadMemory implements MemoryMethods {
|
||||||
isSessionIdUsingChatMessageId? = false
|
isSessionIdUsingChatMessageId? = false
|
||||||
|
|
||||||
constructor(fields: MotorheadMemoryInput & MotorheadMemoryExtendedInput) {
|
constructor(fields: MotorheadMemoryInput & MotorheadMemoryExtendedInput) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IMessage, INode, INodeData, INodeParams, MessageType } from '../../../src/Interface'
|
import { IMessage, INode, INodeData, INodeParams, MemoryMethods, MessageType } from '../../../src/Interface'
|
||||||
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||||
import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
|
import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
|
||||||
import { ICommonObject } from '../../../src'
|
import { ICommonObject } from '../../../src'
|
||||||
@@ -167,7 +167,7 @@ interface ZepMemoryExtendedInput {
|
|||||||
k?: number
|
k?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZepMemoryExtended extends ZepMemory {
|
class ZepMemoryExtended extends ZepMemory implements MemoryMethods {
|
||||||
isSessionIdUsingChatMessageId? = false
|
isSessionIdUsingChatMessageId? = false
|
||||||
lastN?: number
|
lastN?: number
|
||||||
|
|
||||||
|
|||||||
@@ -214,16 +214,19 @@ export abstract class FlowiseMemory extends BufferMemory implements MemoryMethod
|
|||||||
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
|
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
|
||||||
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
|
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
|
||||||
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
|
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
|
||||||
|
abstract resumeMessages(messages: IMessage[]): Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class FlowiseWindowMemory extends BufferWindowMemory implements MemoryMethods {
|
export abstract class FlowiseWindowMemory extends BufferWindowMemory implements MemoryMethods {
|
||||||
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
|
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
|
||||||
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
|
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
|
||||||
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
|
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
|
||||||
|
abstract resumeMessages(messages: IMessage[]): Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class FlowiseSummaryMemory extends ConversationSummaryMemory implements MemoryMethods {
|
export abstract class FlowiseSummaryMemory extends ConversationSummaryMemory implements MemoryMethods {
|
||||||
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
|
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
|
||||||
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
|
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
|
||||||
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
|
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
|
||||||
|
abstract resumeMessages(messages: IMessage[]): Promise<void>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1052,29 +1052,3 @@ export const getAllValuesFromJson = (obj: any): any[] => {
|
|||||||
extractValues(obj)
|
extractValues(obj)
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
export const replaceEnvVariables = async (question: string, appDataSource: DataSource): Promise<string> => {
|
|
||||||
// the incoming question can have more than one env variable with the pattern {{ env.VARIABLE_NAME }}
|
|
||||||
// extract all the env variables from the question and iterate through them
|
|
||||||
const envVariables = question.match(/{{[^}]*}}/g)
|
|
||||||
if (envVariables) {
|
|
||||||
for (const envVariable of envVariables) {
|
|
||||||
// this is needed as the user can have spaces between the curly braces and the env keyword
|
|
||||||
// extract the variable name from the env variable
|
|
||||||
const variableName = envVariable.replace(/{{\s*env.|\s*}}/g, '')
|
|
||||||
// get the value of the env variable from the database
|
|
||||||
const variable = await appDataSource.getRepository(Variable).findOneBy({
|
|
||||||
name: variableName
|
|
||||||
})
|
|
||||||
if (variable) {
|
|
||||||
let value = variable.value
|
|
||||||
if (variable.type === 'runtime') {
|
|
||||||
value = process.env[variable.name] as string
|
|
||||||
}
|
|
||||||
// replace the env variable with the value from the database
|
|
||||||
question = question.replace(envVariable, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return question
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -215,7 +215,7 @@ const Variables = () => {
|
|||||||
</Toolbar>
|
</Toolbar>
|
||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
{variables.length <= 0 && (
|
{variables.length === 0 && (
|
||||||
<Stack sx={{ alignItems: 'center', justifyContent: 'center' }} flexDirection='column'>
|
<Stack sx={{ alignItems: 'center', justifyContent: 'center' }} flexDirection='column'>
|
||||||
<Box sx={{ p: 2, height: 'auto' }}>
|
<Box sx={{ p: 2, height: 'auto' }}>
|
||||||
<img
|
<img
|
||||||
|
|||||||
Reference in New Issue
Block a user