# Human‑in‑the‑Loop 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 - Python 3.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`, you’ll 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!