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

feat: add support for calling tools in CustomFunction #3143

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
@@ -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'
Expand All @@ -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'
Expand All @@ -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',
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down
Loading