feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
@@ -1,60 +1,60 @@
|
||||
# LangGraph Agent with OpenAI Integration
|
||||
# Graph with Reflection and Rewriting
|
||||
|
||||
This project demonstrates a simple LangGraph agent that integrates with the OpenAI LLM via the `langchain-openai` package. The agent processes a single prompt and returns the model's response.
|
||||
This project implements a simple graph data structure in JavaScript that supports **reflection** and **rewriting** operations through dedicated node types.
|
||||
|
||||
## Requirements
|
||||
## Features
|
||||
|
||||
- Python 3.10+
|
||||
- `langchain-openai` (automatically installed via `requirements.txt`)
|
||||
- `langgraph`
|
||||
- `langchain`
|
||||
- `openai`
|
||||
- **Graph**: Stores nodes and directed edges.
|
||||
- **Node**: Base class for all nodes.
|
||||
- **ReflectionNode**: Creates copies of its target nodes and their outgoing edges.
|
||||
- **RewritingNode**: Replaces a target node with a new node while preserving graph connectivity.
|
||||
- **Traversal**: Depth‑first traversal of the graph.
|
||||
|
||||
Install the dependencies:
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
npm install
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Set your OpenAI API key as an environment variable:
|
||||
## Running Tests
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY="your-openai-api-key"
|
||||
npm test
|
||||
```
|
||||
|
||||
Alternatively, you can create a `.env` file in the project root with the following content:
|
||||
The test suite verifies:
|
||||
|
||||
```
|
||||
OPENAI_API_KEY=your-openai-api-key
|
||||
- Reflection node correctly duplicates target nodes.
|
||||
- Rewriting node correctly replaces target nodes.
|
||||
- Circular references are handled safely.
|
||||
- Graph traversal works after modifications.
|
||||
|
||||
## Usage Example
|
||||
|
||||
```js
|
||||
const { Graph, Node, ReflectionNode, RewritingNode } = require('./src/index');
|
||||
|
||||
const graph = new Graph();
|
||||
graph.addNode(new Node('A'));
|
||||
graph.addNode(new Node('B'));
|
||||
graph.addNode(new Node('C'));
|
||||
graph.addEdge('A', 'B');
|
||||
graph.addEdge('B', 'C');
|
||||
|
||||
const r = new ReflectionNode('R');
|
||||
graph.addNode(r);
|
||||
graph.addEdge('R', 'B');
|
||||
r.reflect(graph);
|
||||
|
||||
const w = new RewritingNode('W');
|
||||
graph.addNode(w);
|
||||
graph.addEdge('W', 'C');
|
||||
const d = new Node('D');
|
||||
w.rewrite(graph, 'C', d);
|
||||
|
||||
console.log(graph.traverse('A'));
|
||||
```
|
||||
|
||||
## Running the Agent
|
||||
## License
|
||||
|
||||
You can run the agent from the command line:
|
||||
|
||||
```bash
|
||||
python -m src.agent "Hello, how are you?"
|
||||
```
|
||||
|
||||
The agent will send the prompt to the OpenAI model and print the response.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
├── requirements.txt
|
||||
├── src
|
||||
│ └── agent.py
|
||||
└── README.md
|
||||
```
|
||||
|
||||
- `requirements.txt` – lists all Python package dependencies.
|
||||
- `src/agent.py` – contains the LangGraph agent implementation and a simple CLI.
|
||||
- `README.md` – this documentation file.
|
||||
|
||||
## Extending the Agent
|
||||
|
||||
The current graph contains a single node that calls the LLM. You can extend it by adding more nodes (e.g., for tool usage, memory, or custom logic) and connecting them in the graph.
|
||||
|
||||
Happy coding!
|
||||
MIT
|
||||
Reference in New Issue
Block a user