2.2 KiB
2.2 KiB
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_PROVIDERenvironment variable. - Unit tests – Verify node behavior with mocked LLM responses.
Requirements
- Python 3.10+
langchainopenai(for OpenAI provider)python-dotenv(optional, for loading environment variables)
Install dependencies:
pip install -r requirements.txt
Configuration
Set the LLM provider by defining the LLM_PROVIDER environment variable:
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:
python -m src.main "Your input text goes here."
Or pipe text via stdin:
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:
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:
- Create a new class inheriting from
BaseNodeinsrc/nodes.py. - Implement the
processmethod. - Add the node to the graph in
src/graph.pyand connect it withadd_edge.
License
MIT License