make tool verbosity override agent verbosity (#2173)

Currently, if a tool is set to verbose, an agent can override it by
passing in its own verbose flag. This is not ideal if we want to stream
back responses from agents, as we want the llm and tools to be sending
back events but nothing else. This also makes the behavior consistent
with ts.
This commit is contained in:
Ankush Gola 2023-03-30 01:05:58 -04:00 committed by GitHub
parent f5a4bf0ce4
commit 529a1f39b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,22 +55,24 @@ class BaseTool(BaseModel):
**kwargs: Any **kwargs: Any
) -> str: ) -> str:
"""Run the tool.""" """Run the tool."""
if verbose is None: if not self.verbose and verbose is not None:
verbose = self.verbose verbose_ = verbose
else:
verbose_ = self.verbose
self.callback_manager.on_tool_start( self.callback_manager.on_tool_start(
{"name": self.name, "description": self.description}, {"name": self.name, "description": self.description},
tool_input, tool_input,
verbose=verbose, verbose=verbose_,
color=start_color, color=start_color,
**kwargs, **kwargs,
) )
try: try:
observation = self._run(tool_input) observation = self._run(tool_input)
except (Exception, KeyboardInterrupt) as e: except (Exception, KeyboardInterrupt) as e:
self.callback_manager.on_tool_error(e, verbose=verbose) self.callback_manager.on_tool_error(e, verbose=verbose_)
raise e raise e
self.callback_manager.on_tool_end( self.callback_manager.on_tool_end(
observation, verbose=verbose, color=color, name=self.name, **kwargs observation, verbose=verbose_, color=color, name=self.name, **kwargs
) )
return observation return observation
@ -83,13 +85,15 @@ class BaseTool(BaseModel):
**kwargs: Any **kwargs: Any
) -> str: ) -> str:
"""Run the tool asynchronously.""" """Run the tool asynchronously."""
if verbose is None: if not self.verbose and verbose is not None:
verbose = self.verbose verbose_ = verbose
else:
verbose_ = self.verbose
if self.callback_manager.is_async: if self.callback_manager.is_async:
await self.callback_manager.on_tool_start( await self.callback_manager.on_tool_start(
{"name": self.name, "description": self.description}, {"name": self.name, "description": self.description},
tool_input, tool_input,
verbose=verbose, verbose=verbose_,
color=start_color, color=start_color,
**kwargs, **kwargs,
) )
@ -97,7 +101,7 @@ class BaseTool(BaseModel):
self.callback_manager.on_tool_start( self.callback_manager.on_tool_start(
{"name": self.name, "description": self.description}, {"name": self.name, "description": self.description},
tool_input, tool_input,
verbose=verbose, verbose=verbose_,
color=start_color, color=start_color,
**kwargs, **kwargs,
) )
@ -106,16 +110,16 @@ class BaseTool(BaseModel):
observation = await self._arun(tool_input) observation = await self._arun(tool_input)
except (Exception, KeyboardInterrupt) as e: except (Exception, KeyboardInterrupt) as e:
if self.callback_manager.is_async: if self.callback_manager.is_async:
await self.callback_manager.on_tool_error(e, verbose=verbose) await self.callback_manager.on_tool_error(e, verbose=verbose_)
else: else:
self.callback_manager.on_tool_error(e, verbose=verbose) self.callback_manager.on_tool_error(e, verbose=verbose_)
raise e raise e
if self.callback_manager.is_async: if self.callback_manager.is_async:
await self.callback_manager.on_tool_end( await self.callback_manager.on_tool_end(
observation, verbose=verbose, color=color, name=self.name, **kwargs observation, verbose=verbose_, color=color, name=self.name, **kwargs
) )
else: else:
self.callback_manager.on_tool_end( self.callback_manager.on_tool_end(
observation, verbose=verbose, color=color, name=self.name, **kwargs observation, verbose=verbose_, color=color, name=self.name, **kwargs
) )
return observation return observation