Skip to content

Commit 91659d6

Browse files
committed
counting for tool retries on the acutal usage
1 parent 0076ea7 commit 91659d6

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/crewai/tools/tool_usage.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,40 @@ def use(self, tool_string: str):
6060
self._printer.print(content=f"\n\n{error}\n", color="yellow")
6161
return error
6262
tool = self._select_tool(calling.function_name)
63-
return self._use(tool=tool, calling=calling)
63+
return self._use(tool_string=tool_string, tool=tool, calling=calling)
6464

65-
def _use(self, tool: BaseTool, calling: ToolCalling) -> None:
66-
if self._check_tool_repeated_usage(calling=calling):
67-
result = self._i18n.errors("task_repeated_usage").format(
68-
tool=calling.function_name, tool_input=calling.arguments
69-
)
70-
else:
71-
self.tools_handler.on_tool_start(calling=calling)
65+
def _use(self, tool_string: str, tool: BaseTool, calling: ToolCalling) -> None:
66+
try:
67+
if self._check_tool_repeated_usage(calling=calling):
68+
result = self._i18n.errors("task_repeated_usage").format(
69+
tool=calling.function_name, tool_input=calling.arguments
70+
)
71+
else:
72+
self.tools_handler.on_tool_start(calling=calling)
7273

73-
result = self.tools_handler.cache.read(
74-
tool=calling.function_name, input=calling.arguments
75-
)
74+
result = self.tools_handler.cache.read(
75+
tool=calling.function_name, input=calling.arguments
76+
)
7677

77-
if not result:
78-
result = tool._run(**calling.arguments)
79-
self.tools_handler.on_tool_end(calling=calling, output=result)
78+
if not result:
79+
result = tool._run(**calling.arguments)
80+
self.tools_handler.on_tool_end(calling=calling, output=result)
8081

81-
self._printer.print(content=f"\n\n{result}\n", color="yellow")
82-
self._telemetry.tool_usage(
83-
llm=self.llm, tool_name=tool.name, attempts=self._run_attempts
84-
)
82+
self._printer.print(content=f"\n\n{result}\n", color="yellow")
83+
self._telemetry.tool_usage(
84+
llm=self.llm, tool_name=tool.name, attempts=self._run_attempts
85+
)
8586

86-
result = self._format_result(result=result)
87-
return result
87+
result = self._format_result(result=result)
88+
return result
89+
except Exception:
90+
self._run_attempts += 1
91+
if self._run_attempts > self._max_parsing_attempts:
92+
self._telemetry.tool_usage_error(llm=self.llm)
93+
return ToolUsageErrorException(
94+
self._i18n.errors("tool_usage_error")
95+
).message
96+
return self.use(tool_string=tool_string)
8897

8998
def _format_result(self, result: Any) -> None:
9099
self.task.used_tools += 1

0 commit comments

Comments
 (0)