(nodeData: INodeData, input: string | Record<string, any>, options: ICommonObject)
| 338 | } |
| 339 | |
| 340 | async run(nodeData: INodeData, input: string | Record<string, any>, options: ICommonObject): Promise<any> { |
| 341 | let llmIds: ICommonObject | undefined |
| 342 | let analyticHandlers = options.analyticHandlers as AnalyticHandler |
| 343 | |
| 344 | try { |
| 345 | const abortController = options.abortController as AbortController |
| 346 | |
| 347 | // Extract input parameters |
| 348 | const model = nodeData.inputs?.llmModel as string |
| 349 | const modelConfig = nodeData.inputs?.llmModelConfig as ICommonObject |
| 350 | if (!model) { |
| 351 | throw new Error('Model is required') |
| 352 | } |
| 353 | const modelName = modelConfig?.model ?? modelConfig?.modelName |
| 354 | |
| 355 | // Extract memory and configuration options |
| 356 | const enableMemory = nodeData.inputs?.llmEnableMemory as boolean |
| 357 | const memoryType = nodeData.inputs?.llmMemoryType as string |
| 358 | const userMessage = nodeData.inputs?.llmUserMessage as string |
| 359 | const _llmUpdateState = nodeData.inputs?.llmUpdateState |
| 360 | const _llmStructuredOutput = nodeData.inputs?.llmStructuredOutput |
| 361 | const llmMessages = (nodeData.inputs?.llmMessages as unknown as ILLMMessage[]) ?? [] |
| 362 | |
| 363 | // Extract runtime state and history |
| 364 | const state = options.agentflowRuntime?.state as ICommonObject |
| 365 | const pastChatHistory = (options.pastChatHistory as BaseMessageLike[]) ?? [] |
| 366 | const runtimeChatHistory = (options.agentflowRuntime?.chatHistory as BaseMessageLike[]) ?? [] |
| 367 | const prependedChatHistory = options.prependedChatHistory as IMessage[] |
| 368 | const chatId = options.chatId as string |
| 369 | |
| 370 | // Initialize the LLM model instance |
| 371 | const nodeInstanceFilePath = options.componentNodes[model].filePath as string |
| 372 | const nodeModule = await import(nodeInstanceFilePath) |
| 373 | const newLLMNodeInstance = new nodeModule.nodeClass() |
| 374 | const newNodeData = { |
| 375 | ...nodeData, |
| 376 | credential: modelConfig['FLOWISE_CREDENTIAL_ID'], |
| 377 | inputs: { |
| 378 | ...nodeData.inputs, |
| 379 | ...modelConfig |
| 380 | } |
| 381 | } |
| 382 | let llmNodeInstance = (await newLLMNodeInstance.init(newNodeData, '', options)) as BaseChatModel |
| 383 | |
| 384 | // Prepare messages array |
| 385 | const messages: BaseMessageLike[] = [] |
| 386 | |
| 387 | // Prepend history ONLY if it is the first node |
| 388 | if (prependedChatHistory.length > 0 && !runtimeChatHistory.length) { |
| 389 | for (const msg of prependedChatHistory) { |
| 390 | const role: string = msg.role === 'apiMessage' ? 'assistant' : 'user' |
| 391 | const content: string = msg.content ?? '' |
| 392 | messages.push({ |
| 393 | role, |
| 394 | content |
| 395 | }) |
| 396 | } |
| 397 | } |
nothing calls this directly
no test coverage detected