Cutting new version with tool ussage bug fix

This commit is contained in:
João Moura
2024-02-15 23:19:12 -03:00
parent 3ef502024d
commit 19b6c15fff
6 changed files with 104 additions and 98 deletions

View File

@@ -16,13 +16,11 @@ class ToolsHandler:
self.cache = cache
self.last_used_tool = {}
def on_tool_start(self, calling: ToolCalling) -> Any:
"""Run when tool starts running."""
self.last_used_tool = calling
def on_tool_end(self, calling: ToolCalling, output: str) -> Any:
def on_tool_use(self, calling: ToolCalling, output: str) -> Any:
"""Run when tool ends running."""
if self.last_used_tool.tool_name != CacheTools().name:
print(f"Tool {calling.tool_name} has been used.")
self.last_used_tool = calling
if calling.tool_name != CacheTools().name:
self.cache.add(
tool=calling.tool_name,
input=calling.arguments,

View File

@@ -61,13 +61,13 @@ class ToolUsage:
calling = self._tool_calling(tool_string)
if isinstance(calling, ToolUsageErrorException):
error = calling.message
self._printer.print(content=f"\n\n{error}\n", color="yellow")
self._printer.print(content=f"\n\n{error}\n", color="red")
return error
try:
tool = self._select_tool(calling.tool_name)
except Exception as e:
error = getattr(e, "message", str(e))
self._printer.print(content=f"\n\n{error}\n", color="yellow")
self._printer.print(content=f"\n\n{error}\n", color="red")
return error
return self._use(tool_string=tool_string, tool=tool, calling=calling)
@@ -94,8 +94,6 @@ class ToolUsage:
except Exception:
pass
self.tools_handler.on_tool_start(calling=calling)
result = self.tools_handler.cache.read(
tool=calling.tool_name, input=calling.arguments
)
@@ -107,18 +105,19 @@ class ToolUsage:
self._run_attempts += 1
if self._run_attempts > self._max_parsing_attempts:
self._telemetry.tool_usage_error(llm=self.llm)
return ToolUsageErrorException(
error = ToolUsageErrorException(
self._i18n.errors("tool_usage_exception").format(error=e)
).message
self._printer.print(content=f"\n\n{error}\n", color="red")
return error
return self.use(tool_string=tool_string)
self.tools_handler.on_tool_end(calling=calling, output=result)
self.tools_handler.on_tool_use(calling=calling, output=result)
self._printer.print(content=f"\n\n{result}\n", color="yellow")
self._telemetry.tool_usage(
llm=self.llm, tool_name=tool.name, attempts=self._run_attempts
)
result = self._format_result(result=result)
return result

View File

@@ -2,8 +2,13 @@ class Printer:
def print(self, content: str, color: str):
if color == "yellow":
self._print_yellow(content)
elif color == "red":
self._print_red(content)
else:
print(content)
def _print_yellow(self, content):
print("\033[93m {}\033[00m".format(content))
def _print_red(self, content):
print("\033[91m {}\033[00m".format(content))