Skip to content

Commit cbc1467

Browse files
authored
verbose flag (#683)
1 parent 27cef08 commit cbc1467

File tree

17 files changed

+184
-169
lines changed

17 files changed

+184
-169
lines changed

langchain/agents/agent.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ def _should_continue(self, iterations: int) -> bool:
241241
return iterations < self.max_iterations
242242

243243
def _return(self, output: AgentFinish, intermediate_steps: list) -> Dict[str, Any]:
244-
if self.verbose:
245-
self.callback_manager.on_agent_finish(output, color="green")
244+
self.callback_manager.on_agent_finish(
245+
output, color="green", verbose=self.verbose
246+
)
246247
final_output = output.return_values
247248
if self.return_intermediate_steps:
248249
final_output["intermediate_steps"] = intermediate_steps
@@ -272,35 +273,35 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, Any]:
272273
# Otherwise we lookup the tool
273274
if output.tool in name_to_tool_map:
274275
tool = name_to_tool_map[output.tool]
275-
if self.verbose:
276-
self.callback_manager.on_tool_start(
277-
{"name": str(tool.func)[:60] + "..."}, output, color="green"
278-
)
276+
self.callback_manager.on_tool_start(
277+
{"name": str(tool.func)[:60] + "..."},
278+
output,
279+
color="green",
280+
verbose=self.verbose,
281+
)
279282
try:
280283
# We then call the tool on the tool input to get an observation
281284
observation = tool.func(output.tool_input)
282285
color = color_mapping[output.tool]
283286
return_direct = tool.return_direct
284287
except Exception as e:
285-
if self.verbose:
286-
self.callback_manager.on_tool_error(e)
288+
self.callback_manager.on_tool_error(e, verbose=self.verbose)
287289
raise e
288290
else:
289-
if self.verbose:
290-
self.callback_manager.on_tool_start(
291-
{"name": "N/A"}, output, color="green"
292-
)
291+
self.callback_manager.on_tool_start(
292+
{"name": "N/A"}, output, color="green", verbose=self.verbose
293+
)
293294
observation = f"{output.tool} is not a valid tool, try another one."
294295
color = None
295296
return_direct = False
296-
if self.verbose:
297-
llm_prefix = "" if return_direct else self.agent.llm_prefix
298-
self.callback_manager.on_tool_end(
299-
observation,
300-
color=color,
301-
observation_prefix=self.agent.observation_prefix,
302-
llm_prefix=llm_prefix,
303-
)
297+
llm_prefix = "" if return_direct else self.agent.llm_prefix
298+
self.callback_manager.on_tool_end(
299+
observation,
300+
color=color,
301+
observation_prefix=self.agent.observation_prefix,
302+
llm_prefix=llm_prefix,
303+
verbose=self.verbose,
304+
)
304305
intermediate_steps.append((output, observation))
305306
if return_direct:
306307
# Set the log to "" because we do not want to log it.

langchain/callbacks/base.py

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@ class BaseCallbackHandler(BaseModel, ABC):
1515
ignore_chain: bool = False
1616
ignore_agent: bool = False
1717

18+
@property
19+
def always_verbose(self) -> bool:
20+
"""Whether to call verbose callbacks even if verbose is False."""
21+
return False
22+
1823
@abstractmethod
1924
def on_llm_start(
2025
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
2126
) -> None:
2227
"""Run when LLM starts running."""
2328

2429
@abstractmethod
25-
def on_llm_end(
26-
self,
27-
response: LLMResult,
28-
) -> None:
30+
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
2931
"""Run when LLM ends running."""
3032

3133
@abstractmethod
32-
def on_llm_error(self, error: Exception) -> None:
34+
def on_llm_error(self, error: Exception, **kwargs: Any) -> None:
3335
"""Run when LLM errors."""
3436

