feat: solution for 'Повторный экзамен #2: Граф с рефлексией на код'
This commit is contained in:
@@ -1,38 +1,26 @@
|
|||||||
# Graph with Reflection on Code
|
# Graph with Reflection on Code
|
||||||
|
|
||||||
A lightweight Python project that demonstrates how to build a **LangGraph** workflow powered by **LangChain** and the **OpenAI** API.
|
This repository demonstrates how to build a conversational agent that can analyze and reflect on Python code using **LangGraph** and **LangChain OpenAI**. The agent can parse code, generate explanations, and answer questions about the code structure.
|
||||||
The graph processes a piece of code, generates a reflection on it, and returns a concise summary.
|
|
||||||
|
|
||||||
> **Repository**: <https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-2-graf-s-refleksiey-na>
|
## Features
|
||||||
|
|
||||||
---
|
- **LangGraph**: Orchestrates the conversation flow and manages state across multiple turns.
|
||||||
|
- **LangChain OpenAI**: Provides language model capabilities via OpenAI’s GPT-4 (or any compatible model).
|
||||||
|
- Code parsing and analysis using the `ast` module.
|
||||||
|
- Interactive CLI for asking questions about a Python file.
|
||||||
|
|
||||||
## 📌 Overview
|
## Getting Started
|
||||||
|
|
||||||
- **LangGraph** – orchestrates the flow of data between nodes.
|
### Prerequisites
|
||||||
- **LangChain** – provides the language model wrappers and utilities.
|
|
||||||
- **OpenAI** – the LLM that performs code analysis and reflection.
|
|
||||||
|
|
||||||
The workflow consists of three main nodes:
|
- Python 3.10+
|
||||||
|
- An OpenAI API key. Set it in your environment:
|
||||||
|
|
||||||
1. **Input Node** – receives raw code.
|
```bash
|
||||||
2. **Analysis Node** – calls the OpenAI model to analyze the code.
|
export OPENAI_API_KEY="your_api_key_here"
|
||||||
3. **Reflection Node** – generates a reflection and summary.
|
```
|
||||||
|
|
||||||
The graph is defined in `graph.py` and can be executed via the CLI or imported as a library.
|
### Installation
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 🚀 Features
|
|
||||||
|
|
||||||
- **Code Analysis** – extracts key functions, classes, and comments.
|
|
||||||
- **Reflection Generation** – produces a human‑readable reflection on the code quality, style, and potential improvements.
|
|
||||||
- **Modular Design** – each node can be replaced or extended independently.
|
|
||||||
- **OpenAI Integration** – uses the `gpt-4o-mini` model by default (configurable).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 🛠️ Installation
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Clone the repository
|
# Clone the repository
|
||||||
@@ -41,76 +29,53 @@ cd povtornyy-ekzamen-2-graf-s-refleksiey-na
|
|||||||
|
|
||||||
# Create a virtual environment (optional but recommended)
|
# Create a virtual environment (optional but recommended)
|
||||||
python -m venv .venv
|
python -m venv .venv
|
||||||
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
|
||||||
|
|
||||||
# Install dependencies
|
# Install dependencies
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
```
|
```
|
||||||
|
|
||||||
> **Requirements**
|
`requirements.txt` contains:
|
||||||
> - Python 3.10+
|
|
||||||
> - `langgraph`, `langchain`, `openai` (listed in `requirements.txt`)
|
|
||||||
> - An OpenAI API key set as the environment variable `OPENAI_API_KEY`.
|
|
||||||
|
|
||||||
---
|
```
|
||||||
|
langchain==0.2.0
|
||||||
|
langgraph==0.1.0
|
||||||
|
openai==1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
## 📦 Usage
|
### Usage
|
||||||
|
|
||||||
### Command‑Line
|
Run the main script and provide the path to a Python file you want to analyze:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python main.py --file path/to/your_code.py
|
python main.py path/to/your_script.py
|
||||||
```
|
```
|
||||||
|
|
||||||
The script will:
|
You will be prompted to ask questions about the code. The agent will respond using the OpenAI model and the conversation graph.
|
||||||
|
|
||||||
1. Load the file content.
|
### Example
|
||||||
2. Run it through the LangGraph workflow.
|
|
||||||
3. Print the reflection and summary to the console.
|
|
||||||
|
|
||||||
### Programmatic
|
```bash
|
||||||
|
$ python main.py example.py
|
||||||
```python
|
Enter your question (or type 'exit' to quit): What does the `add` function do?
|
||||||
from graph import CodeReflectionGraph
|
The `add` function takes two numbers, `a` and `b`, and returns their sum.
|
||||||
|
|
||||||
graph = CodeReflectionGraph()
|
|
||||||
result = graph.run(code="def hello():\n print('Hello, world!')")
|
|
||||||
print(result["reflection"])
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
## Project Structure
|
||||||
|
|
||||||
## 📁 Project Structure
|
|
||||||
|
|
||||||
```
|
```
|
||||||
povtornyy-ekzamen-2-graf-s-refleksiey-na/
|
povtornyy-ekzamen-2-graf-s-refleksiey-na/
|
||||||
├── graph.py # LangGraph workflow definition
|
├── main.py # Entry point
|
||||||
├── main.py # CLI entry point
|
├── code_analyzer.py # Code parsing utilities
|
||||||
├── requirements.txt # Python dependencies
|
├── graph.py # LangGraph definition
|
||||||
├── README.md # This file
|
├── requirements.txt
|
||||||
└── tests/
|
└── README.md
|
||||||
└── test_graph.py # Unit tests
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
## License
|
||||||
|
|
||||||
## 🤝 Contributing
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
||||||
|
|
||||||
Feel free to open issues or submit pull requests.
|
|
||||||
Please follow the existing coding style and add tests for new features.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📄 License
|
*This project was developed as part of a coursework assignment. It showcases the integration of LangGraph and LangChain OpenAI for code analysis and reflection.*
|
||||||
|
|
||||||
MIT License – see the [LICENSE](LICENSE) file for details.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 📞 Contact
|
|
||||||
|
|
||||||
- **Author**: Artur Kuzakhmetov
|
|
||||||
- **Email**: artur.kuzakhmetov@example.com
|
|
||||||
|
|
||||||
---
|
|
||||||
END
|
|
||||||
Reference in New Issue
Block a user