1
- import ast
2
- import json
3
- from typing import Literal , Optional , Dict , Any
1
+ from typing import Optional
4
2
from langevals_core .base_evaluator import (
5
3
BaseEvaluator ,
6
- EvaluationResultSkipped ,
7
4
EvaluatorEntry ,
8
5
EvaluationResult ,
9
6
EvaluatorSettings ,
10
7
SingleEvaluationResult ,
11
- EvaluationResultError ,
12
8
)
13
- import markdown
14
9
from pydantic import Field
15
- import sqlglot
16
10
17
11
18
12
class ExactMatchSettings (EvaluatorSettings ):
19
- pass
13
+ case_sensitive : bool = Field (
14
+ default = False ,
15
+ description = "True if the comparison should be case-sensitive, False otherwise" ,
16
+ )
17
+ trim_whitespace : bool = Field (
18
+ default = True ,
19
+ description = "True if the comparison should trim whitespace, False otherwise" ,
20
+ )
21
+ remove_punctuation : bool = Field (
22
+ default = True ,
23
+ description = "True if the comparison should remove punctuation, False otherwise" ,
24
+ )
25
+
20
26
21
27
22
28
class ExactMatchResult (EvaluationResult ):
@@ -35,7 +41,8 @@ class ExactMatchEvaluator(
35
41
BaseEvaluator [ExactMatchEntry , ExactMatchSettings , ExactMatchResult ]
36
42
):
37
43
"""
38
- A simple evaluator that checks if the output matches the input exactly.
44
+ A simple evaluator that checks if the output matches the input exactly, with some
45
+ extra bells and whistles to help with whitespace related shenanigans.
39
46
"""
40
47
41
48
name = "Exact Match Evaluator"
@@ -44,7 +51,26 @@ class ExactMatchEvaluator(
44
51
is_guardrail = False
45
52
46
53
def evaluate (self , entry : ExactMatchEntry ) -> SingleEvaluationResult :
47
- if entry .input == entry .output :
48
- return ExactMatchResult (passed = True )
54
+ # Get input and output
55
+ input_text = entry .input or ""
56
+ output_text = entry .output or ""
57
+
58
+ # Apply settings
59
+ if self .settings .trim_whitespace :
60
+ input_text = input_text .strip ()
61
+ output_text = output_text .strip ()
62
+
63
+ if self .settings .remove_punctuation :
64
+ input_text = '' .join (char for char in input_text if char .isalnum () or char .isspace ())
65
+ output_text = '' .join (char for char in output_text if char .isalnum () or char .isspace ())
66
+
67
+ if not self .settings .case_sensitive :
68
+ input_text = input_text .lower ()
69
+ output_text = output_text .lower ()
70
+
71
+ # Perform comparison
72
+ passed = input_text == output_text
49
73
50
- return ExactMatchResult (passed = False )
74
+ # Return result
75
+ return ExactMatchResult (passed = passed )
76
+
0 commit comments