From 10639d877514e50008c9474339e8bd681e7de4bf Mon Sep 17 00:00:00 2001 From: Piotr Mardziel Date: Tue, 5 Nov 2024 16:22:35 -0800 Subject: [PATCH] Update base_tool.py --- src/crewai_tools/tools/base_tool.py | 95 +---------------------------- 1 file changed, 1 insertion(+), 94 deletions(-) diff --git a/src/crewai_tools/tools/base_tool.py b/src/crewai_tools/tools/base_tool.py index dee3b9317..60c477c41 100644 --- a/src/crewai_tools/tools/base_tool.py +++ b/src/crewai_tools/tools/base_tool.py @@ -5,100 +5,7 @@ from langchain_core.tools import StructuredTool from pydantic import BaseModel, ConfigDict, Field, validator from pydantic import BaseModel as PydanticBaseModel - -class BaseTool(BaseModel, ABC): - class _ArgsSchemaPlaceholder(PydanticBaseModel): - pass - - model_config = ConfigDict() - - name: str - """The unique name of the tool that clearly communicates its purpose.""" - description: str - """Used to tell the model how/when/why to use the tool.""" - args_schema: Type[PydanticBaseModel] = Field(default_factory=_ArgsSchemaPlaceholder) - """The schema for the arguments that the tool accepts.""" - description_updated: bool = False - """Flag to check if the description has been updated.""" - cache_function: Optional[Callable] = lambda _args, _result: True - """Function that will be used to determine if the tool should be cached, should return a boolean. If None, the tool will be cached.""" - result_as_answer: bool = False - """Flag to check if the tool should be the final agent answer.""" - - @validator("args_schema", always=True, pre=True) - def _default_args_schema( - cls, v: Type[PydanticBaseModel] - ) -> Type[PydanticBaseModel]: - if not isinstance(v, cls._ArgsSchemaPlaceholder): - return v - - return type( - f"{cls.__name__}Schema", - (PydanticBaseModel,), - { - "__annotations__": { - k: v for k, v in cls._run.__annotations__.items() if k != "return" - }, - }, - ) - - def model_post_init(self, __context: Any) -> None: - self._generate_description() - - super().model_post_init(__context) - - def run( - self, - *args: Any, - **kwargs: Any, - ) -> Any: - print(f"Using Tool: {self.name}") - return self._run(*args, **kwargs) - - @abstractmethod - def _run( - self, - *args: Any, - **kwargs: Any, - ) -> Any: - """Here goes the actual implementation of the tool.""" - - def to_langchain(self) -> StructuredTool: - self._set_args_schema() - return StructuredTool( - name=self.name, - description=self.description, - args_schema=self.args_schema, - func=self._run, - ) - - def _set_args_schema(self): - if self.args_schema is None: - class_name = f"{self.__class__.__name__}Schema" - self.args_schema = type( - class_name, - (PydanticBaseModel,), - { - "__annotations__": { - k: v - for k, v in self._run.__annotations__.items() - if k != "return" - }, - }, - ) - - def _generate_description(self): - args = [] - args_description = [] - for arg, attribute in self.args_schema.schema()["properties"].items(): - if "type" in attribute: - args.append(f"{arg}: '{attribute['type']}'") - if "description" in attribute: - args_description.append(f"{arg}: '{attribute['description']}'") - - description = self.description.replace("\n", " ") - self.description = f"{self.name}({', '.join(args)}) - {description} {', '.join(args_description)}" - +from crewai.tools.base_tool import BaseTool class Tool(BaseTool): func: Callable