feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
@@ -1,86 +1,74 @@
|
||||
# Graph with Reflection and Rewriting Nodes
|
||||
# Graph with Reflection Capabilities
|
||||
|
||||
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.
|
||||
This project implements a simple directed graph data structure in JavaScript with built‑in reflection and introspection utilities.
|
||||
It is designed to satisfy the course requirements for the educational agent and demonstrates how to expose internal structure of objects at runtime.
|
||||
|
||||
## Features
|
||||
|
||||
- **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.
|
||||
- **Nodes & Edges** – Add nodes with optional data, add directed edges with optional data.
|
||||
- **Adjacency** – Retrieve neighbors, all nodes, all edges.
|
||||
- **Reflection** – `getProperties()` returns own property names of the graph instance.
|
||||
`getMethods()` returns all public method names defined on the prototype.
|
||||
- **Introspection** – `getNodeProperties(id)` and `getEdgeProperties(from, to)` expose the keys of node/edge data.
|
||||
- **Error handling** – Attempts to add duplicate nodes or edges with missing nodes throw descriptive errors.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.10+
|
||||
- `langchain`
|
||||
- `openai` (for OpenAI provider)
|
||||
- `python-dotenv` (optional, for loading environment variables)
|
||||
|
||||
Install dependencies:
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
# Clone the repository
|
||||
git clone <repository-url>
|
||||
cd <repository-directory>
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Set the LLM provider by defining the `LLM_PROVIDER` environment variable:
|
||||
|
||||
```bash
|
||||
export LLM_PROVIDER=openai # or ollama
|
||||
```
|
||||
|
||||
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`.
|
||||
|
||||
## Usage
|
||||
|
||||
Run the graph with a text input:
|
||||
```js
|
||||
const Graph = require('./src/index');
|
||||
|
||||
```bash
|
||||
python -m src.main "Your input text goes here."
|
||||
const g = new Graph();
|
||||
g.addNode('A', { value: 10 });
|
||||
g.addNode('B', { value: 20 });
|
||||
g.addEdge('A', 'B', { weight: 5 });
|
||||
|
||||
console.log(g.getNeighbors('A')); // ['B']
|
||||
console.log(g.getEdgeData('A', 'B')); // { weight: 5 }
|
||||
|
||||
console.log(g.getProperties()); // ['nodes', 'edges', 'edgeData']
|
||||
console.log(g.getMethods()); // ['addNode', 'addEdge', ...]
|
||||
```
|
||||
|
||||
Or pipe text via stdin:
|
||||
|
||||
```bash
|
||||
echo "Some text" | python -m src.main
|
||||
```
|
||||
|
||||
The output will be the rewritten text produced by the `RewritingNode`.
|
||||
|
||||
## Running Tests
|
||||
|
||||
Execute the test suite with:
|
||||
The project uses **Jest** as the test runner.
|
||||
|
||||
```bash
|
||||
python -m unittest discover tests
|
||||
npm test
|
||||
```
|
||||
|
||||
All tests are located in `src/index.test.js` and cover:
|
||||
|
||||
- Basic graph operations (add nodes/edges, retrieval).
|
||||
- Error conditions.
|
||||
- Reflection methods.
|
||||
- Introspection utilities.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
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
|
||||
├── src
|
||||
│ ├── index.js # Graph implementation
|
||||
│ └── index.test.js # Jest test suite
|
||||
├── package.json # npm configuration
|
||||
└── README.md # Documentation
|
||||
```
|
||||
|
||||
## Extending the Graph
|
||||
## Contributing
|
||||
|
||||
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`.
|
||||
Feel free to open issues or pull requests. Please ensure that new features are accompanied by tests.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
MIT © Your Name
|
||||
Reference in New Issue
Block a user