Home / Class/ StructuredChatAgent Class — langchain Architecture

StructuredChatAgent Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  826e6477_6abe_d406_5d34_9df3fbec8707["StructuredChatAgent"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7["Agent"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|extends| 37fcc3f3_2798_3648_915c_2bffdd19bff7
  960fedcb_7c5c_a4a8_1c04_3d7fa9869b1e["base.py"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|defined in| 960fedcb_7c5c_a4a8_1c04_3d7fa9869b1e
  34d27034_327a_1df1_9827_e697f9642133["observation_prefix()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| 34d27034_327a_1df1_9827_e697f9642133
  2b53352a_8901_7669_9a64_be667798628b["llm_prefix()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| 2b53352a_8901_7669_9a64_be667798628b
  b386edf5_a650_8b71_2e10_b6329c9f0984["_construct_scratchpad()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| b386edf5_a650_8b71_2e10_b6329c9f0984
  81ebad27_b83f_0f42_afc8_372e9551a3bf["_validate_tools()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| 81ebad27_b83f_0f42_afc8_372e9551a3bf
  9b0d5ae0_54f8_f509_bed8_ff75b67abd77["_get_default_output_parser()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| 9b0d5ae0_54f8_f509_bed8_ff75b67abd77
  d8fa59af_994b_832d_4a38_755e0d841dc8["_stop()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| d8fa59af_994b_832d_4a38_755e0d841dc8
  54ba14c3_66dc_1c53_97a4_c6a0c1b24dbd["create_prompt()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| 54ba14c3_66dc_1c53_97a4_c6a0c1b24dbd
  4036bc45_ce9e_61e8_00e6_622f409e339f["from_llm_and_tools()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| 4036bc45_ce9e_61e8_00e6_622f409e339f
  e38eb1a4_a2ac_6830_d721_eefdd18e9a31["_agent_type()"]
  826e6477_6abe_d406_5d34_9df3fbec8707 -->|method| e38eb1a4_a2ac_6830_d721_eefdd18e9a31

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/structured_chat/base.py lines 39–163

class StructuredChatAgent(Agent):
    """Structured Chat Agent."""

    output_parser: AgentOutputParser = Field(
        default_factory=StructuredChatOutputParserWithRetries,
    )
    """Output parser for the agent."""

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

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

    def _construct_scratchpad(
        self,
        intermediate_steps: list[tuple[AgentAction, str]],
    ) -> str:
        agent_scratchpad = super()._construct_scratchpad(intermediate_steps)
        if not isinstance(agent_scratchpad, str):
            msg = "agent_scratchpad should be of type string."
            raise ValueError(msg)  # noqa: TRY004
        if agent_scratchpad:
            return (
                f"This was your previous work "
                f"(but I haven't seen any of it! I only see what "
                f"you return as final answer):\n{agent_scratchpad}"
            )
        return agent_scratchpad

    @classmethod
    def _validate_tools(cls, tools: Sequence[BaseTool]) -> None:
        pass

    @classmethod
    @override
    def _get_default_output_parser(
        cls,
        llm: BaseLanguageModel | None = None,
        **kwargs: Any,
    ) -> AgentOutputParser:
        return StructuredChatOutputParserWithRetries.from_llm(llm=llm)

    @property
    @override
    def _stop(self) -> list[str]:
        return ["Observation:"]

    @classmethod
    @override
    def create_prompt(
        cls,
        tools: Sequence[BaseTool],
        prefix: str = PREFIX,
        suffix: str = SUFFIX,
        human_message_template: str = HUMAN_MESSAGE_TEMPLATE,
        format_instructions: str = FORMAT_INSTRUCTIONS,
        input_variables: list[str] | None = None,
        memory_prompts: list[BasePromptTemplate] | None = None,
    ) -> BasePromptTemplate:
        tool_strings = []
        for tool in tools:
            args_schema = re.sub("}", "}}", re.sub("{", "{{", str(tool.args)))
            tool_strings.append(f"{tool.name}: {tool.description}, args: {args_schema}")
        formatted_tools = "\n".join(tool_strings)
        tool_names = ", ".join([tool.name for tool in tools])
        format_instructions = format_instructions.format(tool_names=tool_names)
        template = f"{prefix}\n\n{formatted_tools}\n\n{format_instructions}\n\n{suffix}"
        if input_variables is None:
            input_variables = ["input", "agent_scratchpad"]
        _memory_prompts = memory_prompts or []
        messages = [
            SystemMessagePromptTemplate.from_template(template),
            *_memory_prompts,
            HumanMessagePromptTemplate.from_template(human_message_template),
        ]
        return ChatPromptTemplate(input_variables=input_variables, messages=messages)  # type: ignore[arg-type]

Extends

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free