|
| 1 | +--- |
| 2 | +title: 🦜🔘➡️ LangGraph integration |
| 3 | +sidebar_label: LangGraph |
| 4 | +description: Learn how to integrate Apify with 🦜🔗🔘 LangGraph, in order to use Apify Actors with agents. |
| 5 | +sidebar_position: 1 |
| 6 | +slug: /integrations/langgraph |
| 7 | +--- |
| 8 | + |
| 9 | +**Learn how to integrate Apify with LangGraph, in order to use Apify Actors with agents.** |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## What is LangGraph |
| 14 | + |
| 15 | +[LangGraph](https://www.langchain.com/langgraph) is a Python library designed for constructing stateful, multi-actor applications with Large Language Models (LLMs), allowing developers to build complex AI agent workflows that can leverage tools, APIs, and databases. |
| 16 | + |
| 17 | +:::note Explore LangGraph |
| 18 | + |
| 19 | +For more in-depth details on LangGraph, check out its [official documentation](https://langchain-ai.github.io/langgraph/). |
| 20 | + |
| 21 | +::: |
| 22 | + |
| 23 | +## How to use Apify with LangGraph |
| 24 | + |
| 25 | +This guide will demonstrate how to use Apify Actors with LangGraph by building a ReAct agent that will use the [RAG Web Browser](https://apify.com/apify/rag-web-browser) Actor to search Google for TikTok profiles and [TikTok Data Extractor](https://apify.com/clockworks/free-tiktok-scraper) Actor to extract data from the TikTok profiles to analyze the profiles. |
| 26 | + |
| 27 | +### Prerequisites |
| 28 | + |
| 29 | +#### Apify API token |
| 30 | + |
| 31 | +To use Apify Actors in LangGraph, you need an Apify API token. If you don't have one, you can learn how to obtain it in the [Apify documentation](https://docs.apify.com/platform/integrations/api). |
| 32 | + |
| 33 | +#### OpenAI API key |
| 34 | + |
| 35 | +In order to work with agents in LangGraph, you need an OpenAI API key. If you don't have one, you can get it from the [OpenAI platform](https://platform.openai.com/account/api-keys). |
| 36 | + |
| 37 | +### Installation |
| 38 | + |
| 39 | +Before we start, we need to install all dependencies: |
| 40 | + |
| 41 | +```bash |
| 42 | +pip install langgraph langchain-apify langchain-openai |
| 43 | +``` |
| 44 | + |
| 45 | +### Building the TikTok profile search and analysis agent |
| 46 | + |
| 47 | +First, import all required packages: |
| 48 | + |
| 49 | +```python |
| 50 | +import os |
| 51 | + |
| 52 | +from langchain_apify import ApifyActorsTool |
| 53 | +from langchain_core.messages import HumanMessage |
| 54 | +from langchain_openai import ChatOpenAI |
| 55 | +from langgraph.prebuilt import create_react_agent |
| 56 | +``` |
| 57 | + |
| 58 | +Next, set the environment variables for the Apify API token and OpenAI API key: |
| 59 | + |
| 60 | +```python |
| 61 | +os.environ["OPENAI_API_KEY"] = "Your OpenAI API key" |
| 62 | +os.environ["APIFY_API_TOKEN"] = "Your Apify API token" |
| 63 | +``` |
| 64 | + |
| 65 | +Instantiate LLM and Apify Actors tools: |
| 66 | + |
| 67 | +```python |
| 68 | +llm = ChatOpenAI(model="gpt-4o-mini") |
| 69 | + |
| 70 | +browser = ApifyActorsTool("apify/rag-web-browser") |
| 71 | +tiktok = ApifyActorsTool("clockworks/free-tiktok-scraper") |
| 72 | +``` |
| 73 | + |
| 74 | +Create the ReAct agent with the LLM and Apify Actors tools: |
| 75 | + |
| 76 | +```python |
| 77 | +tools = [browser, tiktok] |
| 78 | +agent_executor = create_react_agent(llm, tools) |
| 79 | +``` |
| 80 | + |
| 81 | +Finally, run the agent and stream the messages: |
| 82 | + |
| 83 | +```python |
| 84 | +for state in agent_executor.stream( |
| 85 | + stream_mode="values", |
| 86 | + input={ |
| 87 | + "messages": [ |
| 88 | + HumanMessage(content="Search the web for OpenAI TikTok profile and analyze their profile.") |
| 89 | + ] |
| 90 | + }): |
| 91 | + state["messages"][-1].pretty_print() |
| 92 | +``` |
| 93 | + |
| 94 | +:::note Search and analysis may take some time |
| 95 | + |
| 96 | +The agent tool call may take some time as it searches the web for OpenAI TikTok profiles and analyzes them. |
| 97 | + |
| 98 | +::: |
| 99 | + |
| 100 | +You will see the agent's messages in the console, which will show each step of the agent's workflow. |
| 101 | + |
| 102 | +```text |
| 103 | +================================ Human Message ================================= |
| 104 | +
|
| 105 | +Search the web for OpenAI TikTok profile and analyze their profile. |
| 106 | +================================== AI Message ================================== |
| 107 | +Tool Calls: |
| 108 | + apify_actor_apify_rag-web-browser (call_y2rbmQ6gYJYC2lHzWJAoKDaq) |
| 109 | + Call ID: call_y2rbmQ6gYJYC2lHzWJAoKDaq |
| 110 | + Args: |
| 111 | + run_input: {"query":"OpenAI TikTok profile","maxResults":1} |
| 112 | +
|
| 113 | +... |
| 114 | +
|
| 115 | +================================== AI Message ================================== |
| 116 | +
|
| 117 | +The OpenAI TikTok profile is titled "OpenAI (@openai) Official." Here are some key details about the profile: |
| 118 | +
|
| 119 | +- **Followers**: 592.3K |
| 120 | +- **Likes**: 3.3M |
| 121 | +- **Description**: The profile features "low key research previews" and includes videos that showcase their various projects and research developments. |
| 122 | +
|
| 123 | +### Profile Overview: |
| 124 | +- **Profile URL**: [OpenAI TikTok Profile](https://www.tiktok.com/@openai?lang=en) |
| 125 | +- **Content Focus**: The posts primarily involve previews of OpenAI's research and various AI-related innovations. |
| 126 | +
|
| 127 | +... |
| 128 | +
|
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +If you want to test the whole example, you can simply create a new file, `langgraph_integration.py`, and copy the whole code into it. |
| 133 | + |
| 134 | +```python |
| 135 | +import os |
| 136 | + |
| 137 | +from langchain_apify import ApifyActorsTool |
| 138 | +from langchain_core.messages import HumanMessage |
| 139 | +from langchain_openai import ChatOpenAI |
| 140 | +from langgraph.prebuilt import create_react_agent |
| 141 | + |
| 142 | +os.environ["OPENAI_API_KEY"] = "Your OpenAI API key" |
| 143 | +os.environ["APIFY_API_TOKEN"] = "Your Apify API token" |
| 144 | + |
| 145 | +llm = ChatOpenAI(model="gpt-4o-mini") |
| 146 | + |
| 147 | +browser = ApifyActorsTool("apify/rag-web-browser") |
| 148 | +tiktok = ApifyActorsTool("clockworks/free-tiktok-scraper") |
| 149 | + |
| 150 | +tools = [browser, tiktok] |
| 151 | +agent_executor = create_react_agent(llm, tools) |
| 152 | + |
| 153 | +for state in agent_executor.stream( |
| 154 | + stream_mode="values", |
| 155 | + input={ |
| 156 | + "messages": [ |
| 157 | + HumanMessage(content="Search the web for OpenAI TikTok profile and analyze their profile.") |
| 158 | + ] |
| 159 | + }): |
| 160 | + state["messages"][-1].pretty_print() |
| 161 | +``` |
| 162 | + |
| 163 | +## Resources |
| 164 | + |
| 165 | +- [Apify Actors](https://docs.apify.com/platform/actors) |
| 166 | +- [LangGraph - How to Create a ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) |
0 commit comments