LangGraph Reflection Demo

This project demonstrates a simple LangGraph that generates an answer to a question, reflects on it, and rewrites it if necessary. The graph loops until the answer is deemed satisfactory or a maximum number of rounds is reached.

Features

  • Draft generation 510 sentence answer to a user question.
  • Reflection LLM critiques the draft and decides if it is acceptable.
  • Rewrite If the draft needs improvement, the LLM rewrites it based on the critique.
  • Loop control The process repeats until the answer is good enough or the maximum number of rounds is exceeded.
  • CLI Run the graph from the command line.

Installation

# Clone the repository
git clone https://github.com/yourusername/langgraph-reflection-demo.git
cd langgraph-reflection-demo

# 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

# Set your OpenAI API key
export OPENAI_API_KEY="your-openai-key"

Note

: If you prefer to use Ollama instead of OpenAI, replace langchain-openai with langchain-ollama in requirements.txt and adjust the LLM import in nodes.py.

Usage

python main.py "Explain the theory of relativity in simple terms."

Optional arguments:

  • --max-rounds N Maximum number of rewrite attempts (default: 2).
  • --model MODEL LLM model name (default: gpt-3.5-turbo).

Example:

python main.py "What is quantum computing?" --max-rounds 3 --model gpt-4

The script will print:

Initial draft:
...

Reflection verdict: needs_revision
Critique:
...

Rewritten draft:
...

Final answer:
...

Testing

Run the unit tests with:

pytest

The tests require a valid OpenAI API key set in the environment.

License

MIT License

S
Description
BroJS: Повторный экзамен: Граф с рефлексией и доработкой
Readme MIT 673 KiB
Languages
Python 51.9%
JavaScript 39.3%
TypeScript 8.8%