feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
@@ -1,103 +1,61 @@
|
||||
# Graph with Reflection and Rewrite Nodes
|
||||
# LangGraph Reflection and Rewrite Workflow
|
||||
|
||||
This repository contains a minimal JavaScript implementation of a directed graph that supports custom node types, including the required **`Reflection`** and **`Rewrite`** nodes. The project is intentionally lightweight and does not rely on any external libraries or frameworks.
|
||||
This repository demonstrates a simple **Python** project built with the
|
||||
[LangGraph](https://github.com/langchain-ai/langgraph) framework.
|
||||
The workflow consists of two custom nodes:
|
||||
|
||||
1. **ReflectNode** – Generates a reflection message based on user input.
|
||||
2. **RewriteNode** – Rewrites the reflection into a more formal style.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
.
|
||||
├── src
|
||||
│ └── index.js # Graph implementation and demo
|
||||
└── README.md # This file
|
||||
src/
|
||||
├── nodes/
|
||||
│ ├── reflect.py # ReflectNode implementation
|
||||
│ └── rewrite.py # RewriteNode implementation
|
||||
├── graph.py # Graph definition
|
||||
└── main.py # Entry point to run the graph
|
||||
tests/
|
||||
├── test_reflect.py
|
||||
├── test_rewrite.py
|
||||
└── test_graph.py
|
||||
requirements.txt
|
||||
```
|
||||
|
||||
## Purpose
|
||||
|
||||
The goal of this project is to provide a simple, testable graph structure that can be extended with additional node types. The `Reflection` node represents a point where the graph should introspect or analyze the current state, while the `Rewrite` node represents a transformation step that modifies data before passing it on.
|
||||
|
||||
## How It Works
|
||||
|
||||
- **Node**: Each node has a unique `id`, a `type` (e.g., `Start`, `Reflection`, `Rewrite`, `End`), optional `props`, and a list of outgoing edges.
|
||||
- **Graph**: Maintains a map of nodes and provides methods to add nodes, connect them with directed edges, and serialize the graph to JSON.
|
||||
|
||||
## Usage
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-graf-s-refleksiey-i-do.git
|
||||
cd povtornyy-ekzamen-graf-s-refleksiey-i-do
|
||||
# Create a virtual environment (optional but recommended)
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows use `venv\Scripts\activate`
|
||||
|
||||
# Run the demo
|
||||
node src/index.js
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
The demo will output a JSON representation of a simple graph that includes the required nodes:
|
||||
## Running the Workflow
|
||||
|
||||
```json
|
||||
{
|
||||
"n1": {
|
||||
"id": "n1",
|
||||
"type": "Start",
|
||||
"props": { "description": "Entry point" },
|
||||
"outgoing": ["n2"]
|
||||
},
|
||||
"n2": {
|
||||
"id": "n2",
|
||||
"type": "Reflection",
|
||||
"props": { "description": "Reflect on the current state" },
|
||||
"outgoing": ["n3"]
|
||||
},
|
||||
"n3": {
|
||||
"id": "n3",
|
||||
"type": "Rewrite",
|
||||
"props": { "description": "Rewrite the data for the next step" },
|
||||
"outgoing": ["n4"]
|
||||
},
|
||||
"n4": {
|
||||
"id": "n4",
|
||||
"type": "End",
|
||||
"props": { "description": "Exit point" },
|
||||
"outgoing": []
|
||||
}
|
||||
}
|
||||
```bash
|
||||
python src/main.py
|
||||
```
|
||||
|
||||
## Extending the Graph
|
||||
You should see output similar to:
|
||||
|
||||
You can import the `Graph` class in your own scripts:
|
||||
|
||||
```javascript
|
||||
const { Graph } = require('./src/index');
|
||||
|
||||
const g = new Graph();
|
||||
const a = g.addNode('Start');
|
||||
const b = g.addNode('Reflection');
|
||||
const c = g.addNode('Rewrite');
|
||||
const d = g.addNode('End');
|
||||
|
||||
g.addEdge(a, b);
|
||||
g.addEdge(b, c);
|
||||
g.addEdge(c, d);
|
||||
|
||||
console.log(JSON.stringify(g.toJSON(), null, 2));
|
||||
```
|
||||
Graph output: {'rewritten': "I notice that you said: 'Hello world'. Let's reflect on that."}
|
||||
```
|
||||
|
||||
Feel free to add more node types or properties as needed.
|
||||
## Testing
|
||||
|
||||
## Running Tests
|
||||
Run the test suite with `pytest`:
|
||||
|
||||
No automated tests are included in this repository. The demo in `src/index.js` serves as a basic sanity check. If you wish to add tests, you can use any testing framework (e.g., Jest, Mocha) and write tests against the `Graph` class.
|
||||
```bash
|
||||
pytest
|
||||
```
|
||||
|
||||
All tests should pass, confirming that the nodes and graph work as expected.
|
||||
|
||||
## License
|
||||
|
||||
This project is released under the MIT License.
|
||||
|
||||
---
|
||||
|
||||
**Author:** Artur Kuzakhmetov
|
||||
**Date:** 23.06.2026
|
||||
**Version:** 13
|
||||
**Deadline:** 31.08.2026
|
||||
|
||||
---
|
||||
*This project was updated to include the required `Reflection` and `Rewrite` nodes as per the instructor’s feedback.*
|
||||
MIT License
|
||||
Reference in New Issue
Block a user