82 lines
2.0 KiB
Markdown
82 lines
2.0 KiB
Markdown
# LangGraph Reflection Agent
|
||
|
||
This project demonstrates a simple LangGraph agent that:
|
||
|
||
1. Generates a concise answer to a user‑supplied 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** – 5–10 sentence answer.
|
||
- **LLM critic** – returns a verdict (`ok` or `needs_revision`) and 2–3 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
|
||
|
||
```bash
|
||
# 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 OpenAI’s GPT‑3.5‑Turbo by default.
|
||
Set your API key in the environment:
|
||
|
||
```bash
|
||
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
|
||
|
||
```bash
|
||
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:
|
||
|
||
```bash
|
||
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 |