Files
task-6a1864fa8a94f887e50d46f0/README.md
T
2026-06-02 15:59:43 +00:00

65 lines
1.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
# SelfCorrecting LangGraph Agent
This repository contains a minimal example of a **selfcorrecting agent** built with
[LangGraph](https://langchain-ai.github.io/langgraph/) and
[LangChain](https://langchain.com/). The agent:
1. **Receives a naturallanguage task** from the user.
2. **Executes the task** via an *unreliable* tool that fails 30% of the time.
3. **Asks an LLM** (OpenAI GPT4omini) to judge whether the result is correct.
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
use a LLM as a *judge*, and how to implement retry logic.
## Setup
```bash
# Optional: create a virtual environment
python -m venv venv
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
```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
Попытка 1: результат Result of 2+2
Попытка 2: результат Result of 2+2
Итог:
Успех за 2 попыток. Результат: Result of 2+2
```
The exact number of attempts may vary because the tool fails randomly.
---
### How it works
- **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.