57 lines
2.3 KiB
Markdown
57 lines
2.3 KiB
Markdown
# Human‑in‑the‑loop (interrupt / resume) – LangGraph example
|
||
|
||
## What this project does
|
||
|
||
This repository contains a minimal, self‑contained example that demonstrates how to use **LangGraph**’s custom interrupt mechanism for *Human‑in‑the‑loop* (HITL). The graph pauses at a node, asks the user for confirmation via an interactive console prompt, and then resumes execution with the chosen answer.
|
||
|
||
The key concepts shown are:
|
||
|
||
1. **State definition** – a `TypedDict` that holds data produced by the user.
|
||
2. **Interrupt node** – uses `interrupt(payload)` to pause the graph.
|
||
3. **Resume handling** – the main loop detects `__interrupt__`, shows a question, collects an answer and sends it back with `Command(resume=payload)`.
|
||
4. **Checkpointing** – `InMemorySaver` keeps the graph state so that resumption works correctly.
|
||
|
||
## File structure
|
||
|
||
| File | Purpose |
|
||
|------|---------|
|
||
| `requirements.txt` | Python dependencies (langgraph, questionary, python‑dotenv) |
|
||
| `main.py` | Entry point – builds and runs the graph, handles interrupts |
|
||
| `README.md` | Project description and usage instructions |
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# Create a virtual environment (optional but recommended)
|
||
python -m venv .venv
|
||
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
|
||
|
||
# Install dependencies
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
## Running the example
|
||
|
||
```bash
|
||
python main.py
|
||
```
|
||
|
||
You will see a prompt asking whether you want to continue. Choose an option and the graph will resume, printing the final state.
|
||
|
||
## How it works (high‑level)
|
||
|
||
1. **Graph construction** – two nodes: `ask_user` (interrupt) and `process_answer` (continues after resume).
|
||
2. **Interrupt payload** – a dictionary with `type`, `question`, and `options`.
|
||
3. **Main loop** – streams the graph, intercepts `__interrupt__`, uses `questionary.select` to get user input, then resumes the graph with the answer attached.
|
||
4. **Final state** – after completion, the final state is printed showing the chosen value.
|
||
|
||
## Extending the example
|
||
|
||
- Replace the console prompt with a web UI or chatbot interface.
|
||
- Add more nodes before/after the interrupt to build richer workflows.
|
||
- Persist checkpoints to disk using `FileSaver` if you need long‑term state.
|
||
|
||
---
|
||
|
||
**Author:** Kirill Kutlakhmetov
|