|
| 1 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 2 | +import { initializeAgentExecutorWithOptions, AgentExecutor } from 'langchain/agents' |
| 3 | +import { Tool } from 'langchain/tools' |
| 4 | +import { CustomChainHandler, getBaseClasses } from '../../../src/utils' |
| 5 | +import { BaseLanguageModel } from 'langchain/base_language' |
| 6 | +import { flatten } from 'lodash' |
| 7 | + |
| 8 | +class OpenAIFunctionAgent_Agents implements INode { |
| 9 | + label: string |
| 10 | + name: string |
| 11 | + description: string |
| 12 | + type: string |
| 13 | + icon: string |
| 14 | + category: string |
| 15 | + baseClasses: string[] |
| 16 | + inputs: INodeParams[] |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.label = 'OpenAI Function Agent' |
| 20 | + this.name = 'openAIFunctionAgent' |
| 21 | + this.type = 'AgentExecutor' |
| 22 | + this.category = 'Agents' |
| 23 | + this.icon = 'openai.png' |
| 24 | + this.description = `An agent that uses OpenAI's Function Calling functionality to pick the tool and args to call` |
| 25 | + this.baseClasses = [this.type, ...getBaseClasses(AgentExecutor)] |
| 26 | + this.inputs = [ |
| 27 | + { |
| 28 | + label: 'Allowed Tools', |
| 29 | + name: 'tools', |
| 30 | + type: 'Tool', |
| 31 | + list: true |
| 32 | + }, |
| 33 | + { |
| 34 | + label: 'OpenAI Chat Model', |
| 35 | + name: 'model', |
| 36 | + description: |
| 37 | + 'Only works with gpt-3.5-turbo-0613 and gpt-4-0613. Refer <a target="_blank" href="https://platform.openai.com/docs/guides/gpt/function-calling">docs</a> for more info', |
| 38 | + type: 'BaseChatModel' |
| 39 | + } |
| 40 | + ] |
| 41 | + } |
| 42 | + |
| 43 | + async init(nodeData: INodeData): Promise<any> { |
| 44 | + const model = nodeData.inputs?.model as BaseLanguageModel |
| 45 | + let tools = nodeData.inputs?.tools as Tool[] |
| 46 | + tools = flatten(tools) |
| 47 | + |
| 48 | + const executor = await initializeAgentExecutorWithOptions(tools, model, { |
| 49 | + agentType: 'openai-functions', |
| 50 | + verbose: process.env.DEBUG === 'true' ? true : false |
| 51 | + }) |
| 52 | + return executor |
| 53 | + } |
| 54 | + |
| 55 | + async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> { |
| 56 | + const executor = nodeData.instance as AgentExecutor |
| 57 | + |
| 58 | + if (options.socketIO && options.socketIOClientId) { |
| 59 | + const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId) |
| 60 | + const result = await executor.run(input, [handler]) |
| 61 | + return result |
| 62 | + } else { |
| 63 | + const result = await executor.run(input) |
| 64 | + return result |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +module.exports = { nodeClass: OpenAIFunctionAgent_Agents } |
0 commit comments