Use LangChain with Arcade
In this guide, we'll walk through how to use Arcade AI tools with LangChain to build powerful AI applications.
Prerequisites
-
Install the required packages:
pip install arcade-ai langchain-openai langchain-arcade
Import the necessary packages
Begin by importing the required libraries:
import os
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_arcade import ArcadeToolManager
from langchain_openai import ChatOpenAI
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:
manager = ArcadeToolManager(api_key=arcade_api_key)
Retrieve specific tools
Fetch specific tools by specifying their names. Tool names follow the format ToolkitName.ToolName
:
tools = manager.get_tools(tools=["Web.ScrapeUrl"])
print(manager.tools)
You may notice that "" and "." are used interchangeably in the tool names. This is due to the fact that some language models do not allow "." in tool names. Given this, Arcade allows you to use either "" or "." in tool names interchangeably.
Output:
['Web_ScrapeUrl']
This retrieves the ScrapeUrl
tool from the Web
toolkit.
Retrieve a Toolkit
You can initialize all tools from a specific toolkit:
# Clear existing tools and initialize new ones from the "Search" toolkit
manager.init_tools(toolkits=["Search"])
print(manager.tools)
Output:
['Search_SearchGoogle']
This replaces the current tools with those from the Search
toolkit.
Add more tools from other toolkits
Use get_tools
to add additional tools without clearing existing ones:
# Add tools from the "Math" toolkit
tools = manager.get_tools(toolkits=["Math"])
print(manager.tools)
Output:
['Search_SearchGoogle', 'Math_Add', 'Math_Divide', 'Math_Multiply', 'Math_Sqrt', 'Math_Subtract', 'Math_SumList', 'Math_SumRange']
Now you have several mathematical tools alongside your search tool.
Create the LLM and Agent
Define your agent and wrap it with an executor for processing:
prompt = hub.pull("hwchase17/openai-functions-agent")
llm = ChatOpenAI(api_key=openai_api_key)
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
Execute the agent with example queries
Try out your agent with sample inputs:
agent_executor.invoke({"input": "Lookup Seymour Cray on Google"})
Output:
> Entering new AgentExecutor chain...
Invoking: `Search_SearchGoogle` with `{'query': 'Seymour Cray'}`
[Search results about Seymour Cray]
> Finished chain.
agent_executor.invoke({"input": "What is 1234567890 * 9876543210?"})
Output:
> Entering new AgentExecutor chain...
Invoking: `Math_Multiply` with `{'a': 1234567890, 'b': 9876543210}`
12193263111263526900
> Finished chain.
Tips for Selecting Tools
- Relevance: Include only the tools necessary for your agent's tasks to optimize performance.
- Avoid Conflicts: Be cautious of tools with overlapping functionalities that might cause ambiguity.
Next Steps
Now that you've set up an agent with Arcade AI tools, you can:
- Explore more complex queries and tasks.
- Integrate additional toolkits to expand capabilities.
- Customize the agent's prompt for specific behaviors.
- Experiment with different language models for varied performance.