This tutorial builds on Add Memory.
interrupt
function. Calling interrupt
inside a node will pause execution. Execution can be resumed, together with new input from a human, by passing in a Command. interrupt
is ergonomically similar to Python’s built-in input()
, with some caveats.
1. Add the human_assistance
tool
Starting with the existing code from the Add memory to the chatbot tutorial, add the human_assistance
tool to the chatbot. This tool uses interrupt
to receive information from a human.
Let’s first select a chat model:
- OpenAI
- Anthropic
- Azure
- Google Gemini
- AWS Bedrock
StateGraph
with an additional tool:
For more information and examples of human-in-the-loop workflows, see Human-in-the-loop.
2. Compile the graph
We compile the graph with a checkpointer, as before:3. Visualize the graph
Visualizing the graph, you get the same layout as before – just with the added tool!
4. Prompt the chatbot
Now, prompt the chatbot with a question that will engage the newhuman_assistance
tool:
Additional information
Take a closer look at the Similar to Python’s built-in
human_assistance
tool:input()
function, calling interrupt
inside the tool will pause execution. Progress is persisted based on the checkpointer; so if it is persisting with Postgres, it can resume at any time as long as the database is alive. In this example, it is persisting with the in-memory checkpointer and can resume any time if the Python kernel is running.5. Resume execution
To resume execution, pass aCommand
object containing data expected by the tool. The format of this data can be customized based on needs. For this example, use a dict with a key "data"
:
interrupt
to add human-in-the-loop execution to your chatbot, allowing for human oversight and intervention when needed. This opens up the potential UIs you can create with your AI systems. Since you have already added a checkpointer, as long as the underlying persistence layer is running, the graph can be paused indefinitely and resumed at any time as if nothing had happened.
Check out the code snippet below to review the graph from this tutorial:
- OpenAI
- Anthropic
- Azure
- Google Gemini
- AWS Bedrock