diff --git a/crewai_tools/tools/__init__.py b/crewai_tools/tools/__init__.py index a72fda2..6164efb 100644 --- a/crewai_tools/tools/__init__.py +++ b/crewai_tools/tools/__init__.py @@ -37,3 +37,4 @@ from .youtube_channel_search_tool.youtube_channel_search_tool import YoutubeChannelSearchTool from .youtube_video_search_tool.youtube_video_search_tool import YoutubeVideoSearchTool from .spider_tool.spider_tool import SpiderTool +from .apify_tool.apify_tool import ApifyTool \ No newline at end of file diff --git a/crewai_tools/tools/apify_tool/README.md b/crewai_tools/tools/apify_tool/README.md new file mode 100644 index 0000000..96601b4 --- /dev/null +++ b/crewai_tools/tools/apify_tool/README.md @@ -0,0 +1,14 @@ + +The ApifyTool class provides the following methods: + +- `run_actor`: Run an Apify actor with the given input. +- `wait_for_actor_run`: Wait for an Apify actor run to complete. +- `get_dataset`: Get the dataset associated with the given actor run. +- `get_dataset_items`: Get all items from the given dataset. +- `get_key_value_store`: Get the key-value store associated with the given actor run. +- `download_file`: Download a file from the key-value store of the given actor run. +- `store_dataset`: Store data in an Apify dataset. + +You can use these methods to interact with various Apify services, such as running actors, waiting for actor runs to complete, fetching datasets, accessing the key-value store, downloading files, and storing data in datasets. + +Make sure to replace `"your_apify_token"` and `"your_actor_id"` with your actual Apify token and the ID of the actor you want to run, respectively. \ No newline at end of file diff --git a/crewai_tools/tools/apify_tool/apify_tool.py b/crewai_tools/tools/apify_tool/apify_tool.py new file mode 100644 index 0000000..3cdffa4 --- /dev/null +++ b/crewai_tools/tools/apify_tool/apify_tool.py @@ -0,0 +1,71 @@ +import typing as t +from crewai_tools.tools.base_tool import BaseTool +from apify_client import ApifyClient, ActorRun, Dataset + +class ApifyTool(BaseTool): + def __init__(self, apify_token: str): + self.client = ApifyClient(apify_token) + + def _run(self, query: str) -> str: + """ + Run the Apify tool with the given query. + """ + # Parse the query and determine the appropriate Apify action + # For example, you could use natural language processing to extract the intent + # and map it to an Apify action + + # Execute the Apify action using the client + # For example, to run an actor: + actor_run = self.run_actor(actor_id="your_actor_id", run_input={"query": query}) + + # Wait for the actor to complete + actor_run = self.wait_for_actor_run(actor_run) + + # Return the result + return actor_run.output + + def run_actor(self, actor_id: str, run_input: dict) -> ActorRun: + """ + Run an Apify actor with the given input. + """ + return self.client.actor.call_run(actor_id=actor_id, run_input=run_input) + + def wait_for_actor_run(self, actor_run: ActorRun) -> ActorRun: + """ + Wait for an Apify actor run to complete. + """ + while not actor_run.is_finished(): + actor_run = self.client.actor.get_run(actor_run.id) + + return actor_run + + def get_dataset(self, actor_run: ActorRun, dataset_id: str = None) -> Dataset: + """ + Get the dataset associated with the given actor run. + """ + if dataset_id: + return self.client.dataset.get(dataset_id) + else: + return actor_run.dataset + + def get_dataset_items(self, dataset: Dataset) -> List[Dict[str, Any]]: + """ + Get all items from the given dataset. + """ + return list(dataset.iterate_items()) + + def get_key_value_store(self, actor_run: ActorRun) -> Dict[str, Any]: + """ + Get the key-value store associated with the given actor run. + """ + return actor_run.key_value_store + + def download_file(self, actor_run: ActorRun, key: str, file_path: str): + """ + Download a file from the key-value store of the given actor run. + """ + with open(file_path, "wb") as file: + file.write(actor_run.key_value_store.get_file(key).read()) + + async def _arun(self, query: str) -> str: + raise NotImplementedError("This tool does not support async mode.") \ No newline at end of file