86 lines
2.2 KiB
Markdown
86 lines
2.2 KiB
Markdown
# Graph with Reflection and Rewriting Nodes
|
||
|
||
This project demonstrates a simple data processing graph in **Python** that uses **LangChain** with **OpenAI** or **Ollama** to perform reflection and rewriting of text.
|
||
The graph is built from reusable node classes and can be extended with additional nodes as needed.
|
||
|
||
## Features
|
||
|
||
- **ReflectionNode** – Generates reflective insights from input text using an LLM.
|
||
- **RewritingNode** – Rewrites the reflection in a specified style (e.g., formal, concise).
|
||
- **Graph** – Connects nodes and executes them in sequence.
|
||
- **Configurable LLM provider** – Switch between OpenAI and Ollama via the `LLM_PROVIDER` environment variable.
|
||
- **Unit tests** – Verify node behavior with mocked LLM responses.
|
||
|
||
## Requirements
|
||
|
||
- Python 3.10+
|
||
- `langchain`
|
||
- `openai` (for OpenAI provider)
|
||
- `python-dotenv` (optional, for loading environment variables)
|
||
|
||
Install dependencies:
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
## Configuration
|
||
|
||
Set the LLM provider by defining the `LLM_PROVIDER` environment variable:
|
||
|
||
```bash
|
||
export LLM_PROVIDER=openai # or ollama
|
||
```
|
||
|
||
If using OpenAI, ensure that the `OPENAI_API_KEY` environment variable is set.
|
||
If using Ollama, ensure that the Ollama server is running locally and the model name matches the one configured in `src/llm_integration.py`.
|
||
|
||
## Usage
|
||
|
||
Run the graph with a text input:
|
||
|
||
```bash
|
||
python -m src.main "Your input text goes here."
|
||
```
|
||
|
||
Or pipe text via stdin:
|
||
|
||
```bash
|
||
echo "Some text" | python -m src.main
|
||
```
|
||
|
||
The output will be the rewritten text produced by the `RewritingNode`.
|
||
|
||
## Running Tests
|
||
|
||
Execute the test suite with:
|
||
|
||
```bash
|
||
python -m unittest discover tests
|
||
```
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
src/
|
||
├── llm_integration.py # LLM client factory
|
||
├── nodes.py # Node definitions
|
||
├── graph.py # Graph construction and execution
|
||
└── main.py # CLI entry point
|
||
tests/
|
||
└── test_nodes.py # Unit tests for nodes
|
||
requirements.txt
|
||
README.md
|
||
```
|
||
|
||
## Extending the Graph
|
||
|
||
To add new nodes:
|
||
|
||
1. Create a new class inheriting from `BaseNode` in `src/nodes.py`.
|
||
2. Implement the `process` method.
|
||
3. Add the node to the graph in `src/graph.py` and connect it with `add_edge`.
|
||
|
||
## License
|
||
|
||
MIT License |