mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 08:12:39 +00:00
fix: remove debug prints and improve state management (#1815)
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -79,7 +79,6 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
self.iterations = 0
|
self.iterations = 0
|
||||||
self.log_error_after = 3
|
self.log_error_after = 3
|
||||||
self.have_forced_answer = False # Track if we've hit max iterations
|
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] = {
|
self.tool_name_to_tool_map: Dict[str, BaseTool] = {
|
||||||
tool.name: tool for tool in self.tools
|
tool.name: tool for tool in self.tools
|
||||||
}
|
}
|
||||||
@@ -113,7 +112,13 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
def _invoke_loop(self, formatted_answer=None):
|
def _invoke_loop(self, formatted_answer=None):
|
||||||
try:
|
try:
|
||||||
while not isinstance(formatted_answer, AgentFinish):
|
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(
|
answer = self.llm.call(
|
||||||
self.messages,
|
self.messages,
|
||||||
callbacks=self.callbacks,
|
callbacks=self.callbacks,
|
||||||
@@ -161,10 +166,11 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
|
|
||||||
formatted_answer.result = tool_result.result
|
formatted_answer.result = tool_result.result
|
||||||
if tool_result.result_as_answer:
|
if tool_result.result_as_answer:
|
||||||
|
# For tool results marked as final answers, return just the result
|
||||||
return AgentFinish(
|
return AgentFinish(
|
||||||
thought="",
|
thought="",
|
||||||
output=tool_result.result,
|
output=str(tool_result.result),
|
||||||
text=formatted_answer.text,
|
text=str(tool_result.result),
|
||||||
)
|
)
|
||||||
self._show_logs(formatted_answer)
|
self._show_logs(formatted_answer)
|
||||||
|
|
||||||
@@ -174,20 +180,35 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
# Check if we should force an answer and update state
|
# Check if we should force an answer and update state
|
||||||
should_force = self._should_force_answer()
|
should_force = self._should_force_answer()
|
||||||
if should_force:
|
if should_force:
|
||||||
# Set have_forced_answer to True as soon as we hit max iterations
|
|
||||||
self.have_forced_answer = True
|
self.have_forced_answer = True
|
||||||
# Return final answer with warning
|
# Extract the final answer from the last tool result or observation
|
||||||
formatted_answer.text += (
|
if isinstance(formatted_answer, AgentAction):
|
||||||
f'\n{self._i18n.errors("force_final_answer")}'
|
result = str(formatted_answer.result).strip()
|
||||||
)
|
else:
|
||||||
self._printer.print(
|
# Try to extract from Final Answer or Observation
|
||||||
content="Forcing final answer",
|
parts = formatted_answer.text.split("Final Answer:")
|
||||||
color="yellow"
|
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(
|
return AgentFinish(
|
||||||
thought="",
|
thought="",
|
||||||
output=formatted_answer.text,
|
output=final_answer,
|
||||||
text=formatted_answer.text,
|
text=final_answer,
|
||||||
)
|
)
|
||||||
self.messages.append(
|
self.messages.append(
|
||||||
self._format_msg(formatted_answer.text, role="assistant")
|
self._format_msg(formatted_answer.text, role="assistant")
|
||||||
|
|||||||
Reference in New Issue
Block a user