8da4497d5865ecb62e92e96665fbcaef8f8c2ccb
LangGraph Draft‑Critique‑Rewrite Agent
Overview
This project implements a small LangGraph agent that:
- Generates a short answer (5‑10 sentences) to a user question.
- Critiques the draft for completeness, concreteness and absence of filler.
- If the critique says needs_revision, rewrites the draft incorporating the critique.
- Loops until the verdict is ok or a maximum of two rewrites is reached.
- Provides a simple CLI entry point.
The implementation is fully self‑contained and requires only a modern Python interpreter (3.10+).
Installation
# Create and activate a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
Dependencies
langgraphlangchain-openailangchain-ollama
Configuration
The agent uses an LLM via the langchain_openai.ChatOpenAI wrapper. It supports both OpenAI and Ollama providers.
| Variable | Description | Example |
|---|---|---|
OPENAI_API_KEY |
API key for OpenAI. | export OPENAI_API_KEY=sk-… |
OLLAMA_BASE_URL |
Base URL for a local Ollama instance. | export OLLAMA_BASE_URL=http://localhost:11434/v1 |
If neither variable is set the agent falls back to the default model gpt-4o-mini (this will require network access to OpenAI).
Usage
Run the CLI entry point:
python agent.py
You will be prompted to type a question. The agent will then display the final draft, the critique, and the verdict.
Example
$ python agent.py
Enter your question: What is the capital of France?
Final Result:
Draft: The capital of France is Paris.
Critique: ok
Verdict: ok
Development Notes
- The graph uses
ENDfromlanggraphfor terminal states. - The
reflectnode parses a JSON response, falling back to a default verdict if parsing fails. - The
rewritenode increments the round counter. - All LLM calls are wrapped in
try/exceptblocks to surface errors clearly.
License
MIT License.
Description
Languages
Python
100%