mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
fix: Add to_structured_tool method to BaseTool
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -6,6 +6,8 @@ from pydantic import BaseModel, ConfigDict, Field, create_model, validator
|
|||||||
from pydantic.fields import FieldInfo
|
from pydantic.fields import FieldInfo
|
||||||
from pydantic import BaseModel as PydanticBaseModel
|
from pydantic import BaseModel as PydanticBaseModel
|
||||||
|
|
||||||
|
from crewai.tools.structured_tool import CrewStructuredTool
|
||||||
|
|
||||||
def _create_model_fields(fields: Dict[str, Tuple[Any, FieldInfo]]) -> Dict[str, Any]:
|
def _create_model_fields(fields: Dict[str, Tuple[Any, FieldInfo]]) -> Dict[str, Any]:
|
||||||
"""Helper function to create model fields with proper type hints."""
|
"""Helper function to create model fields with proper type hints."""
|
||||||
return {name: (annotation, field) for name, (annotation, field) in fields.items()}
|
return {name: (annotation, field) for name, (annotation, field) in fields.items()}
|
||||||
@@ -69,6 +71,17 @@ class BaseTool(BaseModel, ABC):
|
|||||||
) -> Any:
|
) -> Any:
|
||||||
"""Here goes the actual implementation of the tool."""
|
"""Here goes the actual implementation of the tool."""
|
||||||
|
|
||||||
|
def to_structured_tool(self) -> CrewStructuredTool:
|
||||||
|
"""Convert this tool to a CrewStructuredTool instance."""
|
||||||
|
self._set_args_schema()
|
||||||
|
return CrewStructuredTool(
|
||||||
|
name=self.name,
|
||||||
|
description=self.description,
|
||||||
|
args_schema=self.args_schema,
|
||||||
|
func=self._run,
|
||||||
|
result_as_answer=self.result_as_answer,
|
||||||
|
)
|
||||||
|
|
||||||
def _set_args_schema(self) -> None:
|
def _set_args_schema(self) -> None:
|
||||||
if self.args_schema is None:
|
if self.args_schema is None:
|
||||||
class_name = f"{self.__class__.__name__}Schema"
|
class_name = f"{self.__class__.__name__}Schema"
|
||||||
|
|||||||
Reference in New Issue
Block a user