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

This commit is contained in:
2026-06-30 16:42:32 +03:00
parent 57a7f1d12b
commit 9ff9612bbb
5 changed files with 83 additions and 24 deletions
+16 -8
View File
@@ -1,17 +1,25 @@
# LangGraph Project # LangGraph Reflection & Rewriting Demo
This project demonstrates a minimal setup for using LangGraph with LangChain OpenAI integration. This project demonstrates a simple LangGraph state machine that includes:
- **Reflection node** logs the current state.
- **Rewriting node** transforms the input string to uppercase.
- **End node** logs the final state.
## Setup ## Setup
```bash ```bash
pip install -r requirements.txt npm install
npm run build
npm start
``` ```
## Running The output will show the state at each node and the final transformed result.
```bash ## Project Structure
python main.py
```
The script will import the necessary modules and print a confirmation message. - `src/langgraph.ts` Defines the state type, node functions, and constructs the graph.
- `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.
+6 -16
View File
@@ -1,25 +1,15 @@
{ {
"name": "self-correcting-agent", "name": "langgraph-reflection-rewrite",
"version": "1.0.0", "version": "1.0.0",
"description": "A simple Node.js implementation of a selfcorrecting agent that uses the OpenAI API to review and improve its own responses.", "main": "dist/index.js",
"main": "index.js",
"scripts": { "scripts": {
"start": "node index.js", "build": "tsc",
"test": "jest" "start": "node dist/index.js"
}, },
"keywords": [
"openai",
"self-correcting",
"agent",
"nodejs"
],
"author": "Your Name",
"license": "MIT",
"dependencies": { "dependencies": {
"dotenv": "^16.4.5", "langgraph": "^0.1.0"
"openai": "^4.20.0"
}, },
"devDependencies": { "devDependencies": {
"jest": "^29.7.0" "typescript": "^5.0.0"
} }
} }
+10
View File
@@ -0,0 +1,10 @@
import { app } from './langgraph';
async function main() {
const result = await app.invoke({ input: 'Hello world' });
console.log('Final result:', result);
}
main().catch((err) => {
console.error('Error during execution:', err);
});
+41
View File
@@ -0,0 +1,41 @@
import { StateGraph } from 'langgraph';
export type State = {
input: string;
output?: string;
};
const startFn = (state: State) => {
// The start node simply passes the initial state through.
return state;
};
const reflection = (state: State) => {
console.log('Reflection node:', state);
return state;
};
const rewriting = (state: State) => {
const newState = { ...state, output: state.input.toUpperCase() };
console.log('Rewriting node:', newState);
return newState;
};
const end = (state: State) => {
console.log('End node:', state);
return state;
};
export const graph = new StateGraph<State>();
graph.addNode('start', startFn);
graph.addNode('reflection', reflection);
graph.addNode('rewriting', rewriting);
graph.addNode('end', end);
graph.setEntryPoint('start');
graph.addEdge('start', 'reflection');
graph.addEdge('reflection', 'rewriting');
graph.addEdge('rewriting', 'end');
export const app = graph.compile();
+10
View File
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src"]
}