11import uuid
22from abc import ABC , abstractmethod
3+ from collections .abc import Callable
34from copy import copy as shallow_copy
45from hashlib import md5
5- from typing import Any , Callable , Dict , List , Optional , TypeVar
6+ from typing import Any , TypeVar
67
78from pydantic import (
89 UUID4 ,
2526from crewai .tools .base_tool import BaseTool , Tool
2627from crewai .utilities import I18N , Logger , RPMController
2728from crewai .utilities .config import process_config
28- from crewai .utilities .converter import Converter
2929from crewai .utilities .string_utils import interpolate_only
3030
3131T = TypeVar ("T" , bound = "BaseAgent" )
@@ -81,17 +81,17 @@ class BaseAgent(ABC, BaseModel):
8181
8282 __hash__ = object .__hash__ # type: ignore
8383 _logger : Logger = PrivateAttr (default_factory = lambda : Logger (verbose = False ))
84- _rpm_controller : Optional [ RPMController ] = PrivateAttr (default = None )
84+ _rpm_controller : RPMController | None = PrivateAttr (default = None )
8585 _request_within_rpm_limit : Any = PrivateAttr (default = None )
86- _original_role : Optional [ str ] = PrivateAttr (default = None )
87- _original_goal : Optional [ str ] = PrivateAttr (default = None )
88- _original_backstory : Optional [ str ] = PrivateAttr (default = None )
86+ _original_role : str | None = PrivateAttr (default = None )
87+ _original_goal : str | None = PrivateAttr (default = None )
88+ _original_backstory : str | None = PrivateAttr (default = None )
8989 _token_process : TokenProcess = PrivateAttr (default_factory = TokenProcess )
9090 id : UUID4 = Field (default_factory = uuid .uuid4 , frozen = True )
9191 role : str = Field (description = "Role of the agent" )
9292 goal : str = Field (description = "Objective of the agent" )
9393 backstory : str = Field (description = "Backstory of the agent" )
94- config : Optional [ Dict [ str , Any ]] = Field (
94+ config : dict [ str , Any ] | None = Field (
9595 description = "Configuration for the agent" , default = None , exclude = True
9696 )
9797 cache : bool = Field (
@@ -100,15 +100,15 @@ class BaseAgent(ABC, BaseModel):
100100 verbose : bool = Field (
101101 default = False , description = "Verbose mode for the Agent Execution"
102102 )
103- max_rpm : Optional [ int ] = Field (
103+ max_rpm : int | None = Field (
104104 default = None ,
105105 description = "Maximum number of requests per minute for the agent execution to be respected." ,
106106 )
107107 allow_delegation : bool = Field (
108108 default = False ,
109109 description = "Enable agent to delegate and ask questions among each other." ,
110110 )
111- tools : Optional [ List [ BaseTool ]] = Field (
111+ tools : list [ BaseTool ] | None = Field (
112112 default_factory = list , description = "Tools at agents' disposal"
113113 )
114114 max_iter : int = Field (
@@ -122,41 +122,41 @@ class BaseAgent(ABC, BaseModel):
122122 )
123123 crew : Any = Field (default = None , description = "Crew to which the agent belongs." )
124124 i18n : I18N = Field (default = I18N (), description = "Internationalization settings." )
125- cache_handler : Optional [ InstanceOf [CacheHandler ]] = Field (
125+ cache_handler : InstanceOf [CacheHandler ] | None = Field (
126126 default = None , description = "An instance of the CacheHandler class."
127127 )
128128 tools_handler : InstanceOf [ToolsHandler ] = Field (
129129 default_factory = ToolsHandler ,
130130 description = "An instance of the ToolsHandler class." ,
131131 )
132- tools_results : List [ Dict [str , Any ]] = Field (
132+ tools_results : list [ dict [str , Any ]] = Field (
133133 default = [], description = "Results of the tools used by the agent."
134134 )
135- max_tokens : Optional [ int ] = Field (
135+ max_tokens : int | None = Field (
136136 default = None , description = "Maximum number of tokens for the agent's execution."
137137 )
138- knowledge : Optional [ Knowledge ] = Field (
138+ knowledge : Knowledge | None = Field (
139139 default = None , description = "Knowledge for the agent."
140140 )
141- knowledge_sources : Optional [ List [ BaseKnowledgeSource ]] = Field (
141+ knowledge_sources : list [ BaseKnowledgeSource ] | None = Field (
142142 default = None ,
143143 description = "Knowledge sources for the agent." ,
144144 )
145- knowledge_storage : Optional [ Any ] = Field (
145+ knowledge_storage : Any | None = Field (
146146 default = None ,
147147 description = "Custom knowledge storage for the agent." ,
148148 )
149149 security_config : SecurityConfig = Field (
150150 default_factory = SecurityConfig ,
151151 description = "Security configuration for the agent, including fingerprinting." ,
152152 )
153- callbacks : List [Callable ] = Field (
153+ callbacks : list [Callable ] = Field (
154154 default = [], description = "Callbacks to be used for the agent"
155155 )
156156 adapted_agent : bool = Field (
157157 default = False , description = "Whether the agent is adapted"
158158 )
159- knowledge_config : Optional [ KnowledgeConfig ] = Field (
159+ knowledge_config : KnowledgeConfig | None = Field (
160160 default = None ,
161161 description = "Knowledge configuration for the agent such as limits and threshold" ,
162162 )
@@ -168,7 +168,7 @@ def process_model_config(cls, values):
168168
169169 @field_validator ("tools" )
170170 @classmethod
171- def validate_tools (cls , tools : List [Any ]) -> List [BaseTool ]:
171+ def validate_tools (cls , tools : list [Any ]) -> list [BaseTool ]:
172172 """Validate and process the tools provided to the agent.
173173
174174 This method ensures that each tool is either an instance of BaseTool
@@ -221,7 +221,7 @@ def validate_and_set_attributes(self):
221221
222222 @field_validator ("id" , mode = "before" )
223223 @classmethod
224- def _deny_user_set_id (cls , v : Optional [ UUID4 ] ) -> None :
224+ def _deny_user_set_id (cls , v : UUID4 | None ) -> None :
225225 if v :
226226 raise PydanticCustomError (
227227 "may_not_set_field" , "This field is not to be set by the user." , {}
@@ -252,8 +252,8 @@ def key(self):
252252 def execute_task (
253253 self ,
254254 task : Any ,
255- context : Optional [ str ] = None ,
256- tools : Optional [ List [ BaseTool ]] = None ,
255+ context : str | None = None ,
256+ tools : list [ BaseTool ] | None = None ,
257257 ) -> str :
258258 pass
259259
@@ -262,9 +262,8 @@ def create_agent_executor(self, tools=None) -> None:
262262 pass
263263
264264 @abstractmethod
265- def get_delegation_tools (self , agents : List ["BaseAgent" ]) -> List [BaseTool ]:
265+ def get_delegation_tools (self , agents : list ["BaseAgent" ]) -> list [BaseTool ]:
266266 """Set the task tools that init BaseAgenTools class."""
267- pass
268267
269268 def copy (self : T ) -> T : # type: ignore # Signature of "copy" incompatible with supertype "BaseModel"
270269 """Create a deep copy of the Agent."""
@@ -309,7 +308,7 @@ def copy(self: T) -> T: # type: ignore # Signature of "copy" incompatible with
309308
310309 copied_data = self .model_dump (exclude = exclude )
311310 copied_data = {k : v for k , v in copied_data .items () if v is not None }
312- copied_agent = type (self )(
311+ return type (self )(
313312 ** copied_data ,
314313 llm = existing_llm ,
315314 tools = self .tools ,
@@ -318,9 +317,7 @@ def copy(self: T) -> T: # type: ignore # Signature of "copy" incompatible with
318317 knowledge_storage = copied_knowledge_storage ,
319318 )
320319
321- return copied_agent
322-
323- def interpolate_inputs (self , inputs : Dict [str , Any ]) -> None :
320+ def interpolate_inputs (self , inputs : dict [str , Any ]) -> None :
324321 """Interpolate inputs into the agent description and backstory."""
325322 if self ._original_role is None :
326323 self ._original_role = self .role
@@ -362,5 +359,5 @@ def set_rpm_controller(self, rpm_controller: RPMController) -> None:
362359 self ._rpm_controller = rpm_controller
363360 self .create_agent_executor ()
364361
365- def set_knowledge (self , crew_embedder : Optional [ Dict [ str , Any ]] = None ):
362+ def set_knowledge (self , crew_embedder : dict [ str , Any ] | None = None ):
366363 pass
0 commit comments