Back to template gallery

πŸ¦œοΈπŸ”— LangGraph AI agent

Example of how to use LangGraph with Apify Actors to create a social media analysis tool-calling agent.

Language

python

Tools

langgraph

Features

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
11import logging
12
13from apify import Actor
14from langchain_openai import ChatOpenAI
15from langgraph.prebuilt import create_react_agent
16
17from src.models import AgentStructuredOutput
18from src.tools import tool_calculator_sum, tool_scrape_instagram_profile_posts
19from src.utils import log_state
20
21
22async def main() -> None:
23    """Define a main entry point for the Apify Actor.
24
25    This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.
26    Asynchronous execution is required for communication with Apify platform, and it also enhances performance in
27    the field of web scraping significantly.
28
29    Raises:
30        ValueError: If the input is missing required attributes.
31    """
32    async with Actor:
33        # Charge for Actor start
34        await Actor.charge('actor-start')
35
36        # Handle input
37        actor_input = await Actor.get_input()
38
39        query = actor_input.get('query')
40        model_name = actor_input.get('modelName', 'gpt-4o-mini')
41        if actor_input.get('debug', False):
42            Actor.log.setLevel(logging.DEBUG)
43        if not query:
44            msg = 'Missing "query" attribute in input!'
45            raise ValueError(msg)
46
47        llm = ChatOpenAI(model=model_name)
48
49        # Create the ReAct agent graph
50        # see https://langchain-ai.github.io/langgraph/reference/prebuilt/?h=react#langgraph.prebuilt.chat_agent_executor.create_react_agent
51        tools = [tool_calculator_sum, tool_scrape_instagram_profile_posts]
52        graph = create_react_agent(llm, tools, response_format=AgentStructuredOutput)
53
54        inputs: dict = {'messages': [('user', query)]}
55        response: AgentStructuredOutput | None = None
56        last_message: str | None = None
57        async for state in graph.astream(inputs, stream_mode='values'):
58            log_state(state)
59            if 'structured_response' in state:
60                response = state['structured_response']
61                last_message = state['messages'][-1].content
62                break
63
64        if not response or not last_message:
65            Actor.log.error('Failed to get a response from the ReAct agent!')
66            await Actor.fail(status_message='Failed to get a response from the ReAct agent!')
67            return
68
69        # Charge for task completion
70        await Actor.charge('task-completed')
71
72        # Push results to the key-value store and dataset
73        store = await Actor.open_key_value_store()
74        await store.set_value('response.txt', last_message)
75        Actor.log.info('Saved the "response.txt" file into the key-value store!')
76
77        await Actor.push_data(
78            {
79                'response': last_message,
80                'structured_response': response.dict() if response else {},
81            }
82        )
83        Actor.log.info('Pushed the into the dataset!')

Python LangGraph template

A template for LangGraph projects in Python for building AI agents with Apify Actors. The template provides a basic structure and an example LangGraph ReAct agent that calls Actors as tools in a workflow.

How it works

A ReAct 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 and a calculator tool to sum a list of numbers to calculate the total number of likes and comments. The agent is configured to also output structured data, which is pushed to the dataset, while textual output is stored in the key-value store as a response.txt file.

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 system prompt in src/main.py. For more information, refer to the LangGraph ReAct agent documentation and the LangChain tools documentation.

For a more advanced multi-agent example, see the Finance Monitoring Agent actor or visit the LangGraph 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

Already have a solution in mind?

Sign up for a free Apify account and deploy your code to the platform in just a few minutes! If you want a head start without coding it yourself, browse our Store of existing solutions.