Files
dz/solutions/69a86305c46fd26feae6bcaa_Human-in-the-Loop_через_middleware/README.md
T

137 lines
3.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# HumanintheLoop Middleware Demo
A minimal LangChain project that demonstrates how to pause an agent before every tool call and let a user approve or reject the action via the terminal.
---
## Table of Contents
- [Project Overview](#project-overview)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Running the Demo](#running-the-demo)
- [1️⃣ `main.py` Interactive Agent](#1-mainpy---interactive-agent)
- [2️⃣ `tool_example.py` Custom Tool](#2-tool_exempley---custom-tool)
- [Example Interaction](#example-interaction)
- [Project Structure](#project-structure)
---
## Project Overview
The agent is created with **HumanInTheLoopMiddleware**.
When the agent wants to use a tool (e.g., `get_weather`), it pauses, prints a prompt in the terminal, and waits for user input:
| Decision | Effect |
|----------|--------|
| `approve` | Tool call proceeds |
| `reject` | Tool call is skipped; the agent continues reasoning |
| `edit` | (Optional) User can modify the tool arguments before resuming |
The middleware automatically handles the pause/resume logic via `Command(resume={"decisions": [...]})`.
---
## Prerequisites
- Python3.10+
- A working OpenAI API key (or any LLM provider supported by LangChain)
Set your key in an environment variable:
```bash
export OPENAI_API_KEY="sk-..."
```
---
## Installation
1. **Clone the repo**
```bash
git clone https://github.com/yourusername/human-in-loop-demo.git
cd human-in-loop-demo
```
2. **Create a virtual environment (optional but recommended)**
```bash
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
```
3. **Install dependencies**
```bash
pip install -r requirements.txt
```
> `requirements.txt` contains:
> ```
> langchain==0.2.*
> langgraph==0.1.*
> python-dotenv # optional, for .env support
> ```
---
## Running the Demo
### 1️⃣ `main.py` Interactive Agent
```bash
python main.py
```
You will see a prompt like:
```
Agent: What would you like to know?
User: Tell me the weather in London.
Agent: (pausing) Would you like to call tool 'get_weather' with arguments {'location': 'London'}? [approve/reject/edit]
>
```
Type `approve`, `reject`, or `edit` and press **Enter**.
If you choose `edit`, youll be prompted to modify the JSON arguments.
### 2️⃣ `tool_example.py` Custom Tool
The demo includes a simple weather tool (`get_weather`).
You can add more tools by editing `tools/__init__.py` or creating new modules.
---
## Example Interaction
```
$ python main.py
Agent: Hi! How can I help you today?
User: What's the weather in Paris?
Agent: (pausing) Would you like to call tool 'get_weather' with arguments {'location': 'Paris'}? [approve/reject/edit]
> approve
Tool get_weather called. Result: "Sunny, 22°C"
Agent: The current weather in Paris is Sunny, 22°C.
User: Thanks!
```
If you type `reject`, the agent will continue without calling the tool:
```
Agent: (pausing) Would you like to call tool 'get_weather' with arguments {'location': 'Paris'}? [approve/reject/edit]
> reject
Agent: I couldn't retrieve the weather. Could you provide more details?
```
---
## Project Structure
```
human-in-loop-demo/
├── main.py # Entry point runs the interactive agent
├── tool_example.py # Example custom tool (get_weather)
├── requirements.txt # Dependencies
└── README.md # This file
```
Feel free to extend the project by adding new tools, customizing prompts, or integrating a different LLM provider. Happy hacking!