add missing methods, abstract classes

This commit is contained in:
Henry
2023-12-26 15:54:50 +00:00
parent 3126442e67
commit 6306904cfc
6 changed files with 13 additions and 37 deletions
@@ -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 { ChainValues, AgentStep, AgentFinish, AgentAction, BaseMessage, FunctionMessage, AIMessage } from 'langchain/schema'
import { OutputParserException } from 'langchain/schema/output_parser'
@@ -7,7 +7,6 @@ import { formatToOpenAIFunction } from 'langchain/tools'
import { ToolInputParsingException, Tool } from '@langchain/core/tools'
import { getBaseClasses } from '../../../src/utils'
import { flatten } from 'lodash'
import { BaseChatMemory } from 'langchain/memory'
import { RunnableSequence } from 'langchain/schema/runnable'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import { ChatPromptTemplate, MessagesPlaceholder } from 'langchain/prompts'
@@ -65,7 +64,7 @@ class OpenAIFunctionAgent_Agents implements INode {
}
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)
if (memory) executor.memory = memory
@@ -74,7 +73,7 @@ class OpenAIFunctionAgent_Agents implements INode {
}
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)
@@ -120,7 +119,7 @@ const formatAgentSteps = (steps: AgentStep[]): BaseMessage[] =>
const prepareAgent = (nodeData: INodeData, sessionId?: string) => {
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
let tools = nodeData.inputs?.tools
tools = flatten(tools)
@@ -143,7 +142,7 @@ const prepareAgent = (nodeData: INodeData, sessionId?: string) => {
[inputKey]: (i: { input: string; steps: AgentStep[] }) => i.input,
agent_scratchpad: (i: { input: string; steps: AgentStep[] }) => formatAgentSteps(i.steps),
[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 ?? []
}
},
@@ -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 { ICommonObject } from '../../../src'
import { MotorheadMemory, MotorheadMemoryInput, InputValues, MemoryVariables, OutputValues, getBufferString } from 'langchain/memory'
@@ -136,7 +136,7 @@ interface MotorheadMemoryExtendedInput {
isSessionIdUsingChatMessageId: boolean
}
class MotorheadMemoryExtended extends MotorheadMemory {
class MotorheadMemoryExtended extends MotorheadMemory implements MemoryMethods {
isSessionIdUsingChatMessageId? = false
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 { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
import { ICommonObject } from '../../../src'
@@ -167,7 +167,7 @@ interface ZepMemoryExtendedInput {
k?: number
}
class ZepMemoryExtended extends ZepMemory {
class ZepMemoryExtended extends ZepMemory implements MemoryMethods {
isSessionIdUsingChatMessageId? = false
lastN?: number
+3
View File
@@ -214,16 +214,19 @@ export abstract class FlowiseMemory extends BufferMemory implements MemoryMethod
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
abstract resumeMessages(messages: IMessage[]): Promise<void>
}
export abstract class FlowiseWindowMemory extends BufferWindowMemory implements MemoryMethods {
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
abstract resumeMessages(messages: IMessage[]): Promise<void>
}
export abstract class FlowiseSummaryMemory extends ConversationSummaryMemory implements MemoryMethods {
abstract getChatMessages(overrideSessionId?: string, returnBaseMessages?: boolean): Promise<IMessage[] | BaseMessage[]>
abstract addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId?: string): Promise<void>
abstract clearChatMessages(overrideSessionId?: string): Promise<void>
abstract resumeMessages(messages: IMessage[]): Promise<void>
}
-26
View File
@@ -1052,29 +1052,3 @@ export const getAllValuesFromJson = (obj: any): any[] => {
extractValues(obj)
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
}
+1 -1
View File
@@ -215,7 +215,7 @@ const Variables = () => {
</Toolbar>
</Box>
</Stack>
{variables.length <= 0 && (
{variables.length === 0 && (
<Stack sx={{ alignItems: 'center', justifyContent: 'center' }} flexDirection='column'>
<Box sx={{ p: 2, height: 'auto' }}>
<img