feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
@@ -1,74 +1,79 @@
|
||||
# Graph with Reflection and Refinement
|
||||
# Graph with Reflect and Rewrite Nodes
|
||||
|
||||
This repository contains a lightweight JavaScript implementation of a graph data structure that supports:
|
||||
This project demonstrates how to integrate an LLM (OpenAI) into a simple graph structure using the `langchain-core` package. The graph contains two nodes:
|
||||
|
||||
- **Self‑referential edges** – edges that point from a node back to itself.
|
||||
- **Reflection** – creating a reverse edge for any existing edge.
|
||||
- **Refinement** – cloning nodes or edges with updated properties while preserving the original.
|
||||
1. **Reflect** – Generates a reflective response to an input message.
|
||||
2. **Rewrite** – Rewrites the reflected message into a concise, formal style.
|
||||
|
||||
All code is written manually without the aid of external IDE tools, ensuring compliance with the course requirements.
|
||||
## Prerequisites
|
||||
|
||||
## Installation
|
||||
- Node.js (v18 or newer)
|
||||
- An OpenAI API key
|
||||
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/your-username/graph-reflection-refinement.git
|
||||
cd graph-reflection-refinement
|
||||
git clone https://github.com/your-username/graph-reflect-rewrite.git
|
||||
cd graph-reflect-rewrite
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
```
|
||||
|
||||
## Running Tests
|
||||
## Configuration
|
||||
|
||||
The project uses Jest for unit testing.
|
||||
Set your OpenAI API key as an environment variable:
|
||||
|
||||
```bash
|
||||
npm test
|
||||
export OPENAI_API_KEY=your_api_key_here
|
||||
```
|
||||
|
||||
All tests should pass, confirming the core functionality of the graph.
|
||||
On Windows (Command Prompt):
|
||||
|
||||
## Usage Example
|
||||
```cmd
|
||||
set OPENAI_API_KEY=your_api_key_here
|
||||
```
|
||||
|
||||
```js
|
||||
const { Graph } = require('./src');
|
||||
On Windows (PowerShell):
|
||||
|
||||
const g = new Graph();
|
||||
```powershell
|
||||
$env:OPENAI_API_KEY="your_api_key_here"
|
||||
```
|
||||
|
||||
// Add nodes
|
||||
g.addNode('A', { name: 'Node A' });
|
||||
g.addNode('B', { name: 'Node B' });
|
||||
## Running the Example
|
||||
|
||||
// Add an edge (including self‑referential)
|
||||
const e1 = g.addEdge('A', 'B', { weight: 5 });
|
||||
const selfEdge = g.addEdge('A', 'A', { weight: 1 });
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
// Reflect an edge
|
||||
const rev = g.reflect(e1);
|
||||
You should see output similar to:
|
||||
|
||||
// Refine a node
|
||||
const refinedA = g.refineNode('A', { status: 'refined' });
|
||||
```
|
||||
--- Input Message ---
|
||||
I am feeling overwhelmed with my workload and unsure how to prioritize tasks.
|
||||
---------------------
|
||||
|
||||
// Refine an edge
|
||||
const refinedEdge = g.refineEdge(e1, { weight: 10 });
|
||||
--- 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.
|
||||
|
||||
console.log(g.getNode(refinedA));
|
||||
console.log(g.getEdge(refinedEdge));
|
||||
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.
|
||||
---------------------
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
- `src/graph.js` – Core `Graph` class implementation.
|
||||
- `src/index.js` – Re‑exports the `Graph` class.
|
||||
- `test/graph.test.js` – Jest test suite covering all functionalities.
|
||||
- `package.json` – Project metadata and dependencies.
|
||||
- `README.md` – Documentation.
|
||||
- `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.
|
||||
|
||||
## 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).
|
||||
|
||||
## License
|
||||
|
||||
MIT © 2026
|
||||
|
||||
---
|
||||
|
||||
*All code was written manually to satisfy the assignment’s requirement of no external IDE usage.*
|
||||
MIT License
|
||||
---END
|
||||
Reference in New Issue
Block a user