62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
|
import { initializeAgentExecutorWithOptions, AgentExecutor } from 'langchain/agents'
|
|
import { Tool } from 'langchain/tools'
|
|
import { getBaseClasses } from '../../../src/utils'
|
|
import { BaseLanguageModel } from 'langchain/base_language'
|
|
import { flatten } from 'lodash'
|
|
|
|
class MRKLAgentLLM_Agents implements INode {
|
|
label: string
|
|
name: string
|
|
description: string
|
|
type: string
|
|
icon: string
|
|
category: string
|
|
baseClasses: string[]
|
|
inputs: INodeParams[]
|
|
|
|
constructor() {
|
|
this.label = 'MRKL Agent for LLMs'
|
|
this.name = 'mrklAgentLLM'
|
|
this.type = 'AgentExecutor'
|
|
this.category = 'Agents'
|
|
this.icon = 'agent.svg'
|
|
this.description = 'Agent that uses the ReAct Framework to decide what action to take, optimized to be used with LLMs'
|
|
this.baseClasses = [this.type, ...getBaseClasses(AgentExecutor)]
|
|
this.inputs = [
|
|
{
|
|
label: 'Allowed Tools',
|
|
name: 'tools',
|
|
type: 'Tool',
|
|
list: true
|
|
},
|
|
{
|
|
label: 'Language Model',
|
|
name: 'model',
|
|
type: 'BaseLanguageModel'
|
|
}
|
|
]
|
|
}
|
|
|
|
async init(nodeData: INodeData): Promise<any> {
|
|
const model = nodeData.inputs?.model as BaseLanguageModel
|
|
let tools = nodeData.inputs?.tools as Tool[]
|
|
tools = flatten(tools)
|
|
|
|
const executor = await initializeAgentExecutorWithOptions(tools, model, {
|
|
agentType: 'zero-shot-react-description',
|
|
verbose: process.env.DEBUG === 'true' ? true : false
|
|
})
|
|
return executor
|
|
}
|
|
|
|
async run(nodeData: INodeData, input: string): Promise<string> {
|
|
const executor = nodeData.instance as AgentExecutor
|
|
const result = await executor.call({ input })
|
|
|
|
return result?.output
|
|
}
|
|
}
|
|
|
|
module.exports = { nodeClass: MRKLAgentLLM_Agents }
|