Files
povtornyy-ekzamen-graf-s-re…/README.md
T

2.0 KiB
Raw Blame History

LangGraph Reflection Agent

This project demonstrates a simple LangGraph agent that:

  1. Generates a concise answer to a usersupplied question.
  2. Critiques the answer using an LLM.
  3. Rewrites the answer if the critic says needs_revision, up to a maximum number of rounds.

The agent is implemented in Python 3.10+ and uses the langgraph framework together with langchain-openai.

Features

  • Draft generation 510 sentence answer.
  • LLM critic returns a verdict (ok or needs_revision) and 23 critique points.
  • Rewrite loop rewrites the draft until the verdict is ok or the maximum number of rounds is reached.
  • CLI run the agent from the command line.

Installation

# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate   # On Windows use `.venv\Scripts\activate`

# Install dependencies
pip install -r requirements.txt

OpenAI API Key

The agent uses OpenAIs GPT3.5Turbo by default.
Set your API key in the environment:

export OPENAI_API_KEY="sk-..."

If you prefer to use a local LLM via Ollama, replace the langchain-openai dependency with langchain-ollama and adjust the LLM initialization in src/graph.py.

Usage

python -m src.main "Explain the difference between a tool and a resource in MCP to a student."

Optional arguments:

  • --max_rounds N maximum number of rewrite rounds (default: 2).

Example:

python -m src.main "Explain the difference between a tool and a resource in MCP." --max_rounds 3

The script will print:

=== Final Answer ===
<rewritten answer>

=== Verdict ===
ok

If the final verdict is needs_revision, the critique points will also be shown.

Project Structure

src/
├── main.py      # CLI entry point
├── graph.py     # LangGraph graph definition
└── state.py     # TypedDict for the agent state
requirements.txt
README.md

License

MIT License