From 5156fc4792724b1ce31e84ce834903476bf0ce8c Mon Sep 17 00:00:00 2001 From: Greyson Lalonde Date: Tue, 2 Dec 2025 08:57:04 -0500 Subject: [PATCH] chore: update docs --- docs/en/learn/create-custom-tools.mdx | 50 +++++++++++++++++++++++ docs/ko/learn/create-custom-tools.mdx | 52 +++++++++++++++++++++++- docs/pt-BR/learn/create-custom-tools.mdx | 52 +++++++++++++++++++++++- 3 files changed, 152 insertions(+), 2 deletions(-) diff --git a/docs/en/learn/create-custom-tools.mdx b/docs/en/learn/create-custom-tools.mdx index d8c123b34..b9d67b49c 100644 --- a/docs/en/learn/create-custom-tools.mdx +++ b/docs/en/learn/create-custom-tools.mdx @@ -66,5 +66,55 @@ def my_cache_strategy(arguments: dict, result: str) -> bool: cached_tool.cache_function = my_cache_strategy ``` +### Creating Async Tools + +CrewAI supports async tools for non-blocking I/O operations. This is useful when your tool needs to make HTTP requests, database queries, or other I/O-bound operations. + +#### Using the `@tool` Decorator with Async Functions + +The simplest way to create an async tool is using the `@tool` decorator with an async function: + +```python Code +import aiohttp +from crewai.tools import tool + +@tool("Async Web Fetcher") +async def fetch_webpage(url: str) -> str: + """Fetch content from a webpage asynchronously.""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.text() +``` + +#### Subclassing `BaseTool` with Async Support + +For more control, subclass `BaseTool` and implement both `_run` (sync) and `_arun` (async) methods: + +```python Code +import requests +import aiohttp +from crewai.tools import BaseTool +from pydantic import BaseModel, Field + +class WebFetcherInput(BaseModel): + """Input schema for WebFetcher.""" + url: str = Field(..., description="The URL to fetch") + +class WebFetcherTool(BaseTool): + name: str = "Web Fetcher" + description: str = "Fetches content from a URL" + args_schema: type[BaseModel] = WebFetcherInput + + def _run(self, url: str) -> str: + """Synchronous implementation.""" + return requests.get(url).text + + async def _arun(self, url: str) -> str: + """Asynchronous implementation for non-blocking I/O.""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.text() +``` + 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. diff --git a/docs/ko/learn/create-custom-tools.mdx b/docs/ko/learn/create-custom-tools.mdx index 05ea69ac4..a468968ac 100644 --- a/docs/ko/learn/create-custom-tools.mdx +++ b/docs/ko/learn/create-custom-tools.mdx @@ -63,5 +63,55 @@ def my_cache_strategy(arguments: dict, result: str) -> bool: cached_tool.cache_function = my_cache_strategy ``` +### 비동기 도구 생성하기 + +CrewAI는 논블로킹 I/O 작업을 위한 비동기 도구를 지원합니다. 이는 HTTP 요청, 데이터베이스 쿼리 또는 기타 I/O 바운드 작업이 필요한 경우에 유용합니다. + +#### `@tool` 데코레이터와 비동기 함수 사용하기 + +비동기 도구를 만드는 가장 간단한 방법은 `@tool` 데코레이터와 async 함수를 사용하는 것입니다: + +```python Code +import aiohttp +from crewai.tools import tool + +@tool("Async Web Fetcher") +async def fetch_webpage(url: str) -> str: + """Fetch content from a webpage asynchronously.""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.text() +``` + +#### 비동기 지원으로 `BaseTool` 서브클래싱하기 + +더 많은 제어를 위해 `BaseTool`을 상속하고 `_run`(동기) 및 `_arun`(비동기) 메서드를 모두 구현할 수 있습니다: + +```python Code +import requests +import aiohttp +from crewai.tools import BaseTool +from pydantic import BaseModel, Field + +class WebFetcherInput(BaseModel): + """Input schema for WebFetcher.""" + url: str = Field(..., description="The URL to fetch") + +class WebFetcherTool(BaseTool): + name: str = "Web Fetcher" + description: str = "Fetches content from a URL" + args_schema: type[BaseModel] = WebFetcherInput + + def _run(self, url: str) -> str: + """Synchronous implementation.""" + return requests.get(url).text + + async def _arun(self, url: str) -> str: + """Asynchronous implementation for non-blocking I/O.""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.text() +``` + 이 가이드라인을 준수하고 새로운 기능과 협업 도구를 도구 생성 및 관리 프로세스에 통합함으로써, -CrewAI 프레임워크의 모든 기능을 활용할 수 있으며, AI agent의 개발 경험과 효율성을 모두 높일 수 있습니다. \ No newline at end of file +CrewAI 프레임워크의 모든 기능을 활용할 수 있으며, AI agent의 개발 경험과 효율성을 모두 높일 수 있습니다. diff --git a/docs/pt-BR/learn/create-custom-tools.mdx b/docs/pt-BR/learn/create-custom-tools.mdx index 0cc01ab46..0dbfb2340 100644 --- a/docs/pt-BR/learn/create-custom-tools.mdx +++ b/docs/pt-BR/learn/create-custom-tools.mdx @@ -66,5 +66,55 @@ def my_cache_strategy(arguments: dict, result: str) -> bool: cached_tool.cache_function = my_cache_strategy ``` +### Criando Ferramentas Assíncronas + +O CrewAI suporta ferramentas assíncronas para operações de I/O não bloqueantes. Isso é útil quando sua ferramenta precisa fazer requisições HTTP, consultas a banco de dados ou outras operações de I/O. + +#### Usando o Decorador `@tool` com Funções Assíncronas + +A maneira mais simples de criar uma ferramenta assíncrona é usando o decorador `@tool` com uma função async: + +```python Code +import aiohttp +from crewai.tools import tool + +@tool("Async Web Fetcher") +async def fetch_webpage(url: str) -> str: + """Fetch content from a webpage asynchronously.""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.text() +``` + +#### Subclassificando `BaseTool` com Suporte Assíncrono + +Para maior controle, herde de `BaseTool` e implemente os métodos `_run` (síncrono) e `_arun` (assíncrono): + +```python Code +import requests +import aiohttp +from crewai.tools import BaseTool +from pydantic import BaseModel, Field + +class WebFetcherInput(BaseModel): + """Input schema for WebFetcher.""" + url: str = Field(..., description="The URL to fetch") + +class WebFetcherTool(BaseTool): + name: str = "Web Fetcher" + description: str = "Fetches content from a URL" + args_schema: type[BaseModel] = WebFetcherInput + + def _run(self, url: str) -> str: + """Synchronous implementation.""" + return requests.get(url).text + + async def _arun(self, url: str) -> str: + """Asynchronous implementation for non-blocking I/O.""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.text() +``` + Seguindo essas orientações e incorporando novas funcionalidades e ferramentas de colaboração nos seus processos de criação e gerenciamento de ferramentas, -você pode aproveitar ao máximo as capacidades do framework CrewAI, aprimorando tanto a experiência de desenvolvimento quanto a eficiência dos seus agentes de IA. \ No newline at end of file +você pode aproveitar ao máximo as capacidades do framework CrewAI, aprimorando tanto a experiência de desenvolvimento quanto a eficiência dos seus agentes de IA.