From 778b7af83fecd9ccbcdc4254412d6b70f396fc15 Mon Sep 17 00:00:00 2001 From: Sam Partee Date: Sun, 26 Jan 2025 23:38:59 -0800 Subject: [PATCH] Update Langgraph examples (#231) Clean up of the Langgraph examples that are used for the documentation. Mostly just a restructuring for the update to ``langchain_arcade==1.0.0`` --- ...tools.py => langchain_tool_arcade_auth.py} | 0 .../langchain/langgraph_arcade_minimal.py | 50 +++++++++++++++++ .../langchain/langgraph_use_cached_auth.py | 53 ------------------- examples/langchain/simple_chain.py | 37 ------------- 4 files changed, 50 insertions(+), 90 deletions(-) rename examples/langchain/{authorizing_langchain_tools.py => langchain_tool_arcade_auth.py} (100%) create mode 100644 examples/langchain/langgraph_arcade_minimal.py delete mode 100644 examples/langchain/langgraph_use_cached_auth.py delete mode 100644 examples/langchain/simple_chain.py diff --git a/examples/langchain/authorizing_langchain_tools.py b/examples/langchain/langchain_tool_arcade_auth.py similarity index 100% rename from examples/langchain/authorizing_langchain_tools.py rename to examples/langchain/langchain_tool_arcade_auth.py diff --git a/examples/langchain/langgraph_arcade_minimal.py b/examples/langchain/langgraph_arcade_minimal.py new file mode 100644 index 00000000..940d28e3 --- /dev/null +++ b/examples/langchain/langgraph_arcade_minimal.py @@ -0,0 +1,50 @@ +import os + +from langchain_arcade import ArcadeToolManager +from langchain_openai import ChatOpenAI +from langgraph.checkpoint.memory import MemorySaver +from langgraph.errors import NodeInterrupt +from langgraph.prebuilt import create_react_agent + +# 1) Set API keys (place your real keys in env variables or directly below) +arcade_api_key = os.environ.get("ARCADE_API_KEY", "YOUR_ARCADE_API_KEY") +openai_api_key = os.environ.get("OPENAI_API_KEY", "YOUR_OPENAI_API_KEY") + +# 2) Create an ArcadeToolManager and fetch tools from the "Google" toolkit. +manager = ArcadeToolManager(api_key=arcade_api_key) + +# Tool names follow the format "ToolkitName.ToolName" +tools = manager.get_tools(tools=["Web.ScrapeUrl"]) +print(manager.tools) + +# Get all tools from a toolkit +tools = manager.get_tools(toolkits=["Google"]) +print(manager.tools) + +# 3) Create a ChatOpenAI model and bind the Arcade tools. +model = ChatOpenAI(model="gpt-4o", api_key=openai_api_key) +bound_model = model.bind_tools(tools) + +# 4) Use MemorySaver for checkpointing. +memory = MemorySaver() + +# 5) Create a ReAct-style agent from the prebuilt function. +graph = create_react_agent(model=bound_model, tools=tools, checkpointer=memory) + +# 6) Provide basic config and a user query. +# Note: user_id is required for the tool to be authorized +config = {"configurable": {"thread_id": "1", "user_id": "user@example.coom"}} +user_input = {"messages": [("user", "List any new and important emails in my inbox.")]} + +# 7) Stream the agent's output. If the tool is unauthorized, it may trigger NodeInterrupt. +try: + for chunk in graph.stream(user_input, config, stream_mode="values"): + chunk["messages"][-1].pretty_print() +except NodeInterrupt as exc: + print(f"\nNodeInterrupt occurred: {exc}") + print("Please authorize the tool or update the request, then re-run.") + +# If you need to authorize, you can do so via: +# auth_res = manager.authorize("Google_ListEmails", user_id="someone@example.com") +# manager.wait_for_auth(auth_res.id) +# Then run the graph again or edit the final tool call and call graph.stream(None, config). diff --git a/examples/langchain/langgraph_use_cached_auth.py b/examples/langchain/langgraph_use_cached_auth.py deleted file mode 100644 index 0c0371b0..00000000 --- a/examples/langchain/langgraph_use_cached_auth.py +++ /dev/null @@ -1,53 +0,0 @@ -import os - -# Import necessary modules and classes -from langchain_arcade import ArcadeToolManager -from langchain_core.messages import HumanMessage -from langchain_openai import ChatOpenAI -from langgraph.prebuilt import create_react_agent - -""" -Example showing how to use pre-auth'd tokens for tools -this will not wait for the user to authorize the tool -if the tool is not authorized, it will return an error - -to have the user authorize the tool, you can see the -example in langgraph_with_user_auth.py -""" - - -arcade_api_key = os.environ["ARCADE_API_KEY"] -openai_api_key = os.environ["OPENAI_API_KEY"] - -# Initialize the tool manager that fetches -# tools from arcade and wraps them as langgraph tools -tool_manager = ArcadeToolManager(api_key=arcade_api_key) -tools = tool_manager.get_tools() - -# Create an instance of the AI language model -model = ChatOpenAI(model="gpt-4o", api_key=openai_api_key) - -# Init a prebuilt agent that can use tools -# in a REACT style langgraph -graph = create_react_agent(model, tools=tools) - -# Define the initial input message from the user -inputs = { - "messages": [HumanMessage(content="Check and see if I have any important emails in my inbox")], -} - -# Configuration parameters for the agent and tools -config = { - "configurable": { - "thread_id": "2", - "user_id": "user@example.com", - } -} - -# 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 - if last_message.content: - print(last_message.content) diff --git a/examples/langchain/simple_chain.py b/examples/langchain/simple_chain.py deleted file mode 100644 index f258fc94..00000000 --- a/examples/langchain/simple_chain.py +++ /dev/null @@ -1,37 +0,0 @@ -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 - -arcade_api_key = os.environ["ARCADE_API_KEY"] -openai_api_key = os.environ["OPENAI_API_KEY"] - -# Pull relevant agent model. -prompt = hub.pull("hwchase17/openai-functions-agent") - -# Get all the tools available in Arcade -manager = ArcadeToolManager(api_key=arcade_api_key) - -# Tool names follow the format "ToolkitName.ToolName" -tools = manager.get_tools(tools=["Web.ScrapeUrl"]) -print(manager.tools) - -# clear and init new tools from a toolkit -manager.init_tools(toolkits=["Search"]) -print(manager.tools) -# get more tools -tools = manager.get_tools(toolkits=["Math"]) -print(manager.tools) - -# init the LLM -llm = ChatOpenAI(api_key=openai_api_key) - -# Define agent -agent = create_openai_functions_agent(llm, tools, prompt) -agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) - -# Try a few examples -agent_executor.invoke({"input": "Lookup Seymour Cray on Google"}) -agent_executor.invoke({"input": "What is 1234567890 * 9876543210?"})