How to add human-in-the-loop processes to the prebuilt ReAct agent¶
Prerequisites
This guide assumes familiarity with the following:
This guide will show how to add human-in-the-loop processes to the prebuilt ReAct agent. Please see this tutorial for how to get started with the prebuilt ReAct agent
You can add a a breakpoint before tools are called by passing interrupt_before=["tools"]
to create_react_agent
. Note that you need to be using a checkpointer for this to work.
Setup¶
First, let's install the required packages and set our API keys
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("OPENAI_API_KEY")
Set up LangSmith for LangGraph development
Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here.
Code¶
# First we initialize the model we want to use.
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o", temperature=0)
# For this tutorial we will use custom tool that returns pre-defined values for weather in two cities (NYC & SF)
from typing import Literal
from langchain_core.tools import tool
@tool
def get_weather(location: str):
"""Use this to get weather information from a given location."""
if location.lower() in ["nyc", "new york"]:
return "It might be cloudy in nyc"
elif location.lower() in ["sf", "san francisco"]:
return "It's always sunny in sf"
else:
raise AssertionError("Unknown Location")
tools = [get_weather]
# We need a checkpointer to enable human-in-the-loop patterns
from langgraph.checkpoint.memory import MemorySaver
memory = MemorySaver()
# Define the graph
from langgraph.prebuilt import create_react_agent
graph = create_react_agent(
model, tools=tools, interrupt_before=["tools"], checkpointer=memory
)
Usage¶
def print_stream(stream):
"""A utility to pretty print the stream."""
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
from langchain_core.messages import HumanMessage
config = {"configurable": {"thread_id": "42"}}
inputs = {"messages": [("user", "what is the weather in SF, CA?")]}
print_stream(graph.stream(inputs, config, stream_mode="values"))
================================[1m Human Message [0m=================================
what is the weather in SF, CA?
==================================[1m Ai Message [0m==================================
Tool Calls:
get_weather (call_YjOKDkgMGgUZUpKIasYk1AdK)
Call ID: call_YjOKDkgMGgUZUpKIasYk1AdK
Args:
location: SF, CA
We can verify that our graph stopped at the right place:
Now we can either approve or edit the tool call before proceeding to the next node. If we wanted to approve the tool call, we would simply continue streaming the graph withNone
input. If we wanted to edit the tool call we need to update the state to have the correct tool call, and then after the update has been applied we can continue.
We can try resuming and we will see an error arise:
==================================[1m Ai Message [0m==================================
Tool Calls:
get_weather (call_YjOKDkgMGgUZUpKIasYk1AdK)
Call ID: call_YjOKDkgMGgUZUpKIasYk1AdK
Args:
location: SF, CA
=================================[1m Tool Message [0m=================================
Name: get_weather
Error: AssertionError('Unknown Location')
Please fix your mistakes.
==================================[1m Ai Message [0m==================================
Tool Calls:
get_weather (call_CLu9ofeBhtWF2oheBspxXkfE)
Call ID: call_CLu9ofeBhtWF2oheBspxXkfE
Args:
location: San Francisco, CA
Let's show how we would edit the tool call to search for "San Francisco" instead of "San Francisco, CA" - since our tool as written treats "San Francisco, CA" as an unknown location. We will update the state and then resume streaming the graph and should see no errors arise:
state = graph.get_state(config)
last_message = state.values["messages"][-1]
last_message.tool_calls[0]["args"] = {"location": "San Francisco"}
graph.update_state(config, {"messages": [last_message]})
{'configurable': {'thread_id': '42',
'checkpoint_ns': '',
'checkpoint_id': '1ef801d1-5b93-6bb9-8004-a088af1f9cec'}}
==================================[1m Ai Message [0m==================================
Tool Calls:
get_weather (call_CLu9ofeBhtWF2oheBspxXkfE)
Call ID: call_CLu9ofeBhtWF2oheBspxXkfE
Args:
location: San Francisco
=================================[1m Tool Message [0m=================================
Name: get_weather
It's always sunny in sf
==================================[1m Ai Message [0m==================================
The weather in San Francisco is currently sunny.