mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 19:00:59 +03:00
add custom tool
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-tool" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-subtask" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
<path d="M7 10h3v-3l-3.5 -3.5a6 6 0 0 1 8 8l6 6a2 2 0 0 1 -3 3l-6 -6a6 6 0 0 1 -8 -8l3.5 3.5"></path>
|
||||
<path d="M6 9l6 0"></path>
|
||||
<path d="M4 5l4 0"></path>
|
||||
<path d="M6 5v11a1 1 0 0 0 1 1h5"></path>
|
||||
<path d="M12 7m0 1a1 1 0 0 1 1 -1h6a1 1 0 0 1 1 1v2a1 1 0 0 1 -1 1h-6a1 1 0 0 1 -1 -1z"></path>
|
||||
<path d="M12 15m0 1a1 1 0 0 1 1 -1h6a1 1 0 0 1 1 1v2a1 1 0 0 1 -1 1h-6a1 1 0 0 1 -1 -1z"></path>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 396 B After Width: | Height: | Size: 598 B |
@@ -0,0 +1,108 @@
|
||||
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses } from '../../../src/utils'
|
||||
import { DynamicStructuredTool } from './core'
|
||||
import { z } from 'zod'
|
||||
import { DataSource } from 'typeorm'
|
||||
|
||||
class CustomTool_Tools implements INode {
|
||||
label: string
|
||||
name: string
|
||||
description: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
inputs: INodeParams[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'Custom Tool'
|
||||
this.name = 'customTool'
|
||||
this.type = 'CustomTool'
|
||||
this.icon = 'customtool.svg'
|
||||
this.category = 'Tools'
|
||||
this.description = `Use custom tool you've created in Flowise within chatflow`
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Select Tool',
|
||||
name: 'selectedTool',
|
||||
type: 'asyncOptions',
|
||||
loadMethod: 'listTools'
|
||||
}
|
||||
]
|
||||
this.baseClasses = [this.type, 'Tool', ...getBaseClasses(DynamicStructuredTool)]
|
||||
}
|
||||
|
||||
//@ts-ignore
|
||||
loadMethods = {
|
||||
async listTools(nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> {
|
||||
const returnData: INodeOptionsValue[] = []
|
||||
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
|
||||
if (appDataSource === undefined || !appDataSource) {
|
||||
return returnData
|
||||
}
|
||||
|
||||
const tools = await appDataSource.getRepository(databaseEntities['Tool']).find()
|
||||
|
||||
for (let i = 0; i < tools.length; i += 1) {
|
||||
const data = {
|
||||
label: tools[i].name,
|
||||
name: tools[i].id,
|
||||
description: tools[i].description
|
||||
} as INodeOptionsValue
|
||||
returnData.push(data)
|
||||
}
|
||||
return returnData
|
||||
}
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
|
||||
const selectedToolId = nodeData.inputs?.selectedTool as string
|
||||
|
||||
const appDataSource = options.appDataSource as DataSource
|
||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||
|
||||
try {
|
||||
const tool = await appDataSource.getRepository(databaseEntities['Tool']).findOneBy({
|
||||
id: selectedToolId
|
||||
})
|
||||
|
||||
if (!tool) throw new Error(`Tool ${selectedToolId} not found`)
|
||||
const obj = {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
schema: z.object(convertSchemaToZod(tool.schema)),
|
||||
code: tool.func
|
||||
}
|
||||
return new DynamicStructuredTool(obj)
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const convertSchemaToZod = (schema: string) => {
|
||||
try {
|
||||
const parsedSchema = JSON.parse(schema)
|
||||
const zodObj: any = {}
|
||||
for (const sch of parsedSchema) {
|
||||
if (sch.type === 'string') {
|
||||
if (sch.required) z.string({ required_error: `${sch.property} required` }).describe(sch.description)
|
||||
zodObj[sch.property] = z.string().describe(sch.description)
|
||||
} else if (sch.type === 'number') {
|
||||
if (sch.required) z.number({ required_error: `${sch.property} required` }).describe(sch.description)
|
||||
zodObj[sch.property] = z.number().describe(sch.description)
|
||||
} else if (sch.type === 'boolean') {
|
||||
if (sch.required) z.boolean({ required_error: `${sch.property} required` }).describe(sch.description)
|
||||
zodObj[sch.property] = z.boolean().describe(sch.description)
|
||||
}
|
||||
}
|
||||
return zodObj
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: CustomTool_Tools }
|
||||
@@ -0,0 +1,78 @@
|
||||
import { z } from 'zod'
|
||||
import { CallbackManagerForToolRun } from 'langchain/callbacks'
|
||||
import { StructuredTool, ToolParams } from 'langchain/tools'
|
||||
import { NodeVM } from 'vm2'
|
||||
import { availableDependencies } from '../../../src/utils'
|
||||
|
||||
export interface BaseDynamicToolInput extends ToolParams {
|
||||
name: string
|
||||
description: string
|
||||
code: string
|
||||
returnDirect?: boolean
|
||||
}
|
||||
|
||||
export interface DynamicStructuredToolInput<
|
||||
// eslint-disable-next-line
|
||||
T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>
|
||||
> extends BaseDynamicToolInput {
|
||||
func?: (input: z.infer<T>, runManager?: CallbackManagerForToolRun) => Promise<string>
|
||||
schema: T
|
||||
}
|
||||
|
||||
export class DynamicStructuredTool<
|
||||
// eslint-disable-next-line
|
||||
T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>
|
||||
> extends StructuredTool {
|
||||
name: string
|
||||
|
||||
description: string
|
||||
|
||||
code: string
|
||||
|
||||
func: DynamicStructuredToolInput['func']
|
||||
|
||||
schema: T
|
||||
|
||||
constructor(fields: DynamicStructuredToolInput<T>) {
|
||||
super(fields)
|
||||
this.name = fields.name
|
||||
this.description = fields.description
|
||||
this.code = fields.code
|
||||
this.func = fields.func
|
||||
this.returnDirect = fields.returnDirect ?? this.returnDirect
|
||||
this.schema = fields.schema
|
||||
}
|
||||
|
||||
protected async _call(arg: z.output<T>): Promise<string> {
|
||||
let sandbox: any = {}
|
||||
if (typeof arg === 'object' && Object.keys(arg).length) {
|
||||
for (const item in arg) {
|
||||
sandbox[`$${item}`] = arg[item]
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
console: 'inherit',
|
||||
sandbox,
|
||||
require: {
|
||||
external: false as boolean | { modules: string[] },
|
||||
builtin: ['*']
|
||||
}
|
||||
} as any
|
||||
|
||||
const external = JSON.stringify(availableDependencies)
|
||||
if (external) {
|
||||
const deps = JSON.parse(external)
|
||||
if (deps && deps.length) {
|
||||
options.require.external = {
|
||||
modules: deps
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const vm = new NodeVM(options)
|
||||
const response = await vm.run(`module.exports = async function() {${this.code}}()`, __dirname)
|
||||
|
||||
return response
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-tool" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
<path d="M7 10h3v-3l-3.5 -3.5a6 6 0 0 1 8 8l6 6a2 2 0 0 1 -3 3l-6 -6a6 6 0 0 1 -8 -8l3.5 3.5"></path>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 396 B |
Reference in New Issue
Block a user