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
This commit is contained in:
Daniel D'Abate 2024-06-20 01:03:45 +02:00 committed by GitHub
parent 842bfc66fe
commit d4f80394d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 4 deletions

View File

@ -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
}
}