Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Decision Flow #2282

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class FixedDecisionChoice {
constructor(public input: string, public response: string) {}
}
99 changes: 99 additions & 0 deletions packages/components/nodes/tools/FixedDecision/FixedDecisionTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { z } from 'zod'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { FixedDecisionChoice } from './FixedDecisionChoice'
import { CallbackManagerForToolRun } from '@langchain/core/callbacks/manager'
import { DynamicStructuredTool } from '@langchain/core/tools'
import { getBaseClasses } from '../../../src'

class FixedDecision_Tools implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'Fixed Decision Tool'
this.name = 'fixedDecisionTool'
this.version = 1.0
this.type = 'FixedDecisionTool'
this.icon = 'fixedDecisionTool.svg'
this.category = 'Tools'
this.description = 'Use as tool for agent that needs to run simple fixed defined flows'
this.baseClasses = [this.type, 'Tool', ...getBaseClasses(DynamicStructuredTool)]
this.inputs = [
{
label: 'Tool Name',
name: 'name',
type: 'string',
placeholder: 'fixed_decision'
},
{
label: 'Tool Description',
name: 'description',
type: 'string',
description: 'When the agent should use it for fixed decision(s)',
rows: 3,
placeholder: 'Returns the matching choice for the prompt'
},
{
label: 'Choices',
name: 'choices',
type: 'FixedDecisionChoice',
list: true
},
{
label: 'Return Direct',
name: 'returnDirect',
type: 'boolean',
description: 'Leave it switched on for exact response from the matched choice',
default: true,
optional: true
},
{
label: 'Fallback Response',
name: 'fallbackResponse',
type: 'string',
description: 'This is returned when no matching input is found',
rows: 4,
default: 'No matching input found.',
additionalParams: true
}
]
}

async init(nodeData: INodeData): Promise<any> {
const name = nodeData.inputs?.name as string
const description = nodeData.inputs?.description as string
const choices = nodeData.inputs?.choices as FixedDecisionChoice[]
const returnDirect = nodeData.inputs?.returnDirect as boolean
const fallbackResponse = nodeData.inputs?.fallbackResponse as string

const input = {
name,
description
} as any

if (returnDirect) input.returnDirect = returnDirect

const func = async ({ input }: { input: string }, _: CallbackManagerForToolRun) => {
// uses `includes` for more permissive matching
const choice = choices.filter((choice) => choice.input.toLowerCase().includes(input.toLowerCase()))

return choice?.[0]?.response ?? fallbackResponse
}

const schema = z.object({
input: z.string().describe('query to look up in tool')
})

return new DynamicStructuredTool({ ...input, func, schema })
}
}

module.exports = { nodeClass: FixedDecision_Tools }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { FixedDecisionChoice } from '../FixedDecision/FixedDecisionChoice'

class FixedDecisionChoiceNode implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]

constructor() {
this.label = 'Fixed Decision Choice'
this.name = 'fixedDecisionChoice'
this.version = 1.0
this.type = 'FixedDecisionChoice'
this.icon = 'fixedDecisionChoice.svg'
this.category = 'Tools'
this.description = 'Use choice(s) with Fixed Decision Tool to created a fixed decision flow'
this.baseClasses = [this.type]
this.inputs = [
{
label: 'Input',
name: 'input',
type: 'string',
rows: 5,
description: "This will be compared to the tool's input using an inclusive matching method",
placeholder: 'What is love?'
},
{
label: 'Response',
name: 'response',
type: 'string',
rows: 5,
placeholder: "Baby don't hurt me."
}
]
}

async init(nodeData: INodeData): Promise<any> {
return new FixedDecisionChoice(nodeData.inputs?.input, nodeData.inputs?.response)
}
}

module.exports = { nodeClass: FixedDecisionChoiceNode }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/ui/src/views/chatmessage/ChatMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ export const ChatMessage = ({ open, chatflowid, isDialog, previews, setPreviews
let allMessages = [...cloneDeep(prevMessages)]
if (allMessages[allMessages.length - 1].type === 'apiMessage') {
allMessages[allMessages.length - 1].id = data?.chatMessageId

if (data.text) allMessages[allMessages.length - 1].message = data.text
}
return allMessages
})
Expand Down