CH06. Human-in-the-loop (human intervention)
Agents may be unreliable and may require human input to successfully perform tasks.
Similarly, for some tasks, you may want to require human intervention and “approval” before execution to ensure that everything is running as intended.
LangGraph supports human-in-the-loop workflows in several ways.
We'll start this tutorial by using LangGraph's interrupt_before function to always interrupt the tool node.
Copy
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv
# Load API key information
load_dotenv()Copy
TrueCopy
# LangSmith set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH17-LangGraph-Modules")Copy
Copy
Now compile the graph and specify interrupt_before before the tools node.
Copy
Copy
Copy
Copy
Let's check the graph status to make sure it's working properly.
Copy
Copy
(In the previous tutorial) .next did not exist because END was reached.
However, now .next is designated as tools.
Now let's check the tool call.
Copy
Copy
Next, we will continue the graph from where it left off.
LangGraph makes it easy to continue traversing a graph.
Just pass None as input.
Copy
Copy
Now, we have added human-intervention-capable execution to our chatbot using interrupts, allowing for human supervision and intervention when needed. This could potentially provide a UI for our system later on.
Since we have already added a checkpointer, the graph will be paused indefinitely and can be resumed at any time. Below is how to get the state history using the get_state_history method.
You can specify a desired state through the state history and start over from that point.
Copy
Copy
It is important to note that a checkpoint is saved for every step of the graph.
The desired point is stored in the to_replay variable. This can be used to specify a point from which to restart.
Copy
Copy
to_replay.config contains checkpoint_id.
Copy
Copy
Providing this checkpoint_id value allows LangGraph's checkpointer to load the state at that point in time.
However, in this case, the input value must be passed as None.
Let's check it out with the example below.
Copy
Copy
Last updated