🐝 BeeAI agent
Example of how to use Bee Agent Framework with Apify Actors to create a social media analysis agent.
src/main.ts
1import { Actor, log } from 'apify';
2import { BeeAgent } from 'bee-agent-framework/agents/bee/agent';
3import { UnconstrainedMemory } from 'bee-agent-framework/memory/unconstrainedMemory';
4import { z } from 'zod';
5import { LangChainChatModel } from 'bee-agent-framework/adapters/langchain/backend/chat';
6import { ChatOpenAI } from '@langchain/openai';
7import { OpenAIChatModel } from 'bee-agent-framework/adapters/openai/backend/chat';
8import { CalculatorSumTool } from './tools/calculator.js';
9import { InstagramScrapeTool } from './tools/instagram.js';
10import { StructuredOutputGenerator } from './structured_response_generator.js';
11
12// This is an ESM project, and as such, it requires you to specify extensions in your relative imports.
13// Read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
14// Note that we need to use `.js` even when inside TS files
15// import { router } from './routes.js';
16
17// Actor input schema
18interface Input {
19 query: string;
20 modelName: string;
21 debug?: boolean;
22}
23
24// The init() call configures the Actor for its environment. It's recommended to start every Actor with an init().
25await Actor.init();
26
27// Charge for Actor start
28await Actor.charge({ eventName: 'actor-start' });
29
30// Handle input
31const {
32 // The query default value is provided only for template testing purposes.
33 // You can remove it.
34 query,
35 modelName,
36 debug,
37} = await Actor.getInput() as Input;
38if (debug) {
39 log.setLevel(log.LEVELS.DEBUG);
40}
41if (!query) {
42 throw new Error('An agent query is required.');
43}
44
45/**
46 * Actor code
47*/
48// Create a ReAct agent that can use tools.
49// See https://i-am-bee.github.io/bee-agent-framework/#/agents?id=bee-agent
50// In order to use PPE, the LangChain adapter must be used
51// otherwise, the token usage is not tracked.
52log.debug(`Using model: ${modelName}`);
53const llm = new LangChainChatModel(
54 new ChatOpenAI({ model: modelName }),
55);
56// The LangChain adapter does not work with the structured output generation
57// for some reason.
58// Create a separate LLM for structured output generation.
59const llmStructured = new OpenAIChatModel(modelName);
60const agent = new BeeAgent({
61 llm,
62 memory: new UnconstrainedMemory(),
63 tools: [new CalculatorSumTool(),
64 new InstagramScrapeTool()],
65});
66
67// Store tool messages for later structured output generation.
68// This can be removed if you don't need structured output.
69const structuredOutputGenerator = new StructuredOutputGenerator(llmStructured);
70
71// Prompt the agent with the query.
72// Debug log agent status updates, e.g., thoughts, tool calls, etc.
73const response = await agent
74 .run({ prompt: query })
75 .observe((emitter) => {
76 emitter.on('update', async ({ update }) => {
77 log.debug(`Agent (${update.key}) 🤖 : ${update.value}`);
78
79 // Save tool messages for later structured output generation.
80 // This can be removed if you don't need structured output.
81 if (['tool_name', 'tool_output', 'tool_input'].includes(update.key as string)) {
82 structuredOutputGenerator.processToolMessage(
83 update.key as 'tool_name' | 'tool_output' | 'tool_input',
84 update.value,
85 );
86 }
87 // End of tool message saving.
88 });
89 });
90
91log.info(`Agent 🤖 : ${response.result.text}`);
92
93// Hacky way to get the structured output.
94// Using the stored tool messages and the user query to create a structured output.
95const structuredResponse = await structuredOutputGenerator.generateStructuredOutput(query,
96 z.object({
97 totalLikes: z.number(),
98 totalComments: z.number(),
99 mostPopularPosts: z.array(z.object({
100 url: z.string(),
101 likes: z.number(),
102 comments: z.number(),
103 timestamp: z.string(),
104 caption: z.string().nullable().optional(),
105 alt: z.string().nullable().optional(),
106 })),
107 }));
108log.debug(`Structured response: ${JSON.stringify(structuredResponse)}`);
109
110// Charge for task completion
111await Actor.charge({ eventName: 'task-completed' });
112
113// Push results to the dataset.
114await Actor.pushData({
115 query,
116 response: response.result.text,
117 // This can be removed if you don't need structured output.
118 structuredResponse: structuredResponse.object,
119});
120log.info('Pushed the data into the dataset!');
121
122// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit().
123await Actor.exit();
TypeScript BeeAI agent Template
A template for BeeAI agent projects in TypeScript for building AI agents with Apify Actors. This template offers a structured setup and an example ReAct agent utilizing Instagram Scraper and a calculator tool in a workflow context.
How it Works
A ReAct agent is employed, equipped with tools to respond to user queries. The agent processes a user query, decides on the tools to use, and in what sequence, to achieve the desired outcome. Here, the agent leverages an Instagram Scraper to fetch posts from a profile and a calculator tool to compute sums, such as totaling likes or comments. The agent produces textual and structured output, which is saved to a dataset.
How to Use
Add or modify tools in the src/tool_calculator.ts
and src/tool_instagram.ts
files, and ensure they are included in the agent's tool list in src/main.ts
. Additionally, you can update the agent's system prompt or other configurations within src/main.ts
. For more information, refer to the BeeAI 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({ eventName: '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 JavaScript - a toolkit for building Apify Actors and scrapers in JavaScript
- 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 Axios and extract data from page's HTML with Cheerio.
A scraper example that uses Cheerio to parse HTML. It's fast, but it can't run the website's JavaScript or pass JS anti-scraping challenges.
Example of a Puppeteer and headless Chrome web scraper. Headless browsers render JavaScript and are harder to block, but they're slower than plain HTTP.
Web scraper example with Crawlee, Playwright and headless Chrome. Playwright is more modern, user-friendly and harder to block than Puppeteer.
Web scraper example with Crawlee, Playwright and headless Camoufox. Camoufox is a custom stealthy fork of Firefox. Try this template if you're facing anti-scraping challenges.
Example of using the Playwright Test project to run automated website tests in the cloud and display their results. Usable as an API.