feat: add support for calling tools in CustomFunction (#3143)

This commit is contained in:
YISH 2024-09-06 00:47:47 +08:00 committed by GitHub
parent 0e30db172a
commit 1e8839e21b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import { flatten } from 'lodash'
import { type StructuredTool } from '@langchain/core/tools'
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { NodeVM } from 'vm2'
import { DataSource } from 'typeorm'
@ -19,7 +21,7 @@ class CustomFunction_Utilities implements INode {
constructor() {
this.label = 'Custom JS Function'
this.name = 'customFunction'
this.version = 2.0
this.version = 3.0
this.type = 'CustomFunction'
this.icon = 'customfunction.svg'
this.category = 'Utilities'
@ -43,6 +45,14 @@ class CustomFunction_Utilities implements INode {
optional: true,
placeholder: 'My Function'
},
{
label: 'Additional Tools',
description: 'Tools can be used in the function with $tools.{tool_name}.invoke(args)',
name: 'tools',
type: 'Tool',
list: true,
optional: true
},
{
label: 'Javascript Function',
name: 'javascriptFunction',
@ -71,6 +81,7 @@ class CustomFunction_Utilities implements INode {
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables
const appDataSource = options.appDataSource as DataSource
const databaseEntities = options.databaseEntities as IDatabaseEntity
const tools = Object.fromEntries((flatten(nodeData.inputs?.tools) as StructuredTool[])?.map((tool) => [tool.name, tool]) ?? [])
const variables = await getVars(appDataSource, databaseEntities, nodeData)
const flow = {
@ -109,6 +120,7 @@ class CustomFunction_Utilities implements INode {
let sandbox: any = { $input: input }
sandbox['$vars'] = prepareSandboxVars(variables)
sandbox['$flow'] = flow
sandbox['$tools'] = tools
if (Object.keys(inputVars).length) {
for (const item in inputVars) {