docs: Improve "Creating and Utilizing Tools in crewAI" documentation

This commit is contained in:
Rip&Tear (aider)
2024-09-04 18:31:09 +08:00
parent 18a2722e4d
commit b955416458

View File

@@ -1,12 +1,26 @@
---
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.
description: A comprehensive guide on crafting, using, and managing custom tools within the crewAI framework, including advanced functionalities and best practices.
---
## 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.
# Creating and Utilizing Tools in crewAI
### Prerequisites
This guide provides detailed instructions on creating custom tools for the crewAI framework, efficiently managing and utilizing these tools, and incorporating advanced functionalities. By mastering tool creation and management, you can significantly enhance the capabilities of your AI agents and improve overall system performance.
## Table of Contents
1. [Prerequisites](#prerequisites)
2. [Creating Custom Tools](#creating-custom-tools)
- [Subclassing `BaseTool`](#subclassing-basetool)
- [Using the `tool` Decorator](#using-the-tool-decorator)
3. [Advanced Tool Features](#advanced-tool-features)
- [Implementing Caching](#implementing-caching)
- [Error Handling](#error-handling)
- [Dynamic Tool Calling](#dynamic-tool-calling)
4. [Best Practices](#best-practices)
5. [Conclusion](#conclusion)
## Prerequisites
Before creating your own tools, ensure you have the crewAI extra tools package installed:
@@ -14,51 +28,126 @@ Before creating your own tools, ensure you have the crewAI extra tools package i
pip install 'crewai[tools]'
```
## Creating Custom Tools
### Subclassing `BaseTool`
To create a personalized tool, inherit from `BaseTool` and define the necessary attributes and the `_run` method.
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 = "What this tool does. It's vital for effective utilization."
name: str = "My Custom Tool"
description: str = "This tool performs a specific task. Provide a clear description for effective utilization."
def _run(self, argument: str) -> str:
# Your tool's logic here
return "Tool's result"
# Implement your tool's logic here
result = f"Processed: {argument}"
return result
```
### Using the `tool` Decorator
Alternatively, you can use the tool decorator `@tool`. This approach allows you to define the tool's attributes and functionality directly within a function, offering a concise and efficient way to create specialized tools tailored to your needs.
For simpler tools, use the `@tool` decorator to define the tool's attributes and functionality directly within a function:
```python
from crewai_tools import tool
@tool("Tool Name")
def my_simple_tool(question: str) -> str:
"""Tool description for clarity."""
# Tool logic here
return "Tool output"
@tool("Simple Calculator")
def simple_calculator(operation: str, a: float, b: float) -> float:
"""Performs basic arithmetic operations (add, subtract, multiply, divide)."""
if operation == "add":
return a + b
elif operation == "subtract":
return a - b
elif operation == "multiply":
return a * b
elif operation == "divide":
return a / b if b != 0 else "Error: Division by zero"
else:
return "Error: Invalid operation"
```
### Defining a Cache Function for the Tool
## Advanced Tool Features
To optimize tool performance with caching, define custom caching strategies using the `cache_function` attribute.
### Implementing Caching
Optimize tool performance with custom caching strategies:
```python
@tool("Tool with Caching")
def cached_tool(argument: str) -> str:
"""Tool functionality description."""
return "Cacheable result"
from crewai_tools import tool
def my_cache_strategy(arguments: dict, result: str) -> bool:
# Define custom caching logic
return True if some_condition else False
@tool("Cacheable Tool")
def cacheable_tool(input_data: str) -> str:
"""Tool that benefits from caching results."""
# Simulate complex processing
import time
time.sleep(2)
return f"Processed: {input_data}"
cached_tool.cache_function = my_cache_strategy
def cache_strategy(arguments: dict, result: str) -> bool:
# Implement your caching logic here
return len(arguments['input_data']) > 10 # Cache results for longer inputs
cacheable_tool.cache_function = 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.
### Error Handling
Implement robust error handling in your tools:
```python
from crewai_tools import BaseTool
class RobustTool(BaseTool):
name: str = "Robust Processing Tool"
description: str = "Processes data with error handling."
def _run(self, data: str) -> str:
try:
# Your processing logic here
result = self._process_data(data)
return result
except ValueError as e:
return f"Error: Invalid input - {str(e)}"
except Exception as e:
return f"Unexpected error occurred: {str(e)}"
def _process_data(self, data: str) -> str:
# Implement your actual data processing logic here
if not data:
raise ValueError("Empty input")
return f"Successfully processed: {data}"
```
### Dynamic Tool Calling
Implement dynamic tool selection based on input:
```python
from crewai_tools import tool
@tool("Dynamic Tool Selector")
def dynamic_tool_selector(task_description: str) -> str:
"""Selects and calls the appropriate tool based on the task description."""
if "calculate" in task_description.lower():
return simple_calculator("add", 5, 3) # Example usage
elif "process" in task_description.lower():
return cacheable_tool("Sample input")
else:
return "No suitable tool found for the given task."
```
## Best Practices
1. **Clear Documentation**: Provide detailed descriptions for each tool, including expected inputs and outputs.
2. **Modular Design**: Create small, focused tools that can be combined for complex tasks.
3. **Consistent Naming**: Use clear, descriptive names for your tools and their parameters.
4. **Error Handling**: Implement comprehensive error handling to make your tools robust.
5. **Performance Optimization**: Use caching for expensive operations when appropriate.
6. **Testing**: Thoroughly test your tools with various inputs to ensure reliability.
## Conclusion
By adhering to these guidelines and incorporating advanced functionalities into your tool creation and management processes, you can leverage the full capabilities of the crewAI framework. This approach enhances both the development experience and the efficiency of your AI agents, allowing for more sophisticated and powerful AI-driven applications.