You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use validation results to communicate the outcome of requirement validation (#54)
* validate returns a ValidationResult
* tests now working for ValidationResult
(openai backend mypy fix)
* overload simple validate for easier dynamic reasons
---------
Co-authored-by: Hendrik Strobelt <[email protected]>
"""ValidationResults store the output of a Requirement's validation. They can be used to return additional info from validation functions, which is useful for sampling/repairing."""
A ValidationResult's result field always contains a definitive pass/fail. The other fields can be used to communicate additional information about that result.
48
+
49
+
Args:
50
+
result: a boolean that is true if the requirement passed
51
+
reason: a reason for the result
52
+
score: if your validator gives you a score back, you can add this as metadata
53
+
"""
54
+
self._result=result
55
+
self._reason=reason
56
+
self._score=score
57
+
58
+
@property
59
+
defreason(self) ->str|None:
60
+
returnself._reason
61
+
62
+
@property
63
+
defscore(self) ->float|None:
64
+
returnself._score
65
+
66
+
defas_bool(self) ->bool:
67
+
""""""
68
+
returnself._result
69
+
70
+
def__bool__(self) ->bool:
71
+
returnself.as_bool()
72
+
73
+
38
74
classRequirement(Component):
39
75
"""Requirements are a special type of Component used as input to the Validate step in Instruct/Validate/Repair patterns."""
- this operates over the more recent _model output_, not the most recent message.
172
224
- Model outputs are sometimes parsed into more complex types (eg by a `Formatter.parse` call or an OutputProcessor). This validation logic will interpret the most recent output as a string, regardless of whether it has a more complex parsed representation.
225
+
226
+
Args:
227
+
fn: the simple validation function that takes a string and returns either a bool or (bool, str)
228
+
reason: only used if the provided function returns a bool; if the validation function fails, a static reason for that failure to give to the llm when repairing
173
229
"""
174
230
175
-
defvalidate(ctx: Context) ->bool:
231
+
defvalidate(ctx: Context) ->ValidationResult:
176
232
o=ctx.last_output()
177
233
ifoisNoneoro.valueisNone:
178
234
FancyLogger.get_logger().warn(
179
235
"Last output of context was None. That might be a problem. We return validation as False to be able to continue..."
180
236
)
181
-
returnFalse
182
-
returnfn(o.value)
237
+
returnValidationResult(
238
+
False
239
+
) # Don't pass in the static reason since the function didn't run.
240
+
241
+
result=fn(o.value)
242
+
243
+
# Only confirm that the result conforms to the fn type requirements here. Functions can
244
+
# declare return types and then deviate from them.
245
+
246
+
# Oneliner that checks the tuple actually contains (bool, str)
0 commit comments