67 lines
2.2 KiB
Markdown
67 lines
2.2 KiB
Markdown
# LangGraph Human-in-the-loop Demo
|
||
|
||
This project demonstrates how to create a LangGraph that pauses execution to ask the user for confirmation via a custom interrupt. The user is prompted in the console using `questionary`, and the graph resumes once the user provides an answer.
|
||
|
||
## Features
|
||
|
||
- **Custom interrupt node**: Triggers a pause and sends a structured payload (type, question, options).
|
||
- **Human-in-the-loop**: The graph waits for user input before continuing.
|
||
- **State persistence**: Uses an in-memory checkpoint to resume execution after the interrupt.
|
||
- **Simple console UI**: Uses `questionary` for interactive prompts.
|
||
|
||
## Requirements
|
||
|
||
- Python 3.10 or newer
|
||
- `langgraph`
|
||
- `questionary`
|
||
|
||
Install the dependencies with:
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
## Running the Demo
|
||
|
||
```bash
|
||
python src/main.py
|
||
```
|
||
|
||
You will see a prompt:
|
||
|
||
```
|
||
Уверены, что хотите продолжить?
|
||
❯ approve
|
||
❯ reject
|
||
```
|
||
|
||
Select an option. After you choose, the script will print the final state, which includes the user's answer.
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
├── src/
|
||
│ └── main.py # Main script with graph definition and run loop
|
||
├── requirements.txt # Python dependencies
|
||
└── README.md # This file
|
||
```
|
||
|
||
## How It Works
|
||
|
||
1. **Graph Definition**
|
||
The graph has a single node `ask_user`.
|
||
- On first run, it triggers an interrupt with a payload containing a question and options.
|
||
- After the user responds, the node receives the answer via the `resume` parameter and stores it in the state.
|
||
|
||
2. **Interrupt Handling**
|
||
The main loop listens for chunks from `graph.stream()`.
|
||
- When an interrupt chunk is detected (`"__interrupt__"` key), it displays the question using `questionary.select`.
|
||
- The chosen answer is sent back to the graph via `Command(resume=answer)`.
|
||
|
||
3. **Resuming Execution**
|
||
The graph resumes from the same node, now with the answer available in the state.
|
||
The final state is printed to the console.
|
||
|
||
Feel free to extend the graph with more nodes or integrate it with other LangChain components.
|
||
|
||
Enjoy experimenting with LangGraph and human-in-the-loop workflows! |