Spaces:
Sleeping
Sleeping
Update crewai/crew.py
Browse files- crewai/crew.py +19 -70
crewai/crew.py
CHANGED
|
@@ -26,11 +26,9 @@ from crewai.utilities import I18N, Logger, RPMController
|
|
| 26 |
class Crew(BaseModel):
|
| 27 |
"""
|
| 28 |
Represents a group of agents, defining how they should collaborate and the tasks they should perform.
|
| 29 |
-
|
| 30 |
Attributes:
|
| 31 |
tasks: List of tasks assigned to the crew.
|
| 32 |
agents: List of agents part of this crew.
|
| 33 |
-
manager_llm: The language model that will run manager agent.
|
| 34 |
process: The process flow that the crew will follow (e.g., sequential).
|
| 35 |
verbose: Indicates the verbosity level for logging during execution.
|
| 36 |
config: Configuration settings for the crew.
|
|
@@ -48,9 +46,6 @@ class Crew(BaseModel):
|
|
| 48 |
agents: List[Agent] = Field(default_factory=list)
|
| 49 |
process: Process = Field(default=Process.sequential)
|
| 50 |
verbose: Union[int, bool] = Field(default=0)
|
| 51 |
-
manager_llm: Optional[Any] = Field(
|
| 52 |
-
description="Language model that will run the agent.", default=None
|
| 53 |
-
)
|
| 54 |
config: Optional[Union[Json, Dict[str, Any]]] = Field(default=None)
|
| 55 |
id: UUID4 = Field(default_factory=uuid.uuid4, frozen=True)
|
| 56 |
max_rpm: Optional[int] = Field(
|
|
@@ -94,17 +89,6 @@ class Crew(BaseModel):
|
|
| 94 |
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
|
| 95 |
return self
|
| 96 |
|
| 97 |
-
@model_validator(mode="after")
|
| 98 |
-
def check_manager_llm(self):
|
| 99 |
-
"""Validates that the language model is set when using hierarchical process."""
|
| 100 |
-
if self.process == Process.hierarchical and not self.manager_llm:
|
| 101 |
-
raise PydanticCustomError(
|
| 102 |
-
"missing_manager_llm",
|
| 103 |
-
"Attribute `manager_llm` is required when using hierarchical process.",
|
| 104 |
-
{},
|
| 105 |
-
)
|
| 106 |
-
return self
|
| 107 |
-
|
| 108 |
@model_validator(mode="after")
|
| 109 |
def check_config(self):
|
| 110 |
"""Validates that the crew is properly configured with agents and tasks."""
|
|
@@ -121,8 +105,7 @@ class Crew(BaseModel):
|
|
| 121 |
if self.agents:
|
| 122 |
for agent in self.agents:
|
| 123 |
agent.set_cache_handler(self._cache_handler)
|
| 124 |
-
|
| 125 |
-
agent.set_rpm_controller(self._rpm_controller)
|
| 126 |
return self
|
| 127 |
|
| 128 |
def _setup_from_config(self):
|
|
@@ -139,10 +122,8 @@ class Crew(BaseModel):
|
|
| 139 |
|
| 140 |
def _create_task(self, task_config: Dict[str, Any]) -> Task:
|
| 141 |
"""Creates a task instance from its configuration.
|
| 142 |
-
|
| 143 |
Args:
|
| 144 |
task_config: The configuration of the task.
|
| 145 |
-
|
| 146 |
Returns:
|
| 147 |
A task instance.
|
| 148 |
"""
|
|
@@ -158,31 +139,18 @@ class Crew(BaseModel):
|
|
| 158 |
agent.i18n = I18N(language=self.language)
|
| 159 |
|
| 160 |
if self.process == Process.sequential:
|
| 161 |
-
return self.
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
f"The process '{self.process}' is not implemented yet."
|
| 167 |
-
)
|
| 168 |
|
| 169 |
-
def
|
| 170 |
"""Executes tasks sequentially and returns the final output."""
|
| 171 |
task_output = ""
|
| 172 |
for task in self.tasks:
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
agent for agent in self.agents if agent != task.agent
|
| 176 |
-
]
|
| 177 |
-
task.tools += AgentTools(agents=agents_for_delegation).tools()
|
| 178 |
-
|
| 179 |
-
role = task.agent.role if task.agent is not None else "None"
|
| 180 |
-
self._logger.log("debug", f"Working Agent: {role}")
|
| 181 |
-
self._logger.log("info", f"Starting Task: {task.description}")
|
| 182 |
-
|
| 183 |
-
output = task.execute(context=task_output)
|
| 184 |
-
if not task.async_execution:
|
| 185 |
-
task_output = output
|
| 186 |
|
| 187 |
role = task.agent.role if task.agent is not None else "None"
|
| 188 |
self._logger.log("debug", f"[{role}] Task output: {task_output}\n\n")
|
|
@@ -192,33 +160,14 @@ class Crew(BaseModel):
|
|
| 192 |
|
| 193 |
return task_output
|
| 194 |
|
| 195 |
-
def
|
| 196 |
-
"""
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
backstory=i18n.retrieve("hierarchical_manager_agent", "backstory"),
|
| 203 |
-
tools=AgentTools(agents=self.agents).tools(),
|
| 204 |
-
llm=self.manager_llm,
|
| 205 |
-
verbose=True,
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
task_output = ""
|
| 209 |
-
for task in self.tasks:
|
| 210 |
-
self._logger.log("debug", f"Working Agent: {manager.role}")
|
| 211 |
-
self._logger.log("info", f"Starting Task: {task.description}")
|
| 212 |
-
|
| 213 |
-
task_output = task.execute(
|
| 214 |
-
agent=manager, context=task_output, tools=manager.tools
|
| 215 |
-
)
|
| 216 |
-
|
| 217 |
-
self._logger.log(
|
| 218 |
-
"debug", f"[{manager.role}] Task output: {task_output}\n\n"
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
if self.max_rpm:
|
| 222 |
-
self._rpm_controller.stop_rpm_counter()
|
| 223 |
|
| 224 |
-
|
|
|
|
|
|
|
|
|
| 26 |
class Crew(BaseModel):
|
| 27 |
"""
|
| 28 |
Represents a group of agents, defining how they should collaborate and the tasks they should perform.
|
|
|
|
| 29 |
Attributes:
|
| 30 |
tasks: List of tasks assigned to the crew.
|
| 31 |
agents: List of agents part of this crew.
|
|
|
|
| 32 |
process: The process flow that the crew will follow (e.g., sequential).
|
| 33 |
verbose: Indicates the verbosity level for logging during execution.
|
| 34 |
config: Configuration settings for the crew.
|
|
|
|
| 46 |
agents: List[Agent] = Field(default_factory=list)
|
| 47 |
process: Process = Field(default=Process.sequential)
|
| 48 |
verbose: Union[int, bool] = Field(default=0)
|
|
|
|
|
|
|
|
|
|
| 49 |
config: Optional[Union[Json, Dict[str, Any]]] = Field(default=None)
|
| 50 |
id: UUID4 = Field(default_factory=uuid.uuid4, frozen=True)
|
| 51 |
max_rpm: Optional[int] = Field(
|
|
|
|
| 89 |
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
|
| 90 |
return self
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
@model_validator(mode="after")
|
| 93 |
def check_config(self):
|
| 94 |
"""Validates that the crew is properly configured with agents and tasks."""
|
|
|
|
| 105 |
if self.agents:
|
| 106 |
for agent in self.agents:
|
| 107 |
agent.set_cache_handler(self._cache_handler)
|
| 108 |
+
agent.set_rpm_controller(self._rpm_controller)
|
|
|
|
| 109 |
return self
|
| 110 |
|
| 111 |
def _setup_from_config(self):
|
|
|
|
| 122 |
|
| 123 |
def _create_task(self, task_config: Dict[str, Any]) -> Task:
|
| 124 |
"""Creates a task instance from its configuration.
|
|
|
|
| 125 |
Args:
|
| 126 |
task_config: The configuration of the task.
|
|
|
|
| 127 |
Returns:
|
| 128 |
A task instance.
|
| 129 |
"""
|
|
|
|
| 139 |
agent.i18n = I18N(language=self.language)
|
| 140 |
|
| 141 |
if self.process == Process.sequential:
|
| 142 |
+
return self._sequential_loop()
|
| 143 |
+
else:
|
| 144 |
+
raise NotImplementedError(
|
| 145 |
+
f"The process '{self.process}' is not implemented yet."
|
| 146 |
+
)
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
def _sequential_loop(self) -> str:
|
| 149 |
"""Executes tasks sequentially and returns the final output."""
|
| 150 |
task_output = ""
|
| 151 |
for task in self.tasks:
|
| 152 |
+
self._prepare_and_execute_task(task)
|
| 153 |
+
task_output = task.execute(task_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
role = task.agent.role if task.agent is not None else "None"
|
| 156 |
self._logger.log("debug", f"[{role}] Task output: {task_output}\n\n")
|
|
|
|
| 160 |
|
| 161 |
return task_output
|
| 162 |
|
| 163 |
+
def _prepare_and_execute_task(self, task: Task) -> None:
|
| 164 |
+
"""Prepares and logs information about the task being executed.
|
| 165 |
+
Args:
|
| 166 |
+
task: The task to be executed.
|
| 167 |
+
"""
|
| 168 |
+
if task.agent is not None and task.agent.allow_delegation:
|
| 169 |
+
task.tools += AgentTools(agents=self.agents).tools()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
role = task.agent.role if task.agent is not None else "None"
|
| 172 |
+
self._logger.log("debug", f"Working Agent: {role}")
|
| 173 |
+
self._logger.log("info", f"Starting Task: {task.description}")
|