Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ApifyTool with additional methods #79

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crewai_tools/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I think we just miss one extra import on the other init.py file 💪🏼

14 changes: 14 additions & 0 deletions crewai_tools/tools/apify_tool/README.md
Original file line number Diff line number Diff line change
@@ -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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind taking some inspiration from existing READMEs for other tools, it would be nice to have most following a standard

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.
71 changes: 71 additions & 0 deletions crewai_tools/tools/apify_tool/apify_tool.py
Original file line number Diff line number Diff line change
@@ -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})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want the agent to pass the actor id in this case together with the query?
Also might be worth having another tool to lsit the actor ids so it can decide / learn what to call?


# 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.")
Comment on lines +42 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these specifically cpould be broken into other tools as well so it's pretty specific, hte way it's setup now the agent won't be able to use them