mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 16:48:30 +00:00
60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
from typing import Any, Callable
|
|
|
|
from pydantic import BaseModel as PydanticBaseModel
|
|
|
|
from crewai.tools.base_tool import BaseTool
|
|
from crewai.tools.structured_tool import CrewStructuredTool
|
|
|
|
|
|
class Tool(BaseTool):
|
|
func: Callable
|
|
"""The function that will be executed when the tool is called."""
|
|
|
|
def _run(self, *args: Any, **kwargs: Any) -> Any:
|
|
return self.func(*args, **kwargs)
|
|
|
|
|
|
def to_langchain(
|
|
tools: list[BaseTool | CrewStructuredTool],
|
|
) -> list[CrewStructuredTool]:
|
|
return [t.to_structured_tool() if isinstance(t, BaseTool) else t for t in tools]
|
|
|
|
|
|
def tool(*args):
|
|
"""
|
|
Decorator to create a tool from a function.
|
|
"""
|
|
|
|
def _make_with_name(tool_name: str) -> Callable:
|
|
def _make_tool(f: Callable) -> BaseTool:
|
|
if f.__doc__ is None:
|
|
raise ValueError("Function must have a docstring")
|
|
if f.__annotations__ is None:
|
|
raise ValueError("Function must have type annotations")
|
|
|
|
class_name = "".join(tool_name.split()).title()
|
|
args_schema = type(
|
|
class_name,
|
|
(PydanticBaseModel,),
|
|
{
|
|
"__annotations__": {
|
|
k: v for k, v in f.__annotations__.items() if k != "return"
|
|
},
|
|
},
|
|
)
|
|
|
|
return Tool(
|
|
name=tool_name,
|
|
description=f.__doc__,
|
|
func=f,
|
|
args_schema=args_schema,
|
|
)
|
|
|
|
return _make_tool
|
|
|
|
if len(args) == 1 and callable(args[0]):
|
|
return _make_with_name(args[0].__name__)(args[0])
|
|
if len(args) == 1 and isinstance(args[0], str):
|
|
return _make_with_name(args[0])
|
|
raise ValueError("Invalid arguments")
|