feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'

This commit is contained in:
2026-06-29 12:16:00 +03:00
parent 366ec8dee7
commit 707c35d49d
5 changed files with 189 additions and 110 deletions
+43 -51
View File
@@ -1,90 +1,82 @@
# LangGraph Research Brief Agent
# LangGraph Reflection Agent
This project demonstrates how to build a LangGraph agent that generates a short research brief for a given topic.
The agent:
This project demonstrates a simple LangGraph agent that:
1. Creates an outline of 45 research steps.
2. For each step, performs a web search (via Tavily) and writes a concise note.
3. Synthesizes all notes into a coherent brief.
1. Generates a concise answer to a usersupplied 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.
## Prerequisites
The agent is implemented in Python 3.10+ and uses the `langgraph` framework together with `langchain-openai`.
- Python 3.10+
- A **Tavily** API key (free tier available).
- An **OpenAI** API key (or any compatible LLM provider).
## Features
## Setup
- **Draft generation** 510 sentence answer.
- **LLM critic** returns a verdict (`ok` or `needs_revision`) and 23 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
# Clone the repository
git clone https://github.com/your-username/langgraph-research-brief.git
cd langgraph-research-brief
# Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
python -m venv .venv
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
# Install dependencies
pip install -r requirements.txt
```
Create a `.env` file in the project root based on the example:
### OpenAI API Key
The agent uses OpenAIs GPT3.5Turbo by default.
Set your API key in the environment:
```bash
cp .env.example .env
export OPENAI_API_KEY="sk-..."
```
Edit `.env` and replace the placeholders with your actual keys:
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`.
```
OPENAI_API_KEY=sk-...
TAVILY_API_KEY=your_tavily_key
```
## Running the Agent
## Usage
```bash
python src/main.py
python -m src.main "Explain the difference between a tool and a resource in MCP to a student."
```
You should see output similar to:
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:
```
=== Outline ===
1. Identify the security requirements for MCP integration
2. Review LangChain's authentication mechanisms
3. Evaluate secure communication protocols
4. Test the integration in a sandbox environment
5. Document best practices and compliance checks
=== Final Answer ===
<rewritten answer>
=== Notes ===
[Step 1] ... (58 sentence note)
[Step 2] ... (58 sentence note)
...
=== Verdict ===
ok
=== Final Brief ===
...
```
If the final verdict is `needs_revision`, the critique points will also be shown.
## Project Structure
```
src/
├── main.py # Entry point
├── graph.py # LangGraph definition
── nodes.py # Node implementations
├── state.py # TypedDict for state
├── .env.example # Environment variable template
├── main.py # CLI entry point
├── graph.py # LangGraph graph definition
── state.py # TypedDict for the agent state
requirements.txt
README.md
```
## Customization
- **Topic**: Change the `default_topic` variable in `src/main.py` to generate a brief on a different subject.
- **LLM**: Swap `ChatOpenAI` for another provider (e.g., Ollama) by adjusting the imports and initialization in `src/nodes.py`.
- **Search**: Replace `TavilySearchResults` with another search tool if desired.
## License
MIT License