TrajectoryEvalChain Class — langchain Architecture
Architecture documentation for the TrajectoryEvalChain class in trajectory_eval_chain.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 3d3366c2_5416_d37e_bd23_be619d12afcb["TrajectoryEvalChain"] 28f1d4e2_d5f6_ba6b_50d5_3dd7eadf116a["AgentTrajectoryEvaluator"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|extends| 28f1d4e2_d5f6_ba6b_50d5_3dd7eadf116a 649622c5_b1b0_2ee7_22ee_c9c12162f9c3["LLMEvalChain"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|extends| 649622c5_b1b0_2ee7_22ee_c9c12162f9c3 d009a608_c505_bd50_7200_0de8a69ba4b7["BaseChatModel"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|extends| d009a608_c505_bd50_7200_0de8a69ba4b7 c646e46b_908e_bc3a_44e1_fafa62570a54["trajectory_eval_chain.py"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|defined in| c646e46b_908e_bc3a_44e1_fafa62570a54 652dbc75_fe93_31cd_f2db_b8450a7412a1["requires_reference()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| 652dbc75_fe93_31cd_f2db_b8450a7412a1 27fae820_0472_f86c_00e0_27ae8a58917f["_tools_description()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| 27fae820_0472_f86c_00e0_27ae8a58917f 036ea71a_b011_ff25_4927_06d4798e917a["get_agent_trajectory()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| 036ea71a_b011_ff25_4927_06d4798e917a f4e0e086_a46a_b987_c2d9_ce6bbdd523d2["_format_reference()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| f4e0e086_a46a_b987_c2d9_ce6bbdd523d2 f941d7d8_d462_4474_ee8c_f0905dc47ba1["from_llm()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| f941d7d8_d462_4474_ee8c_f0905dc47ba1 d0554d34_7348_e8a0_c0a8_20d22fcb17ca["input_keys()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| d0554d34_7348_e8a0_c0a8_20d22fcb17ca 00dc047e_d212_cb5a_b126_fd44b2bbabcd["output_keys()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| 00dc047e_d212_cb5a_b126_fd44b2bbabcd ecca9fb5_244d_e8b6_c7b7_0e146a0ba9a0["prep_inputs()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| ecca9fb5_244d_e8b6_c7b7_0e146a0ba9a0 b7bd5bac_843c_03aa_73f7_6648f0eb2bb0["_call()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| b7bd5bac_843c_03aa_73f7_6648f0eb2bb0 6a1264f7_c866_b36a_8d73_c1d4c5af1b9b["_acall()"] 3d3366c2_5416_d37e_bd23_be619d12afcb -->|method| 6a1264f7_c866_b36a_8d73_c1d4c5af1b9b
Relationship Graph
Source Code
libs/langchain/langchain_classic/evaluation/agents/trajectory_eval_chain.py lines 96–418
class TrajectoryEvalChain(AgentTrajectoryEvaluator, LLMEvalChain):
"""A chain for evaluating ReAct style agents.
This chain is used to evaluate ReAct style agents by reasoning about
the sequence of actions taken and their outcomes.
Based on the paper "ReAct: Synergizing Reasoning and Acting in Language Models"
(https://arxiv.org/abs/2210.03629)
Example:
```python
from langchain_classic.agents import AgentType, initialize_agent
from langchain_openai import ChatOpenAI
from langchain_classic.evaluation import TrajectoryEvalChain
from langchain_classic.tools import tool
@tool
def geography_answers(country: str, question: str) -> str:
\"\"\"Very helpful answers to geography questions.\"\"\"
return f"{country}? IDK - We may never know {question}."
model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent = initialize_agent(
tools=[geography_answers],
llm=model,
agent=AgentType.OPENAI_FUNCTIONS,
return_intermediate_steps=True,
)
question = "How many dwell in the largest minor region in Argentina?"
response = agent(question)
eval_chain = TrajectoryEvalChain.from_llm(
llm=model, agent_tools=[geography_answers], return_reasoning=True
)
result = eval_chain.evaluate_agent_trajectory(
input=question,
agent_trajectory=response["intermediate_steps"],
prediction=response["output"],
reference="Paris",
)
print(result["score"]) # noqa: T201
# 0
```
"""
agent_tools: list[BaseTool] | None = None
"""A list of tools available to the agent."""
eval_chain: LLMChain
"""The language model chain used for evaluation."""
output_parser: TrajectoryOutputParser = Field(
default_factory=TrajectoryOutputParser,
)
"""The output parser used to parse the output."""
return_reasoning: bool = False
"""DEPRECATED. Reasoning always returned."""
model_config = ConfigDict(
extra="ignore",
)
@property
def requires_reference(self) -> bool:
"""Whether this evaluator requires a reference label."""
return False
@property
def _tools_description(self) -> str:
"""Get the description of the agent tools.
Returns:
The description of the agent tools.
"""
if self.agent_tools is None:
return ""
return "\n\n".join(
[
f"""Tool {i}: {tool.name}
Description: {tool.description}"""
for i, tool in enumerate(self.agent_tools, 1)
Source
Frequently Asked Questions
What is the TrajectoryEvalChain class?
TrajectoryEvalChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/evaluation/agents/trajectory_eval_chain.py.
Where is TrajectoryEvalChain defined?
TrajectoryEvalChain is defined in libs/langchain/langchain_classic/evaluation/agents/trajectory_eval_chain.py at line 96.
What does TrajectoryEvalChain extend?
TrajectoryEvalChain extends AgentTrajectoryEvaluator, LLMEvalChain, BaseChatModel.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free