Files
task-69a86305-human-in-the-…/README.md
T

86 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
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 Agent with LangGraph
This repository contains a minimal example of a **HumanintheLoop** (HITL) agent built on top of [LangGraph](https://github.com/langchain-ai/langgraph).
The agent pauses whenever it needs to call an external tool, presents the tool output to the user and waits for a decision (`approve` or `reject`). After the decision is made the conversation continues automatically.
> **⚠️ Prerequisites** The example uses OpenAIcompatible APIs.
> Make sure you have an API key set in the environment variable `OPENAI_API_KEY`.
---
## 📦 Installation
```bash
# 1️⃣ Clone the repo (or copy solution.py into a new folder)
git clone https://github.com/your-username/hitl-langgraph.git
cd hitl-langgraph
# 2️⃣ Create and activate a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .\.venv\Scripts\activate
# 3️⃣ Install dependencies
pip install --upgrade pip
pip install langchain-openai langgraph langchain-tools
```
> **Tip** If you want to use a different LLM (e.g., Anthropic, Gemini), replace the `ChatOpenAI` import with the appropriate wrapper and adjust the model name.
---
## 🚀 Running the Agent
The main logic is in `solution.py`.
Run it directly:
```bash
python solution.py
```
### What Happens?
1. The agent starts a conversation.
2. When it decides to call the `get_weather` tool, the execution pauses.
3. The tool output (e.g., `"Погода в Москва на 2024-05-28: солнечно 25°C."`) is printed to the console.
4. You are prompted to type **approve** or **reject**:
- `approve`: the agent resumes with the tool result as normal input.
- `reject`: the agent receives a `ToolMessage` indicating rejection and can decide what to do next.
---
## 🔧 Example Interaction
```text
$ python solution.py
Agent: Какую погоду вы хотите узнать?
User: Москва на 2024-05-28
Agent (calling tool): get_weather(city='Москва', date='2024-05-28')
Tool output: Погода в Москва на 2024-05-28: солнечно 25°C.
Please type 'approve' or 'reject': approve
Agent: Спасибо! Как ещё могу помочь?
```
If you type `reject`, the agent will receive a rejection message and can, for example, ask for clarification.
---
## 📁 Project Structure
| File | Purpose |
|------|---------|
| `solution.py` | Full implementation of the HITL agent. |
| `README.md` | This documentation file. |
---
## 🛠️ Customization
- **Add more tools** Decorate any function with `@tool` and add it to the `tools=[...]` list.
- **Change prompt** Edit `system_prompt` in `create_react_agent`.
- **Persist memory** Replace `MemorySaver()` with a filebased or database checkpoint if you need persistence across runs.
---
## 📜 License
MIT © 2026. Feel free to fork and adapt for your own projects!