mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
docs: add docs about asynchronous tool support
This commit is contained in:
@@ -32,6 +32,7 @@ The Enterprise Tools Repository includes:
|
|||||||
- **Customizability**: Provides the flexibility to develop custom tools or utilize existing ones, catering to the specific needs of agents.
|
- **Customizability**: Provides the flexibility to develop custom tools or utilize existing ones, catering to the specific needs of agents.
|
||||||
- **Error Handling**: Incorporates robust error handling mechanisms to ensure smooth operation.
|
- **Error Handling**: Incorporates robust error handling mechanisms to ensure smooth operation.
|
||||||
- **Caching Mechanism**: Features intelligent caching to optimize performance and reduce redundant operations.
|
- **Caching Mechanism**: Features intelligent caching to optimize performance and reduce redundant operations.
|
||||||
|
- **Asynchronous Support**: Handles both synchronous and asynchronous tools, enabling non-blocking operations.
|
||||||
|
|
||||||
## Using CrewAI Tools
|
## Using CrewAI Tools
|
||||||
|
|
||||||
@@ -177,6 +178,62 @@ class MyCustomTool(BaseTool):
|
|||||||
return "Tool's result"
|
return "Tool's result"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Asynchronous Tool Support
|
||||||
|
|
||||||
|
CrewAI supports asynchronous tools, allowing you to implement tools that perform non-blocking operations like network requests, file I/O, or other async operations without blocking the main execution thread.
|
||||||
|
|
||||||
|
### Creating Async Tools
|
||||||
|
|
||||||
|
You can create async tools in two ways:
|
||||||
|
|
||||||
|
#### 1. Using the `tool` Decorator with Async Functions
|
||||||
|
|
||||||
|
```python Code
|
||||||
|
from crewai.tools import tool
|
||||||
|
|
||||||
|
@tool("fetch_data_async")
|
||||||
|
async def fetch_data_async(query: str) -> str:
|
||||||
|
"""Asynchronously fetch data based on the query."""
|
||||||
|
# Simulate async operation
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
return f"Data retrieved for {query}"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Implementing Async Methods in Custom Tool Classes
|
||||||
|
|
||||||
|
```python Code
|
||||||
|
from crewai.tools import BaseTool
|
||||||
|
|
||||||
|
class AsyncCustomTool(BaseTool):
|
||||||
|
name: str = "async_custom_tool"
|
||||||
|
description: str = "An asynchronous custom tool"
|
||||||
|
|
||||||
|
async def _run(self, query: str = "") -> str:
|
||||||
|
"""Asynchronously run the tool"""
|
||||||
|
# Your async implementation here
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
return f"Processed {query} asynchronously"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using Async Tools
|
||||||
|
|
||||||
|
Async tools work seamlessly in both standard Crew workflows and Flow-based workflows:
|
||||||
|
|
||||||
|
```python Code
|
||||||
|
# In standard Crew
|
||||||
|
agent = Agent(role="researcher", tools=[async_custom_tool])
|
||||||
|
|
||||||
|
# In Flow
|
||||||
|
class MyFlow(Flow):
|
||||||
|
@start()
|
||||||
|
async def begin(self):
|
||||||
|
crew = Crew(agents=[agent])
|
||||||
|
result = await crew.kickoff_async()
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
The CrewAI framework automatically handles the execution of both synchronous and asynchronous tools, so you don't need to worry about how to call them differently.
|
||||||
|
|
||||||
### Utilizing the `tool` Decorator
|
### Utilizing the `tool` Decorator
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
|
|||||||
Reference in New Issue
Block a user