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

3.5 KiB
Raw Blame History

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

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:

export OPENAI_API_KEY="sk-..."

Installation

  1. Clone the repo

    git clone https://github.com/yourusername/human-in-loop-demo.git
    cd human-in-loop-demo
    
  2. Create a virtual environment (optional but recommended)

    python -m venv .venv
    source .venv/bin/activate  # Windows: .venv\Scripts\activate
    
  3. Install dependencies

    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

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!