{"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;CACxB,QAAQ,WAAR;EACE,KAAK,+BACH,OAAOA,iBAAAA,cAAc,kBAAkB;GACrC,OAAOC,cAAAA,cAAc,gBAAgB,KAAK,KAAK;GAC/C;GACA,yBAAyB;GACzB;GACA;EACF,CAAC;EACH,KAAK,oCACH,OAAOD,iBAAAA,cAAc,kBAAkB;GACrC,OAAOE,gBAAAA,UAAU,gBAAgB,KAAK,KAAK;GAC3C;GACA,yBAAyB;GACzB;GACA;EACF,CAAC;EACH,KAAK,yCACH,OAAOF,iBAAAA,cAAc,kBAAkB;GACrC,OAAOG,gBAAAA,wBAAwB,gBAAgB,KAAK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