Skip to content

Commit

Permalink
Merge pull request #1397 from FlowiseAI/feature/Utilities
Browse files Browse the repository at this point in the history
Feature/Utilities
  • Loading branch information
HenryHengZJ authored Dec 17, 2023
2 parents 08ddd83 + e8af8b0 commit 0d73d53
Show file tree
Hide file tree
Showing 23 changed files with 1,783 additions and 711 deletions.
32 changes: 1 addition & 31 deletions packages/components/nodes/tools/CustomTool/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,7 @@ import { z } from 'zod'
import { CallbackManagerForToolRun } from 'langchain/callbacks'
import { StructuredTool, ToolParams } from 'langchain/tools'
import { NodeVM } from 'vm2'

/*
* List of dependencies allowed to be import in vm2
*/
const availableDependencies = [
'@dqbd/tiktoken',
'@getzep/zep-js',
'@huggingface/inference',
'@pinecone-database/pinecone',
'@supabase/supabase-js',
'axios',
'cheerio',
'chromadb',
'cohere-ai',
'd3-dsv',
'form-data',
'graphql',
'html-to-text',
'langchain',
'linkifyjs',
'mammoth',
'moment',
'node-fetch',
'pdf-parse',
'pdfjs-dist',
'playwright',
'puppeteer',
'srt-parser-2',
'typeorm',
'weaviate-ts-client'
]
import { availableDependencies } from '../../../src/utils'

export interface BaseDynamicToolInput extends ToolParams {
name: string
Expand Down
124 changes: 124 additions & 0 deletions packages/components/nodes/utilities/CustomFunction/CustomFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { NodeVM } from 'vm2'
import { availableDependencies, handleEscapeCharacters } from '../../../src/utils'

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

constructor() {
this.label = 'Custom JS Function'
this.name = 'customFunction'
this.version = 1.0
this.type = 'CustomFunction'
this.icon = 'customfunction.svg'
this.category = 'Utilities'
this.description = `Execute custom javascript function`
this.baseClasses = [this.type, 'Utilities']
this.inputs = [
{
label: 'Input Variables',
name: 'functionInputVariables',
description: 'Input variables can be used in the function with prefix $. For example: $var',
type: 'json',
optional: true,
acceptVariable: true,
list: true
},
{
label: 'Function Name',
name: 'functionName',
type: 'string',
optional: true,
placeholder: 'My Function'
},
{
label: 'Javascript Function',
name: 'javascriptFunction',
type: 'code'
}
]
this.outputs = [
{
label: 'Output',
name: 'output',
baseClasses: ['string', 'number', 'boolean', 'json', 'array']
}
]
}

async init(nodeData: INodeData, input: string): Promise<any> {
const javascriptFunction = nodeData.inputs?.javascriptFunction as string
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables

let inputVars: ICommonObject = {}
if (functionInputVariablesRaw) {
try {
inputVars =
typeof functionInputVariablesRaw === 'object' ? functionInputVariablesRaw : JSON.parse(functionInputVariablesRaw)
} catch (exception) {
throw new Error("Invalid JSON in the PromptTemplate's promptValues: " + exception)
}
}

let sandbox: any = { $input: input }

if (Object.keys(inputVars).length) {
for (const item in inputVars) {
sandbox[`$${item}`] = inputVars[item]
}
}

const defaultAllowBuiltInDep = [
'assert',
'buffer',
'crypto',
'events',
'http',
'https',
'net',
'path',
'querystring',
'timers',
'tls',
'url',
'zlib'
]

const builtinDeps = process.env.TOOL_FUNCTION_BUILTIN_DEP
? defaultAllowBuiltInDep.concat(process.env.TOOL_FUNCTION_BUILTIN_DEP.split(','))
: defaultAllowBuiltInDep
const externalDeps = process.env.TOOL_FUNCTION_EXTERNAL_DEP ? process.env.TOOL_FUNCTION_EXTERNAL_DEP.split(',') : []
const deps = availableDependencies.concat(externalDeps)

const nodeVMOptions = {
console: 'inherit',
sandbox,
require: {
external: { modules: deps },
builtin: builtinDeps
}
} as any

const vm = new NodeVM(nodeVMOptions)
try {
const response = await vm.run(`module.exports = async function() {${javascriptFunction}}()`, __dirname)
if (typeof response === 'string') {
return handleEscapeCharacters(response, false)
}
return response
} catch (e) {
throw new Error(e)
}
}
}

