CrewAI agent
Example of how to use CrewAI with Apify Actors to create a social media analysis tool-calling agent.
src/main.py
src/tools.py
1"""Module defines the main entry point for the Apify Actor.2
3Feel free to modify this file to suit your specific needs.4
5To build Apify Actors, utilize the Apify SDK toolkit, read more at the official documentation:6https://docs.apify.com/sdk/python7"""8
9from __future__ import annotations10
11import os12
13from apify import Actor14from crewai import Agent, Crew, Task15from crewai_tools import ApifyActorsTool16
17
18async def main() -> None:19 """Define a main entry point for the Apify Actor.20
21 This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.22 Asynchronous execution is required for communication with the Apify platform, and it also enhances performance in23 the field of web scraping significantly.24
25 Raises:26 ValueError: If the input is missing required attributes.27 """28 async with Actor:29 apify_token = os.getenv('APIFY_TOKEN')30 if not apify_token:31 raise ValueError('APIFY_TOKEN environment variable must be set for authentication.')32 # Set the env var that ApifyActorsTool expects33 os.environ['APIFY_API_TOKEN'] = apify_token34
35 # Charge for Actor start36 await Actor.charge('actor-start')37
38 # Handle input39 actor_input = await Actor.get_input()40
41 query = actor_input.get('query')42 model_name = actor_input.get('modelName', 'gpt-4o-mini')43 if not query:44 msg = 'Missing "query" attribute in input!'45 raise ValueError(msg)46
47 # Create a toolkit for the agent48 # containing the Instagram scraper tool49 tools = [ApifyActorsTool('apify/instagram-scraper')]50
51 # Create an agent52 # For more information, see https://docs.crewai.com/concepts/agents53 agent = Agent(54 role='Social Media Analytics Expert',55 goal='Analyze and provide insights about social media profiles and content.',56 backstory=(57 'I am an expert social media analyst specializing in Instagram analysis. '58 'I help users understand social media data and extract meaningful insights '59 'from profiles and posts.'60 ),61 tools=tools,62 verbose=True,63 llm=model_name,64 )65
66 # Create a task assigned to the agent67 # For more information, see https://docs.crewai.com/concepts/tasks68 task = Task(69 description=query,70 expected_output='A helpful response to the user query.',71 agent=agent,72 )73
74 # Create a one-man crew75 # For more information, see https://docs.crewai.com/concepts/crews76 crew = Crew(agents=[agent], tasks=[task])77
78 # Kick off the crew and get the response79 crew_output = crew.kickoff()80 raw_response = crew_output.raw81
82 # Log total token usage83 Actor.log.info('Total tokens used by the model: %s', crew_output.token_usage.total_tokens)84
85 # Charge for task completion86 await Actor.charge('task-completed')87
88 # Push results to the dataset89 await Actor.push_data(90 {91 'query': query,92 'response': raw_response,93 }94 )95 Actor.log.info('Pushed the data into the dataset!')
Python CrewAI template
A template for CrewAI projects in Python for building AI agents with Apify Actors. The template provides a basic structure and an example agent that calls Actors via ApifyActorsTool
in a workflow using the CrewAI Apify Actors integration.
For a detailed guide, visit the How to build an AI agent article.
How it works
An agent is created and given a set of tools to accomplish a task. The agent receives a query from the user and decides which tools to use and in what order to complete the task. In this template, the agent uses ApifyActorsTool('apify/instagram-scraper')
from crewai_tools
to run the Instagram Scraper Actor and analyze scraped posts. The agent produces textual output, which is saved to a dataset.
How to use
Tools are provided via crewai_tools
and configured in src/main.py
. To change tools, edit the tools
list in src/main.py
. You can also update the agent prompts in src/main.py
. For more information, refer to the CrewAI agent documentation and the CrewAI tools documentation.
Pay Per Event
This template uses the Pay Per Event (PPE) monetization model, which provides flexible pricing based on defined events.
To charge users, define events in JSON format and save them on the Apify platform. Here is an example schema with the task-completed
event:
[{"task-completed": {"eventTitle": "Task completed","eventDescription": "Cost per query answered.","eventPriceUsd": 0.1}}]
In the Actor, trigger the event with:
await Actor.charge(event_name='task-completed')
This approach allows you to programmatically charge users directly from your Actor, covering the costs of execution and related services, such as LLM input/output tokens.
To set up the PPE model for this Actor:
- Configure the OpenAI API key environment variable: provide your OpenAI API key to the
OPENAI_API_KEY
in the Actor's Environment variables. - Configure Pay Per Event: establish the Pay Per Event pricing schema in the Actor's Monetization settings. First, set the Pricing model to
Pay per event
and add the schema. An example schema can be found in .actor/pay_per_event.json.
Included features
- Apify SDK for Python - a toolkit for building Apify Actors and scrapers in Python
- Input schema - define and easily validate a schema for your Actor's input
- Dataset - store structured data where each object stored has the same attributes
- Key-value store - store any kind of data, such as JSON documents, images, or text files
Resources
Start with Python
Scrape single page with provided URL with HTTPX and extract data from page's HTML with Beautiful Soup.
BeautifulSoup
Example of a web scraper that uses Python HTTPX to scrape HTML from URLs provided on input, parses it using BeautifulSoup and saves results to storage.
Playwright + Chrome
Crawler example that uses headless Chrome driven by Playwright to scrape a website. Headless browsers render JavaScript and can help when getting blocked.
Selenium + Chrome
Scraper example built with Selenium and headless Chrome browser to scrape a website and save the results to storage. A popular alternative to Playwright.
Empty Python project
Empty template with basic structure for the Actor with Apify SDK that allows you to easily add your own functionality.
Standby Python project
Template with basic structure for an Actor using Standby mode that allows you to easily add your own functionality.