mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
fix: implement _remember_format in ToolUsage class (#1815)
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -127,10 +127,6 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
"Invalid response from LLM call - None or empty."
|
"Invalid response from LLM call - None or empty."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Remember output format before parsing
|
|
||||||
if hasattr(self.task, 'output_format'):
|
|
||||||
self._remember_format()
|
|
||||||
|
|
||||||
if not self.use_stop_words:
|
if not self.use_stop_words:
|
||||||
try:
|
try:
|
||||||
self._format_answer(answer)
|
self._format_answer(answer)
|
||||||
@@ -192,7 +188,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
except OutputParserException as e:
|
except OutputParserException as e:
|
||||||
error_msg = "Error on parsing tool." if "Error parsing tool usage" in str(e) else e.error
|
error_msg = e.error
|
||||||
|
if "Error parsing tool usage" in str(e):
|
||||||
|
error_msg = "Error on parsing tool."
|
||||||
self.messages.append({"role": "user", "content": error_msg})
|
self.messages.append({"role": "user", "content": error_msg})
|
||||||
if self.iterations > self.log_error_after:
|
if self.iterations > self.log_error_after:
|
||||||
self._printer.print(
|
self._printer.print(
|
||||||
|
|||||||
@@ -145,6 +145,10 @@ class ToolUsage:
|
|||||||
from_cache = False
|
from_cache = False
|
||||||
|
|
||||||
result = None # type: ignore # Incompatible types in assignment (expression has type "None", variable has type "str")
|
result = None # type: ignore # Incompatible types in assignment (expression has type "None", variable has type "str")
|
||||||
|
|
||||||
|
# Remember output format if needed
|
||||||
|
self._remember_format()
|
||||||
|
|
||||||
# check if cache is available
|
# check if cache is available
|
||||||
if self.tools_handler.cache:
|
if self.tools_handler.cache:
|
||||||
result = self.tools_handler.cache.read( # type: ignore # Incompatible types in assignment (expression has type "str | None", variable has type "str")
|
result = self.tools_handler.cache.read( # type: ignore # Incompatible types in assignment (expression has type "str | None", variable has type "str")
|
||||||
@@ -259,14 +263,20 @@ class ToolUsage:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def _should_remember_format(self) -> bool:
|
def _should_remember_format(self) -> bool:
|
||||||
return self.task.used_tools % self._remember_format_after_usages == 0
|
"""Check if we should remember the output format based on usage count."""
|
||||||
|
return self.task.used_tools >= self._remember_format_after_usages
|
||||||
|
|
||||||
def _remember_format(self, result: str) -> None:
|
def _remember_format(self, result: str = None) -> str:
|
||||||
result = str(result)
|
"""Remember the output format for the task and optionally format the result."""
|
||||||
result += "\n\n" + self._i18n.slice("tools").format(
|
if hasattr(self.task, 'output_format') and self._should_remember_format():
|
||||||
tools=self.tools_description, tool_names=self.tools_names
|
self.task.output_format = True
|
||||||
)
|
|
||||||
return result # type: ignore # No return value expected
|
if result is not None:
|
||||||
|
result = str(result)
|
||||||
|
result += "\n\n" + self._i18n.slice("tools").format(
|
||||||
|
tools=self.tools_description, tool_names=self.tools_names
|
||||||
|
)
|
||||||
|
return result if result is not None else ""
|
||||||
|
|
||||||
def _check_tool_repeated_usage(
|
def _check_tool_repeated_usage(
|
||||||
self, calling: Union[ToolCalling, InstructorToolCalling]
|
self, calling: Union[ToolCalling, InstructorToolCalling]
|
||||||
|
|||||||
Reference in New Issue
Block a user