From 845563ce9e43ad1a97d8755541ea16c06545886b 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: Tue, 2 Jun 2026 15:59:43 +0000 Subject: [PATCH] Update README.md --- README.md | 61 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index e13b338..6e0a168 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,17 @@ -# Self‑correcting LangGraph Agent +# Self‑Correcting LangGraph Agent -This repository contains a small demo of a **self‑correcting LangGraph agent**. The agent receives a *task* string, executes it via an unreliable tool, then asks an LLM to judge the result. If the judge says the result is **failed**, the agent retries until it reaches a maximum number of attempts. +This repository contains a minimal example of a **self‑correcting agent** built with +[LangGraph](https://langchain-ai.github.io/langgraph/) and +[LangChain](https://langchain.com/). The agent: -## Features +1. **Receives a natural‑language task** from the user. +2. **Executes the task** via an *unreliable* tool that fails 30 % of the time. +3. **Asks an LLM** (OpenAI GPT‑4o‑mini) to judge whether the result is correct. +4. **Retries automatically** until the judge says *success* or the maximum number + of attempts is reached. -- **Unreliable tool** – 30 % chance of raising an exception. -- **LLM judge** – forces the model to answer only `success` or `failed`. -- **Retry logic** – automatically retries until success or a maximum number of attempts. -- **LangGraph** – low‑level graph with three nodes: `execute_task`, `verify_result`, `handle_error`. +The code demonstrates how to build a small state machine with LangGraph, how to +use a LLM as a *judge*, and how to implement retry logic. ## Setup @@ -18,34 +22,43 @@ source venv/bin/activate # Windows: venv\Scripts\activate # Install dependencies pip install -r requirements.txt + +# Set your OpenAI API key +export OPENAI_API_KEY=YOUR_KEY # Windows: set OPENAI_API_KEY=YOUR_KEY ``` -## Running the Agent +## Running the agent ```bash -python agent.py +python agent.py "Вычисли 2+2" ``` -You will be prompted to enter a task. The agent will then perform the task, verify the result, and retry if necessary. Example output: +You can also run the script without arguments – it will prompt you for a task. + +## Example output ``` -Self‑correcting LangGraph agent demo -Enter a task: 2+2 +Введите задачу: 2+2 +Попытка 1: результат Result of 2+2 +Попытка 2: результат Result of 2+2 ---- Result --- -Task: 2+2 -Attempts: 2 -Status: success -Result: 22 +Итог: +Успех за 2 попыток. Результат: Result of 2+2 ``` -## Project Structure +The exact number of attempts may vary because the tool fails randomly. -- `agent.py` – main implementation. -- `README.md` – this documentation. -- `requirements.txt` – Python dependencies. +--- -## Notes +### How it works -- The LLM used is OpenAI's `gpt-4o-mini`. If you prefer Ollama, change the `ChatOpenAI` import to `ChatOllama` and adjust the model name accordingly. -- The unreliable tool is a toy example; replace it with a real tool for production use. +- **State** – `AgentState` tracks the task, result, number of attempts, status and + 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.