mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 11:00:55 +03:00
Feature/Add message history to agents (#3031)
add message history to agents
This commit is contained in:
@@ -11,6 +11,7 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models'
|
||||
import { addImagesToMessages, llmSupportsVision } from '../../src/multiModalUtils'
|
||||
import { ICommonObject, IDatabaseEntity, INodeData, ISeqAgentsState, IVisionChatModal } from '../../src/Interface'
|
||||
import { availableDependencies, defaultAllowBuiltInDep, getVars, prepareSandboxVars } from '../../src/utils'
|
||||
import { ChatPromptTemplate, BaseMessagePromptTemplateLike } from '@langchain/core/prompts'
|
||||
|
||||
export const checkCondition = (input: string | number | undefined, condition: string, value: string | number = ''): boolean => {
|
||||
if (!input && condition === 'Is Empty') return true
|
||||
@@ -344,3 +345,34 @@ export class RunnableCallable<I = unknown, O = unknown> extends Runnable<I, O> {
|
||||
return returnValue
|
||||
}
|
||||
}
|
||||
|
||||
export const checkMessageHistory = async (
|
||||
nodeData: INodeData,
|
||||
options: ICommonObject,
|
||||
prompt: ChatPromptTemplate,
|
||||
promptArrays: BaseMessagePromptTemplateLike[],
|
||||
sysPrompt: string
|
||||
) => {
|
||||
const messageHistory = nodeData.inputs?.messageHistory
|
||||
|
||||
if (messageHistory) {
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
const vm = await getVM(appDataSource, databaseEntities, nodeData, {})
|
||||
try {
|
||||
const response = await vm.run(`module.exports = async function() {${messageHistory}}()`, __dirname)
|
||||
if (!Array.isArray(response)) throw new Error('Returned message history must be an array')
|
||||
if (sysPrompt) {
|
||||
// insert at index 1
|
||||
promptArrays.splice(1, 0, ...response)
|
||||
} else {
|
||||
promptArrays.unshift(...response)
|
||||
}
|
||||
prompt = ChatPromptTemplate.fromMessages(promptArrays)
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
return prompt
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user