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/python
7"""
8
9from __future__ import annotations
10
11from apify import Actor
12from crewai import Agent, Crew, Task
13
14from src.tools import InstagramScraperTool
15
16
17async def main() -> None:
18 """Define a main entry point for the Apify Actor.
19
20 This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.
21 Asynchronous execution is required for communication with the Apify platform, and it also enhances performance in
22 the field of web scraping significantly.
23
24 Raises:
25 ValueError: If the input is missing required attributes.
26 """
27 async with Actor:
28 # Charge for Actor start
29 await Actor.charge('actor-start')
30
31 # Handle input
32 actor_input = await Actor.get_input()
33
34 query = actor_input.get('query')
35 model_name = actor_input.get('modelName', 'gpt-4o-mini')
36 if not query:
37 msg = 'Missing "query" attribute in input!'
38 raise ValueError(msg)
39
40 # Create a toolkit for the agent
41 tools = [InstagramScraperTool()]
42
43 # Create an agent
44 # For more information, see https://docs.crewai.com/concepts/agents
45 agent = Agent(
46 role='Social Media Analytics Expert',
47 goal='Analyze and provide insights about social media profiles and content.',
48 backstory=(
49 'I am an expert social media analyst specializing in Instagram analysis. '
50 'I help users understand social media data and extract meaningful insights '
51 'from profiles and posts.'
52 ),
53 tools=tools,
54 verbose=True,
55 llm=model_name,
56 )
57
58 # Create a task assigned to the agent
59 # For more information, see https://docs.crewai.com/concepts/tasks
60 task = Task(
61 description=query,
62 expected_output='A helpful response to the user query.',
63 agent=agent,
64 )
65
66 # Create a one-man crew
67 # For more information, see https://docs.crewai.com/concepts/crews
68 crew = Crew(agents=[agent], tasks=[task])
69
70 # Kick off the crew and get the response
71 crew_output = crew.kickoff()
72 raw_response = crew_output.raw
73
74 # Log total token usage
75 Actor.log.info('Total tokens used by the model: %s', crew_output.token_usage.total_tokens)
76
77 # Charge for task completion
78 await Actor.charge('task-completed')
79
80 # Push results to the dataset
81 await Actor.push_data(
82 {
83 'query': query,
84 'response': raw_response,
85 }
86 )
87 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 as tools in a workflow.
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 case, the agent is provided with an Instagram Scraper Actor to scrape Instagram profile posts. The agent produces textual output, which is saved to a dataset.
How to use
Add or modify the agent tools in the src/tools.py
file, and make sure to include new tools in the agent tools list in src/main.py
. Additionally, you can 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:
1[ 2 { 3 "task-completed": { 4 "eventTitle": "Task completed", 5 "eventDescription": "Cost per query answered.", 6 "eventPriceUsd": 0.1 7 } 8 } 9]
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
Scrape single page with provided URL with HTTPX and extract data from page's HTML with Beautiful Soup.
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.
Crawler example that uses headless Chrome driven by Playwright to scrape a website. Headless browsers render JavaScript and can help when getting blocked.
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 template with basic structure for the Actor with Apify SDK that allows you to easily add your own functionality.
Template with basic structure for an Actor using Standby mode that allows you to easily add your own functionality.