{"version":3,"file":"index.cjs","names":["Agent","AIMessage","HumanMessage","TEMPLATE_TOOL_RESPONSE","ChatConversationalAgentOutputParserWithRetries","DEFAULT_PREFIX","PREFIX_END","DEFAULT_SUFFIX","SystemMessagePromptTemplate","MessagesPlaceholder","HumanMessagePromptTemplate","ChatPromptTemplate","LLMChain"],"sources":["../../../src/agents/chat_convo/index.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { ToolInterface } from \"@langchain/core/tools\";\nimport {\n  ChatPromptTemplate,\n  HumanMessagePromptTemplate,\n  MessagesPlaceholder,\n  SystemMessagePromptTemplate,\n  renderTemplate,\n} from \"@langchain/core/prompts\";\nimport type { AgentStep } from \"@langchain/core/agents\";\nimport {\n  type BaseMessage,\n  HumanMessage,\n  AIMessage,\n} from \"@langchain/core/messages\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\nimport { Optional } from \"../../types/type-utils.js\";\nimport { Agent, AgentArgs, OutputParserArgs } from \"../agent.js\";\nimport { AgentActionOutputParser, AgentInput } from \"../types.js\";\nimport { ChatConversationalAgentOutputParserWithRetries } from \"./outputParser.js\";\nimport {\n  PREFIX_END,\n  DEFAULT_PREFIX,\n  DEFAULT_SUFFIX,\n  TEMPLATE_TOOL_RESPONSE,\n} from \"./prompt.js\";\n\n/**\n * Interface defining the structure of arguments used to create a prompt\n * for the ChatConversationalAgent class.\n */\nexport interface ChatConversationalCreatePromptArgs {\n  /** String to put after the list of tools. */\n  systemMessage?: string;\n  /** String to put before the list of tools. */\n  humanMessage?: string;\n  /** List of input variables the final prompt will expect. */\n  inputVariables?: string[];\n  /** Output parser to use for formatting. */\n  outputParser?: AgentActionOutputParser;\n}\n\n/**\n * Type that extends the AgentInput interface for the\n * ChatConversationalAgent class, making the outputParser property\n * optional.\n */\nexport type ChatConversationalAgentInput = Optional<AgentInput, \"outputParser\">;\n\n/**\n * Agent for the MRKL chain.\n * @augments Agent\n */\nexport class ChatConversationalAgent extends Agent {\n  static lc_name() {\n    return \"ChatConversationalAgent\";\n  }\n\n  lc_namespace = [\"langchain\", \"agents\", \"chat_convo\"];\n\n  declare ToolType: ToolInterface;\n\n  constructor(input: ChatConversationalAgentInput) {\n    const outputParser =\n      input.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();\n    super({ ...input, outputParser });\n  }\n\n  _agentType() {\n    return \"chat-conversational-react-description\" as const;\n  }\n\n  observationPrefix() {\n    return \"Observation: \";\n  }\n\n  llmPrefix() {\n    return \"Thought:\";\n  }\n\n  _stop(): string[] {\n    return [\"Observation:\"];\n  }\n\n  static validateTools(tools: ToolInterface[]) {\n    const descriptionlessTool = tools.find((tool) => !tool.description);\n    if (descriptionlessTool) {\n      const msg =\n        `Got a tool ${descriptionlessTool.name} without a description.` +\n        ` This agent requires descriptions for all tools.`;\n      throw new Error(msg);\n    }\n  }\n\n  /**\n   * Constructs the agent scratchpad based on the agent steps. It returns an\n   * array of base messages representing the thoughts of the agent.\n   * @param steps The agent steps to construct the scratchpad from.\n   * @returns An array of base messages representing the thoughts of the agent.\n   */\n  async constructScratchPad(steps: AgentStep[]): Promise<BaseMessage[]> {\n    const thoughts: BaseMessage[] = [];\n    for (const step of steps) {\n      thoughts.push(new AIMessage(step.action.log));\n      thoughts.push(\n        new HumanMessage(\n          renderTemplate(TEMPLATE_TOOL_RESPONSE, \"f-string\", {\n            observation: step.observation,\n          })\n        )\n      );\n    }\n    return thoughts;\n  }\n\n  /**\n   * Returns the default output parser for the ChatConversationalAgent\n   * class. It takes optional fields as arguments to customize the output\n   * parser.\n   * @param fields Optional fields to customize the output parser.\n   * @returns The default output parser for the ChatConversationalAgent class.\n   */\n  static getDefaultOutputParser(\n    fields?: OutputParserArgs & {\n      toolNames: string[];\n    }\n  ): AgentActionOutputParser {\n    if (fields?.llm) {\n      return ChatConversationalAgentOutputParserWithRetries.fromLLM(\n        fields.llm,\n        {\n          toolNames: fields.toolNames,\n        }\n      );\n    }\n    return new ChatConversationalAgentOutputParserWithRetries({\n      toolNames: fields?.toolNames,\n    });\n  }\n\n  /**\n   * Create prompt in the style of the ChatConversationAgent.\n   *\n   * @param tools - List of tools the agent will have access to, used to format the prompt.\n   * @param args - Arguments to create the prompt with.\n   * @param args.systemMessage - String to put before the list of tools.\n   * @param args.humanMessage - String to put after the list of tools.\n   * @param args.outputParser - Output parser to use for formatting.\n   */\n  static createPrompt(\n    tools: ToolInterface[],\n    args?: ChatConversationalCreatePromptArgs\n  ) {\n    const systemMessage = (args?.systemMessage ?? DEFAULT_PREFIX) + PREFIX_END;\n    const humanMessage = args?.humanMessage ?? DEFAULT_SUFFIX;\n    const toolStrings = tools\n      .map((tool) => `${tool.name}: ${tool.description}`)\n      .join(\"\\n\");\n    const toolNames = tools.map((tool) => tool.name);\n    const outputParser =\n      args?.outputParser ??\n      ChatConversationalAgent.getDefaultOutputParser({ toolNames });\n    const formatInstructions = outputParser.getFormatInstructions({\n      toolNames,\n    });\n    const renderedHumanMessage = renderTemplate(humanMessage, \"f-string\", {\n      format_instructions: formatInstructions,\n      tools: toolStrings,\n    });\n    const messages = [\n      SystemMessagePromptTemplate.fromTemplate(systemMessage),\n      new MessagesPlaceholder(\"chat_history\"),\n      HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),\n      new MessagesPlaceholder(\"agent_scratchpad\"),\n    ];\n    return ChatPromptTemplate.fromMessages(messages);\n  }\n\n  /**\n   * Creates an instance of the ChatConversationalAgent class from a\n   * BaseLanguageModel and a set of tools. It takes optional arguments to\n   * customize the agent.\n   * @param llm The BaseLanguageModel to create the agent from.\n   * @param tools The set of tools to create the agent from.\n   * @param args Optional arguments to customize the agent.\n   * @returns An instance of the ChatConversationalAgent class.\n   */\n  static fromLLMAndTools(\n    llm: BaseLanguageModelInterface,\n    tools: ToolInterface[],\n    args?: ChatConversationalCreatePromptArgs & AgentArgs\n  ) {\n    ChatConversationalAgent.validateTools(tools);\n    const outputParser =\n      args?.outputParser ??\n      ChatConversationalAgent.getDefaultOutputParser({\n        llm,\n        toolNames: tools.map((tool) => tool.name),\n      });\n    const prompt = ChatConversationalAgent.createPrompt(tools, {\n      ...args,\n      outputParser,\n    });\n    const chain = new LLMChain({\n      prompt,\n      llm,\n      callbacks: args?.callbacks ?? args?.callbackManager,\n    });\n    return new ChatConversationalAgent({\n      llmChain: chain,\n      outputParser,\n      allowedTools: tools.map((t) => t.name),\n    });\n  }\n}\n"],"mappings":";;;;;;;;;;;;AAqDA,IAAa,0BAAb,MAAa,gCAAgCA,cAAAA,MAAM;CACjD,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe;EAAC;EAAa;EAAU;EAAa;CAIpD,YAAY,OAAqC;EAC/C,MAAM,eACJ,MAAM,gBAAgB,wBAAwB,wBAAwB;AACxE,QAAM;GAAE,GAAG;GAAO;GAAc,CAAC;;CAGnC,aAAa;AACX,SAAO;;CAGT,oBAAoB;AAClB,SAAO;;CAGT,YAAY;AACV,SAAO;;CAGT,QAAkB;AAChB,SAAO,CAAC,eAAe;;CAGzB,OAAO,cAAc,OAAwB;EAC3C,MAAM,sBAAsB,MAAM,MAAM,SAAS,CAAC,KAAK,YAAY;AACnE,MAAI,qBAAqB;GACvB,MAAM,MACJ,cAAc,oBAAoB,KAAK;AAEzC,SAAM,IAAI,MAAM,IAAI;;;;;;;;;CAUxB,MAAM,oBAAoB,OAA4C;EACpE,MAAM,WAA0B,EAAE;AAClC,OAAK,MAAM,QAAQ,OAAO;AACxB,YAAS,KAAK,IAAIC,yBAAAA,UAAU,KAAK,OAAO,IAAI,CAAC;AAC7C,YAAS,KACP,IAAIC,yBAAAA,cAAAA,GAAAA,wBAAAA,gBACaC,eAAAA,wBAAwB,YAAY,EACjD,aAAa,KAAK,aACnB,CAAC,CACH,CACF;;AAEH,SAAO;;;;;;;;;CAUT,OAAO,uBACL,QAGyB;AACzB,MAAI,QAAQ,IACV,QAAOC,qBAAAA,+CAA+C,QACpD,OAAO,KACP,EACE,WAAW,OAAO,WACnB,CACF;AAEH,SAAO,IAAIA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