feat: solution for 'Экзамен: Самокорректирующийся агент'
This commit is contained in:
@@ -1,65 +1,35 @@
|
||||
# LangGraph Reflection Demo
|
||||
# LangGraph Agent Implementation
|
||||
|
||||
This project demonstrates a simple LangGraph agent that:
|
||||
1. Generates a short answer (5–10 sentences) to a user‑supplied question.
|
||||
2. Critiques the answer for completeness, concreteness, and fluff.
|
||||
3. If the critique indicates `needs_revision`, rewrites the answer up to a maximum number of rounds.
|
||||
This repository contains a minimal implementation of a LangGraph agent using the `langgraph` library. The agent demonstrates how to:
|
||||
|
||||
## Features
|
||||
|
||||
- **Separate nodes** for drafting, reflecting, and rewriting.
|
||||
- **LLM‑based critic** that returns a verdict (`ok` or `needs_revision`) and 2–3 critique points.
|
||||
- **Controlled loop**: rewrites only if the verdict is `needs_revision` and the round count is below `max_rounds`.
|
||||
- **CLI interface**: pass a question via `-q` or input interactively.
|
||||
- **Configurable maximum rounds** via `-m` (default 2).
|
||||
- Define a state dataclass for graph data.
|
||||
- Create a simple graph with nodes and edges.
|
||||
- Execute the graph and retrieve the final state.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.10+
|
||||
- `langgraph`
|
||||
- `langchain-openai`
|
||||
- `langgraph==0.0.38`
|
||||
|
||||
Install dependencies:
|
||||
Install the dependencies with:
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Usage
|
||||
## Running the Agent
|
||||
|
||||
1. **Set your OpenAI API key**:
|
||||
Execute the agent directly:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY="your_api_key_here"
|
||||
python langgraph_agent.py
|
||||
```
|
||||
|
||||
2. **Run the demo**:
|
||||
|
||||
```bash
|
||||
python src/main.py -q "Explain the difference between a tool and a resource in MCP."
|
||||
```
|
||||
|
||||
Or simply:
|
||||
|
||||
```bash
|
||||
python src/main.py
|
||||
```
|
||||
|
||||
and enter the question when prompted.
|
||||
|
||||
The script will output the final answer, the number of rounds performed, the verdict, and the critique points.
|
||||
|
||||
## Project Structure
|
||||
You should see output similar to:
|
||||
|
||||
```
|
||||
src/
|
||||
├── main.py # CLI entry point
|
||||
├── graph.py # LangGraph definition
|
||||
└── nodes.py # Node implementations
|
||||
requirements.txt
|
||||
README.md
|
||||
Final state messages: ['Hello from LangGraph!']
|
||||
```
|
||||
|
||||
## License
|
||||
## Extending the Agent
|
||||
|
||||
MIT License
|
||||
Feel free to add more nodes, incorporate LLM calls, or integrate with other frameworks such as LangChain. The current structure provides a solid foundation for building more complex conversational agents.
|
||||
Reference in New Issue
Block a user