Skip to content

Commit

Permalink
Merge pull request #2001 from Agenta-AI/fix/json-diff-evaluator-issue…
Browse files Browse the repository at this point in the history
…-with-json-loads

fix(backend) json-diff evaluator
  • Loading branch information
jp-agenta committed Aug 21, 2024
2 parents e70befb + f1f0c42 commit b776aef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
18 changes: 17 additions & 1 deletion agenta-backend/agenta_backend/services/evaluators_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,25 @@ def auto_json_diff(
lm_providers_keys: Dict[str, Any], # pylint: disable=unused-argument
) -> Result:
try:
output = output.get("data", "") if isinstance(output, dict) else output

if isinstance(output, dict):
output = json.dumps(output)
elif isinstance(output, str):
try:
json.loads(output)
except:
raise Exception(
f"Evaluator 'auto_json_diff' requires string outputs to be JSON strings."
)
else:
raise Exception(
f"Evaluator 'auto_json_diff' requires the output to be either a JSON string or a JSON object, but received {type(output).__name__} instead."
)

correct_answer = get_correct_answer(data_point, settings_values)
average_score = compare_jsons(
ground_truth=correct_answer,
ground_truth=json.loads(correct_answer),
app_output=json.loads(output),
settings_values=settings_values,
)
Expand Down
21 changes: 3 additions & 18 deletions agenta-backend/agenta_backend/tests/unit/test_evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,7 @@ def test_auto_contains_json(output, expected):
[
(
{
"correct_answer": {
"user": {
"name": "John",
"details": {"age": 30, "location": "New York"},
}
}
"correct_answer": '{"user": {"name": "John", "details": {"age": 30, "location": "New York"}}}'
},
'{"user": {"name": "John", "details": {"age": 30, "location": "New York"}}}',
{
Expand All @@ -205,12 +200,7 @@ def test_auto_contains_json(output, expected):
),
(
{
"correct_answer": {
"user": {
"name": "John",
"details": {"age": 30, "location": "New York"},
}
}
"correct_answer": '{"user": {"name": "John", "details": {"age": "30", "location": "New York"}}}'
},
'{"user": {"name": "John", "details": {"age": "30", "location": "New York"}}}',
{
Expand All @@ -224,12 +214,7 @@ def test_auto_contains_json(output, expected):
),
(
{
"correct_answer": {
"user": {
"name": "John",
"details": {"age": 30, "location": "New York"},
}
}
"correct_answer": '{"user": {"name": "John", "details": {"age": 30, "location": "New York"}}}'
},
'{"USER": {"NAME": "John", "DETAILS": {"AGE": 30, "LOCATION": "New York"}}}',
{
Expand Down

0 comments on commit b776aef

Please sign in to comment.