Human-in-the-loop

Open in Colabarrow-up-rightOpen in GitHubarrow-up-right

Overview

This tutorial covers Human in the loop .

Agents are not always reliable, and human intervention may be required to ensure tasks are executed successfully.

In some cases, you might want human approval before proceeding to confirm that everything is functioning as intended. LangGraph supports these Human-in-the-loop workflows.

In this tutorial, you'll learn how to use LangGraph's interrupt_before feature to pause execution, enabling human oversight and control.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setuparrow-up-right for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorialarrow-up-right for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

Function Setup

Convenient functions for smooth execution.

Graph Setup

This setup initializes a graph-based workflow for a chatbot, defining the state, tools, and nodes while establishing edges and a memory saver for persistent interactions.

We compile the graph with a checkpointer.

Let's visualize the graph!

png

Adding Human Feedback

Let's now perform Human-in-the-loop !

The chatbot invoked the tool, but its execution was interrupted.

Let's check the graph state!

The reason for the interruption is that we set tools in interrupt_before while streaming the graph, causing the process to stop before executing the tools node. As a result, the next node ( .next ) becomes tools .

Additionally, in the previous tutorial , .next was empty because the process had reached the final END node. However, with the interrupt set, .next is now set to tools .

Now, let's check the last message in the snapshot!

Next, let's resume the graph from the point where it was interrupted.

LangGraph makes it easy to resume graph execution.

Simply pass None as the input .

We used interrupt to enable human intervention in the chatbot's execution.

Additionally, with the inclusion of a checkpointer , the graph can resume execution even after being indefinitely paused.

Below is how you can use get_state_history to retrieve the state history.

By specifying the desired state from the history, you can restart execution from that point.

It is important to note that the checkpointer saves data at every step of the graph!

The desired point is stored in to_replay . This allows you to specify the starting point for resuming execution.

The checkpoint_id is stored in to_replay.config .

Using this checkpoint_id , LangGraph's checkpointer can load the state at that specific point.

Note that the input must be set to None in this case.

Last updated