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

This commit is contained in:
2026-07-01 16:43:56 +03:00
parent 89b60e8f03
commit e9a6f09c70
8 changed files with 383 additions and 143 deletions
+51 -44
View File
@@ -1,79 +1,86 @@
# Graph with Reflect and Rewrite Nodes
# Graph with Reflection and Rewriting Nodes
This project demonstrates how to integrate an LLM (OpenAI) into a simple graph structure using the `langchain-core` package. The graph contains two nodes:
This project demonstrates a simple data processing graph in **Python** that uses **LangChain** with **OpenAI** or **Ollama** to perform reflection and rewriting of text.
The graph is built from reusable node classes and can be extended with additional nodes as needed.
1. **Reflect** Generates a reflective response to an input message.
2. **Rewrite** Rewrites the reflected message into a concise, formal style.
## Features
## Prerequisites
- **ReflectionNode** Generates reflective insights from input text using an LLM.
- **RewritingNode** Rewrites the reflection in a specified style (e.g., formal, concise).
- **Graph** Connects nodes and executes them in sequence.
- **Configurable LLM provider** Switch between OpenAI and Ollama via the `LLM_PROVIDER` environment variable.
- **Unit tests** Verify node behavior with mocked LLM responses.
- Node.js (v18 or newer)
- An OpenAI API key
## Requirements
## Setup
- Python 3.10+
- `langchain`
- `openai` (for OpenAI provider)
- `python-dotenv` (optional, for loading environment variables)
Install dependencies:
```bash
# Clone the repository
git clone https://github.com/your-username/graph-reflect-rewrite.git
cd graph-reflect-rewrite
# Install dependencies
npm install
pip install -r requirements.txt
```
## Configuration
Set your OpenAI API key as an environment variable:
Set the LLM provider by defining the `LLM_PROVIDER` environment variable:
```bash
export OPENAI_API_KEY=your_api_key_here
export LLM_PROVIDER=openai # or ollama
```
On Windows (Command Prompt):
If using OpenAI, ensure that the `OPENAI_API_KEY` environment variable is set.
If using Ollama, ensure that the Ollama server is running locally and the model name matches the one configured in `src/llm_integration.py`.
```cmd
set OPENAI_API_KEY=your_api_key_here
```
## Usage
On Windows (PowerShell):
```powershell
$env:OPENAI_API_KEY="your_api_key_here"
```
## Running the Example
Run the graph with a text input:
```bash
npm start
python -m src.main "Your input text goes here."
```
You should see output similar to:
Or pipe text via stdin:
```bash
echo "Some text" | python -m src.main
```
--- Input Message ---
I am feeling overwhelmed with my workload and unsure how to prioritize tasks.
---------------------
--- Final Output ---
I have taken a moment to reflect on your situation. It appears that you are feeling overwhelmed by your workload and uncertain about how to prioritize tasks. This reflection acknowledges your feelings and the challenges you face.
The output will be the rewritten text produced by the `RewritingNode`.
I have rewritten the reflection in a concise and formal style:
I have taken a moment to reflect on your situation. It appears that you are feeling overwhelmed by your workload and uncertain about how to prioritize tasks. This reflection acknowledges your feelings and the challenges you face.
---------------------
## Running Tests
Execute the test suite with:
```bash
python -m unittest discover tests
```
## Project Structure
- `src/index.js` Entry point that builds and runs the graph.
- `src/graph.js` Simple graph implementation.
- `src/nodes/reflect.js` Reflect node implementation.
- `src/nodes/rewrite.js` Rewrite node implementation.
```
src/
├── llm_integration.py # LLM client factory
├── nodes.py # Node definitions
├── graph.py # Graph construction and execution
└── main.py # CLI entry point
tests/
└── test_nodes.py # Unit tests for nodes
requirements.txt
README.md
```
## Extending the Graph
You can add more nodes by creating new modules in `src/nodes/` and adding them to the graph in `src/index.js`. Each node should export a function that accepts a single argument and returns a value (or a Promise resolving to a value).
To add new nodes:
1. Create a new class inheriting from `BaseNode` in `src/nodes.py`.
2. Implement the `process` method.
3. Add the node to the graph in `src/graph.py` and connect it with `add_edge`.
## License
MIT License
---END
MIT License