feat: solution for 'Экзамен: Самокорректирующийся агент'
This commit is contained in:
@@ -1,87 +1,44 @@
|
||||
# Graph with Reflection and Rewrite Nodes
|
||||
# Self‑Correcting Agent
|
||||
|
||||
This library provides a simple directed graph implementation with two special node types:
|
||||
This repository demonstrates a minimal self‑correcting agent that uses the **LangChain OpenAI** provider to generate responses from an LLM.
|
||||
|
||||
- **ReflectionNode** – forwards all input values to its outputs unchanged.
|
||||
- **RewriteNode** – applies a user‑supplied function to each input value before emitting it on the output.
|
||||
## Prerequisites
|
||||
|
||||
- Node.js v18 or newer (ESM support required)
|
||||
- An OpenAI API key set in the environment variable `OPENAI_API_KEY`
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install graph-reflection-rewrite
|
||||
npm install
|
||||
```
|
||||
|
||||
## 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
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
## API
|
||||
The script will send a prompt to the LLM and print the response.
|
||||
|
||||
### `Graph`
|
||||
## Project Structure
|
||||
|
||||
| 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. |
|
||||
- `src/agent.js` – Contains the logic to interact with the LLM.
|
||||
- `src/index.js` – Entry point that demonstrates usage.
|
||||
- `package.json` – Project metadata and dependencies.
|
||||
|
||||
### `BaseNode`
|
||||
## Adding a Different LLM Provider
|
||||
|
||||
| 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:
|
||||
If you prefer to use another provider (e.g., Ollama), replace the dependency and imports:
|
||||
|
||||
```bash
|
||||
npm test
|
||||
npm install langchain-ollama
|
||||
```
|
||||
|
||||
The project uses Jest with TypeScript support (`ts-jest`).
|
||||
```js
|
||||
import { Ollama } from 'langchain-ollama';
|
||||
```
|
||||
|
||||
## License
|
||||
Adjust the model initialization accordingly.
|
||||
|
||||
MIT
|
||||
---
|
||||
Reference in New Issue
Block a user