-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjson_schema.py
More file actions
444 lines (397 loc) · 17.2 KB
/
json_schema.py
File metadata and controls
444 lines (397 loc) · 17.2 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import json
import re
from typing import Any, Dict, List, Optional, Union
from ..models import EvaluateResult, Message, MetricResult, ChatCompletionContentPartTextParam
from ..typed_interface import reward_function
from .function_calling import (
calculate_jaccard_similarity,
extract_schema_properties,
normalize_schema,
)
@reward_function
def json_schema_reward(
messages: Union[List[Message], List[Dict[str, Any]]],
ground_truth: Optional[Union[List[Message], List[Dict[str, Any]]]] = None,
json_content: Optional[Union[Dict[str, Any], str]] = None,
expected_schema: Optional[Union[Dict[str, Any], str]] = None,
**kwargs,
) -> EvaluateResult:
"""
Evaluate JSON content against an expected schema using Jaccard similarity.
The model's response (containing JSON) is assumed to be the last message in the `messages` list.
This reward function compares the structure of JSON content against an
expected schema and calculates a similarity score using Jaccard similarity.
It repurposes the same approach used for function calling validation but for
general JSON schema validation.
Args:
messages: List of conversation messages, where `messages[-1]` is the model's response.
ground_truth: Optional. Expected assistant response trajectory. Not directly used by this reward.
json_content: The JSON content to evaluate (if not provided, extracts
from the last message).
expected_schema: The expected schema for the JSON content.
**kwargs: Additional keyword arguments.
Returns:
EvaluateResult with score and metrics
"""
metrics = {}
if json_content is None:
if not messages:
return EvaluateResult(
score=0.0,
reason="No messages provided to extract JSON content.",
metrics={"error": MetricResult(score=0.0, reason="No messages provided", is_score_valid=False)},
)
last_message = messages[-1]
content_text = ""
if isinstance(last_message, Message):
if last_message.role == "assistant" and last_message.content is not None:
# Coerce to string if content is list parts
if isinstance(last_message.content, str):
content_text = last_message.content
else:
try:
parts: List[ChatCompletionContentPartTextParam] = last_message.content # type: ignore[assignment]
content_text = "\n".join(getattr(p, "text", "") for p in parts)
except Exception:
content_text = ""
else:
return EvaluateResult(
score=0.0,
reason="Last message is not a valid assistant response to extract JSON from.",
metrics={
"error": MetricResult(
score=0.0,
reason="Invalid assistant message for JSON extraction.",
is_score_valid=False,
)
},
)
elif isinstance(last_message, dict):
if last_message.get("role") == "assistant" and last_message.get("content") is not None:
raw_content = last_message.get("content", "")
content_text = raw_content if isinstance(raw_content, str) else ""
else:
return EvaluateResult(
score=0.0,
reason="Last message is not a valid assistant response (dict) to extract JSON from.",
metrics={
"error": MetricResult(
score=0.0,
reason="Invalid assistant message (dict) for JSON extraction.",
is_score_valid=False,
)
},
)
else:
return EvaluateResult(
score=0.0,
reason=f"Unexpected type for last message: {type(last_message)}.",
metrics={
"error": MetricResult(
score=0.0,
reason="Invalid message type for JSON extraction.",
is_score_valid=False,
)
},
)
extracted_json_str = None
if content_text:
try:
pattern = r"```(?:json)?\s*([\s\S]*?)```"
code_blocks = re.findall(pattern, content_text)
if code_blocks:
extracted_json_str = code_blocks[0]
else:
json_match = re.search(r"(\{[\s\S]*\}|\[[\s\S]*\])", content_text, re.DOTALL)
if json_match:
try:
json.loads(json_match.group(0))
extracted_json_str = json_match.group(0)
except json.JSONDecodeError:
pass
except Exception:
pass
if extracted_json_str:
json_content = extracted_json_str
if not json_content:
return EvaluateResult(
score=0.0,
reason="No JSON content found in messages.",
metrics={
"error": MetricResult(
score=0.0,
reason="No JSON content found in messages",
is_score_valid=False,
)
},
)
if expected_schema is None:
return EvaluateResult(
score=0.0,
reason="No expected schema provided for comparison.",
metrics={
"error": MetricResult(
score=0.0,
reason="No expected schema provided",
is_score_valid=False,
)
},
)
expected_schema = normalize_schema(expected_schema)
try:
if isinstance(json_content, str):
parsed_content = json.loads(json_content)
else:
parsed_content = json_content
except json.JSONDecodeError:
return EvaluateResult(
score=0.0,
reason=f"Invalid JSON content: {json_content}",
metrics={
"error": MetricResult(
score=0.0,
reason=f"Invalid JSON content: {json_content}",
is_score_valid=False,
)
},
)
# Function to recursively build a schema from content
def build_schema_from_content(content: Any) -> Dict[str, Any]:
if isinstance(content, dict):
schema: Dict[str, Any] = {"type": "object", "properties": {}}
for key, value in content.items():
if isinstance(schema["properties"], dict): # Should always be true
schema["properties"][key] = build_schema_from_content(value)
return schema
elif isinstance(content, list):
if content:
return {
"type": "array",
"items": build_schema_from_content(content[0]),
}
return {"type": "array"}
elif isinstance(content, str):
return {"type": "string"}
elif isinstance(content, bool):
return {"type": "boolean"}
elif isinstance(content, (int, float)):
return {"type": "number"}
elif content is None:
return {"type": "null"}
else:
return {"type": "any"}
content_schema = build_schema_from_content(parsed_content)
expected_properties = extract_schema_properties(expected_schema)
actual_properties = extract_schema_properties(content_schema)
schema_similarity = calculate_jaccard_similarity(expected_properties, actual_properties)
missing_props = expected_properties - actual_properties
extra_props = actual_properties - expected_properties
matching_props = expected_properties.intersection(actual_properties)
comparison_details = []
if matching_props:
comparison_details.append(f"Matching properties ({len(matching_props)}):")
for prop, prop_type in sorted(matching_props):
comparison_details.append(f" - {prop}: {prop_type}")
if missing_props:
comparison_details.append(f"Missing properties ({len(missing_props)}):")
for prop, prop_type in sorted(missing_props):
comparison_details.append(f" - {prop}: {prop_type}")
if extra_props:
comparison_details.append(f"Extra properties ({len(extra_props)}):")
for prop, prop_type in sorted(extra_props):
comparison_details.append(f" - {prop}: {prop_type}")
schema_comparison_reason = "\n".join(comparison_details)
metrics["schema_similarity"] = MetricResult(
score=schema_similarity,
reason=f"Schema similarity: {schema_similarity:.2f}\n{schema_comparison_reason}",
is_score_valid=schema_similarity == 1.0,
)
final_score = schema_similarity
final_reason = f"Final score based on schema similarity: {final_score:.2f}."
return EvaluateResult(score=final_score, reason=final_reason, metrics=metrics)
def json_schema_reward_with_llm_judge(
messages: Union[List[Message], List[Dict[str, Any]]],
ground_truth: Optional[Union[List[Message], List[Dict[str, Any]]]] = None,
json_content: Optional[Union[Dict[str, Any], str]] = None,
expected_schema: Optional[Union[Dict[str, Any], str]] = None,
expected_behavior: Optional[str] = None,
openai_api_key: Optional[str] = None,
model: str = "gpt-4o-mini",
temperature: float = 0.0,
weights: Optional[Dict[str, float]] = None,
**kwargs,
) -> EvaluateResult:
"""
Combined reward function that evaluates JSON content using both schema
validation and LLM judgment.
Args:
messages: The conversation messages, where `messages[-1]` is the model's response.
ground_truth: Optional. Expected assistant response trajectory. Not directly used by this reward.
json_content: The JSON content to evaluate (if not provided, extracts
from the last message).
expected_schema: The expected schema for the JSON content.
expected_behavior: Description of the expected behavior/content
openai_api_key: OpenAI API key (if not provided, uses environment variable)
model: Model to use for LLM evaluation (default: gpt-4o-mini)
temperature: Temperature for the model generation (default: 0.0)
weights: Dictionary of weights for each component
(default: {"schema": 0.7, "llm": 0.3})
**kwargs: Additional keyword arguments
Returns:
EvaluateResult with score and metrics
"""
# Import OpenAI at call time to make this optional
try:
from openai import OpenAI
except ImportError:
return EvaluateResult(
score=0.0,
reason="OpenAI package not installed.",
metrics={
"error": MetricResult(
score=0.0,
reason="OpenAI package not installed. Install it with: pip install openai",
is_score_valid=False,
)
},
)
if weights is None:
weights = {"schema": 0.7, "llm": 0.3}
total_weight = sum(weights.values())
normalized_weights = {k: v / total_weight for k, v in weights.items()}
schema_result = json_schema_reward(
messages=messages,
ground_truth=ground_truth,
json_content=json_content,
expected_schema=expected_schema,
**kwargs,
)
llm_score = 0.0
llm_reason = "Skipped: No expected behavior provided"
if expected_behavior:
if json_content is None:
if "error" in schema_result.metrics:
return schema_result
last_message = messages[-1]
assert last_message is not None, "Last message is None"
# Support both dict-shaped messages and pydantic Message objects
if isinstance(last_message, dict):
content = last_message.get("content", "")
else:
try:
content = getattr(last_message, "content", "")
except Exception:
content = ""
json_str_from_msg = ""
try:
pattern = r"```(?:json)?\s*([\s\S]*?)```"
code_blocks = re.findall(pattern, content)
if code_blocks:
json_str_from_msg = code_blocks[0]
else:
json_matches = re.findall(r"\{.*\}", content, re.DOTALL)
if json_matches:
json_str_from_msg = json_matches[0]
except Exception:
pass
try:
if json_str_from_msg:
json_content = json.loads(json_str_from_msg)
except json.JSONDecodeError:
json_content = json_str_from_msg
if isinstance(json_content, dict):
json_str_for_llm = json.dumps(json_content, indent=2)
else:
json_str_for_llm = str(json_content)
expected_schema_str = json.dumps(expected_schema, indent=2) if expected_schema else "No schema provided"
conversation_msg = "No conversation context provided"
if messages:
conversation_parts = []
for msg in messages[:-1]:
if isinstance(msg, dict):
role = msg.get("role", "")
content_part = msg.get("content", "")
else:
# Fallback for Message objects
role = getattr(msg, "role", "")
content_part = getattr(msg, "content", "")
if role and content_part:
conversation_parts.append(f"{role}: {content_part}")
if conversation_parts:
conversation_msg = "\n".join(conversation_parts)
prompt = f"""You are evaluating the quality of JSON content provided by an AI assistant.
Your job is to assess whether the JSON structure and content is appropriate, correctly formatted,
and follows the expected schema and behavior.
CONVERSATION CONTEXT:
{conversation_msg}
JSON CONTENT:
{json_str_for_llm}
EXPECTED SCHEMA:
{expected_schema_str}
EXPECTED BEHAVIOR/CONTENT:
{expected_behavior}
Evaluate the JSON content and provide:
1. A score from 0.0 to 1.0 (where 1.0 is perfect)
2. A detailed explanation of your rating
3. Specific issues or strengths of the JSON content
Format your response as:
SCORE: [number between 0.0 and 1.0]
EXPLANATION: [your detailed explanation]
"""
try:
import os
api_key = openai_api_key or os.environ.get("OPENAI_API_KEY")
if not api_key:
raise ValueError("OpenAI API key not provided")
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model=model,
temperature=temperature,
messages=[{"role": "user", "content": prompt}],
)
llm_response = response.choices[0].message.content or ""
score_match = re.search(r"SCORE:\s*([\d.]+)", llm_response)
explanation_match = re.search(r"EXPLANATION:\s*(.*)", llm_response, re.DOTALL)
if score_match:
try:
llm_score = float(score_match.group(1))
llm_score = max(0.0, min(llm_score, 1.0))
except ValueError:
llm_score = 0.5
else:
llm_score = 0.5
llm_reason = explanation_match.group(1).strip() if explanation_match else "No explanation provided"
except Exception as e:
llm_score = 0.0
llm_reason = f"Error calling OpenAI API: {str(e)}"
combined_metrics = {}
for key, metric_val in schema_result.metrics.items():
if key != "schema_similarity":
combined_metrics[f"schema_{key}"] = metric_val
else:
combined_metrics[key] = metric_val
combined_metrics["llm_judge"] = MetricResult(
score=llm_score,
reason=llm_reason,
is_score_valid=llm_score >= 0.8,
)
combined_metrics["schema_score"] = MetricResult(
score=schema_result.score,
reason=f"Schema validation score: {schema_result.score:.2f}",
is_score_valid=schema_result.score == 1.0,
)
combined_metrics["llm_score"] = MetricResult(
score=llm_score,
reason=f"LLM judge score: {llm_score:.2f}",
is_score_valid=llm_score >= 0.8,
)
schema_weight = normalized_weights.get("schema", 0.7)
llm_weight = normalized_weights.get("llm", 0.3)
final_score = (schema_result.score * schema_weight) + (llm_score * llm_weight)
final_reason = f"Composite score. Schema ({schema_result.score:.2f} * {schema_weight:.2f}) + LLM ({llm_score:.2f} * {llm_weight:.2f})."
combined_metrics["weights"] = MetricResult(
score=0.0,
reason=f"Weights used - Schema: {schema_weight:.2f}, LLM: {llm_weight:.2f}",
is_score_valid=True,
)
return EvaluateResult(score=final_score, reason=final_reason, metrics=combined_metrics)