Update README.md

This commit is contained in:
2026-06-04 23:11:01 +00:00
parent 7d813e74cc
commit 9e331494a4
+33 -44
View File
@@ -1,61 +1,50 @@
# SelfCorrecting LangGraph Agent # SelfCorrecting LangGraph Agent
## Overview This repository contains a minimal implementation of a **selfcorrecting agent** using LangGraph. The agent:
This project implements a **selfcorrecting agent** using LangGraph. The agent 1. Executes a usersupplied task via an unreliable tool that may fail.
performs a task, asks an LLM to judge the result, and retries automatically until 2. Uses an LLM judge to verify the result (must answer `success` or `failed`).
the result is judged **success** or the maximum number of attempts is reached. 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 | ## Requirements
|-----------|---------|
| `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 The project uses the following packages (all versions are >= 1.0.0):
1. **Start** The graph begins at `execute_task`. * `langchain>=1.0.0`
2. **Execute** The tool runs. If it throws an exception, the state status becomes `failed`. * `langgraph>=1.0.0`
3. **Check attempts** If the number of attempts is >= `max_attempts`, the graph ends with status `max_attempts`. * `langchain-openai>=0.2.0`
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`. Install them via:
```bash
pip install -r requirements.txt
```
## Usage ## Usage
```bash ```bash
# Install dependencies # Run the agent for a simple arithmetic task
pip install -r requirements.txt python agent.py "2+2"
# Run the agent
python agent.py "2+2" --max 5
``` ```
The console will show the final state, e.g.: You can also specify the maximum number of attempts:
``` ```bash
--- Final State --- python agent.py "2+2" --max_attempts 5
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
``` ```
## 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. ## 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.