Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] StructuredTool - CrewAI #1585

Open
lgesuellip opened this issue Nov 12, 2024 · 0 comments
Open

[FEATURE] StructuredTool - CrewAI #1585

lgesuellip opened this issue Nov 12, 2024 · 0 comments
Labels
feature-request New feature or request

Comments

@lgesuellip
Copy link

Feature Area

Integration with external tools

Is your feature request related to a an existing bug? Please link it here.

NA

Describe the solution you'd like

Hey Team,

At Pampa, we’ve developed a class for Crewai to create a tool that wraps a given function, similar to Langchain’s StructuredTool. This class allows us to encapsulate any function, making it easily accessible to an agent.

Our goal is to use this to wrap the tool definitions for an API. By defining clear input and output schemas, we’re creating a structured and standardized approach for interacting with the API. You can see this in the following example, assuming that we have already created the StructuredTool Class:

def tool_util(client, tool_id):

  # Wrapper function to execute
  def tool_wrapper(*args, **kwargs):
    response = client.tools.execute(
      tool_name=tool_id,
      inputs=kwargs,
    )
    return response

  # Get tool definition
  tool_def = client.tools.get(tool_id=tool_id)
  
  # Extract tool properties
  tool_name = tool_id
  description = tool_def.get("description", "No description available.")
  args_schema = tool_def.get("args_schema", None)
   
  # Create and return the structured tool
  return StructuredTool.from_function(
    func=tool_wrapper,
    name=tool_name,
    description=description,
    args_schema=args_schema
  )

We believe adding this StructuredTool class to the Crewai tool repository would be beneficial for users.
Let us know what you think, and We’d be happy to discuss further or contribute with the integration.

Describe alternatives you've considered

No response

Additional context

Here’s an example of the possible implementation of the class:

from abc import ABC
from typing import Any, Callable, Optional, Type, Union
from pydantic import BaseModel, ConfigDict, Field
from crewai_tools.tools.base_tool import BaseTool

class StructuredTool(BaseTool):
    """Tool that can operate on any number of inputs."""

    func: Optional[Callable[..., Any]] = None
    """The function to run when the tool is called."""

    def __init__(self, *args: Any, func, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.func = func

    def _run(self, *args: Any, **kwargs: Any) -> Any:
        """Use the tool."""
        if self.func:
            return self.func(*args, **kwargs)
        msg = "StructuredTool does not support invocation without a function."
        raise NotImplementedError(msg)

    @classmethod
    def from_function(
        cls,
        func: Callable,
        name: Optional[str] = None,
        description: Optional[str] = None,
        args_schema: Optional[Type[BaseModel]] = None,
        **kwargs: Any,
    ) -> "StructuredTool":
        """Create tool from a function.

        Args:
            func: The function to create a tool from
            name: The name of the tool. Defaults to the function name
            description: The description of the tool. Defaults to the function docstring
            args_schema: The schema for the function arguments
            **kwargs: Additional arguments to pass to the tool

        Returns:
            The tool

        Raises:
            ValueError: If the function has no docstring and no description was provided
        """
        return cls(
            name=name,
            func=func,
            args_schema=args_schema,  # type: ignore[arg-type]
            description=description,
            **kwargs,
        )

Willingness to Contribute

Yes, I'd be happy to submit a pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant