Implement a self-critique wrapper that:
- Calls generate_func() to get initial output
- Calls critique_func(output) to evaluate
- If critique returns
passed: False, regenerate with feedback - Repeat up to max_iterations
- Return final result with history of all attempts
History Format:
[{'output': ..., 'critique': {'passed': ..., 'feedback': ...}}, ...]
Constraints:
- Always return after max_iterations even if not passed
- passed=True stops early
- Include all attempts in history
Examples
Example 1:
Input:
with_self_critique(lambda: 'draft', lambda x: {'passed': True, 'feedback': 'good'}, 2)Output:
{'final_output': 'draft', 'iterations': 1, 'passed': True, 'history': [{'output': 'draft', 'critique': {'passed': True, 'feedback': 'good'}}]}Explanation: Passes on first try, stops early
Example 2:
Input:
with_self_critique(lambda: 'x', lambda x: {'passed': False, 'feedback': 'bad'}, 1)['passed']Output:
FalseExplanation: Fails critique, hits max iterations, returns passed=False
Starter Code
def with_self_critique(generate_func, critique_func, max_iterations=2):
"""
Wrapper that adds self-critique to any generation function.
Args:
generate_func: Function() -> output
critique_func: Function(output) -> {'passed': bool, 'feedback': str}
max_iterations: Maximum generation-critique cycles
Returns:
dict with 'final_output', 'iterations', 'passed', 'history'
"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready