From 35aff6e84edac4c87d68784e5cfa0b8d030d363d Mon Sep 17 00:00:00 2001 From: MQ Date: Fri, 28 Feb 2025 10:10:56 +0100 Subject: [PATCH] improve code, lazy import, improve readme --- src/crewai_tools/tools/apify_actors/README.md | 6 ++- .../tools/apify_actors/apify_actors.py | 54 ++++++++++++------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/crewai_tools/tools/apify_actors/README.md b/src/crewai_tools/tools/apify_actors/README.md index 03e279d01..a29e3ae97 100644 --- a/src/crewai_tools/tools/apify_actors/README.md +++ b/src/crewai_tools/tools/apify_actors/README.md @@ -1,7 +1,7 @@ # ApifyActorsTool ## Description -The `ApifyActorsTool` is a powerful utility that enables seamless integration of [Apify](https://apify.com/) into your CrewAI workflows. Apify Actors are cloud-based web scraping and automation programs that allow you to extract data, crawl websites, and automate tasks without managing infrastructure. This tool provides an efficient way to run Actors like the [RAG Web Browser](https://apify.com/apify/rag-web-browser) directly within your agents, making it ideal for tasks requiring real-time web data extraction or automation. For more Actors, visit the [Apify Store](https://apify.com/store). +The `ApifyActorsTool` is a powerful utility that enables seamless integration of [Apify Actors](https://apify.com/) into your CrewAI workflows. Apify Actors are cloud-based web scraping and automation programs that allow you to extract data, crawl websites, and automate tasks without managing infrastructure. This tool provides an efficient way to run Actors like the [RAG Web Browser](https://apify.com/apify/rag-web-browser) directly within your agents, making it ideal for tasks requiring real-time web data extraction or automation. For more Actors, visit the [Apify Store](https://apify.com/store). For more details on using Apify with CrewAI, visit the [Apify CrewAI integration documentation](https://docs.apify.com/platform/integrations/crewai). @@ -31,7 +31,9 @@ tool = ApifyActorsTool(actor_name="apify/rag-web-browser") # Run the tool with a specific input, e.g., a search query results = tool.run(run_input={"query": "What is CrewAI?", "maxResults": 5}) -print(results) +for result in results: + print(result['metadata']['url']) + print(result['markdown']) ``` ## Arguments diff --git a/src/crewai_tools/tools/apify_actors/apify_actors.py b/src/crewai_tools/tools/apify_actors/apify_actors.py index ec08a553d..dc229214d 100644 --- a/src/crewai_tools/tools/apify_actors/apify_actors.py +++ b/src/crewai_tools/tools/apify_actors/apify_actors.py @@ -1,16 +1,10 @@ from crewai.tools import BaseTool from pydantic import Field -from typing import Any, Dict, List +from typing import TYPE_CHECKING, Any, Dict, List import os -try: +if TYPE_CHECKING: from langchain_apify import ApifyActorsTool as _ApifyActorsTool -except ImportError: - raise ImportError( - "Could not import langchain_apify python package. " - "Please install it with `pip install langchain-apify` or `uv add langchain-apify`." - ) - class ApifyActorsTool(BaseTool): """Tool that runs Apify Actors. @@ -20,18 +14,30 @@ class ApifyActorsTool(BaseTool): For details, see https://docs.apify.com/platform/integrations/crewai + Args: + actor_name (str): The name of the Apify Actor to run. + *args: Variable length argument list passed to BaseTool. + **kwargs: Arbitrary keyword arguments passed to BaseTool. + + Returns: + List[Dict[str, Any]]: Results from the actor execution. + + Raises: + ValueError: If `APIFY_API_TOKEN` is not set or if the tool is not initialized. + ImportError: If `langchain_apify` package is not installed. + Example: .. code-block:: python from crewai_tools import ApifyActorsTool - tool = ApifyActorsTool(actor_id="apify/rag-web-browser") + tool = ApifyActorsTool(actor_name="apify/rag-web-browser") - results = tool.run({"query": "what is Apify?", "maxResults": 5}) - print(results) + results = tool.run(run_input={"query": "What is CrewAI?", "maxResults": 5}) + for result in results: + print(result['metadata']['url']) + print(result['markdown']) """ - actor_tool: _ApifyActorsTool | None = Field( - default=None, description="Apify Actor Tool" - ) + actor_tool: _ApifyActorsTool = Field(description="Apify Actor Tool") def __init__( self, @@ -47,6 +53,13 @@ class ApifyActorsTool(BaseTool): ) raise ValueError(msg) + try: + from langchain_apify import ApifyActorsTool as _ApifyActorsTool + except ImportError: + raise ImportError( + "Could not import langchain_apify python package. " + "Please install it with `pip install langchain-apify` or `uv add langchain-apify`." + ) actor_tool = _ApifyActorsTool(actor_name) kwargs.update( @@ -68,7 +81,12 @@ class ApifyActorsTool(BaseTool): Raises: ValueError: If 'actor_tool' is not initialized. """ - if self.actor_tool is None: - msg = "ApifyActorsToolCrewAI is not initialized" - raise ValueError(msg) - return self.actor_tool._run(run_input) + try: + return self.actor_tool._run(run_input) + except Exception as e: + msg = ( + f'Failed to run ApifyActorsTool {self.name}. ' + 'Please check your Apify account Actor run logs for more details.' + f'Error: {e}' + ) + raise RuntimeError(msg) from e