-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b3e38a
commit 4a5487f
Showing
1 changed file
with
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
from typing import Any | ||
|
||
import numpy as np | ||
|
||
import evals | ||
import evals.record | ||
from evals.api import CompletionFn | ||
|
||
|
||
def is_valid_json(s): | ||
try: | ||
json.loads(s) | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
|
||
class JsonValidator(evals.Eval): | ||
def __init__( | ||
self, | ||
completion_fns: list[CompletionFn], | ||
samples_jsonl: str, | ||
*args, | ||
max_tokens: int = 500, | ||
**kwargs, | ||
): | ||
super().__init__(completion_fns, *args, **kwargs) | ||
assert len(completion_fns) == 1, "JsonValidator only supports one completion fn" | ||
self.max_tokens = max_tokens | ||
self.samples_jsonl = samples_jsonl | ||
|
||
def eval_sample(self, sample: Any, *_): | ||
prompt = sample["input"] | ||
result = self.completion_fn( | ||
prompt=prompt, | ||
temperature=0.0, | ||
) | ||
sampled = result.get_completions()[0] | ||
return evals.record.record_metrics( | ||
is_valid_json=is_valid_json(sampled), | ||
) | ||
|
||
def run(self, recorder): | ||
samples = self.get_samples() | ||
self.eval_all_samples(recorder, samples) | ||
metrics = recorder.get_metrics() | ||
return {"num_valid_json": np.mean([m["is_valid_json"] for m in metrics])} |