3537
@abstractmethod
@@ -39,11 +41,11 @@ def on_chain_start(
3941
"""Run when chain starts running."""
4042

4143
@abstractmethod
42-
def on_chain_end(self, outputs: Dict[str, Any]) -> None:
44+
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
4345
"""Run when chain ends running."""
4446

4547
@abstractmethod
46-
def on_chain_error(self, error: Exception) -> None:
48+
def on_chain_error(self, error: Exception, **kwargs: Any) -> None:
4749
"""Run when chain errors."""
4850

4951
@abstractmethod
@@ -57,7 +59,7 @@ def on_tool_end(self, output: str, **kwargs: Any) -> None:
5759
"""Run when tool ends running."""
5860

5961
@abstractmethod
60-
def on_tool_error(self, error: Exception) -> None:
62+
def on_tool_error(self, error: Exception, **kwargs: Any) -> None:
6163
"""Run when tool errors."""
6264

6365
@abstractmethod
@@ -91,78 +93,110 @@ class CallbackManager(BaseCallbackManager):
9193
handlers: List[BaseCallbackHandler]
9294

9395
def on_llm_start(
94-
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
96+
self,
97+
serialized: Dict[str, Any],
98+
prompts: List[str],
99+
verbose: bool = False,
100+
**kwargs: Any
95101
) -> None:
96102
"""Run when LLM starts running."""
97103
for handler in self.handlers:
98104
if not handler.ignore_llm:
99-
handler.on_llm_start(serialized, prompts, **kwargs)
105+
if verbose or handler.always_verbose:
106+
handler.on_llm_start(serialized, prompts, **kwargs)
100107

101108
def on_llm_end(
102-
self,
103-
response: LLMResult,
109+
self, response: LLMResult, verbose: bool = False, **kwargs: Any
104110
) -> None:
105111
"""Run when LLM ends running."""
106112
for handler in self.handlers:
107113
if not handler.ignore_llm:
108-
handler.on_llm_end(response)
114+
if verbose or handler.always_verbose:
115+
handler.on_llm_end(response)
109116

110-
def on_llm_error(self, error: Exception) -> None:
117+
def on_llm_error(
118+
self, error: Exception, verbose: bool = False, **kwargs: Any
119+
) -> None:
111120
"""Run when LLM errors."""
112121
for handler in self.handlers:
113122
if not handler.ignore_llm:
114-
handler.on_llm_error(error)
123+
if verbose or handler.always_verbose:
124+
handler.on_llm_error(error)
115125

116126
def on_chain_start(
117-
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
127+
self,
128+
serialized: Dict[str, Any],
129+
inputs: Dict[str, Any],
130+
verbose: bool = False,
131+
**kwargs: Any
118132
) -> None:
119133
"""Run when chain starts running."""
120134
for handler in self.handlers:
121135
if not handler.ignore_chain:
122-
handler.on_chain_start(serialized, inputs, **kwargs)
136+
if verbose or handler.always_verbose:
137+
handler.on_chain_start(serialized, inputs, **kwargs)
123138

124-
def on_chain_end(self, outputs: Dict[str, Any]) -> None:
139+
def on_chain_end(
140+
self, outputs: Dict[str, Any], verbose: bool = False, **kwargs: Any
141+
) -> None:
125142
"""Run when chain ends running."""
126143
for handler in self.handlers:
127144
if not handler.ignore_chain:
128-
handler.on_chain_end(outputs)
145+
if verbose or handler.always_verbose:
146+
handler.on_chain_end(outputs)
129147

130-
def on_chain_error(self, error: Exception) -> None:
148+
def on_chain_error(
149+
self, error: Exception, verbose: bool = False, **kwargs: Any
150+
) -> None:
131151
"""Run when chain errors."""
132152
for handler in self.handlers:
133153
if not handler.ignore_chain:
134-
handler.on_chain_error(error)
154+
if verbose or handler.always_verbose:
155+
handler.on_chain_error(error)
135156

136157
def on_tool_start(
137-
self, serialized: Dict[str, Any], action: AgentAction, **kwargs: Any
158+
self,
159+
serialized: Dict[str, Any],
160+
action: AgentAction,
161+
verbose: bool = False,
162+
**kwargs: Any
138163
) -> None:
139164
"""Run when tool starts running."""
140165
for handler in self.handlers:
141166
if not handler.ignore_agent:
142-
handler.on_tool_start(serialized, action, **kwargs)
167+
if verbose or handler.always_verbose:
168+
handler.on_tool_start(serialized, action, **kwargs)
143169

144-
def on_tool_end(self, output: str, **kwargs: Any) -> None:
170+
def on_tool_end(self, output: str, verbose: bool = False, **kwargs: Any) -> None:
145171
"""Run when tool ends running."""
146172
for handler in self.handlers:
147173
if not handler.ignore_agent:
148-
handler.on_tool_end(output, **kwargs)
174+
if verbose or handler.always_verbose:
175+
handler.on_tool_end(output, **kwargs)
149176

150-
def on_tool_error(self, error: Exception) -> None:
177+
def on_tool_error(
178+
self, error: Exception, verbose: bool = False, **kwargs: Any
179+
) -> None:
151180
"""Run when tool errors."""
152181
for handler in self.handlers:
153182
if not handler.ignore_agent:
154-
handler.on_tool_error(error)
183+
if verbose or handler.always_verbose:
184+
handler.on_tool_error(error)
155185

156-
def on_text(self, text: str, **kwargs: Any) -> None:
186+
def on_text(self, text: str, verbose: bool = False, **kwargs: Any) -> None:
157187
"""Run on additional input from chains and agents."""
158188
for handler in self.handlers:
159-
handler.on_text(text, **kwargs)
189+
if verbose or handler.always_verbose:
190+
handler.on_text(text, **kwargs)
160191

161-
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
192+
def on_agent_finish(
193+
self, finish: AgentFinish, verbose: bool = False, **kwargs: Any
194+
) -> None:
162195
"""Run on agent end."""
163196
for handler in self.handlers:
164197
if not handler.ignore_agent:
165-
handler.on_agent_finish(finish, **kwargs)
198+
if verbose or handler.always_verbose:
199+
handler.on_agent_finish(finish, **kwargs)
166200

167201
def add_handler(self, handler: BaseCallbackHandler) -> None:
168202
"""Add a handler to the callback manager."""

langchain/callbacks/shared.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,15 @@ def on_llm_start(
4141
with self._lock:
4242
self._callback_manager.on_llm_start(serialized, prompts, **kwargs)
4343

44-
def on_llm_end(
45-
self,
46-
response: LLMResult,
47-
) -> None:
44+
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
4845
"""Run when LLM ends running."""
4946
with self._lock:
50-
self._callback_manager.on_llm_end(response)
47+
self._callback_manager.on_llm_end(response, **kwargs)
5148

52-
def on_llm_error(self, error: Exception) -> None:
49+
def on_llm_error(self, error: Exception, **kwargs: Any) -> None:
5350
"""Run when LLM errors."""
5451
with self._lock:
55-
self._callback_manager.on_llm_error(error)
52+
self._callback_manager.on_llm_error(error, **kwargs)
5653

5754
def on_chain_start(
5855
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
@@ -61,15 +58,15 @@ def on_chain_start(
6158
with self._lock:
6259
self._callback_manager.on_chain_start(serialized, inputs, **kwargs)
6360

64-
def on_chain_end(self, outputs: Dict[str, Any]) -> None:
61+
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
6562
"""Run when chain ends running."""
6663
with self._lock:
67-
self._callback_manager.on_chain_end(outputs)
64+
self._callback_manager.on_chain_end(outputs, **kwargs)
6865

69-
def on_chain_error(self, error: Exception) -> None:
66+
def on_chain_error(self, error: Exception, **kwargs: Any) -> None:
7067
"""Run when chain errors."""
7168
with self._lock:
72-
self._callback_manager.on_chain_error(error)
69+
self._callback_manager.on_chain_error(error, **kwargs)
7370

7471
def on_tool_start(
7572
self, serialized: Dict[str, Any], action: AgentAction, **kwargs: Any
@@ -83,10 +80,10 @@ def on_tool_end(self, output: str, **kwargs: Any) -> None:
8380
with self._lock:
8481
self._callback_manager.on_tool_end(output, **kwargs)
8582

86-
def on_tool_error(self, error: Exception) -> None:
83+
def on_tool_error(self, error: Exception, **kwargs: Any) -> None:
8784
"""Run when tool errors."""
8885
with self._lock:
89-
self._callback_manager.on_tool_error(error)
86+
self._callback_manager.on_tool_error(error, **kwargs)
9087

9188
def on_text(self, text: str, **kwargs: Any) -> None:
9289
"""Run on arbitrary text."""

langchain/callbacks/stdout.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def on_llm_start(
1515
"""Print out the prompts."""
1616
pass
1717

18-
def on_llm_end(self, response: LLMResult) -> None:
18+
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
1919
"""Do nothing."""
2020
pass
2121

22-
def on_llm_error(self, error: Exception) -> None:
22+
def on_llm_error(self, error: Exception, **kwargs: Any) -> None:
2323
"""Do nothing."""
2424
pass
2525

@@ -30,11 +30,11 @@ def on_chain_start(
3030
class_name = serialized["name"]
3131
print(f"\n\n\033[1m> Entering new {class_name} chain...\033[0m")
3232

33-
def on_chain_end(self, outputs: Dict[str, Any]) -> None:
33+
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
3434
"""Print out that we finished a chain."""
3535
print("\n\033[1m> Finished chain.\033[0m")
3636

37-
def on_chain_error(self, error: Exception) -> None:
37+
def on_chain_error(self, error: Exception, **kwargs: Any) -> None:
3838
"""Do nothing."""
3939
pass
4040

@@ -61,7 +61,7 @@ def on_tool_end(
6161
print_text(output, color=color)
6262
print_text(f"\n{llm_prefix}")
6363

64-
def on_tool_error(self, error: Exception) -> None:
64+
def on_tool_error(self, error: Exception, **kwargs: Any) -> None:
6565
"""Do nothing."""
6666
pass
6767

langchain/callbacks/streamlit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ def on_llm_start(
1818
for prompt in prompts:
1919
st.write(prompt)
2020

21-
def on_llm_end(self, response: LLMResult) -> None:
21+
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
2222
"""Do nothing."""
2323
pass
2424

25-
def on_llm_error(self, error: Exception) -> None:
25+
def on_llm_error(self, error: Exception, **kwargs: Any) -> None:
2626
"""Do nothing."""
2727
pass
2828

@@ -33,11 +33,11 @@ def on_chain_start(
3333
class_name = serialized["name"]
3434
st.write(f"Entering new {class_name} chain...")
3535

36-
def on_chain_end(self, outputs: Dict[str, Any]) -> None:
36+
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
3737
"""Print out that we finished a chain."""
3838
st.write("Finished chain.")
3939

40-
def on_chain_error(self, error: Exception) -> None:
40+
def on_chain_error(self, error: Exception, **kwargs: Any) -> None:
4141
"""Do nothing."""
4242
pass
4343

@@ -62,7 +62,7 @@ def on_tool_end(
6262
st.write(f"{observation_prefix}{output}")
6363
st.write(llm_prefix)
6464

65-
def on_tool_error(self, error: Exception) -> None:
65+
def on_tool_error(self, error: Exception, **kwargs: Any) -> None:
6666
"""Do nothing."""
6767
pass
6868

langchain/chains/api/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
6666
api_url = self.api_request_chain.predict(
6767
question=question, api_docs=self.api_docs
6868
)
69-
if self.verbose:
70-
self.callback_manager.on_text(api_url, color="green", end="\n")
69+
self.callback_manager.on_text(
70+
api_url, color="green", end="\n", verbose=self.verbose
71+
)
7172
api_response = self.requests_wrapper.run(api_url)
72-
if self.verbose:
73-
self.callback_manager.on_text(api_response, color="yellow", end="\n")
73+
self.callback_manager.on_text(
74+
api_response, color="yellow", end="\n", verbose=self.verbose
75+
)
7476
answer = self.api_answer_chain.predict(
7577
question=question,
7678
api_docs=self.api_docs,

0 commit comments

Comments
 (0)