mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 08:08:32 +00:00
dropped usage of printer here for rich console and dropped non-added value logging
This commit is contained in:
@@ -6,6 +6,8 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from pydantic import BaseModel, Field, GetCoreSchemaHandler
|
from pydantic import BaseModel, Field, GetCoreSchemaHandler
|
||||||
from pydantic_core import CoreSchema, core_schema
|
from pydantic_core import CoreSchema, core_schema
|
||||||
|
from rich.console import Console
|
||||||
|
from rich.text import Text
|
||||||
|
|
||||||
from crewai.agents.agent_builder.base_agent_executor_mixin import CrewAgentExecutorMixin
|
from crewai.agents.agent_builder.base_agent_executor_mixin import CrewAgentExecutorMixin
|
||||||
from crewai.agents.parser import (
|
from crewai.agents.parser import (
|
||||||
@@ -146,6 +148,7 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
self.request_within_rpm_limit = request_within_rpm_limit
|
self.request_within_rpm_limit = request_within_rpm_limit
|
||||||
self.response_model = response_model
|
self.response_model = response_model
|
||||||
self.log_error_after = 3
|
self.log_error_after = 3
|
||||||
|
self._console: Console = Console()
|
||||||
|
|
||||||
# Error context storage for recovery
|
# Error context storage for recovery
|
||||||
self._last_parser_error: OutputParserError | None = None
|
self._last_parser_error: OutputParserError | None = None
|
||||||
@@ -220,7 +223,7 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
return self._state.iterations
|
return self._state.iterations
|
||||||
|
|
||||||
@start()
|
@start()
|
||||||
def initialize_reasoning(self) -> str:
|
def initialize_reasoning(self) -> Literal["initialized"]:
|
||||||
"""Initialize the reasoning flow and emit agent start logs."""
|
"""Initialize the reasoning flow and emit agent start logs."""
|
||||||
self._show_start_logs()
|
self._show_start_logs()
|
||||||
return "initialized"
|
return "initialized"
|
||||||
@@ -248,10 +251,6 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
|
|
||||||
Returns routing decision based on parsing result.
|
Returns routing decision based on parsing result.
|
||||||
"""
|
"""
|
||||||
self._printer.print(
|
|
||||||
content=f"🤖 call_llm_and_parse: About to call LLM (iteration {self.state.iterations})",
|
|
||||||
color="blue",
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
enforce_rpm_limit(self.request_within_rpm_limit)
|
enforce_rpm_limit(self.request_within_rpm_limit)
|
||||||
|
|
||||||
@@ -270,16 +269,18 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
formatted_answer = process_llm_response(answer, self.use_stop_words)
|
formatted_answer = process_llm_response(answer, self.use_stop_words)
|
||||||
self.state.current_answer = formatted_answer
|
self.state.current_answer = formatted_answer
|
||||||
|
|
||||||
# Debug: Check what we parsed
|
if "Final Answer:" in answer and isinstance(formatted_answer, AgentAction):
|
||||||
if "Final Answer:" in answer:
|
warning_text = Text()
|
||||||
self._printer.print(
|
warning_text.append("⚠️ ", style="yellow bold")
|
||||||
content=f"⚠️ LLM returned Final Answer but parsed as: {type(formatted_answer).__name__}",
|
warning_text.append(
|
||||||
color="yellow",
|
f"LLM returned 'Final Answer:' but parsed as AgentAction (tool: {formatted_answer.tool})",
|
||||||
|
style="yellow",
|
||||||
)
|
)
|
||||||
if isinstance(formatted_answer, AgentAction):
|
self._console.print(warning_text)
|
||||||
self._printer.print(
|
preview_text = Text()
|
||||||
content=f"Answer preview: {answer[:200]}...", color="yellow"
|
preview_text.append("Answer preview: ", style="yellow")
|
||||||
)
|
preview_text.append(f"{answer[:200]}...", style="yellow dim")
|
||||||
|
self._console.print(preview_text)
|
||||||
|
|
||||||
return "parsed"
|
return "parsed"
|
||||||
|
|
||||||
@@ -300,11 +301,6 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
@router(call_llm_and_parse)
|
@router(call_llm_and_parse)
|
||||||
def route_by_answer_type(self) -> str:
|
def route_by_answer_type(self) -> str:
|
||||||
"""Route based on whether answer is AgentAction or AgentFinish."""
|
"""Route based on whether answer is AgentAction or AgentFinish."""
|
||||||
answer_type = type(self.state.current_answer).__name__
|
|
||||||
self._printer.print(
|
|
||||||
content=f"🚦 route_by_answer_type: Got {answer_type}",
|
|
||||||
color="yellow",
|
|
||||||
)
|
|
||||||
if isinstance(self.state.current_answer, AgentAction):
|
if isinstance(self.state.current_answer, AgentAction):
|
||||||
return "execute_tool"
|
return "execute_tool"
|
||||||
return "agent_finished"
|
return "agent_finished"
|
||||||
@@ -360,7 +356,10 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
return "tool_completed"
|
return "tool_completed"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._printer.print(content=f"Error in tool execution: {e}", color="red")
|
error_text = Text()
|
||||||
|
error_text.append("❌ Error in tool execution: ", style="red bold")
|
||||||
|
error_text.append(str(e), style="red")
|
||||||
|
self._console.print(error_text)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@listen("initialized")
|
@listen("initialized")
|
||||||
@@ -371,10 +370,6 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
@router(or_(initialize_reasoning, continue_iteration))
|
@router(or_(initialize_reasoning, continue_iteration))
|
||||||
def check_max_iterations(self) -> str:
|
def check_max_iterations(self) -> str:
|
||||||
"""Check if max iterations reached before proceeding with reasoning."""
|
"""Check if max iterations reached before proceeding with reasoning."""
|
||||||
self._printer.print(
|
|
||||||
content=f"🔄 check_max_iterations: iteration {self.state.iterations}/{self.max_iter}",
|
|
||||||
color="cyan",
|
|
||||||
)
|
|
||||||
if has_reached_max_iterations(self.state.iterations, self.max_iter):
|
if has_reached_max_iterations(self.state.iterations, self.max_iter):
|
||||||
return "force_final_answer"
|
return "force_final_answer"
|
||||||
return "continue_reasoning"
|
return "continue_reasoning"
|
||||||
@@ -383,27 +378,35 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
def increment_and_continue(self) -> str:
|
def increment_and_continue(self) -> str:
|
||||||
"""Increment iteration counter and loop back for next iteration."""
|
"""Increment iteration counter and loop back for next iteration."""
|
||||||
self.state.iterations += 1
|
self.state.iterations += 1
|
||||||
self._printer.print(
|
inc_text = Text()
|
||||||
content=f"+ increment_and_continue: Incremented to iteration {self.state.iterations}, looping back",
|
inc_text.append("+ increment_and_continue: ", style="magenta bold")
|
||||||
color="magenta",
|
inc_text.append(
|
||||||
|
f"Incremented to iteration {self.state.iterations}, looping back",
|
||||||
|
style="magenta",
|
||||||
)
|
)
|
||||||
|
self._console.print(inc_text)
|
||||||
return "initialized"
|
return "initialized"
|
||||||
|
|
||||||
@listen(or_("agent_finished", "tool_result_is_final"))
|
@listen(or_("agent_finished", "tool_result_is_final"))
|
||||||
def finalize(self) -> str:
|
def finalize(self) -> str:
|
||||||
"""Finalize execution and emit completion logs."""
|
"""Finalize execution and emit completion logs."""
|
||||||
if self.state.current_answer is None:
|
if self.state.current_answer is None:
|
||||||
self._printer.print(
|
skip_text = Text()
|
||||||
content="⚠️ Finalize called but no answer in state - skipping",
|
skip_text.append("⚠️ ", style="yellow bold")
|
||||||
color="yellow",
|
skip_text.append(
|
||||||
|
"Finalize called but no answer in state - skipping", style="yellow"
|
||||||
)
|
)
|
||||||
|
self._console.print(skip_text)
|
||||||
return "skipped"
|
return "skipped"
|
||||||
|
|
||||||
if not isinstance(self.state.current_answer, AgentFinish):
|
if not isinstance(self.state.current_answer, AgentFinish):
|
||||||
self._printer.print(
|
skip_text = Text()
|
||||||
content=f"⚠️ Finalize called with {type(self.state.current_answer).__name__} instead of AgentFinish - skipping",
|
skip_text.append("⚠️ ", style="yellow bold")
|
||||||
color="yellow",
|
skip_text.append(
|
||||||
|
f"Finalize called with {type(self.state.current_answer).__name__} instead of AgentFinish - skipping",
|
||||||
|
style="yellow",
|
||||||
)
|
)
|
||||||
|
self._console.print(skip_text)
|
||||||
return "skipped"
|
return "skipped"
|
||||||
|
|
||||||
self.state.is_finished = True
|
self.state.is_finished = True
|
||||||
@@ -511,10 +514,13 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
return {"output": formatted_answer.output}
|
return {"output": formatted_answer.output}
|
||||||
|
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
self._printer.print(
|
fail_text = Text()
|
||||||
content="Agent failed to reach a final answer. This is likely a bug - please report it.",
|
fail_text.append("❌ ", style="red bold")
|
||||||
color="red",
|
fail_text.append(
|
||||||
|
"Agent failed to reach a final answer. This is likely a bug - please report it.",
|
||||||
|
style="red",
|
||||||
)
|
)
|
||||||
|
self._console.print(fail_text)
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
handle_unknown_error(self._printer, e)
|
handle_unknown_error(self._printer, e)
|
||||||
@@ -624,10 +630,13 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if train_iteration is None or not isinstance(train_iteration, int):
|
if train_iteration is None or not isinstance(train_iteration, int):
|
||||||
self._printer.print(
|
train_error = Text()
|
||||||
content="Invalid or missing train iteration. Cannot save training data.",
|
train_error.append("❌ ", style="red bold")
|
||||||
color="red",
|
train_error.append(
|
||||||
|
"Invalid or missing train iteration. Cannot save training data.",
|
||||||
|
style="red",
|
||||||
)
|
)
|
||||||
|
self._console.print(train_error)
|
||||||
return
|
return
|
||||||
|
|
||||||
training_handler = CrewTrainingHandler(TRAINING_DATA_FILE)
|
training_handler = CrewTrainingHandler(TRAINING_DATA_FILE)
|
||||||
@@ -647,13 +656,14 @@ class CrewAgentExecutorFlow(Flow[AgentReActState], CrewAgentExecutorMixin):
|
|||||||
if train_iteration in agent_training_data:
|
if train_iteration in agent_training_data:
|
||||||
agent_training_data[train_iteration]["improved_output"] = result.output
|
agent_training_data[train_iteration]["improved_output"] = result.output
|
||||||
else:
|
else:
|
||||||
self._printer.print(
|
train_error = Text()
|
||||||
content=(
|
train_error.append("❌ ", style="red bold")
|
||||||
f"No existing training data for agent {agent_id} and iteration "
|
train_error.append(
|
||||||
f"{train_iteration}. Cannot save improved output."
|
f"No existing training data for agent {agent_id} and iteration "
|
||||||
),
|
f"{train_iteration}. Cannot save improved output.",
|
||||||
color="red",
|
style="red",
|
||||||
)
|
)
|
||||||
|
self._console.print(train_error)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Update the training data and save
|
# Update the training data and save
|
||||||
|
|||||||
Reference in New Issue
Block a user