Use LangGraph with Arcade AI
In this guide, we'll walk through how to use Arcade AI tools with LangGraph to build powerful AI applications.
Prerequisites
-
Install the required packages:
pip install arcade-ai langgraph langchain-openai langchain-arcade
Import the necessary packages
Begin by importing the required libraries:
import os
from langchain_arcade import ArcadeToolManager
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
Set up API keys
Ensure your environment variables ARCADE_API_KEY
and OPENAI_API_KEY
are set with your actual API keys.
arcade_api_key = os.environ["ARCADE_API_KEY"]
openai_api_key = os.environ["OPENAI_API_KEY"]
Initialize the Arcade Tool Manager
The ArcadeToolManager
helps you fetch and manage tools from Arcade AI. Initialize it with your Arcade AI API key:
tool_manager = ArcadeToolManager(api_key=arcade_api_key)
Retrieve tools for LangGraph
Fetch the tools and wrap them as LangGraph tools by setting langgraph=True
:
tools = tool_manager.get_tools(langgraph=True)
Create the language model
Create an instance of the AI language model:
model = ChatOpenAI(model="gpt-4o", api_key=openai_api_key)
Initialize the agent with LangGraph
Initialize a prebuilt agent that can use tools in a ReAct-style LangGraph:
graph = create_react_agent(model, tools=tools)
Define the input message
Set up the initial input message from the user:
inputs = {
"messages": [HumanMessage(content="Star arcadeai/arcade-ai on GitHub!")],
}
Configure the agent and tools
Set the configuration parameters:
config = {
"configurable": {
"thread_id": "2",
"user_id": "[email protected]",
}
}
Execute the LangGraph and stream responses
Stream the assistant's responses by executing the graph:
for chunk in graph.stream(inputs, stream_mode="values", config=config):
# Access the latest message from the conversation
last_message = chunk["messages"][-1]
# Print the assistant's message content
print(last_message.content)