-
Notifications
You must be signed in to change notification settings - Fork 135
feat: update langchain apify #1441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bb8074b
update langchain rag docs
MQ37 939b472
add langgraph docs page
MQ37 630bd02
library -> framework
MQ37 b6b9ded
fix emojis
MQ37 3a5d78d
convert Prerequisites to unordered list
MQ37 1aca6c7
fix lint
MQ37 4ff7ed3
Add link to haystack example
jirispilka d4cb208
Update sources/platform/integrations/ai/langgraph.md
MQ37 12f43d9
Update sources/platform/integrations/ai/langgraph.md
MQ37 c120b16
Update sources/platform/integrations/ai/langgraph.md
MQ37 acd9787
Update sources/platform/integrations/ai/langgraph.md
MQ37 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| --- | ||
| title: 🦜🔘➡️ LangGraph integration | ||
| sidebar_label: LangGraph | ||
| description: Learn how to build AI Agents with Apify and LangGraph 🦜🔘➡️. | ||
| sidebar_position: 1 | ||
| slug: /integrations/langgraph | ||
| --- | ||
|
|
||
| **Learn how to build AI Agents with Apify and LangGraph.** | ||
|
|
||
| --- | ||
|
|
||
| ## What is LangGraph | ||
|
|
||
| [LangGraph](https://www.langchain.com/langgraph) is a framework designed for constructing stateful, multi-agent applications with Large Language Models (LLMs), allowing developers to build complex AI agent workflows that can leverage tools, APIs, and databases. | ||
|
|
||
| :::note Explore LangGraph | ||
|
|
||
| For more in-depth details on LangGraph, check out its [official documentation](https://langchain-ai.github.io/langgraph/). | ||
|
|
||
| ::: | ||
|
|
||
| ## How to use Apify with LangGraph | ||
|
|
||
| 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. | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - **Apify API token**: 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). | ||
|
|
||
| - **OpenAI API key**: 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). | ||
|
|
||
| - **Python packages**: You need to install the following Python packages: | ||
|
|
||
| ```bash | ||
| pip install langgraph langchain-apify langchain-openai | ||
| ``` | ||
|
|
||
| ### Building the TikTok profile search and analysis agent | ||
|
|
||
| First, import all required packages: | ||
|
|
||
| ```python | ||
| import os | ||
|
|
||
| from langchain_apify import ApifyActorsTool | ||
| from langchain_core.messages import HumanMessage | ||
| from langchain_openai import ChatOpenAI | ||
| from langgraph.prebuilt import create_react_agent | ||
| ``` | ||
|
|
||
| Next, set the environment variables for the Apify API token and OpenAI API key: | ||
|
|
||
| ```python | ||
| os.environ["OPENAI_API_KEY"] = "Your OpenAI API key" | ||
| os.environ["APIFY_API_TOKEN"] = "Your Apify API token" | ||
| ``` | ||
|
|
||
| Instantiate LLM and Apify Actors tools: | ||
|
|
||
| ```python | ||
| llm = ChatOpenAI(model="gpt-4o-mini") | ||
|
|
||
| browser = ApifyActorsTool("apify/rag-web-browser") | ||
| tiktok = ApifyActorsTool("clockworks/free-tiktok-scraper") | ||
| ``` | ||
|
|
||
| Create the ReAct agent with the LLM and Apify Actors tools: | ||
|
|
||
| ```python | ||
| tools = [browser, tiktok] | ||
| agent_executor = create_react_agent(llm, tools) | ||
| ``` | ||
|
|
||
| Finally, run the agent and stream the messages: | ||
|
|
||
| ```python | ||
| for state in agent_executor.stream( | ||
| stream_mode="values", | ||
| input={ | ||
| "messages": [ | ||
| HumanMessage(content="Search the web for OpenAI TikTok profile and analyze their profile.") | ||
| ] | ||
| }): | ||
| state["messages"][-1].pretty_print() | ||
| ``` | ||
|
|
||
| :::note Search and analysis may take some time | ||
|
|
||
| The agent tool call may take some time as it searches the web for OpenAI TikTok profiles and analyzes them. | ||
|
|
||
| ::: | ||
|
|
||
| You will see the agent's messages in the console, which will show each step of the agent's workflow. | ||
|
|
||
| ```text | ||
| ================================ Human Message ================================= | ||
|
|
||
| Search the web for OpenAI TikTok profile and analyze their profile. | ||
| ================================== AI Message ================================== | ||
| Tool Calls: | ||
| apify_actor_apify_rag-web-browser (call_y2rbmQ6gYJYC2lHzWJAoKDaq) | ||
| Call ID: call_y2rbmQ6gYJYC2lHzWJAoKDaq | ||
| Args: | ||
| run_input: {"query":"OpenAI TikTok profile","maxResults":1} | ||
|
|
||
| ... | ||
|
|
||
| ================================== AI Message ================================== | ||
|
|
||
| The OpenAI TikTok profile is titled "OpenAI (@openai) Official." Here are some key details about the profile: | ||
|
|
||
| - **Followers**: 592.3K | ||
| - **Likes**: 3.3M | ||
| - **Description**: The profile features "low key research previews" and includes videos that showcase their various projects and research developments. | ||
|
|
||
| ### Profile Overview: | ||
| - **Profile URL**: [OpenAI TikTok Profile](https://www.tiktok.com/@openai?lang=en) | ||
| - **Content Focus**: The posts primarily involve previews of OpenAI's research and various AI-related innovations. | ||
|
|
||
| ... | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| 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. | ||
|
|
||
| ```python | ||
| import os | ||
|
|
||
| from langchain_apify import ApifyActorsTool | ||
| from langchain_core.messages import HumanMessage | ||
| from langchain_openai import ChatOpenAI | ||
| from langgraph.prebuilt import create_react_agent | ||
|
|
||
| os.environ["OPENAI_API_KEY"] = "Your OpenAI API key" | ||
| os.environ["APIFY_API_TOKEN"] = "Your Apify API token" | ||
|
|
||
| llm = ChatOpenAI(model="gpt-4o-mini") | ||
|
|
||
| browser = ApifyActorsTool("apify/rag-web-browser") | ||
| tiktok = ApifyActorsTool("clockworks/free-tiktok-scraper") | ||
|
|
||
| tools = [browser, tiktok] | ||
| agent_executor = create_react_agent(llm, tools) | ||
|
|
||
| for state in agent_executor.stream( | ||
| stream_mode="values", | ||
| input={ | ||
| "messages": [ | ||
| HumanMessage(content="Search the web for OpenAI TikTok profile and analyze their profile.") | ||
| ] | ||
| }): | ||
| state["messages"][-1].pretty_print() | ||
| ``` | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Apify Actors](https://docs.apify.com/platform/actors) | ||
| - [LangGraph - How to Create a ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.