-
Notifications
You must be signed in to change notification settings - Fork 202
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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 💪🏼