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``
This commit is contained in:
parent
7960158ee8
commit
778b7af83f
4 changed files with 50 additions and 90 deletions
50
examples/langchain/langgraph_arcade_minimal.py
Normal file
50
examples/langchain/langgraph_arcade_minimal.py
Normal file
|
|
@ -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).
|
||||||
|
|
@ -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)
|
|
||||||
|
|
@ -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?"})
|
|
||||||
Loading…
Reference in a new issue