Converts a given CrewAI tool into a general Tool
format.
This method ensures that the provided tool is a valid CrewAITool
, sanitizes the tool's name, processes its description, and prepares a function to interact with the tool's arguments. It then returns a standardized Tool
object.
PARAMETER | DESCRIPTION |
tool | The tool to convert, expected to be an instance of CrewAITool . TYPE: Any |
**kwargs | Additional arguments, which are not supported by this method. TYPE: Any DEFAULT: {} |
RETURNS | DESCRIPTION |
Tool | A standardized Tool object converted from the CrewAI tool. TYPE: Tool |
RAISES | DESCRIPTION |
ValueError | If the provided tool is not an instance of CrewAITool , or if any additional arguments are passed. |
Source code in autogen/interop/crewai/crewai.py
| @classmethod
@require_optional_import("crewai", "interop-crewai")
def convert_tool(cls, tool: Any, **kwargs: Any) -> Tool:
"""Converts a given CrewAI tool into a general `Tool` format.
This method ensures that the provided tool is a valid `CrewAITool`, sanitizes
the tool's name, processes its description, and prepares a function to interact
with the tool's arguments. It then returns a standardized `Tool` object.
Args:
tool (Any): The tool to convert, expected to be an instance of `CrewAITool`.
**kwargs (Any): Additional arguments, which are not supported by this method.
Returns:
Tool: A standardized `Tool` object converted from the CrewAI tool.
Raises:
ValueError: If the provided tool is not an instance of `CrewAITool`, or if
any additional arguments are passed.
"""
if not isinstance(tool, CrewAITool):
raise ValueError(f"Expected an instance of `crewai.tools.BaseTool`, got {type(tool)}")
if kwargs:
raise ValueError(f"The CrewAIInteroperability does not support any additional arguments, got {kwargs}")
# needed for type checking
crewai_tool: CrewAITool = tool # type: ignore[no-any-unimported]
name = _sanitize_name(crewai_tool.name)
description = (
crewai_tool.description.split("Tool Description: ")[-1]
+ " (IMPORTANT: When using arguments, put them all in an `args` dictionary)"
)
def func(args: crewai_tool.args_schema) -> Any: # type: ignore[no-any-unimported]
return crewai_tool.run(**args.model_dump())
return Tool(
name=name,
description=description,
func_or_tool=func,
)
|