Home / Class/ ConversationalAgent Class — langchain Architecture

ConversationalAgent Class — langchain Architecture

Architecture documentation for the ConversationalAgent class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  56062173_377c_1c6c_4d10_62181f6c83f8["ConversationalAgent"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7["Agent"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|extends| 37fcc3f3_2798_3648_915c_2bffdd19bff7
  eb3ddad0_deac_37dd_acc3_37596da18b91["base.py"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|defined in| eb3ddad0_deac_37dd_acc3_37596da18b91
  512de41d_1de4_0c53_016f_bc19a5ac9c57["_get_default_output_parser()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| 512de41d_1de4_0c53_016f_bc19a5ac9c57
  4014be3a_34fa_ac79_578f_5156a657f091["_agent_type()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| 4014be3a_34fa_ac79_578f_5156a657f091
  3fa1051b_3ac7_68f6_910a_853796dfa5a5["observation_prefix()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| 3fa1051b_3ac7_68f6_910a_853796dfa5a5
  5e4a485e_9ef0_eab3_307c_c93839655054["llm_prefix()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| 5e4a485e_9ef0_eab3_307c_c93839655054
  3d034d31_079b_a79a_e7e6_64b9cb2b9334["create_prompt()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| 3d034d31_079b_a79a_e7e6_64b9cb2b9334
  11415413_3e29_8e1d_229b_7f514c227457["_validate_tools()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| 11415413_3e29_8e1d_229b_7f514c227457
  f5b65376_6bec_6d97_9db6_78e842df66ea["from_llm_and_tools()"]
  56062173_377c_1c6c_4d10_62181f6c83f8 -->|method| f5b65376_6bec_6d97_9db6_78e842df66ea

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/conversational/base.py lines 34–178

class ConversationalAgent(Agent):
    """An agent that holds a conversation in addition to using tools."""

    ai_prefix: str = "AI"
    """Prefix to use before AI output."""
    output_parser: AgentOutputParser = Field(default_factory=ConvoOutputParser)
    """Output parser for the agent."""

    @classmethod
    @override
    def _get_default_output_parser(
        cls,
        ai_prefix: str = "AI",
        **kwargs: Any,
    ) -> AgentOutputParser:
        return ConvoOutputParser(ai_prefix=ai_prefix)

    @property
    def _agent_type(self) -> str:
        """Return Identifier of agent type."""
        return AgentType.CONVERSATIONAL_REACT_DESCRIPTION

    @property
    def observation_prefix(self) -> str:
        """Prefix to append the observation with.

        Returns:
            "Observation: "
        """
        return "Observation: "

    @property
    def llm_prefix(self) -> str:
        """Prefix to append the llm call with.

        Returns:
            "Thought: "
        """
        return "Thought:"

    @classmethod
    def create_prompt(
        cls,
        tools: Sequence[BaseTool],
        prefix: str = PREFIX,
        suffix: str = SUFFIX,
        format_instructions: str = FORMAT_INSTRUCTIONS,
        ai_prefix: str = "AI",
        human_prefix: str = "Human",
        input_variables: list[str] | None = None,
    ) -> PromptTemplate:
        """Create prompt in the style of the zero-shot agent.

        Args:
            tools: List of tools the agent will have access to, used to format the
                prompt.
            prefix: String to put before the list of tools.
            suffix: String to put after the list of tools.
            format_instructions: Instructions on how to use the tools.
            ai_prefix: String to use before AI output.
            human_prefix: String to use before human output.
            input_variables: List of input variables the final prompt will expect.
                Defaults to `["input", "chat_history", "agent_scratchpad"]`.

        Returns:
            A PromptTemplate with the template assembled from the pieces here.
        """
        tool_strings = "\n".join(
            [f"> {tool.name}: {tool.description}" for tool in tools],
        )
        tool_names = ", ".join([tool.name for tool in tools])
        format_instructions = format_instructions.format(
            tool_names=tool_names,
            ai_prefix=ai_prefix,
            human_prefix=human_prefix,
        )
        template = f"{prefix}\n\n{tool_strings}\n\n{format_instructions}\n\n{suffix}"
        if input_variables is None:
            input_variables = ["input", "chat_history", "agent_scratchpad"]
        return PromptTemplate(template=template, input_variables=input_variables)

Extends

Frequently Asked Questions

What is the ConversationalAgent class?
ConversationalAgent is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/conversational/base.py.
Where is ConversationalAgent defined?
ConversationalAgent is defined in libs/langchain/langchain_classic/agents/conversational/base.py at line 34.
What does ConversationalAgent extend?
ConversationalAgent extends Agent.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free