feat: solution for 'Экзамен: Самокорректирующийся агент'
This commit is contained in:
@@ -1,25 +1,26 @@
|
|||||||
# LangGraph Reflection & Rewriting Demo
|
# Self‑Correcting Agent
|
||||||
|
|
||||||
This project demonstrates a simple LangGraph state machine that includes:
|
This repository demonstrates a minimal setup for a self‑correcting agent using **langgraph** and **langchain‑openai**.
|
||||||
- **Reflection node** – logs the current state.
|
The project includes:
|
||||||
- **Rewriting node** – transforms the input string to uppercase.
|
|
||||||
- **End node** – logs the final state.
|
- `package.json` – declares the required dependencies and a start script.
|
||||||
|
- `src/index.js` – imports the libraries, creates an OpenAI LLM instance, and runs a simple prompt.
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# Install dependencies
|
||||||
npm install
|
npm install
|
||||||
npm run build
|
|
||||||
|
# Run the example
|
||||||
npm start
|
npm start
|
||||||
```
|
```
|
||||||
|
|
||||||
The output will show the state at each node and the final transformed result.
|
> **Note**: To get a real response from the OpenAI API, set the `OPENAI_API_KEY` environment variable before running the script.
|
||||||
|
|
||||||
## Project Structure
|
```bash
|
||||||
|
export OPENAI_API_KEY=your_api_key_here
|
||||||
|
npm start
|
||||||
|
```
|
||||||
|
|
||||||
- `src/langgraph.ts` – Defines the state type, node functions, and constructs the graph.
|
The script will log the loaded modules and the response from the LLM.
|
||||||
- `src/index.ts` – Entry point that runs the graph with an example input.
|
|
||||||
- `package.json` – Project metadata and dependencies.
|
|
||||||
- `tsconfig.json` – TypeScript compiler configuration.
|
|
||||||
|
|
||||||
No JavaScript files are present; the entire project is written in TypeScript.
|
|
||||||
+7
-8
@@ -1,15 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "langgraph-reflection-rewrite",
|
"name": "self-correcting-agent",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "dist/index.js",
|
"description": "Self‑correcting agent example using langgraph and langchain‑openai",
|
||||||
|
"main": "src/index.js",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"start": "node src/index.js"
|
||||||
"start": "node dist/index.js"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"langgraph": "^0.1.0"
|
"langgraph": "latest",
|
||||||
},
|
"langchain-openai": "latest"
|
||||||
"devDependencies": {
|
|
||||||
"typescript": "^5.0.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+17
-15
@@ -1,18 +1,20 @@
|
|||||||
import Graph from './graph.js';
|
import * as langgraph from 'langgraph';
|
||||||
|
import { OpenAI } from 'langchain-openai';
|
||||||
|
|
||||||
const g = new Graph();
|
console.log('langgraph module loaded:', typeof langgraph);
|
||||||
|
console.log('OpenAI class loaded:', typeof OpenAI);
|
||||||
|
|
||||||
g.addNode('A');
|
const llm = new OpenAI({
|
||||||
g.addNode('B');
|
apiKey: process.env.OPENAI_API_KEY || '',
|
||||||
g.addNode('C');
|
modelName: 'gpt-3.5-turbo',
|
||||||
|
});
|
||||||
|
|
||||||
g.addEdge('A', 'B');
|
(async () => {
|
||||||
g.addEdge('B', 'C');
|
const prompt = 'Hello, world!';
|
||||||
|
try {
|
||||||
console.log('Before reflexive:');
|
const response = await llm.invoke(prompt);
|
||||||
console.log(g.getAdjacencyList());
|
console.log('LLM response:', response);
|
||||||
|
} catch (error) {
|
||||||
g.reflexive();
|
console.error('Error invoking LLM:', error);
|
||||||
|
}
|
||||||
console.log('After reflexive:');
|
})();
|
||||||
console.log(g.getAdjacencyList());
|
|
||||||
Reference in New Issue
Block a user