{"version":3,"file":"initialize.cjs","names":["AgentExecutor","ZeroShotAgent","ChatAgent","ChatConversationalAgent","BufferMemory","XMLAgent","StructuredChatAgent","OpenAIAgent"],"sources":["../../src/agents/initialize.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type {\n  StructuredToolInterface,\n  ToolInterface,\n} from \"@langchain/core/tools\";\nimport { CallbackManager } from \"@langchain/core/callbacks/manager\";\nimport { BufferMemory } from \"../memory/buffer_memory.js\";\nimport { ChatAgent } from \"./chat/index.js\";\nimport { ChatConversationalAgent } from \"./chat_convo/index.js\";\nimport { StructuredChatAgent } from \"./structured_chat/index.js\";\nimport { AgentExecutor, AgentExecutorInput } from \"./executor.js\";\nimport { ZeroShotAgent } from \"./mrkl/index.js\";\nimport { OpenAIAgent } from \"./openai_functions/index.js\";\nimport { XMLAgent } from \"./xml/index.js\";\n\n/**\n * Represents the type of an agent in LangChain. It can be\n * \"zero-shot-react-description\", \"chat-zero-shot-react-description\", or\n * \"chat-conversational-react-description\".\n */\ntype AgentType =\n  | \"zero-shot-react-description\"\n  | \"chat-zero-shot-react-description\"\n  | \"chat-conversational-react-description\";\n\nexport async function initializeAgentExecutor(\n  tools: ToolInterface[],\n  llm: BaseLanguageModelInterface,\n  _agentType?: AgentType,\n  _verbose?: boolean,\n  _callbackManager?: CallbackManager\n): Promise<AgentExecutor> {\n  const agentType = _agentType ?? \"zero-shot-react-description\";\n  const verbose = _verbose;\n  const callbackManager = _callbackManager;\n  switch (agentType) {\n    case \"zero-shot-react-description\":\n      return AgentExecutor.fromAgentAndTools({\n        agent: ZeroShotAgent.fromLLMAndTools(llm, tools),\n        tools,\n        returnIntermediateSteps: true,\n        verbose,\n        callbackManager,\n      });\n    case \"chat-zero-shot-react-description\":\n      return AgentExecutor.fromAgentAndTools({\n        agent: ChatAgent.fromLLMAndTools(llm, tools),\n        tools,\n        returnIntermediateSteps: true,\n        verbose,\n        callbackManager,\n      });\n    case \"chat-conversational-react-description\":\n      return AgentExecutor.fromAgentAndTools({\n        agent: ChatConversationalAgent.fromLLMAndTools(llm, tools),\n        tools,\n        verbose,\n        callbackManager,\n      });\n    default:\n      throw new Error(\"Unknown agent type\");\n  }\n}\n\n/**\n * @interface\n */\nexport type InitializeAgentExecutorOptions =\n  | ({\n      agentType: \"zero-shot-react-description\";\n      agentArgs?: Parameters<typeof ZeroShotAgent.fromLLMAndTools>[2];\n      memory?: never;\n    } & Omit<AgentExecutorInput, \"agent\" | \"tools\">)\n  | ({\n      agentType: \"chat-zero-shot-react-description\";\n      agentArgs?: Parameters<typeof ChatAgent.fromLLMAndTools>[2];\n      memory?: never;\n    } & Omit<AgentExecutorInput, \"agent\" | \"tools\">)\n  | ({\n      agentType: \"chat-conversational-react-description\";\n      agentArgs?: Parameters<typeof ChatConversationalAgent.fromLLMAndTools>[2];\n    } & Omit<AgentExecutorInput, \"agent\" | \"tools\">)\n  | ({\n      agentType: \"xml\";\n      agentArgs?: Parameters<typeof XMLAgent.fromLLMAndTools>[2];\n    } & Omit<AgentExecutorInput, \"agent\" | \"tools\">);\n\n/**\n * @interface\n */\nexport type InitializeAgentExecutorOptionsStructured =\n  | ({\n      agentType: \"structured-chat-zero-shot-react-description\";\n      agentArgs?: Parameters<typeof StructuredChatAgent.fromLLMAndTools>[2];\n    } & Omit<AgentExecutorInput, \"agent\" | \"tools\">)\n  | ({\n      agentType: \"openai-functions\";\n      agentArgs?: Parameters<typeof OpenAIAgent.fromLLMAndTools>[2];\n    } & Omit<AgentExecutorInput, \"agent\" | \"tools\">);\n\n/**\n * Initialize an agent executor with options.\n * @param tools Array of tools to use in the agent\n * @param llm LLM or ChatModel to use in the agent\n * @param options Options for the agent, including agentType, agentArgs, and other options for AgentExecutor.fromAgentAndTools\n * @returns AgentExecutor\n */\nexport async function initializeAgentExecutorWithOptions(\n  tools: StructuredToolInterface[],\n  llm: BaseLanguageModelInterface,\n  options: InitializeAgentExecutorOptionsStructured\n): Promise<AgentExecutor>;\nexport async function initializeAgentExecutorWithOptions(\n  tools: ToolInterface[],\n  llm: BaseLanguageModelInterface,\n  options?: InitializeAgentExecutorOptions\n): Promise<AgentExecutor>;\nexport async function initializeAgentExecutorWithOptions(\n  tools: StructuredToolInterface[] | ToolInterface[],\n  llm: BaseLanguageModelInterface,\n  options:\n    | InitializeAgentExecutorOptions\n    | InitializeAgentExecutorOptionsStructured = {\n    agentType:\n      llm._modelType() === \"base_chat_model\"\n        ? \"chat-zero-shot-react-description\"\n        : \"zero-shot-react-description\",\n  }\n): Promise<AgentExecutor> {\n  // Note this tools cast is safe as the overload signatures prevent\n  // the function from being called with a StructuredTool[] when\n  // the agentType is not in InitializeAgentExecutorOptionsStructured\n  switch (options.agentType) {\n    case \"zero-shot-react-description\": {\n      const { agentArgs, tags, ...rest } = options;\n      return AgentExecutor.fromAgentAndTools({\n        tags: [...(tags ?? []), \"zero-shot-react-description\"],\n        agent: ZeroShotAgent.fromLLMAndTools(\n          llm,\n          tools as ToolInterface[],\n          agentArgs\n        ),\n        tools,\n        ...rest,\n      });\n    }\n    case \"chat-zero-shot-react-description\": {\n      const { agentArgs, tags, ...rest } = options;\n      return AgentExecutor.fromAgentAndTools({\n        tags: [...(tags ?? []), \"chat-zero-shot-react-description\"],\n        agent: ChatAgent.fromLLMAndTools(\n          llm,\n          tools as ToolInterface[],\n          agentArgs\n        ),\n        tools,\n        ...rest,\n      });\n    }\n    case \"chat-conversational-react-description\": {\n      const { agentArgs, memory, tags, ...rest } = options;\n      const executor = AgentExecutor.fromAgentAndTools({\n        tags: [...(tags ?? []), \"chat-conversational-react-description\"],\n        agent: ChatConversationalAgent.fromLLMAndTools(\n          llm,\n          tools as ToolInterface[],\n          agentArgs\n        ),\n        tools,\n        memory:\n          memory ??\n          new BufferMemory({\n            returnMessages: true,\n            memoryKey: \"chat_history\",\n            inputKey: \"input\",\n            outputKey: \"output\",\n          }),\n        ...rest,\n      });\n      return executor;\n    }\n    case \"xml\": {\n      const { agentArgs, tags, ...rest } = options;\n      const executor = AgentExecutor.fromAgentAndTools({\n        tags: [...(tags ?? []), \"xml\"],\n        agent: XMLAgent.fromLLMAndTools(\n          llm,\n          tools as ToolInterface[],\n          agentArgs\n        ),\n        tools,\n        ...rest,\n      });\n      return executor;\n    }\n    case \"structured-chat-zero-shot-react-description\": {\n      const { agentArgs, memory, tags, ...rest } = options;\n      const executor = AgentExecutor.fromAgentAndTools({\n        tags: [...(tags ?? []), \"structured-chat-zero-shot-react-description\"],\n        agent: StructuredChatAgent.fromLLMAndTools(llm, tools, agentArgs),\n        tools,\n        memory,\n        ...rest,\n      });\n      return executor;\n    }\n    case \"openai-functions\": {\n      const { agentArgs, memory, tags, ...rest } = options;\n      const executor = AgentExecutor.fromAgentAndTools({\n        tags: [...(tags ?? []), \"openai-functions\"],\n        agent: OpenAIAgent.fromLLMAndTools(llm, tools, agentArgs),\n        tools,\n        memory:\n          memory ??\n          new BufferMemory({\n            returnMessages: true,\n            memoryKey: \"chat_history\",\n            inputKey: \"input\",\n            outputKey: \"output\",\n          }),\n        ...rest,\n      });\n      return executor;\n    }\n    default: {\n      throw new Error(\"Unknown agent type\");\n    }\n  }\n}\n"],"mappings":";;;;;;;;;AAyBA,eAAsB,wBACpB,OACA,KACA,YACA,UACA,kBACwB;CACxB,MAAM,YAAY,cAAc;CAChC,MAAM,UAAU;CAChB,MAAM,kBAAkB;AACxB,SAAQ,WAAR;EACE,KAAK,8BACH,QAAOA,iBAAAA,cAAc,kBAAkB;GACrC,OAAOC,cAAAA,cAAc,gBAAgB,KAAK,MAAM;GAChD;GACA,yBAAyB;GACzB;GACA;GACD,CAAC;EACJ,KAAK,mCACH,QAAOD,iBAAAA,cAAc,kBAAkB;GACrC,OAAOE,gBAAAA,UAAU,gBAAgB,KAAK,MAAM;GAC5C;GACA,yBAAyB;GACzB;GACA;GACD,CAAC;EACJ,KAAK,wCACH,QAAOF,iBAAAA,cAAc,kBAAkB;GACrC,OAAOG,gBAAAA,wBAAwB,gBAAgB,KAA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