From d4f80394d3466fd6babc43e22a11f0343a64e1f6 Mon Sep 17 00:00:00 2001 From: Daniel D'Abate Date: Thu, 20 Jun 2024 01:03:45 +0200 Subject: [PATCH] Feature - Add option to start a new session with each interaction with the Chatflow tool (#2633) * Feature - Add option to start a new session with each interaction with the Chatflow tool * ChatflowTool - Create random chatId when startNewSession is set --- .../nodes/tools/ChatflowTool/ChatflowTool.ts | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/components/nodes/tools/ChatflowTool/ChatflowTool.ts b/packages/components/nodes/tools/ChatflowTool/ChatflowTool.ts index df5e50fe1..850674bc1 100644 --- a/packages/components/nodes/tools/ChatflowTool/ChatflowTool.ts +++ b/packages/components/nodes/tools/ChatflowTool/ChatflowTool.ts @@ -6,6 +6,7 @@ import { CallbackManagerForToolRun, Callbacks, CallbackManager, parseCallbackCon import { StructuredTool } from '@langchain/core/tools' import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface' import { availableDependencies, defaultAllowBuiltInDep, getCredentialData, getCredentialParam } from '../../../src/utils' +import { v4 as uuidv4 } from 'uuid' class ChatflowTool_Tools implements INode { label: string @@ -22,7 +23,7 @@ class ChatflowTool_Tools implements INode { constructor() { this.label = 'Chatflow Tool' this.name = 'ChatflowTool' - this.version = 2.0 + this.version = 3.0 this.type = 'ChatflowTool' this.icon = 'chatflowTool.svg' this.category = 'Tools' @@ -66,6 +67,16 @@ class ChatflowTool_Tools implements INode { optional: true, additionalParams: true }, + { + label: 'Start new session per message', + name: 'startNewSession', + type: 'boolean', + description: + 'Whether to continue the session with the Chatflow tool or start a new one with each interaction. Useful for Chatflows with memory if you want to avoid it.', + default: false, + optional: true, + additionalParams: true + }, { label: 'Use Question from Chat', name: 'useQuestionFromChat', @@ -117,6 +128,8 @@ class ChatflowTool_Tools implements INode { const useQuestionFromChat = nodeData.inputs?.useQuestionFromChat as boolean const customInput = nodeData.inputs?.customInput as string + const startNewSession = nodeData.inputs?.startNewSession as boolean + const baseURL = (nodeData.inputs?.baseURL as string) || (options.baseURL as string) const credentialData = await getCredentialData(nodeData.credential ?? '', options) @@ -136,7 +149,7 @@ class ChatflowTool_Tools implements INode { let name = _name || 'chatflow_tool' - return new ChatflowTool({ name, baseURL, description, chatflowid: selectedChatflowId, headers, input: toolInput }) + return new ChatflowTool({ name, baseURL, description, chatflowid: selectedChatflowId, startNewSession, headers, input: toolInput }) } } @@ -153,6 +166,8 @@ class ChatflowTool extends StructuredTool { chatflowid = '' + startNewSession = false + baseURL = 'http://localhost:3000' headers = {} @@ -166,6 +181,7 @@ class ChatflowTool extends StructuredTool { description, input, chatflowid, + startNewSession, baseURL, headers }: { @@ -173,6 +189,7 @@ class ChatflowTool extends StructuredTool { description: string input: string chatflowid: string + startNewSession: boolean baseURL: string headers: ICommonObject }) { @@ -181,6 +198,7 @@ class ChatflowTool extends StructuredTool { this.description = description this.input = input this.baseURL = baseURL + this.startNewSession = startNewSession this.headers = headers this.chatflowid = chatflowid } @@ -240,9 +258,9 @@ class ChatflowTool extends StructuredTool { const body = { question: inputQuestion, - chatId: flowConfig?.chatId, + chatId: this.startNewSession ? uuidv4() : flowConfig?.chatId, overrideConfig: { - sessionId: flowConfig?.sessionId + sessionId: this.startNewSession ? uuidv4() : flowConfig?.sessionId } }