|
| 1 | +import { INode, INodeData, INodeParams } from '../../../src/Interface' |
| 2 | +import { BaseChatModel } from 'langchain/chat_models' |
| 3 | +import { AutoGPT } from 'langchain/experimental/autogpt' |
| 4 | +import { Tool } from 'langchain/tools' |
| 5 | +import { VectorStoreRetriever } from 'langchain/vectorstores/base' |
| 6 | + |
| 7 | +class AutoGPT_Agents implements INode { |
| 8 | + label: string |
| 9 | + name: string |
| 10 | + description: string |
| 11 | + type: string |
| 12 | + icon: string |
| 13 | + category: string |
| 14 | + baseClasses: string[] |
| 15 | + inputs: INodeParams[] |
| 16 | + |
| 17 | + constructor() { |
| 18 | + this.label = 'AutoGPT' |
| 19 | + this.name = 'autoGPT' |
| 20 | + this.type = 'AutoGPT' |
| 21 | + this.category = 'Agents' |
| 22 | + this.icon = 'autogpt.png' |
| 23 | + this.description = 'Autonomous agent with chain of thoughts for self-guided task completion' |
| 24 | + this.baseClasses = ['AutoGPT'] |
| 25 | + this.inputs = [ |
| 26 | + { |
| 27 | + label: 'Allowed Tools', |
| 28 | + name: 'tools', |
| 29 | + type: 'Tool', |
| 30 | + list: true |
| 31 | + }, |
| 32 | + { |
| 33 | + label: 'Chat Model', |
| 34 | + name: 'model', |
| 35 | + type: 'BaseChatModel' |
| 36 | + }, |
| 37 | + { |
| 38 | + label: 'Vector Store Retriever', |
| 39 | + name: 'vectorStoreRetriever', |
| 40 | + type: 'BaseRetriever' |
| 41 | + }, |
| 42 | + { |
| 43 | + label: 'AutoGPT Name', |
| 44 | + name: 'aiName', |
| 45 | + type: 'string', |
| 46 | + placeholder: 'Tom', |
| 47 | + optional: true |
| 48 | + }, |
| 49 | + { |
| 50 | + label: 'AutoGPT Role', |
| 51 | + name: 'aiRole', |
| 52 | + type: 'string', |
| 53 | + placeholder: 'Assistant', |
| 54 | + optional: true |
| 55 | + }, |
| 56 | + { |
| 57 | + label: 'Maximum Loop', |
| 58 | + name: 'maxLoop', |
| 59 | + type: 'number', |
| 60 | + default: 5, |
| 61 | + optional: true |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + |
| 66 | + async init(nodeData: INodeData): Promise<any> { |
| 67 | + const model = nodeData.inputs?.model as BaseChatModel |
| 68 | + const vectorStoreRetriever = nodeData.inputs?.vectorStoreRetriever as VectorStoreRetriever |
| 69 | + const tools = nodeData.inputs?.tools as Tool[] |
| 70 | + const aiName = (nodeData.inputs?.aiName as string) || 'AutoGPT' |
| 71 | + const aiRole = (nodeData.inputs?.aiRole as string) || 'Assistant' |
| 72 | + const maxLoop = nodeData.inputs?.maxLoop as string |
| 73 | + |
| 74 | + const autogpt = AutoGPT.fromLLMAndTools(model, tools, { |
| 75 | + memory: vectorStoreRetriever, |
| 76 | + aiName, |
| 77 | + aiRole |
| 78 | + }) |
| 79 | + |
| 80 | + autogpt.maxIterations = parseInt(maxLoop, 10) |
| 81 | + |
| 82 | + return autogpt |
| 83 | + } |
| 84 | + |
| 85 | + async run(nodeData: INodeData, input: string): Promise<string> { |
| 86 | + const executor = nodeData.instance as AutoGPT |
| 87 | + try { |
| 88 | + const res = await executor.run([input]) |
| 89 | + return res || 'I have completed all my tasks.' |
| 90 | + } catch (e) { |
| 91 | + console.error(e) |
| 92 | + throw new Error(e) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = { nodeClass: AutoGPT_Agents } |
0 commit comments