1010
1111from evaluation_function .truth_table .evaluate import evaluate_truth_table
1212
13+ _JSON_STRING_NOTE = ("note" , "Response was received as a JSON string and was parsed." )
14+
15+
16+ def _feedback_with_json_note (feedback_items : list , response_was_json_string : bool ) -> list :
17+ if not response_was_json_string :
18+ return feedback_items
19+ return list (feedback_items ) + [_JSON_STRING_NOTE ]
20+
1321
1422def evaluation_function (
1523 response : Any ,
@@ -39,29 +47,37 @@ def evaluation_function(
3947 to output the evaluation response.
4048 """
4149
42-
50+ response_was_json_string = isinstance ( response , str )
4351 try :
52+ if response_was_json_string :
53+ response = json .loads (response )
54+
4455 if not isinstance (answer , dict ):
4556 return Result (
4657 is_correct = False ,
47- feedback_items = [("incorrect input" , f"missing answer object. got { answer } " )]
58+ feedback_items = _feedback_with_json_note (
59+ [("incorrect input" , "missing answer object" )],
60+ response_was_json_string ,
61+ )
4862 )
4963
50- # If response is a string, parse it as JSON
51- if isinstance (response , str ):
52- response = json .loads (response )
53-
5464 if not isinstance (response , dict ):
5565 return Result (
5666 is_correct = False ,
57- feedback_items = [("incorrect input" , "missing response object" )]
67+ feedback_items = _feedback_with_json_note (
68+ [("incorrect input" , "missing response object" )],
69+ response_was_json_string ,
70+ )
5871 )
5972
6073 response_formula = response .get ("formula" , None )
6174 if not isinstance (response_formula , str ):
6275 return Result (
6376 is_correct = False ,
64- feedback_items = [("incorrect input" , "response must be type String" )]
77+ feedback_items = _feedback_with_json_note (
78+ [("incorrect input" , "response must be type String" )],
79+ response_was_json_string ,
80+ )
6581 )
6682
6783 formula = formula_parser (response_formula )
@@ -83,12 +99,18 @@ def evaluation_function(
8399 if num_selected == 0 :
84100 return Result (
85101 is_correct = False ,
86- feedback_items = [("invalid param" , "please select a param" )]
102+ feedback_items = _feedback_with_json_note (
103+ [("invalid param" , "please select a param" )],
104+ response_was_json_string ,
105+ )
87106 )
88107 if num_selected > 1 :
89108 return Result (
90109 is_correct = False ,
91- feedback_items = [("invalid param" , "please only select 1 param" )]
110+ feedback_items = _feedback_with_json_note (
111+ [("invalid param" , "please only select 1 param" )],
112+ response_was_json_string ,
113+ )
92114 )
93115
94116 # Truth table mode: validate response truth table if present
@@ -97,21 +119,33 @@ def evaluation_function(
97119 if response_truth_table is None or not isinstance (response_truth_table , dict ):
98120 return Result (
99121 is_correct = False ,
100- feedback_items = [("incorrect input" , "truthTable required when answer expects truth table" )]
122+ feedback_items = _feedback_with_json_note (
123+ [("incorrect input" , "truthTable required when answer expects truth table" )],
124+ response_was_json_string ,
125+ )
101126 )
102127 variables = response_truth_table .get ("variables" , [])
103128 cells = response_truth_table .get ("cells" , [])
104129
105130 if not isinstance (variables , list ) or not isinstance (cells , list ):
106131 return Result (
107132 is_correct = False ,
108- feedback_items = [("incorrect input" , "truthTable must contain 'variables' and 'cells' arrays" )]
133+ feedback_items = _feedback_with_json_note (
134+ [("incorrect input" , "truthTable must contain 'variables' and 'cells' arrays" )],
135+ response_was_json_string ,
136+ )
109137 )
110138
111139 num_atoms = len (_extract_atoms (formula ))
112140 truth_table_result = evaluate_truth_table (variables , cells , num_atoms )
113141 if not truth_table_result .is_correct :
114- return truth_table_result
142+ return Result (
143+ is_correct = False ,
144+ feedback_items = _feedback_with_json_note (
145+ getattr (truth_table_result , "feedback_items" , []) or [],
146+ response_was_json_string ,
147+ )
148+ )
115149
116150 is_correct = False
117151 feedback = []
@@ -160,11 +194,14 @@ def evaluation_function(
160194 is_correct = True # already validated above
161195
162196 if feedback :
163- return Result (is_correct = False , feedback_items = feedback )
197+ return Result (
198+ is_correct = False ,
199+ feedback_items = _feedback_with_json_note (feedback , response_was_json_string ),
200+ )
164201 return Result (is_correct = is_correct )
165202
166203 except Exception as e :
167204 return Result (
168205 is_correct = False ,
169- feedback_items = [("Error" , str (e ))]
206+ feedback_items = _feedback_with_json_note ( [("Error" , str (e ))], response_was_json_string ),
170207 )
0 commit comments