Update README.md
This commit is contained in:
@@ -1,64 +1,61 @@
|
|||||||
# Self‑Correcting LangGraph Agent
|
# Self‑Correcting LangGraph Agent
|
||||||
|
|
||||||
This repository contains a minimal example of a **self‑correcting agent** built with
|
## Overview
|
||||||
[LangGraph](https://langchain-ai.github.io/langgraph/) and
|
|
||||||
[LangChain](https://langchain.com/). The agent:
|
|
||||||
|
|
||||||
1. **Receives a natural‑language task** from the user.
|
This project implements a **self‑correcting agent** using LangGraph. The agent
|
||||||
2. **Executes the task** via an *unreliable* tool that fails 30 % of the time.
|
performs a task, asks an LLM to judge the result, and retries automatically until
|
||||||
3. **Asks an LLM** (OpenAI GPT‑4o‑mini) to judge whether the result is correct.
|
the result is judged **success** or the maximum number of attempts is reached.
|
||||||
4. **Retries automatically** until the judge says *success* or the maximum number
|
|
||||||
of attempts is reached.
|
|
||||||
|
|
||||||
The code demonstrates how to build a small state machine with LangGraph, how to
|
The key components are:
|
||||||
use a LLM as a *judge*, and how to implement retry logic.
|
|
||||||
|
|
||||||
## Setup
|
| Component | Purpose |
|
||||||
|
|-----------|---------|
|
||||||
|
| `AgentState` | Typed state that tracks the task, result, attempts, status, error and max_attempts |
|
||||||
|
| `unreliable_tool` | Simulates a tool that fails 30 % of the time (used to demonstrate retry logic) |
|
||||||
|
| `verify_result` | LLM judge that must reply with the single word `success` or `failed` |
|
||||||
|
| `handle_error` | Resets the error and sets the status back to `pending` for a retry |
|
||||||
|
| `execute_task` | Runs the unreliable tool and updates the state |
|
||||||
|
| `create_agent` | Builds the LangGraph with the above nodes and a retry loop |
|
||||||
|
| `create_agent_executor` | Compiles the graph into a runnable executor |
|
||||||
|
| CLI | Run the agent from the command line: `python agent.py "2+2" --max 5` |
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
|
||||||
|
1. **Start** – The graph begins at `execute_task`.
|
||||||
|
2. **Execute** – The tool runs. If it throws an exception, the state status becomes `failed`.
|
||||||
|
3. **Check attempts** – If the number of attempts is >= `max_attempts`, the graph ends with status `max_attempts`.
|
||||||
|
4. **Verify** – The LLM judges the result. If the verdict is `success`, the graph ends. If `failed`, it goes to `handle_error`.
|
||||||
|
5. **Retry** – `handle_error` clears the error and sets status to `pending`, then the graph loops back to `execute_task`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Optional: create a virtual environment
|
|
||||||
python -m venv venv
|
|
||||||
source venv/bin/activate # Windows: venv\Scripts\activate
|
|
||||||
|
|
||||||
# Install dependencies
|
# Install dependencies
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
# Set your OpenAI API key
|
# Run the agent
|
||||||
export OPENAI_API_KEY=YOUR_KEY # Windows: set OPENAI_API_KEY=YOUR_KEY
|
python agent.py "2+2" --max 5
|
||||||
```
|
```
|
||||||
|
|
||||||
## Running the agent
|
The console will show the final state, e.g.:
|
||||||
|
|
||||||
```bash
|
|
||||||
python agent.py "Вычисли 2+2"
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also run the script without arguments – it will prompt you for a task.
|
|
||||||
|
|
||||||
## Example output
|
|
||||||
|
|
||||||
```
|
```
|
||||||
Введите задачу: 2+2
|
--- Final State ---
|
||||||
Попытка 1: результат Result of 2+2
|
result: 4
|
||||||
Попытка 2: результат Result of 2+2
|
attempts: 2
|
||||||
|
status: success
|
||||||
Итог:
|
error: None
|
||||||
Успех за 2 попыток. Результат: Result of 2+2
|
max_attempts: 5
|
||||||
```
|
```
|
||||||
|
|
||||||
The exact number of attempts may vary because the tool fails randomly.
|
## Requirements
|
||||||
|
|
||||||
---
|
```text
|
||||||
|
langchain>=1.0.0
|
||||||
|
langgraph>=1.0.0
|
||||||
|
langchain-openai>=1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
### How it works
|
## Note
|
||||||
|
|
||||||
- **State** – `AgentState` tracks the task, result, number of attempts, status and
|
The agent uses the OpenAI API. Make sure the environment variable `OPENAI_API_KEY` is set.
|
||||||
any error.
|
|
||||||
- **Nodes** – `execute_task`, `verify_result`, `handle_error`.
|
|
||||||
- **LLM judge** – a simple prompt that forces the model to answer only
|
|
||||||
"success" or "failed".
|
|
||||||
- **Graph** – a conditional router that loops back to `execute_task` on failure
|
|
||||||
until the maximum attempts are reached.
|
|
||||||
|
|
||||||
Feel free to adapt the tool, the judge prompt, or the retry policy to fit your
|
|
||||||
needs.
|
|
||||||
Reference in New Issue
Block a user