-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathfitness.py
More file actions
177 lines (145 loc) · 6.66 KB
/
Copy pathfitness.py
File metadata and controls
177 lines (145 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Fitness functions for evaluating evolved artifacts.
Uses LLM-as-judge with rubrics to score agent outputs.
Supports length penalties and multi-dimensional scoring.
"""
import dspy
from dataclasses import dataclass
from typing import Optional
from evolution.core.config import EvolutionConfig
@dataclass
class FitnessScore:
"""Multi-dimensional fitness score."""
correctness: float = 0.0 # Did the agent produce correct output? (0-1)
procedure_following: float = 0.0 # Did it follow the skill's procedure? (0-1)
conciseness: float = 0.0 # Was it appropriately concise? (0-1)
length_penalty: float = 0.0 # Penalty for being too verbose (0-1, 0 = no penalty)
feedback: str = "" # Textual feedback for GEPA's reflective analysis
@property
def composite(self) -> float:
"""Weighted composite score."""
raw = (
0.5 * self.correctness
+ 0.3 * self.procedure_following
+ 0.2 * self.conciseness
)
return max(0.0, raw - self.length_penalty)
class LLMJudge:
"""LLM-as-judge scorer with rubric-based evaluation.
Scores agent outputs on multiple dimensions and provides
textual feedback that GEPA can use for reflective mutation.
"""
class JudgeSignature(dspy.Signature):
"""Evaluate an agent's response against an expected behavior rubric.
Score the response on three dimensions (0.0 to 1.0 each):
1. correctness: Did the response correctly address the task?
2. procedure_following: Did it follow the expected approach/procedure?
3. conciseness: Was it appropriately concise without omitting important info?
Also provide specific, actionable feedback on what could be improved.
"""
task_input: str = dspy.InputField(desc="The task the agent was given")
expected_behavior: str = dspy.InputField(desc="Rubric describing what a good response looks like")
agent_output: str = dspy.InputField(desc="The agent's actual response")
skill_text: str = dspy.InputField(desc="The skill/instructions the agent was following")
correctness: float = dspy.OutputField(desc="Score 0.0-1.0: Did the response correctly address the task?")
procedure_following: float = dspy.OutputField(desc="Score 0.0-1.0: Did it follow the expected procedure?")
conciseness: float = dspy.OutputField(desc="Score 0.0-1.0: Appropriately concise?")
feedback: str = dspy.OutputField(desc="Specific, actionable feedback on what could be improved")
def __init__(self, config: EvolutionConfig):
self.config = config
self.judge = dspy.ChainOfThought(self.JudgeSignature)
def score(
self,
task_input: str,
expected_behavior: str,
agent_output: str,
skill_text: str,
artifact_size: Optional[int] = None,
max_size: Optional[int] = None,
) -> FitnessScore:
"""Score an agent output using LLM-as-judge."""
lm = dspy.LM(self.config.eval_model)
with dspy.context(lm=lm):
result = self.judge(
task_input=task_input,
expected_behavior=expected_behavior,
agent_output=agent_output,
skill_text=skill_text,
)
# Parse scores (clamp to 0-1)
correctness = _parse_score(result.correctness)
procedure_following = _parse_score(result.procedure_following)
conciseness = _parse_score(result.conciseness)
# Length penalty
length_penalty = 0.0
if artifact_size is not None and max_size is not None:
ratio = artifact_size / max_size
if ratio > 0.9:
# Penalty ramps from 0 at 90% to 0.3 at 100%+
length_penalty = min(0.3, (ratio - 0.9) * 3.0)
return FitnessScore(
correctness=correctness,
procedure_following=procedure_following,
conciseness=conciseness,
length_penalty=length_penalty,
feedback=str(result.feedback),
)
def skill_fitness_metric(
example: dspy.Example,
prediction: dspy.Prediction,
trace=None,
pred_name=None,
pred_trace=None,
):
"""DSPy-compatible metric function for skill optimization.
This is what gets passed to dspy.GEPA(metric=...). GEPA's
GEPAFeedbackMetric protocol calls it with (gold, pred, trace, pred_name,
pred_trace); MIPROv2 and direct holdout scoring call it with the first
two or three arguments only, so the extra parameters default to None.
Returns a float 0-1 score — except when GEPA requests predictor-level
feedback (pred_name is not None), where it returns
dspy.Prediction(score=..., feedback=...) with a deterministic hint about
which expected-behavior terms are missing, giving the reflection LM
something concrete to act on.
"""
# The prediction should have an 'output' field with the agent's response
agent_output = getattr(prediction, "output", "") or ""
expected = getattr(example, "expected_behavior", "") or ""
if not agent_output.strip():
if pred_name is not None:
return dspy.Prediction(score=0.0, feedback="The response was empty.")
return 0.0
# Quick heuristic scoring (for speed during optimization)
# Full LLM-as-judge scoring is expensive — use it selectively
score = 0.5 # Base score for non-empty output
# Check if key phrases from expected behavior appear
expected_lower = expected.lower()
output_lower = agent_output.lower()
# Simple keyword overlap as a fast proxy
expected_words = set(expected_lower.split())
output_words = set(output_lower.split())
missing: list[str] = []
if expected_words:
overlap = len(expected_words & output_words) / len(expected_words)
score = 0.3 + (0.7 * overlap)
missing = sorted(
w for w in (expected_words - output_words) if len(w) > 4
)[:8]
score = min(1.0, max(0.0, score))
if pred_name is not None:
if missing:
feedback = (
f"Score {score:.2f}. The response does not address these "
f"expected-behavior elements: {', '.join(missing)}."
)
else:
feedback = f"Score {score:.2f}. The response covers the expected behavior."
return dspy.Prediction(score=score, feedback=feedback)
return score
def _parse_score(value) -> float:
"""Parse a score value, handling various LLM output formats."""
if isinstance(value, (int, float)):
return min(1.0, max(0.0, float(value)))
try:
return min(1.0, max(0.0, float(str(value).strip())))
except (ValueError, TypeError):
return 0.5 # Default to neutral on parse failure