Skip to content

Commit 6ad360b

Browse files
authored
Suggestions for better debugging (#765)
Please feel free to disregard any changes you disagree with
1 parent 5198d6f commit 6ad360b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

tests/unit_tests/agents/test_agent.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class FakeListLLM(LLM, BaseModel):
1919
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
2020
"""Increment counter, and then return response in that index."""
2121
self.i += 1
22-
print(self.i)
23-
print(self.responses)
22+
print(f"=== Mock Response #{self.i} ===")
23+
print(self.responses[self.i])
2424
return self.responses[self.i]
2525

2626
@property
@@ -92,7 +92,10 @@ def test_agent_with_callbacks_global() -> None:
9292
output = agent.run("when was langchain made")
9393
assert output == "curses foiled again"
9494

95-
# 1 top level chain run, 2 LLMChain runs, 2 LLM runs, 1 tool run
95+
# 1 top level chain run runs, 2 LLMChain runs, 2 LLM runs, 1 tool run
96+
assert handler.chain_starts == handler.chain_ends == 3
97+
assert handler.llm_starts == handler.llm_ends == 2
98+
assert handler.tool_starts == handler.tool_ends == 1
9699
assert handler.starts == 6
97100
# 1 extra agent end
98101
assert handler.ends == 7
@@ -130,7 +133,10 @@ def test_agent_with_callbacks_local() -> None:
130133
output = agent.run("when was langchain made")
131134
assert output == "curses foiled again"
132135

133-
# 1 top level chain run, 2 LLMChain runs, 2 LLM runs, 1 tool run
136+
# 1 top level chain run, 2 LLMChain starts, 2 LLM runs, 1 tool run
137+
assert handler.chain_starts == handler.chain_ends == 3
138+
assert handler.llm_starts == handler.llm_ends == 2
139+
assert handler.tool_starts == handler.tool_ends == 1
134140
assert handler.starts == 6
135141
# 1 extra agent end
136142
assert handler.ends == 7

tests/unit_tests/callbacks/fake_callback_handler.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,25 @@ def ignore_agent(self) -> bool:
3939
"""Whether to ignore agent callbacks."""
4040
return self.ignore_agent_
4141

42+
# add finer-grained counters for easier debugging of failing tests
43+
chain_starts: int = 0
44+
chain_ends: int = 0
45+
llm_starts: int = 0
46+
llm_ends: int = 0
47+
tool_starts: int = 0
48+
tool_ends: int = 0
49+
agent_ends: int = 0
50+
4251
def on_llm_start(
4352
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
4453
) -> None:
4554
"""Run when LLM starts running."""
55+
self.llm_starts += 1
4656
self.starts += 1
4757

4858
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
4959
"""Run when LLM ends running."""
60+
self.llm_ends += 1
5061
self.ends += 1
5162

5263
def on_llm_error(
@@ -59,10 +70,12 @@ def on_chain_start(
5970
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
6071
) -> None:
6172
"""Run when chain starts running."""
73+
self.chain_starts += 1
6274
self.starts += 1
6375

6476
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
6577
"""Run when chain ends running."""
78+
self.chain_ends += 1
6679
self.ends += 1
6780

6881
def on_chain_error(
@@ -75,10 +88,12 @@ def on_tool_start(
7588
self, serialized: Dict[str, Any], action: AgentAction, **kwargs: Any
7689
) -> None:
7790
"""Run when tool starts running."""
91+
self.tool_starts += 1
7892
self.starts += 1
7993

8094
def on_tool_end(self, output: str, **kwargs: Any) -> None:
8195
"""Run when tool ends running."""
96+
self.tool_ends += 1
8297
self.ends += 1
8398

8499
def on_tool_error(
@@ -93,4 +108,5 @@ def on_text(self, text: str, **kwargs: Any) -> None:
93108

94109
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
95110
"""Run when agent ends running."""
111+
self.agent_ends += 1
96112
self.ends += 1

0 commit comments

Comments
 (0)