2.4 KiB
Self-Reflective LangGraph Agent
Overview
This repository contains a minimal example of a LangGraph agent that writes a short answer to a question, then uses a critic node to evaluate the answer.
If the critic flags the answer as needs_revision, a rewrite node improves the text.
The process repeats until the answer is accepted or the maximum number of rounds is reached.
The agent is built on top of deepagents and uses OpenRouter via langchain_openai.
How to Run
-
Clone the repository
git clone https://github.com/yourname/self_reflective_agent.git cd self_reflective_agent -
Create a virtual environment
python -m venv .venv source .venv/bin/activate # On Windows use .venv\\Scripts\\activate -
Install dependencies
pip install -r requirements.txt -
Set your OpenRouter API key
export OPENAI_API_KEY=sk-or-... -
Run the demo
python main.pyYou should see the final answer printed to the console.
Project Structure
main.py- Main script that defines the graph, the DeepAgent, and runs the demo.requirements.txt- Python dependencies.README.md- This file.
Notes
- The agent uses a fixed temperature of
0.0for deterministic responses. - The maximum number of rewrite rounds is set to
2by default. - All code uses only ASCII punctuation as required by the assignment.
Ответ на замечание преподавателя
Замечание: Переделайте решение: вместо узла reflect используйте try/except для повторных попыток генерации ответа.
Позиция: В коде уже реализован отдельный узел reflect, который отвечает за критический анализ ответа, как требуется в условии. Замечание о замене его try/except нарушает заданный критерий «Рефлексия» и не соответствует требованиям.
Код полностью соответствует условию задания. Указанные изменения не являются обязательными требованиями задания и не вносятся намеренно.