From ef9f85f5d2114138974fe2b5ee0df0f4e7e1590c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Tue, 5 Mar 2024 01:26:07 -0300 Subject: [PATCH] adding support for agents and tasks to be defined of configs --- src/crewai/agent.py | 17 +++++++++++++++++ src/crewai/task.py | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/crewai/agent.py b/src/crewai/agent.py index ce1c80cee..ecf18a42d 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -36,6 +36,7 @@ class Agent(BaseModel): role: The role of the agent. goal: The objective of the agent. backstory: The backstory of the agent. + config: Dict representation of agent configuration. llm: The language model that will run the agent. function_calling_llm: The language model that will the tool calling for this agent, it overrides the crew function_calling_llm. max_iter: Maximum number of iterations for an agent to execute a task. @@ -63,6 +64,10 @@ class Agent(BaseModel): role: str = Field(description="Role of the agent") goal: str = Field(description="Objective of the agent") backstory: str = Field(description="Backstory of the agent") + config: Optional[Dict[str, Any]] = Field( + description="Configuration for the agent", + default=None, + ) max_rpm: Optional[int] = Field( default=None, description="Maximum number of requests per minute for the agent execution to be respected.", @@ -106,6 +111,10 @@ class Agent(BaseModel): description="Language model that will run the agent.", default=None ) + def __init__(__pydantic_self__, **data): + config = data.pop("config", {}) + super().__init__(**config, **data) + @field_validator("id", mode="before") @classmethod def _deny_user_set_id(cls, v: Optional[UUID4]) -> None: @@ -114,6 +123,14 @@ class Agent(BaseModel): "may_not_set_field", "This field is not to be set by the user.", {} ) + @model_validator(mode="after") + def set_attributes_based_on_config(self) -> "Agent": + """Set attributes based on the agent configuration.""" + if self.config: + for key, value in self.config.items(): + setattr(self, key, value) + return self + @model_validator(mode="after") def set_private_attrs(self): """Set private attributes.""" diff --git a/src/crewai/task.py b/src/crewai/task.py index 2d9182d62..3b1fd1a13 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -28,6 +28,10 @@ class Task(BaseModel): expected_output: str = Field( description="Clear definition of expected output for the task." ) + config: Optional[Dict[str, Any]] = Field( + description="Configuration for the agent", + default=None, + ) callback: Optional[Any] = Field( description="Callback to be executed after the task is completed.", default=None ) @@ -67,6 +71,10 @@ class Task(BaseModel): description="Unique identifier for the object, not set by user.", ) + def __init__(__pydantic_self__, **data): + config = data.pop("config", {}) + super().__init__(**config, **data) + @field_validator("id", mode="before") @classmethod def _deny_user_set_id(cls, v: Optional[UUID4]) -> None: @@ -75,6 +83,14 @@ class Task(BaseModel): "may_not_set_field", "This field is not to be set by the user.", {} ) + @model_validator(mode="after") + def set_attributes_based_on_config(self) -> "Task": + """Set attributes based on the agent configuration.""" + if self.config: + for key, value in self.config.items(): + setattr(self, key, value) + return self + @model_validator(mode="after") def check_tools(self): """Check if the tools are set."""