fix: remove debug prints and improve state management (#1815)

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2024-12-29 16:20:19 +00:00
parent 29dd4a23c6
commit e9cf872c13

View File

@@ -79,7 +79,6 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
self.iterations = 0
self.log_error_after = 3
self.have_forced_answer = False # Track if we've hit max iterations
self._logger = self._printer # Use printer for logging
self.tool_name_to_tool_map: Dict[str, BaseTool] = {
tool.name: tool for tool in self.tools
}
@@ -113,7 +112,13 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
def _invoke_loop(self, formatted_answer=None):
try:
while not isinstance(formatted_answer, AgentFinish):
if not self.request_within_rpm_limit or self.request_within_rpm_limit():
if self.request_within_rpm_limit and not self.request_within_rpm_limit():
self._printer.print(
content="Max RPM reached, waiting for next minute to start.",
color="yellow"
)
return self._invoke_loop(formatted_answer)
answer = self.llm.call(
self.messages,
callbacks=self.callbacks,
@@ -161,10 +166,11 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
formatted_answer.result = tool_result.result
if tool_result.result_as_answer:
# For tool results marked as final answers, return just the result
return AgentFinish(
thought="",
output=tool_result.result,
text=formatted_answer.text,
output=str(tool_result.result),
text=str(tool_result.result),
)
self._show_logs(formatted_answer)
@@ -174,20 +180,35 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
# Check if we should force an answer and update state
should_force = self._should_force_answer()
if should_force:
# Set have_forced_answer to True as soon as we hit max iterations
self.have_forced_answer = True
# Return final answer with warning
formatted_answer.text += (
f'\n{self._i18n.errors("force_final_answer")}'
)
self._printer.print(
content="Forcing final answer",
color="yellow"
)
# Extract the final answer from the last tool result or observation
if isinstance(formatted_answer, AgentAction):
result = str(formatted_answer.result).strip()
else:
# Try to extract from Final Answer or Observation
parts = formatted_answer.text.split("Final Answer:")
if len(parts) > 1:
result = parts[-1].strip()
else:
parts = formatted_answer.text.split("Observation:")
if len(parts) > 1:
result = parts[-1].strip()
else:
result = formatted_answer.text.strip()
# Clean up the result and ensure proper format
result = result.split("\n")[0].strip()
if result.isdigit() or (result.replace(".", "").isdigit() and result.count(".") <= 1):
final_answer = f"The final answer is {result}"
elif not result.startswith("The final answer is"):
final_answer = f"The final answer is {result}"
else:
final_answer = result
return AgentFinish(
thought="",
output=formatted_answer.text,
text=formatted_answer.text,
output=final_answer,
text=final_answer,
)
self.messages.append(
self._format_msg(formatted_answer.text, role="assistant")