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

This commit is contained in:
2026-07-01 15:50:16 +03:00
parent e756e363b2
commit 6be5a5c753
7 changed files with 279 additions and 262 deletions
+33 -67
View File
@@ -1,80 +1,46 @@
# Graph Reflection and Refinement Demo
# Graph with Reflection and Rewriting Nodes
This repository demonstrates how to integrate **LangChain LLMs** (OpenAI or Ollama) into a simple Python script that explains graph theory concepts. The project is intentionally minimal to focus on the LLM integration.
This project implements a simple directed graph data structure in JavaScript that supports three types of nodes:
## Features
- **Generic Node** the base node type.
- **Reflection Node** represents a node that reflects on itself.
- **Rewriting Node** represents a node that rewrites or transforms data.
- **OpenAI LLM** support via `langchain-openai`.
- **Ollama LLM** support via `langchain-ollama`.
- Environment variable configuration using `.env` or system variables.
- Simple prompt chain that explains graph reflection and refinement.
## Installation
## Setup
```bash
npm install
```
1. **Clone the repository**
## Running Tests
```bash
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-graf-s-refleksiey-i-do
cd povtornyy-ekzamen-graf-s-refleksiey-i-do
```
2. **Create a virtual environment (recommended)**
```bash
python3 -m venv .venv
source .venv/bin/activate
```
3. **Install dependencies**
```bash
pip install -r requirements.txt
```
4. **Configure environment variables**
Create a `.env` file in the project root (or set system variables) with one of the following:
```dotenv
# For OpenAI
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-3.5-turbo
OPENAI_TEMPERATURE=0.7
# OR for Ollama
OLLAMA_HOST=http://localhost:11434
OLLAMA_MODEL=llama2
OLLAMA_TEMPERATURE=0.7
```
Only one of the two configurations is required.
```bash
npm test
```
## Usage
Run the script:
```js
import { Graph, Node, ReflectionNode, RewritingNode } from './src/index.js';
```bash
python src/main.py
const graph = new Graph();
const n1 = new Node('n1');
const r1 = new ReflectionNode('r1');
const w1 = new RewritingNode('w1');
graph.addNode(n1);
graph.addNode(r1);
graph.addNode(w1);
graph.addEdge('n1', 'r1');
graph.addEdge('r1', 'w1');
graph.traverse('n1', (node) => {
console.log(node.id, node.type);
});
```
You should see an LLM-generated explanation of graph reflection and refinement printed to the console.
## License
## Project Structure
```
povtornyy-ekzamen-graf-s-refleksiey-i-do/
├── src/
│ └── main.py # Core script with LangChain integration
├── requirements.txt # All required Python packages
└── README.md # Project documentation
```
## Notes
- The script automatically selects the LLM based on the presence of environment variables.
- If neither `OPENAI_API_KEY` nor `OLLAMA_HOST` is set, the script will raise an error.
- Feel free to extend the prompt or chain logic to suit more complex use cases.
---
Happy coding!
MIT