68 lines
2.0 KiB
Markdown
68 lines
2.0 KiB
Markdown
# LangGraph Draft‑Critique‑Rewrite Agent
|
||
|
||
## Overview
|
||
This project implements a small LangGraph agent that:
|
||
|
||
1. Generates a short answer (5‑10 sentences) to a user question.
|
||
2. Critiques the draft for completeness, concreteness and absence of filler.
|
||
3. If the critique says *needs_revision*, rewrites the draft incorporating the critique.
|
||
4. Loops until the verdict is *ok* or a maximum of two rewrites is reached.
|
||
5. Provides a simple CLI entry point.
|
||
|
||
The implementation is fully self‑contained and requires only a modern Python interpreter (3.10+).
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# 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**
|
||
- `langgraph`
|
||
- `langchain-openai`
|
||
- `langchain-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:
|
||
|
||
```bash
|
||
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 `END` from `langgraph` for terminal states.
|
||
- The `reflect` node parses a JSON response, falling back to a default verdict if parsing fails.
|
||
- The `rewrite` node increments the round counter.
|
||
- All LLM calls are wrapped in `try/except` blocks to surface errors clearly.
|
||
|
||
## License
|
||
MIT License.
|