ChatAgent Class — langchain Architecture
Architecture documentation for the ChatAgent class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7["ChatAgent"] 37fcc3f3_2798_3648_915c_2bffdd19bff7["Agent"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|extends| 37fcc3f3_2798_3648_915c_2bffdd19bff7 356f1882_f802_ae8d_971b_9adf83ef86ea["base.py"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|defined in| 356f1882_f802_ae8d_971b_9adf83ef86ea 84505bd3_5cbc_2907_db15_266c1d471d88["observation_prefix()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 84505bd3_5cbc_2907_db15_266c1d471d88 61513a51_b63c_7f8d_7770_1d457e8d96a2["llm_prefix()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 61513a51_b63c_7f8d_7770_1d457e8d96a2 2afe5c6a_955b_c3e0_01d8_639bb3398225["_construct_scratchpad()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 2afe5c6a_955b_c3e0_01d8_639bb3398225 03335848_7c0b_88ca_e96f_eaa5a72dc146["_get_default_output_parser()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 03335848_7c0b_88ca_e96f_eaa5a72dc146 0e7041cb_6040_49fd_f4a2_f9c6892bdef1["_validate_tools()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 0e7041cb_6040_49fd_f4a2_f9c6892bdef1 c944b985_377f_3d2e_121a_6a399f0486f7["_stop()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| c944b985_377f_3d2e_121a_6a399f0486f7 6ddf1017_f7b1_a745_71bb_6fcfb79c937b["create_prompt()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 6ddf1017_f7b1_a745_71bb_6fcfb79c937b 672a572f_b218_8d99_e7ff_59f1b43194b5["from_llm_and_tools()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| 672a572f_b218_8d99_e7ff_59f1b43194b5 ac228751_ffdd_63a3_3ff6_8ebd49b23e65["_agent_type()"] b3f40ce7_80ee_f7bd_df0e_7eb84f6efab7 -->|method| ac228751_ffdd_63a3_3ff6_8ebd49b23e65
Relationship Graph
Source Code
libs/langchain/langchain_classic/agents/chat/base.py lines 36–178
class ChatAgent(Agent):
"""Chat Agent."""
output_parser: AgentOutputParser = Field(default_factory=ChatOutputParser)
"""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
@override
def _get_default_output_parser(cls, **kwargs: Any) -> AgentOutputParser:
return ChatOutputParser()
@classmethod
def _validate_tools(cls, tools: Sequence[BaseTool]) -> None:
super()._validate_tools(tools)
validate_tools_single_input(class_name=cls.__name__, tools=tools)
@property
def _stop(self) -> list[str]:
return ["Observation:"]
@classmethod
def create_prompt(
cls,
tools: Sequence[BaseTool],
system_message_prefix: str = SYSTEM_MESSAGE_PREFIX,
system_message_suffix: str = SYSTEM_MESSAGE_SUFFIX,
human_message: str = HUMAN_MESSAGE,
format_instructions: str = FORMAT_INSTRUCTIONS,
input_variables: list[str] | None = None,
) -> BasePromptTemplate:
"""Create a prompt from a list of tools.
Args:
tools: A list of tools.
system_message_prefix: The system message prefix.
system_message_suffix: The system message suffix.
human_message: The `HumanMessage`.
format_instructions: The format instructions.
input_variables: The input variables.
Returns:
A prompt template.
"""
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)
template = (
f"{system_message_prefix}\n\n"
f"{tool_strings}\n\n"
f"{format_instructions}\n\n"
f"{system_message_suffix}"
)
messages = [
SystemMessagePromptTemplate.from_template(template),
HumanMessagePromptTemplate.from_template(human_message),
Extends
Source
Frequently Asked Questions
What is the ChatAgent class?
ChatAgent is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/chat/base.py.
Where is ChatAgent defined?
ChatAgent is defined in libs/langchain/langchain_classic/agents/chat/base.py at line 36.
What does ChatAgent extend?
ChatAgent extends Agent.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free