Updating docs

This commit is contained in:
João Moura
2024-04-04 13:25:04 -03:00
parent fcffc4a898
commit a7f007f475
23 changed files with 337 additions and 360 deletions

View File

@@ -1,78 +1,62 @@
---
title: Creating your own Tools
description: Guide on how to create and use custom tools within the crewAI framework.
title: Creating and Utilizing Tools in crewAI
description: Comprehensive guide on crafting, using, and managing custom tools within the crewAI framework, including new functionalities and error handling.
---
## Creating your own Tools
!!! example "Custom Tool Creation"
Developers can craft custom tools tailored to their agents needs or utilize pre-built options.
## Creating and Utilizing Tools in crewAI
This guide provides detailed instructions on creating custom tools for the crewAI framework and how to efficiently manage and utilize these tools, incorporating the latest functionalities such as tool delegation, error handling, and dynamic tool calling. It also highlights the importance of collaboration tools, enabling agents to perform a wide range of actions.
To create your own crewAI tools, you will need to install our extra tools package:
### Prerequisites
Before creating your own tools, ensure you have the crewAI extra tools package installed:
```bash
pip install 'crewai[tools]'
```
Once installed, there are two primary methods for creating a crewAI tool:
### Subclassing `BaseTool`
To define a custom tool, create a new class that inherits from `BaseTool`. Specify the `name`, `description`, and implement the `_run` method to outline its operational logic.
To create a personalized tool, inherit from `BaseTool` and define the necessary attributes and the `_run` method.
```python
from crewai_tools import BaseTool
class MyCustomTool(BaseTool):
name: str = "Name of my tool"
description: str = "Clear description for what this tool is useful for. Your agent will need this information to utilize it effectively."
description: str = "What this tool does. It's vital for effective utilization."
def _run(self, argument: str) -> str:
# Implementation details go here
return "Result from custom tool"
# Your tool's logic here
return "Tool's result"
```
### Utilizing the `tool` Decorator
### Using the `tool` Decorator
For a more straightforward approach, employ the `tool` decorator to create a `Tool` object directly. This method requires specifying the required attributes and functional logic within a decorated function.
Alternatively, use the `tool` decorator for a direct approach to create tools. This requires specifying attributes and the tool's logic within a function.
```python
from crewai_tools import tool
@tool("Name of my tool")
def my_tool(question: str) -> str:
"""Provide a clear description of what this tool is useful for. Your agent will need this information to use it."""
# Implement function logic here
@tool("Tool Name")
def my_simple_tool(question: str) -> str:
"""Tool description for clarity."""
# Tool logic here
return "Tool output"
```
```python
import json
import requests
from crewai import Agent
from crewai.tools import tool
# Decorate the function with the tool decorator from crewAI
@tool("Integration with a Given API")
def integration_tool(argument: str) -> str:
"""Details the integration process with a given API."""
# Implementation details
return "Results to be sent back to the agent"
```
### Defining a Cache Function for the Tool
By default, all tools have caching enabled, meaning that if a tool is called with the same arguments by any agent in the crew, it will return the same result. However, specific scenarios may require more tailored caching strategies. For these cases, use the `cache_function` attribute to assign a function that determines whether the result should be cached.
To optimize tool performance with caching, define custom caching strategies using the `cache_function` attribute.
```python
@tool("Integration with a Given API")
def integration_tool(argument: str) -> str:
"""Integration with a given API."""
# Implementation details
return "Results to be sent back to the agent"
@tool("Tool with Caching")
def cached_tool(argument: str) -> str:
"""Tool functionality description."""
return "Cachable result"
def cache_strategy(arguments: dict, result: str) -> bool:
if result == "some_value":
return True
return False
def my_cache_strategy(arguments: dict, result: str) -> bool:
# Define custom caching logic
return True if some_condition else False
integration_tool.cache_function = cache_strategy
```
cached_tool.cache_function = my_cache_strategy
```
By adhering to these guidelines and incorporating new functionalities and collaboration tools into your tool creation and management processes, you can leverage the full capabilities of the crewAI framework, enhancing both the development experience and the efficiency of your AI agents.