mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
Feature/agentflow v2 (#4298)
* agent flow v2 * chat message background * conditon agent flow * add sticky note * update human input dynamic prompt * add HTTP node * add default tool icon * fix export duplicate agentflow v2 * add agentflow v2 marketplaces * refractor memoization, add iteration nodes * add agentflow v2 templates * add agentflow generator * add migration scripts for mysql, mariadb, posrgres and fix date filters for executions * update agentflow chat history config * fix get all flows error after deletion and rename * add previous nodes from parent node * update generator prompt * update run time state when using iteration nodes * prevent looping connection, prevent duplication of start node, add executeflow node, add nodes agentflow, chat history variable * update embed * convert form input to string * bump openai version * add react rewards * add prompt generator to prediction queue * add array schema to overrideconfig * UI touchup * update embedded chat version * fix node info dialog * update start node and loop default iteration * update UI fixes for agentflow v2 * fix async drop down * add export import to agentflowsv2, executions, fix UI bugs * add default empty object to flowlisttable * add ability to share trace link publicly, allow MCP tool use for Agent and Assistant * add runtime message length to variable, display conditions on UI * fix array validation * add ability to add knowledge from vector store and embeddings for agent * add agent tool require human input * add ephemeral memory to start node * update agent flow node to show vs and embeddings icons * feat: add import chat data functionality for AgentFlowV2 * feat: set chatMessage.executionId to null if not found in import JSON file or database * fix: MariaDB execution migration script to utf8mb4_unicode_520_ci --------- Co-authored-by: Ong Chung Yau <33013947+chungyau97@users.noreply.github.com> Co-authored-by: chungyau97 <chungyau97@gmail.com>
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
import {
|
||||
ICommonObject,
|
||||
IDatabaseEntity,
|
||||
INode,
|
||||
INodeData,
|
||||
INodeOptionsValue,
|
||||
INodeParams,
|
||||
IServerSideEventStreamer
|
||||
} from '../../../src/Interface'
|
||||
import { updateFlowState } from '../utils'
|
||||
import { DataSource } from 'typeorm'
|
||||
import { BaseRetriever } from '@langchain/core/retrievers'
|
||||
import { Document } from '@langchain/core/documents'
|
||||
|
||||
interface IKnowledgeBase {
|
||||
documentStore: string
|
||||
}
|
||||
|
||||
class Retriever_Agentflow implements INode {
|
||||
label: string
|
||||
name: string
|
||||
version: number
|
||||
description: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
color: string
|
||||
hideOutput: boolean
|
||||
hint: string
|
||||
baseClasses: string[]
|
||||
documentation?: string
|
||||
credential: INodeParams
|
||||
inputs: INodeParams[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'Retriever'
|
||||
this.name = 'retrieverAgentflow'
|
||||
this.version = 1.0
|
||||
this.type = 'Retriever'
|
||||
this.category = 'Agent Flows'
|
||||
this.description = 'Retrieve information from vector database'
|
||||
this.baseClasses = [this.type]
|
||||
this.color = '#b8bedd'
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Knowledge (Document Stores)',
|
||||
name: 'retrieverKnowledgeDocumentStores',
|
||||
type: 'array',
|
||||
description: 'Document stores to retrieve information from. Document stores must be upserted in advance.',
|
||||
array: [
|
||||
{
|
||||
label: 'Document Store',
|
||||
name: 'documentStore',
|
||||
type: 'asyncOptions',
|
||||
loadMethod: 'listStores'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'Retriever Query',
|
||||
name: 'retrieverQuery',
|
||||
type: 'string',
|
||||
placeholder: 'Enter your query here',
|
||||
rows: 4,
|
||||
acceptVariable: true
|
||||
},
|
||||
{
|
||||
label: 'Output Format',
|
||||
name: 'outputFormat',
|
||||
type: 'options',
|
||||
options: [
|
||||
{ label: 'Text', name: 'text' },
|
||||
{ label: 'Text with Metadata', name: 'textWithMetadata' }
|
||||
],
|
||||
default: 'text'
|
||||
},
|
||||
{
|
||||
label: 'Update Flow State',
|
||||
name: 'retrieverUpdateState',
|
||||
description: 'Update runtime state during the execution of the workflow',
|
||||
type: 'array',
|
||||
optional: true,
|
||||
acceptVariable: true,
|
||||
array: [
|
||||
{
|
||||
label: 'Key',
|
||||
name: 'key',
|
||||
type: 'asyncOptions',
|
||||
loadMethod: 'listRuntimeStateKeys',
|
||||
freeSolo: true
|
||||
},
|
||||
{
|
||||
label: 'Value',
|
||||
name: 'value',
|
||||
type: 'string',
|
||||
acceptVariable: true,
|
||||
acceptNodeOutputAsVariable: true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
//@ts-ignore
|
||||
loadMethods = {
|
||||
async listRuntimeStateKeys(_: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> {
|
||||
const previousNodes = options.previousNodes as ICommonObject[]
|
||||
const startAgentflowNode = previousNodes.find((node) => node.name === 'startAgentflow')
|
||||
const state = startAgentflowNode?.inputs?.startState as ICommonObject[]
|
||||
return state.map((item) => ({ label: item.key, name: item.key }))
|
||||
},
|
||||
async listStores(_: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> {
|
||||
const returnData: INodeOptionsValue[] = []
|
||||
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
|
||||
if (appDataSource === undefined || !appDataSource) {
|
||||
return returnData
|
||||
}
|
||||
|
||||
const stores = await appDataSource.getRepository(databaseEntities['DocumentStore']).find()
|
||||
for (const store of stores) {
|
||||
if (store.status === 'UPSERTED') {
|
||||
const obj = {
|
||||
name: `${store.id}:${store.name}`,
|
||||
label: store.name,
|
||||
description: store.description
|
||||
}
|
||||
returnData.push(obj)
|
||||
}
|
||||
}
|
||||
return returnData
|
||||
}
|
||||
}
|
||||
|
||||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
|
||||
const retrieverQuery = nodeData.inputs?.retrieverQuery as string
|
||||
const outputFormat = nodeData.inputs?.outputFormat as string
|
||||
const _retrieverUpdateState = nodeData.inputs?.retrieverUpdateState
|
||||
|
||||
const state = options.agentflowRuntime?.state as ICommonObject
|
||||
const chatId = options.chatId as string
|
||||
const isLastNode = options.isLastNode as boolean
|
||||
const isStreamable = isLastNode && options.sseStreamer !== undefined
|
||||
|
||||
const abortController = options.abortController as AbortController
|
||||
|
||||
// Extract knowledge
|
||||
let docs: Document[] = []
|
||||
const knowledgeBases = nodeData.inputs?.retrieverKnowledgeDocumentStores as IKnowledgeBase[]
|
||||
if (knowledgeBases && knowledgeBases.length > 0) {
|
||||
for (const knowledgeBase of knowledgeBases) {
|
||||
const [storeId, _] = knowledgeBase.documentStore.split(':')
|
||||
|
||||
const docStoreVectorInstanceFilePath = options.componentNodes['documentStoreVS'].filePath as string
|
||||
const docStoreVectorModule = await import(docStoreVectorInstanceFilePath)
|
||||
const newDocStoreVectorInstance = new docStoreVectorModule.nodeClass()
|
||||
const docStoreVectorInstance = (await newDocStoreVectorInstance.init(
|
||||
{
|
||||
...nodeData,
|
||||
inputs: {
|
||||
...nodeData.inputs,
|
||||
selectedStore: storeId
|
||||
},
|
||||
outputs: {
|
||||
output: 'retriever'
|
||||
}
|
||||
},
|
||||
'',
|
||||
options
|
||||
)) as BaseRetriever
|
||||
|
||||
docs = await docStoreVectorInstance.invoke(retrieverQuery || input, { signal: abortController?.signal })
|
||||
}
|
||||
}
|
||||
|
||||
const docsText = docs.map((doc) => doc.pageContent).join('\n')
|
||||
|
||||
// Update flow state if needed
|
||||
let newState = { ...state }
|
||||
if (_retrieverUpdateState && Array.isArray(_retrieverUpdateState) && _retrieverUpdateState.length > 0) {
|
||||
newState = updateFlowState(state, _retrieverUpdateState)
|
||||
}
|
||||
|
||||
try {
|
||||
let finalOutput = ''
|
||||
if (outputFormat === 'text') {
|
||||
finalOutput = docsText
|
||||
} else if (outputFormat === 'textWithMetadata') {
|
||||
finalOutput = JSON.stringify(docs, null, 2)
|
||||
}
|
||||
|
||||
if (isStreamable) {
|
||||
const sseStreamer: IServerSideEventStreamer = options.sseStreamer
|
||||
sseStreamer.streamTokenEvent(chatId, finalOutput)
|
||||
}
|
||||
|
||||
// Process template variables in state
|
||||
if (newState && Object.keys(newState).length > 0) {
|
||||
for (const key in newState) {
|
||||
if (newState[key].toString().includes('{{ output }}')) {
|
||||
newState[key] = finalOutput
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const returnOutput = {
|
||||
id: nodeData.id,
|
||||
name: this.name,
|
||||
input: {
|
||||
question: retrieverQuery || input
|
||||
},
|
||||
output: {
|
||||
content: finalOutput
|
||||
},
|
||||
state: newState
|
||||
}
|
||||
|
||||
return returnOutput
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: Retriever_Agentflow }
|
||||
Reference in New Issue
Block a user