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,9 +1,10 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { ICommonObject, IMessage, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { initializeAgentExecutorWithOptions, AgentExecutor } from 'langchain/agents'
|
||||
import { Tool } from 'langchain/tools'
|
||||
import { CustomChainHandler, getBaseClasses } from '../../../src/utils'
|
||||
import { BaseLanguageModel } from 'langchain/base_language'
|
||||
import { flatten } from 'lodash'
|
||||
import { BaseChatMemory, ChatMessageHistory } from 'langchain/memory'
|
||||
import { AIChatMessage, HumanChatMessage } from 'langchain/schema'
|
||||
|
||||
class OpenAIFunctionAgent_Agents implements INode {
|
||||
label: string
|
||||
@@ -30,6 +31,11 @@ class OpenAIFunctionAgent_Agents implements INode {
|
||||
type: 'Tool',
|
||||
list: true
|
||||
},
|
||||
{
|
||||
label: 'Memory',
|
||||
name: 'memory',
|
||||
type: 'BaseChatMemory'
|
||||
},
|
||||
{
|
||||
label: 'OpenAI Chat Model',
|
||||
name: 'model',
|
||||
@@ -42,18 +48,38 @@ class OpenAIFunctionAgent_Agents implements INode {
|
||||
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
const model = nodeData.inputs?.model as BaseLanguageModel
|
||||
let tools = nodeData.inputs?.tools as Tool[]
|
||||
const memory = nodeData.inputs?.memory as BaseChatMemory
|
||||
|
||||
let tools = nodeData.inputs?.tools
|
||||
tools = flatten(tools)
|
||||
|
||||
const executor = await initializeAgentExecutorWithOptions(tools, model, {
|
||||
agentType: 'openai-functions',
|
||||
verbose: process.env.DEBUG === 'true' ? true : false
|
||||
})
|
||||
if (memory) executor.memory = memory
|
||||
|
||||
return executor
|
||||
}
|
||||
|
||||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
|
||||
const executor = nodeData.instance as AgentExecutor
|
||||
const memory = nodeData.inputs?.memory as BaseChatMemory
|
||||
|
||||
if (options && options.chatHistory) {
|
||||
const chatHistory = []
|
||||
const histories: IMessage[] = options.chatHistory
|
||||
|
||||
for (const message of histories) {
|
||||
if (message.type === 'apiMessage') {
|
||||
chatHistory.push(new AIChatMessage(message.message))
|
||||
} else if (message.type === 'userMessage') {
|
||||
chatHistory.push(new HumanChatMessage(message.message))
|
||||
}
|
||||
}
|
||||
memory.chatHistory = new ChatMessageHistory(chatHistory)
|
||||
executor.memory = memory
|
||||
}
|
||||
|
||||
if (options.socketIO && options.socketIOClientId) {
|
||||
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
|
||||
|
||||
@@ -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 |
@@ -33,7 +33,7 @@
|
||||
"form-data": "^4.0.0",
|
||||
"graphql": "^16.6.0",
|
||||
"html-to-text": "^9.0.5",
|
||||
"langchain": "^0.0.94",
|
||||
"langchain": "^0.0.96",
|
||||
"linkifyjs": "^4.1.1",
|
||||
"mammoth": "^1.5.1",
|
||||
"moment": "^2.29.3",
|
||||
@@ -43,6 +43,7 @@
|
||||
"playwright": "^1.35.0",
|
||||
"puppeteer": "^20.7.1",
|
||||
"srt-parser-2": "^1.2.3",
|
||||
"vm2": "^3.9.19",
|
||||
"weaviate-ts-client": "^1.1.0",
|
||||
"ws": "^8.9.0"
|
||||
},
|
||||
|
||||
@@ -2,7 +2,18 @@
|
||||
* Types
|
||||
*/
|
||||
|
||||
export type NodeParamsType = 'options' | 'string' | 'number' | 'boolean' | 'password' | 'json' | 'code' | 'date' | 'file' | 'folder'
|
||||
export type NodeParamsType =
|
||||
| 'asyncOptions'
|
||||
| 'options'
|
||||
| 'string'
|
||||
| 'number'
|
||||
| 'boolean'
|
||||
| 'password'
|
||||
| 'json'
|
||||
| 'code'
|
||||
| 'date'
|
||||
| 'file'
|
||||
| 'folder'
|
||||
|
||||
export type CommonType = string | number | boolean | undefined | null
|
||||
|
||||
@@ -16,6 +27,10 @@ export interface ICommonObject {
|
||||
[key: string]: any | CommonType | ICommonObject | CommonType[] | ICommonObject[]
|
||||
}
|
||||
|
||||
export type IDatabaseEntity = {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
export interface IAttachment {
|
||||
content: string
|
||||
contentType: string
|
||||
@@ -50,6 +65,7 @@ export interface INodeParams {
|
||||
placeholder?: string
|
||||
fileType?: string
|
||||
additionalParams?: boolean
|
||||
loadMethod?: string
|
||||
}
|
||||
|
||||
export interface INodeExecutionData {
|
||||
@@ -74,6 +90,9 @@ export interface INodeProperties {
|
||||
export interface INode extends INodeProperties {
|
||||
inputs?: INodeParams[]
|
||||
output?: INodeOutputsValue[]
|
||||
loadMethods?: {
|
||||
[key: string]: (nodeData: INodeData, options?: ICommonObject) => Promise<INodeOptionsValue[]>
|
||||
}
|
||||
init?(nodeData: INodeData, input: string, options?: ICommonObject): Promise<any>
|
||||
run?(nodeData: INodeData, input: string, options?: ICommonObject): Promise<string | ICommonObject>
|
||||
}
|
||||
@@ -83,6 +102,7 @@ export interface INodeData extends INodeProperties {
|
||||
inputs?: ICommonObject
|
||||
outputs?: ICommonObject
|
||||
instance?: any
|
||||
loadMethod?: string // method to load async options
|
||||
}
|
||||
|
||||
export interface IMessage {
|
||||
|
||||
@@ -18,6 +18,7 @@ export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is no
|
||||
*/
|
||||
export const getBaseClasses = (targetClass: any) => {
|
||||
const baseClasses: string[] = []
|
||||
const skipClassNames = ['BaseLangChain', 'Serializable']
|
||||
|
||||
if (targetClass instanceof Function) {
|
||||
let baseClass = targetClass
|
||||
@@ -26,7 +27,7 @@ export const getBaseClasses = (targetClass: any) => {
|
||||
const newBaseClass = Object.getPrototypeOf(baseClass)
|
||||
if (newBaseClass && newBaseClass !== Object && newBaseClass.name) {
|
||||
baseClass = newBaseClass
|
||||
baseClasses.push(baseClass.name)
|
||||
if (!skipClassNames.includes(baseClass.name)) baseClasses.push(baseClass.name)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
@@ -284,3 +285,31 @@ const handleEscapeDoubleQuote = (value: string): string => {
|
||||
}
|
||||
return newValue === '' ? value : newValue
|
||||
}
|
||||
|
||||
export 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'
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user