Evaluator Optimizer
agentspatternsclaude-cookbooks
Export
Evaluator-Optimizer Workflow
In this workflow, one LLM call generates a response while another provides evaluation and feedback in a loop.
When to use this workflow
This workflow is particularly effective when we have:
- Clear evaluation criteria
- Value from iterative refinement
The two signs of good fit are:
- LLM responses can be demonstrably improved when feedback is provided
- The LLM can provide meaningful feedback itself
[1]
Example Use Case: Iterative coding loop
[ ]
=== GENERATION START ===
Thoughts:
The task requires implementing a Stack with constant time operations including finding minimum.
To achieve O(1) for getMin(), we need to maintain a second stack that keeps track of minimums.
Each time we push, if the value is smaller than current min, we add it to minStack.
When we pop, if the popped value equals current min, we also pop from minStack.
Generated:
```python
class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, x: int) -> None:
self.stack.append(x)
if not self.minStack or x <= self.minStack[-1]:
self.minStack.append(x)
def pop(self) -> None:
if not self.stack:
return
if self.stack[-1] == self.minStack[-1]:
self.minStack.pop()
self.stack.pop()
def getMin(self) -> int:
if not self.minStack:
return None
return self.minStack[-1]
```
=== GENERATION END ===
=== EVALUATION START ===
Status: NEEDS_IMPROVEMENT
Feedback:
While the implementation is generally correct and achieves O(1) time complexity for all operations, there are several areas for improvement:
1. Error Handling:
- pop() should raise an exception when stack is empty rather than silently returning
- getMin() should raise an exception when stack is empty rather than returning None
- These behaviors should be consistent with standard stack implementations
2. Type Hints:
- Return type hint for pop() should be None or void
- Missing type hints for class variables stack and minStack
3. Documentation:
- Missing docstrings for class and methods
- Missing parameter descriptions
4. Edge Cases:
- No explicit handling of invalid input types for push()
The core algorithm is correct and efficient, but the implementation could be more robust and better documented for production use.
=== EVALUATION END ===
=== GENERATION START ===
Thoughts:
Based on the feedback, I'll improve the implementation by:
1. Adding proper error handling with exceptions
2. Including type hints and docstrings
3. Adding input validation
4. Maintaining O(1) time complexity for all operations
Generated:
```python
from typing import List
class MinStack:
"""A stack that supports push, pop, and getting minimum element in O(1) time."""
def __init__(self):
"""Initialize empty stack with two internal lists."""
self.stack: List[int] = []
self.minStack: List[int] = []
def push(self, x: int) -> None:
"""
Push element onto stack and update minimum stack.
Args:
x: Integer to push onto stack
Raises:
TypeError: If x is not an integer
"""
if not isinstance(x, int):
raise TypeError("Input must be an integer")
self.stack.append(x)
if not self.minStack or x <= self.minStack[-1]:
self.minStack.append(x)
def pop(self) -> None:
"""
Remove and return top element from stack.
Raises:
IndexError: If stack is empty
"""
if not self.stack:
raise IndexError("Cannot pop from empty stack")
if self.stack[-1] == self.minStack[-1]:
self.minStack.pop()
self.stack.pop()
def getMin(self) -> int:
"""
Get minimum element in stack.
Returns:
int: Minimum element in stack
Raises:
IndexError: If stack is empty
"""
if not self.minStack:
raise IndexError("Cannot get minimum from empty stack")
return self.minStack[-1]
```
=== GENERATION END ===