Fix static typing errors (#187)

Co-authored-by: João Moura <joaomdmoura@gmail.com>
This commit is contained in:
Guilherme Vieira
2024-01-29 19:52:14 -03:00
committed by GitHub
parent cd77981102
commit 29c31a2404
18 changed files with 135 additions and 87 deletions

View File

@@ -38,12 +38,10 @@ class Crew(BaseModel):
id: A unique identifier for the crew instance.
"""
__hash__ = object.__hash__
__hash__ = object.__hash__ # type: ignore
_rpm_controller: RPMController = PrivateAttr()
_logger: Logger = PrivateAttr()
_cache_handler: Optional[InstanceOf[CacheHandler]] = PrivateAttr(
default=CacheHandler()
)
_cache_handler: InstanceOf[CacheHandler] = PrivateAttr(default=CacheHandler())
model_config = ConfigDict(arbitrary_types_allowed=True)
tasks: List[Task] = Field(default_factory=list)
agents: List[Agent] = Field(default_factory=list)
@@ -69,20 +67,20 @@ class Crew(BaseModel):
"may_not_set_field", "The 'id' field cannot be set by the user.", {}
)
@classmethod
@field_validator("config", mode="before")
@classmethod
def check_config_type(
cls, v: Union[Json, Dict[str, Any]]
) -> Union[Json, Dict[str, Any]]:
"""Validates that the config is a valid type.
Args:
v: The config to be validated.
Returns:
The config if it is valid.
"""
return json.loads(v) if isinstance(v, Json) else v
# TODO: Improve typing
return json.loads(v) if isinstance(v, Json) else v # type: ignore
@model_validator(mode="after")
def set_private_attrs(self) -> "Crew":
@@ -112,6 +110,8 @@ class Crew(BaseModel):
return self
def _setup_from_config(self):
assert self.config is not None, "Config should not be None."
"""Initializes agents and tasks from the provided config."""
if not self.config.get("agents") or not self.config.get("tasks"):
raise PydanticCustomError(
@@ -143,19 +143,24 @@ class Crew(BaseModel):
if self.process == Process.sequential:
return self._sequential_loop()
else:
raise NotImplementedError(
f"The process '{self.process}' is not implemented yet."
)
def _sequential_loop(self) -> str:
"""Executes tasks sequentially and returns the final output."""
task_output = None
task_output = ""
for task in self.tasks:
self._prepare_and_execute_task(task)
task_output = task.execute(task_output)
self._logger.log(
"debug", f"[{task.agent.role}] Task output: {task_output}\n\n"
)
role = task.agent.role if task.agent is not None else "None"
self._logger.log("debug", f"[{role}] Task output: {task_output}\n\n")
if self.max_rpm:
self._rpm_controller.stop_rpm_counter()
return task_output
def _prepare_and_execute_task(self, task: Task) -> None:
@@ -164,8 +169,9 @@ class Crew(BaseModel):
Args:
task: The task to be executed.
"""
if task.agent.allow_delegation:
if task.agent is not None and task.agent.allow_delegation:
task.tools += AgentTools(agents=self.agents).tools()
self._logger.log("debug", f"Working Agent: {task.agent.role}")
role = task.agent.role if task.agent is not None else "None"
self._logger.log("debug", f"Working Agent: {role}")
self._logger.log("info", f"Starting Task: {task.description}")