module.exports = { nodeClass: CustomFunction_Utilities }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions packages/components/nodes/utilities/GetVariable/GetVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'

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

constructor() {
this.label = 'Get Variable'
this.name = 'getVariable'
this.version = 1.0
this.type = 'GetVariable'
this.icon = 'getvar.svg'
this.category = 'Utilities'
this.description = `Get variable that was saved using Set Variable node`
this.baseClasses = [this.type, 'Utilities']
this.inputs = [
{
label: 'Variable Name',
name: 'variableName',
type: 'string',
placeholder: 'var1'
}
]
this.outputs = [
{
label: 'Output',
name: 'output',
baseClasses: ['string', 'number', 'boolean', 'json', 'array']
}
]
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const variableName = nodeData.inputs?.variableName as string
const dynamicVars = options.dynamicVariables as Record<string, unknown>

if (Object.prototype.hasOwnProperty.call(dynamicVars, variableName)) {
return dynamicVars[variableName]
}
return undefined
}
}

module.exports = { nodeClass: GetVariable_Utilities }
1 change: 1 addition & 0 deletions packages/components/nodes/utilities/GetVariable/getvar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions packages/components/nodes/utilities/SetVariable/SetVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'

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

constructor() {
this.label = 'Set Variable'
this.name = 'setVariable'
this.version = 1.0
this.type = 'SetVariable'
this.icon = 'setvar.svg'
this.category = 'Utilities'
this.description = `Set variable which can be retrieved at a later stage. Variable is only available during runtime.`
this.baseClasses = [this.type, 'Utilities']
this.inputs = [
{
label: 'Input',
name: 'input',
type: 'string | number | boolean | json | array',
optional: true,
list: true
},
{
label: 'Variable Name',
name: 'variableName',
type: 'string',
placeholder: 'var1'
}
]
this.outputs = [
{
label: 'Output',
name: 'output',
baseClasses: ['string', 'number', 'boolean', 'json', 'array']
}
]
}

async init(nodeData: INodeData): Promise<any> {
const inputRaw = nodeData.inputs?.input
const variableName = nodeData.inputs?.variableName as string

return { output: inputRaw, dynamicVariables: { [variableName]: inputRaw } }
}
}

module.exports = { nodeClass: SetVariable_Utilities }
1 change: 1 addition & 0 deletions packages/components/nodes/utilities/SetVariable/setvar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,63 @@ import { AIMessage, HumanMessage } from 'langchain/schema'

export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is not empty or blank
/*
* List of dependencies allowed to be import in vm2
*/
export const availableDependencies = [
'@aws-sdk/client-bedrock-runtime',
'@aws-sdk/client-dynamodb',
'@aws-sdk/client-s3',
'@elastic/elasticsearch',
'@dqbd/tiktoken',
'@getzep/zep-js',
'@gomomento/sdk',
'@gomomento/sdk-core',
'@google-ai/generativelanguage',
'@huggingface/inference',
'@notionhq/client',
'@opensearch-project/opensearch',
'@pinecone-database/pinecone',
'@qdrant/js-client-rest',
'@supabase/supabase-js',
'@upstash/redis',
'@zilliz/milvus2-sdk-node',
'apify-client',
'axios',
'cheerio',
'chromadb',
'cohere-ai',
'd3-dsv',
'faiss-node',
'form-data',
'google-auth-library',
'graphql',
'html-to-text',
'ioredis',
'langchain',
'langfuse',
'langsmith',
'linkifyjs',
'llmonitor',
'mammoth',
'moment',
'mongodb',
'mysql2',
'node-fetch',
'node-html-markdown',
'notion-to-md',
'openai',
'pdf-parse',
'pdfjs-dist',
'pg',
'playwright',
'puppeteer',
'redis',
'replicate',
'srt-parser-2',
'typeorm',
'weaviate-ts-client'
]

/**
* Get base classes of components
Expand Down
Loading

0 comments on commit 0d73d53

Please sign in to comment.