Spaces:
Running
Running
Create self_correction.py
Browse files- self_correction.py +67 -0
self_correction.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# self_correction.py
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
|
| 4 |
+
class SelfCorrector:
|
| 5 |
+
"""
|
| 6 |
+
Implements the "Self-Correction Loop" from Paper 2.
|
| 7 |
+
It diagnoses low scores and maps them to correction plans.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
def __init__(self, threshold: float = 3.0):
|
| 11 |
+
self.threshold = threshold
|
| 12 |
+
|
| 13 |
+
def is_good_enough(self, v_fitness_scores: Dict[str, int]) -> bool:
|
| 14 |
+
"""
|
| 15 |
+
Checks if the solution is good enough to be presented.
|
| 16 |
+
Checks if *all* scores are at or above the threshold.
|
| 17 |
+
"""
|
| 18 |
+
print(f"Checking scores {v_fitness_scores} against threshold {self.threshold}")
|
| 19 |
+
for score in v_fitness_scores.values():
|
| 20 |
+
if score < self.threshold:
|
| 21 |
+
print("Score is too low. Initiating Self-Correction.")
|
| 22 |
+
return False
|
| 23 |
+
print("Score is good. Solution accepted.")
|
| 24 |
+
return True
|
| 25 |
+
|
| 26 |
+
def get_correction_plan(self, v_fitness_json: Dict[str, Any]) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Implements the "Diagnostic Error-to-Belbin Role Mapping" (Paper 2).
|
| 29 |
+
It analyzes the full v_fitness JSON (with justifications)
|
| 30 |
+
and generates a "Chain-of-Thought" correction prompt.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
# 1. Find the lowest-scoring criterion
|
| 34 |
+
lowest_score = 5
|
| 35 |
+
lowest_metric = "None"
|
| 36 |
+
for metric, data in v_fitness_json.items():
|
| 37 |
+
if data.get('score', 5) < lowest_score:
|
| 38 |
+
lowest_score = data.get('score', 5)
|
| 39 |
+
lowest_metric = metric
|
| 40 |
+
|
| 41 |
+
failure_justification = v_fitness_json.get(lowest_metric, {}).get('justification', "No justification provided.")
|
| 42 |
+
|
| 43 |
+
# 2. Map low score to a failure diagnosis (from Paper 2)
|
| 44 |
+
if lowest_metric == "Novelty":
|
| 45 |
+
failure_diagnosis = f"Ideation Failure (Low {lowest_metric}). The judge's feedback was: '{failure_justification}'"
|
| 46 |
+
elif lowest_metric == "Usefulness_Feasibility":
|
| 47 |
+
failure_diagnosis = f"Compositional Error (Low {lowest_metric}). The judge's feedback was: '{failure_justification}'"
|
| 48 |
+
elif lowest_metric == "Cultural_Appropriateness":
|
| 49 |
+
failure_diagnosis = f"Sensitivity Error (Low {lowest_metric}). The judge's feedback was: '{failure_justification}'"
|
| 50 |
+
else:
|
| 51 |
+
failure_diagnosis = f"General Quality Failure (Low {lowest_metric}). The judge's feedback was: '{failure_justification}'"
|
| 52 |
+
|
| 53 |
+
# 3. Generate the "Chain-of-Thought" correction prompt (Paper 2, section 4.9.2)
|
| 54 |
+
correction_prompt = f"""
|
| 55 |
+
YOUR PREVIOUS ATTEMPT FAILED.
|
| 56 |
+
|
| 57 |
+
FAILURE ANALYSIS:
|
| 58 |
+
Your solution was evaluated and received a very low score for {lowest_metric}.
|
| 59 |
+
{failure_diagnosis}
|
| 60 |
+
|
| 61 |
+
YOUR TASK:
|
| 62 |
+
You MUST re-generate a new solution. This new solution must *specifically* address this failure.
|
| 63 |
+
1. **Analyze the Failure**: Briefly explain *why* the previous solution failed to be {lowest_metric.lower()}.
|
| 64 |
+
2. **Formulate a New Plan**: Outline a new plan that directly fixes this specific failure.
|
| 65 |
+
3. **Write the Corrected Solution**: Generate the full, new solution based on this plan.
|
| 66 |
+
"""
|
| 67 |
+
return correction_prompt
|