From 9e331494a4b0b51e94dca62904fc1abf3f3603bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Thu, 4 Jun 2026 23:11:01 +0000 Subject: [PATCH] Update README.md --- README.md | 77 ++++++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 6629043..3cb02bb 100644 --- a/README.md +++ b/README.md @@ -1,61 +1,50 @@ # Self‑Correcting LangGraph Agent -## Overview +This repository contains a minimal implementation of a **self‑correcting agent** using LangGraph. The agent: -This project implements a **self‑correcting agent** using LangGraph. The agent -performs a task, asks an LLM to judge the result, and retries automatically until -the result is judged **success** or the maximum number of attempts is reached. +1. Executes a user‑supplied task via an unreliable tool that may fail. +2. Uses an LLM judge to verify the result (must answer `success` or `failed`). +3. Retries until the result is judged `success` or a maximum number of attempts is reached. -The key components are: +The implementation follows the specification from the course assignment. -| 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` | +## Requirements -## How It Works +The project uses the following packages (all versions are >= 1.0.0): -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`. +* `langchain>=1.0.0` +* `langgraph>=1.0.0` +* `langchain-openai>=0.2.0` + +Install them via: + +```bash +pip install -r requirements.txt +``` ## Usage ```bash -# Install dependencies -pip install -r requirements.txt - -# Run the agent -python agent.py "2+2" --max 5 +# Run the agent for a simple arithmetic task +python agent.py "2+2" ``` -The console will show the final state, e.g.: +You can also specify the maximum number of attempts: -``` ---- Final State --- -result: 4 -attempts: 2 -status: success -error: None -max_attempts: 5 -``` - -## Requirements - -```text -langchain>=1.0.0 -langgraph>=1.0.0 -langchain-openai>=1.0.0 +```bash +python agent.py "2+2" --max_attempts 5 ``` -## Note +The output will show the number of attempts and the final status. -The agent uses the OpenAI API. Make sure the environment variable `OPENAI_API_KEY` is set. \ No newline at end of file +## How it works + +* **`unreliable_tool`** – a function that evaluates the task string with a 30 % chance of raising a `ValueError`. +* **`execute_task`** – calls the tool and stores the result or error. +* **`verify_result`** – asks the LLM to judge the result. The LLM must reply with either `success` or `failed`. +* **`handle_error`** – increments the attempt counter and resets the state for a retry. +* **Graph** – a LangGraph `StateGraph` that loops between these nodes until success or the attempt limit is reached. + +## Extending + +Replace `unreliable_tool` with any external API or function. Adjust the LLM prompt in `verify_result` to fit your domain. \ No newline at end of file