A Streamlit application demonstrating the Reflection Agentic Pattern using a Producer-Critic feedback loop for iterative code refinement.
The Reflection Pattern is an agentic design pattern that involves two key agents working together:
- Producer Agent: Generates initial Python code based on user requirements
- Critic Agent: Acts as a Senior Staff Engineer, evaluating the code for logic, efficiency, PEP8 compliance, and best practices
- Feedback Loop: The Producer refines the code based on the Critic's feedback through multiple iterations
This iterative process continues until the code is approved by the Critic or the maximum number of iterations is reached.
User Prompt → Producer Agent → Generated Code
↑ ↓
| Critic Agent
| ↓
└──── Feedback ──┘
- Interactive UI: Clean Streamlit interface with real-time feedback
- Configurable Iterations: Adjust the maximum number of refinement cycles
- Thought Process Visualization: View each iteration's draft and critique
- Syntax Highlighting: Final code displayed with proper formatting
- Download Capability: Export the generated code as a Python file
- Environment Variable Security: No hardcoded API keys
- Python 3.8 or higher
- OpenAI API key
- Internet connection
git clone https://github.com/drkarim/Multi-Agentic-Patterns.git
cd Multi-Agentic-Patterns/reflection-patternpip install -r requirements.txtThe application requires an OpenAI API key to function. Do not hardcode your API key in the code.
$env:OPENAI_API_KEY="sk-your-api-key-here"set OPENAI_API_KEY=sk-your-api-key-hereexport OPENAI_API_KEY="sk-your-api-key-here"Create a .env file in the reflection-pattern/ directory:
OPENAI_API_KEY=sk-your-api-key-hereThe application will automatically load this file using python-dotenv.
After setting the environment variable, run:
streamlit run app.pyThe application will open in your default web browser at http://localhost:8501.
- Use the sidebar to adjust the Max Iterations (1-5)
- Verify that the OpenAI API key is detected (green checkmark)
In the main text area, describe the Python code you want to generate. Examples:
- "Create a function to calculate Fibonacci numbers using memoization"
- "Write a class for managing a simple todo list with add, remove, and list operations"
- "Implement a binary search algorithm with proper error handling"
Click the 🚀 Generate Code button to start the reflection process.
- Metrics: View the total iterations, status, and max allowed
- Thought Process: Expand each iteration to see the draft code and critique
- Final Code: Review the approved or final version with syntax highlighting
Use the 📥 Download Code button to save the generated code as a .py file.
Try these prompts to see the Reflection Pattern in action:
-
Email Validator
Create a function to validate email addresses using regex with proper error handling -
Fibonacci with Memoization
Write a function to calculate Fibonacci numbers using memoization for efficiency -
Binary Search
Implement a binary search algorithm that handles edge cases and returns the index -
Execution Timer Decorator
Create a decorator that logs the execution time of any function it wraps
reflection-pattern/
├── app.py # Main Streamlit application
├── agents.py # Producer and Critic agent implementations
├── requirements.txt # Python dependencies
├── README.md # This file
└── .env # (Optional) Environment variables
- Framework: LangChain with OpenAI's GPT models
- Producer Agent: Uses
temperature=0.7for creative code generation - Critic Agent: Uses
temperature=0.3for consistent, analytical feedback - Pattern: LCEL (LangChain Expression Language) for chain composition
- Default:
gpt-4.1-mini(configurable inagents.py) - Can be changed to
gpt-4,gpt-3.5-turbo, or other OpenAI models
- API keys are retrieved from environment variables only
- No hardcoded credentials in the codebase
.envfile support for local development (should be added to.gitignore)
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is part of the Multi-Agentic-Patterns repository. Please refer to the main repository for license information.
Solution: Ensure you've set the OPENAI_API_KEY environment variable before running the app.
Solution: Make sure all dependencies are installed:
pip install -r requirements.txtSolution: Check your internet connection and verify your OpenAI API key is valid and has available credits.
- Repository Owner: drkarim
- Pattern Implementation: Manus AI
- LangChain team for the excellent framework
- Streamlit for the intuitive UI library
- OpenAI for the powerful language models