-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By popular request -- this scorer simply compares two values and tells you whether they're equal or not. Of course, things get a little tricky if one thing is an object and the other is a string (not an uncommon scenario when generating JSON).
- Loading branch information
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { makePartial, ScorerWithPartial } from "./partial"; | ||
|
||
/** | ||
* A simple scorer that tests whether two values are equal. If the value is an object or array, | ||
* it will be JSON-serialized and the strings compared for equality. | ||
*/ | ||
export const ExactMatch: ScorerWithPartial<unknown, {}> = makePartial( | ||
(args) => { | ||
const maybeObject = needsJSON(args.output) || needsJSON(args.expected); | ||
const [output, expected] = [ | ||
normalizeValue(args.output ?? null, maybeObject), | ||
normalizeValue(args.expected ?? null, maybeObject), | ||
]; | ||
|
||
const score = output === expected ? 1 : 0; | ||
|
||
return { | ||
name: "ExactMatch", | ||
score, | ||
}; | ||
}, | ||
"ExactMatch", | ||
); | ||
|
||
function needsJSON(value: unknown): boolean { | ||
return typeof value === "object" || Array.isArray(value); | ||
} | ||
|
||
export function normalizeValue(value: unknown, maybeObject: boolean): string { | ||
if (needsJSON(value)) { | ||
return JSON.stringify(value); | ||
} | ||
try { | ||
if (typeof value === "string" && maybeObject) { | ||
return JSON.stringify(JSON.parse(value)); | ||
} | ||
} catch (e) { | ||
// That's ok, just return the string representation | ||
} | ||
return `${value}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import json | ||
from typing import Any | ||
|
||
from braintrust_core.score import Score | ||
|
||
from autoevals.partial import ScorerWithPartial | ||
|
||
|
||
class ExactMatch(ScorerWithPartial): | ||
""" | ||
A simple scorer that tests whether two values are equal. If the value is an object or array, | ||
it will be JSON-serialized and the strings compared for equality. | ||
""" | ||
|
||
def _run_eval_sync(self, output, expected=None, **kwargs): | ||
maybe_object = needs_json(output) or needs_json(expected) | ||
output, expected = normalize_value(output, maybe_object), normalize_value(expected, maybe_object) | ||
score = 1 if output == expected else 0 | ||
|
||
return Score(name=self._name(), score=score) | ||
|
||
|
||
def needs_json(value: Any) -> bool: | ||
return isinstance(value, (dict, list)) | ||
|
||
|
||
def normalize_value(value: Any, maybe_object: bool) -> str: | ||
if needs_json(value): | ||
return json.dumps(value) | ||
|
||
try: | ||
if maybe_object: | ||
return json.dumps(json.loads(value)) | ||
except json.JSONDecodeError: | ||
pass | ||
|
||
return str(value) |