Skip to content

Commit 6aa111d

Browse files
committed
Merge branch 'master' into feat/header-facelift
2 parents ecf7818 + 8ee9ad7 commit 6aa111d

File tree

13 files changed

+1249
-691
lines changed

13 files changed

+1249
-691
lines changed

apify-docs-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apify/docs-theme",
3-
"version": "1.0.177",
3+
"version": "1.0.178",
44
"description": "",
55
"main": "./src/index.js",
66
"files": [

apify-docs-theme/src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ const themeConfig = {
270270
algolia: {
271271
appId: 'N8EOCSBQGH',
272272
apiKey: 'e97714a64e2b4b8b8fe0b01cd8592870', // search only (public) API key
273-
indexName: 'test_test_apify_sdk',
273+
indexName: 'apify_sdk_v2',
274274
placeholder: 'Search documentation',
275275
algoliaOptions: {
276276
facetFilters: ['version:VERSION'],

apify-docs-theme/src/theme/SearchBar/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function SearchBar({ onClick }) {
6666
<div onClick={onClick}>
6767
<ApifySearch
6868
algoliaAppId={siteConfig.themeConfig.algolia.appId}
69-
algoliaIndexName='test_test_apify_sdk'
69+
algoliaIndexName='apify_sdk_v2'
7070
algoliaKey={siteConfig.themeConfig.algolia.apiKey}
7171
filters={`version:${getVersion()}`}
7272
navigate={navigate}

package-lock.json

Lines changed: 654 additions & 667 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"typescript-eslint": "^8.29.1"
6767
},
6868
"dependencies": {
69-
"@apify/ui-library": "^0.76.0",
69+
"@apify/ui-library": "^0.79.0",
7070
"@docusaurus/core": "3.7.0",
7171
"@docusaurus/faster": "3.7.0",
7272
"@docusaurus/plugin-client-redirects": "3.7.0",

sources/academy/ai/ai-agents.mdx

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
---
2+
title: Build and monetize AI Agents on Apify
3+
description: This guide shows you how to create an AI agent using the CrewAI Python framework and Apify platform. You will build an Instagram analysis agent that integrates with large language models (LLMs) and web scrapers.
4+
sidebar_label: AI Agents
5+
sidebar_position: 1
6+
slug: /ai/ai-agents
7+
---
8+
9+
**This guide shows you how to create an AI agent using the CrewAI Python framework and Apify platform. You will build an Instagram analysis agent that integrates with large language models (LLMs) and web scrapers.**
10+
11+
---
12+
13+
AI agents are goal-oriented systems that make independent decisions. They interact with environments using predefined tools and workflows to automate complex tasks.
14+
15+
On Apify, AI agents are built as Actors—serverless cloud programs for web scraping, data processing, and AI deployment. Apify evolved from running scrapers in the cloud to supporting LLMs that follow predefined workflows with dynamically defined goals.
16+
17+
## Prerequisites
18+
19+
To build an effective AI agent, you need prompts to guide it, tools for external interactions, a large language model (LLM) to connect the components, an agentic framework to handle LLM behavior, and a platform to run, deploy, and scale the solution.
20+
21+
## Benefits of using Apify for AI agents
22+
23+
Apify provides a complete platform for building and deploying AI agents with the following benefits:
24+
25+
- _Serverless execution_ - without infrastructure management
26+
- _Stateful execution_ - with agent memory capabilities
27+
- _Monetization options_ - through usage-based charging
28+
- _Extensive tool ecosystem_ - with thousands of available Actors
29+
- _Scalability and reliability_ - for production environments
30+
- _Pre-integrated tools_ - for web scraping and automation
31+
32+
## Building an AI agent
33+
34+
### Step 1: Define the use case
35+
36+
This tutorial creates a social media analysis agent that analyzes Instagram posts based on user queries using the [Instagram Scraper Actor](LINK HERE).
37+
38+
_Example:_
39+
40+
- _Input:_ "Analyze the last 10 posts from @openai and summarize AI trends."
41+
- _Output:_ Trend analysis based on post content.
42+
43+
### Step 2: Configure input and output
44+
45+
Define the input format (URL, JSON configuration, or text query) and output format (text response or structured data) for your agent.
46+
47+
_Example input:_
48+
49+
- User query: "Analyze @openai posts for AI trends"
50+
- OpenAI model selection (e.g., `gpt-4`)
51+
52+
_Example output:_
53+
54+
- Text response with insights
55+
- Data stored in Apify [Dataset](/platform/storage/dataset)
56+
57+
:::note Agent memory
58+
59+
Agents can include memory for storing information between conversations. Single-task agents typically do not require memory.
60+
61+
:::
62+
63+
### Step 3: Set up the development environment
64+
65+
Install the Apify CLI, which allows you to create, run, and deploy Actors from your local machine.
66+
67+
```bash
68+
npm install -g @apify/cli
69+
```
70+
71+
Create a new Actor project from the CrewAI template and navigate into the new directory.
72+
73+
```bash
74+
apify create agent-actor -t python-crewai
75+
cd agent-actor
76+
```
77+
78+
### Step 4: Understand the project structure
79+
80+
The template includes:
81+
82+
- `.actor/` – Actor configuration files.
83+
- `actor.json` – The Actor's definition.
84+
- `input_schema.json` – Defines the UI for the Actor's input.
85+
- `dataset_schema.json` – Defines the structure of the output data.
86+
- `pay_per_event.json` – Configuration for monetization.
87+
- `src/` – Source code
88+
- `main.py` – The main script for Actor execution, agent, and task definition.
89+
- `tools.py` – Implementations of the tools the agent can use.
90+
- `models.py` – Pydantic models for structured tool output.
91+
- `ppe_utils.py` – Helper functions for pay-per-event monetization.
92+
93+
### Step 5: Define input and output schemas
94+
95+
Update `.actor/input_schema.json` to define the Actor's inputs. This schema generates a user interface for running the Actor on the Apify platform.
96+
97+
```json
98+
{
99+
"title": "Instagram Analysis Agent Input",
100+
"type": "object",
101+
"schemaVersion": 1,
102+
"properties": {
103+
"query": {
104+
"title": "Query",
105+
"type": "string",
106+
"description": "Task for the agent to perform",
107+
"example": "Analyze @openai posts for AI trends"
108+
},
109+
"modelName": {
110+
"title": "Model Name",
111+
"type": "string",
112+
"description": "OpenAI model to use",
113+
"default": "gpt-4"
114+
}
115+
},
116+
"required": ["query"]
117+
}
118+
```
119+
120+
Define the dataset schema in `.actor/dataset_schema.json`. This helps structure the data pushed to the dataset.
121+
122+
```json
123+
{
124+
"title": "Instagram Analysis Output",
125+
"type": "object",
126+
"properties": {
127+
"query": {
128+
"title": "Query",
129+
"type": "string"
130+
},
131+
"response": {
132+
"title": "Response",
133+
"type": "string"
134+
}
135+
}
136+
}
137+
```
138+
139+
### Step 6: Configure tools
140+
141+
The Instagram post scraper tool is implemented using the [Instagram Scraper Actor](LINK HERE). The tool returns structured output as Pydantic models defined in `src/models.py`:
142+
143+
```python
144+
class InstagramPost(BaseModel):
145+
id: str
146+
url: str
147+
caption: str
148+
timestamp: datetime
149+
likes_count: int
150+
comments_count: int
151+
```
152+
153+
The tool is defined in `src/tools.py` and includes:
154+
155+
- Tool description and argument schema for the agent
156+
- Integration with Instagram Scraper Actor
157+
- Data retrieval and formatting
158+
159+
### Step 7: Implement the agent
160+
161+
The agent implementation in `src/main.py` includes:
162+
163+
1. Handle Actor input: Read the user's query and any other parameters from the Actor input.
164+
165+
```python
166+
async def main():
167+
async with Actor:
168+
actor_input = await Actor.get_input()
169+
query = actor_input.get("query")
170+
model_name = actor_input.get("modelName", "gpt-4")
171+
```
172+
173+
1. Define the agent: Instantiate the agent, giving it a role, a goal, and access to the tools you configured.
174+
175+
```python
176+
agent = Agent(
177+
role="Social Media Analyst",
178+
goal="Analyze Instagram posts and provide insights",
179+
backstory="Expert in social media analysis and trend identification",
180+
tools=[instagram_scraper_tool],
181+
llm=ChatOpenAI(model=model_name)
182+
)
183+
```
184+
185+
1. Create task and crew: Define the task for the agent to complete based on the user's query.
186+
187+
```python
188+
task = Task(
189+
description=query,
190+
agent=agent,
191+
expected_output="Detailed analysis with insights"
192+
)
193+
194+
crew = Crew(
195+
agents=[agent],
196+
tasks=[task]
197+
)
198+
```
199+
200+
1. Execute and save results: Kick off the crew to run the task and save the final result to the Actor's default dataset.
201+
202+
```python
203+
result = crew.kickoff()
204+
await Actor.push_data({
205+
"query": query,
206+
"response": str(result)
207+
})
208+
```
209+
210+
### Step 8: Test locally
211+
212+
Run the agent on your local machine using the Apify CLI. Ensure you have set any required environment variables (e.g., `OPENAI_API_KEY`).
213+
214+
```bash
215+
apify run
216+
```
217+
218+
### Step 9: Deploy to Apify
219+
220+
Push your Actor's code to the Apify platform.
221+
222+
```bash
223+
apify push
224+
```
225+
226+
After deployment:
227+
228+
1. Navigate to your Actor's settings.
229+
1. Set `OPENAI_API_KEY` as a secret environment variable.
230+
1. Rebuild the Actor version to apply the changes.
231+
232+
### Step 10: Test the deployed agent
233+
234+
Run the agent on the platform with a sample query and monitor the results in the output dataset.
235+
236+
```text
237+
Analyze the posts of the @openai and @googledeepmind and summarize me current trends in the AI.
238+
```
239+
240+
:::info Troubleshooting
241+
242+
Common issues and solutions:
243+
244+
- _Agent fails to call tools:_ Check that the tool descriptions in src/tools.py are clear and the argument schemas are correct.
245+
- _Instagram scraper fails:_ Verify that the Instagram usernames exist and are public. Check the scraper Actor's run logs for specific errors.
246+
- _Missing API key:_ Ensure OPENAI_API_KEY is set as a secret environment variable in your Actor's Settings.
247+
248+
:::
249+
250+
## Monetizing your AI agent
251+
252+
Apify's pay-per-event (PPE) pricing model allows charging users based on specific triggered events through the API or SDKs.
253+
254+
### Step 1: Define chargeable events
255+
256+
You can configure charges for events like the Actor starting, a task completing successfully, or custom events such as specific API calls.
257+
258+
Example event definition:
259+
260+
```json
261+
{
262+
"eventName": "task-completed",
263+
"description": "Charge for completed analysis task",
264+
"price": 0.10
265+
}
266+
```
267+
268+
### Step 2: Implement charging in code
269+
270+
Add charging logic to your code:
271+
272+
```python
273+
await Actor.charge({
274+
"eventName": "task-completed",
275+
"amount": 1
276+
})
277+
```
278+
279+
### Step 3: Configure PPE settings
280+
281+
1. Enable pay-per-event monetization in Actor settings.
282+
1. Define events from `pay_per_event.json`.
283+
1. Set pricing for each event.
284+
285+
### Step 4: Publish the agent
286+
287+
Before making your agent public on [Apify Store](https://apify.com/store), complete the following checklist:
288+
289+
- Update README with usage instructions.
290+
- Validate `input_schema.json` and `dataset_schema.json`.
291+
- Verify `OPENAI_API_KEY` environment variable is handled correctly.
292+
- Check monetization settings on the Actor publication page.
293+
- Test the Actor thoroughly.
294+
- Set your Actor's visibility to public.
295+
296+
## Next steps
297+
298+
To continue developing AI agents:
299+
300+
1. _Use the CrewAI template:_ Start with `apify create agent-actor -t python-crewai`
301+
2. _Explore other templates:_ Visit the Apify templates page for alternatives
302+
3. _Review existing agents:_ Check the AI Agents collection on Apify Store
303+
4. _Publish and monetize:_ Deploy with `apify push` and enable monetization

sources/academy/sidebars.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ module.exports = {
1212
},
1313
],
1414
},
15+
{
16+
type: 'category',
17+
label: 'AI',
18+
collapsible: false,
19+
className: 'section-header',
20+
items: [
21+
{
22+
type: 'autogenerated',
23+
dirName: 'ai',
24+
},
25+
],
26+
},
1527
{
1628
type: 'category',
1729
label: 'Apify Platform',
294 KB
Loading
292 KB
Loading

0 commit comments

Comments
 (0)