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

This commit is contained in:
2026-07-01 14:40:09 +03:00
parent f14d41830d
commit 153b04b33c
10 changed files with 312 additions and 95 deletions
+75 -26
View File
@@ -1,38 +1,87 @@
# Self-Correcting Agent
# Graph with Reflection and Rewrite Nodes
This project demonstrates a minimal selfcorrecting agent built with **LangChain** and **LangGraph**.
The agent simply echoes user input, but the structure can be extended to include more complex logic and tools.
This library provides a simple directed graph implementation with two special node types:
## Setup
- **ReflectionNode** forwards all input values to its outputs unchanged.
- **RewriteNode** applies a usersupplied function to each input value before emitting it on the output.
## Installation
```bash
# Install Python dependencies
pip install -r requirements.txt
# (Optional) Install Node.js dependencies if needed
npm install
npm install graph-reflection-rewrite
```
## Running
## Usage
```ts
import { Graph, RewriteFunction } from 'graph-reflection-rewrite';
const graph = new Graph();
// Create a reflection node
const refNode = graph.createNode('reflection');
// Create a rewrite node that doubles numbers
const rewriteNode = graph.createNode('rewrite', {
func: (value: number) => value * 2
});
// Connect nodes
graph.addEdge(refNode.id, 'output', rewriteNode.id, 'input');
// Provide initial input to the reflection node
refNode.inputs.set('input', 5);
// Run the graph
graph.run();
// Inspect results
console.log(rewriteNode.outputs.get('input')); // 10
```
## API
### `Graph`
| Method | Description |
|--------|-------------|
| `createNode(type, options?)` | Creates a node of the specified type. For `rewrite` nodes, `options` must contain a `func` property. |
| `addNode(node)` | Adds an existing node instance to the graph. |
| `addEdge(from, out, to, in)` | Connects the output of one node to the input of another. |
| `run()` | Executes all nodes in the graph, propagating data along edges. |
| `getNode(id)` | Retrieves a node by its ID. |
### `BaseNode`
| Property | Type | Description |
|----------|------|-------------|
| `id` | `string` | Unique identifier. |
| `type` | `string` | Node type (`reflection` or `rewrite`). |
| `inputs` | `Map<string, any>` | Input values keyed by input names. |
| `outputs` | `Map<string, any>` | Output values keyed by output names. |
| `process()` | `void` | Override to implement node logic. |
### `ReflectionNode`
- Inherits from `BaseNode`.
- `process()` copies all inputs to outputs with the same keys.
### `RewriteNode`
- Inherits from `BaseNode`.
- Constructor accepts a `func: (value: any) => any`.
- `process()` applies `func` to each input and stores the result in the corresponding output.
## Testing
Run the test suite with:
```bash
python main.py
npm test
```
You should see output similar to:
The project uses Jest with TypeScript support (`ts-jest`).
```
Resulting state:
HumanMessage: Hello, agent!
AIMessage: Echo: Hello, agent!
```
## License
## Project Structure
- `src/` Core logic (nodes and graph construction).
- `main.py` Entry point that builds and runs the graph.
- `requirements.txt` Python dependencies.
- `package.json` Node.js dependencies (optional).
- `README.md` Project documentation.
Feel free to extend the nodes or add new tools to create a more sophisticated agent.
MIT